Skip to content
41 changes: 27 additions & 14 deletions aware.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
+----------------------------------------------------------------------+
| PHP Version 5 / aware |
+----------------------------------------------------------------------+
| Copyright (c) 2009 Mikko Koppanen |
| Copyright (c) Mikko Koppanen, Jess Portnoy |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
Expand All @@ -12,11 +12,14 @@
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Mikko Kopppanen <[email protected]> |
| Authors: |
| Mikko Kopppanen <[email protected]> |
| Jess Portnoy <[email protected]> |
+----------------------------------------------------------------------+
*/

#include "php_aware_private.h"
#include "php_aware.h"
#include "Zend/zend_builtin_functions.h"
#include "ext/standard/php_string.h"

Expand All @@ -39,7 +42,7 @@ PHP_FUNCTION(aware_event_trigger)
{
char *message;
int message_len;
char *error_filename;
const char *error_filename;
int error_lineno = 0;
long type;

Expand Down Expand Up @@ -176,8 +179,8 @@ PHP_FUNCTION(aware_set_error_handler)

/* free previous error handler */
if (AWARE_G(user_error_handler)) {
zval_dtor(AWARE_G(user_error_handler));
FREE_ZVAL(AWARE_G(user_error_handler));
//zval_ptr_dtor(&AWARE_G(user_error_handler));
//FREE_ZVAL(AWARE_G(user_error_handler));
}

ALLOC_INIT_ZVAL(AWARE_G(user_error_handler));
Expand All @@ -189,7 +192,7 @@ PHP_FUNCTION(aware_set_error_handler)
zval_dtor(EG(user_error_handler));
ZVAL_STRING(EG(user_error_handler), "__aware_error_handler_callback", 1);
} else {
zval_dtor(AWARE_G(user_error_handler));
zval_ptr_dtor(&AWARE_G(user_error_handler));
FREE_ZVAL(AWARE_G(user_error_handler));
AWARE_G(user_error_handler) = NULL;
}
Expand All @@ -202,8 +205,8 @@ PHP_FUNCTION(aware_restore_error_handler)
AWARE_G(orig_restore_error_handler)(INTERNAL_FUNCTION_PARAM_PASSTHRU);

if (AWARE_G(user_error_handler)) {
zval_dtor(AWARE_G(user_error_handler));
FREE_ZVAL(AWARE_G(user_error_handler));
zval_ptr_dtor(&AWARE_G(user_error_handler));
//FREE_ZVAL(AWARE_G(user_error_handler));
AWARE_G(user_error_handler) = NULL;
}

Expand All @@ -219,8 +222,8 @@ PHP_FUNCTION(aware_restore_error_handler)
zend_ptr_stack_push(&AWARE_G(user_error_handlers), tmp);

if (AWARE_G(user_error_handler)) {
zval_dtor(AWARE_G(user_error_handler));
FREE_ZVAL(AWARE_G(user_error_handler));
zval_ptr_dtor(&AWARE_G(user_error_handler));
//FREE_ZVAL(AWARE_G(user_error_handler));
}
ALLOC_INIT_ZVAL(AWARE_G(user_error_handler));
ZVAL_STRING(AWARE_G(user_error_handler), Z_STRVAL_P(tmp), 1);
Expand Down Expand Up @@ -294,7 +297,12 @@ void php_aware_capture_error_ex(zval *event, int type, const char *error_filenam
if (AWARE_G(log_backtrace)) {
zval *btrace;
ALLOC_INIT_ZVAL(btrace);
#if PHP_API_VERSION <= PHP_5_3_X_API_NO
zend_fetch_debug_backtrace(btrace, 0, 0 TSRMLS_CC);
#else
// TODO: introduce a directive for the amount of stack frames returned instead of hard coded 1000?
zend_fetch_debug_backtrace(btrace, 0, 0 TSRMLS_CC,1000);
#endif
add_assoc_zval(event, "backtrace", btrace);
}

Expand Down Expand Up @@ -358,7 +366,7 @@ void php_aware_invoke_handler(int type TSRMLS_DC, const char *error_filename, co
va_end(args);
}

static void php_aware_display_error_page(const char *filename)
static void php_aware_display_error_page(char *filename)
{
php_stream *stream = php_stream_open_wrapper(filename, "r", ENFORCE_SAFE_MODE & ~REPORT_ERRORS, NULL);

Expand Down Expand Up @@ -404,7 +412,7 @@ void php_aware_capture_error(int type, const char *error_filename, const uint er
/* Aware internal errors go through here */
MY_AWARE_EXPORTS void php_aware_original_error_cb(int type TSRMLS_DC, const char *format, ...)
{
char *error_filename;
const char *error_filename;
int error_lineno = 0;
va_list args;

Expand Down Expand Up @@ -515,6 +523,8 @@ PHP_INI_BEGIN()

STD_PHP_INI_ENTRY("aware.slow_request_threshold", "0", PHP_INI_PERDIR, OnUpdateLong, slow_request_threshold, zend_aware_globals, aware_globals)
STD_PHP_INI_ENTRY("aware.memory_usage_threshold", "0", PHP_INI_PERDIR, OnUpdateLong, memory_usage_threshold, zend_aware_globals, aware_globals)
STD_PHP_INI_ENTRY("aware.source_baseurl", "https://github.com/youruser/yourrepo", PHP_INI_ALL, OnUpdateString, source_baseurl, zend_aware_globals, aware_globals)
STD_PHP_INI_ENTRY("aware.appname", "JaM", PHP_INI_ALL, OnUpdateString, appname, zend_aware_globals, aware_globals)

/* Display pretty error pages if display_errors=0 and the error is fatal */
STD_PHP_INI_ENTRY("aware.error_page", NULL, PHP_INI_PERDIR, OnUpdateString, error_page, zend_aware_globals, aware_globals)
Expand Down Expand Up @@ -556,6 +566,8 @@ PHP_GINIT_FUNCTION(aware)
aware_globals->orig_set_error_handler = NULL;
aware_globals->user_error_handler = NULL;

aware_globals->source_baseurl = "https://github.com/youruser/yourrepo";
aware_globals->appname = "JaM";
aware_globals->error_page = NULL;

php_aware_cache_init(&(aware_globals->s_cache));
Expand Down Expand Up @@ -612,8 +624,9 @@ static void php_aware_restore_error_handling(TSRMLS_D)
zend_ptr_stack_destroy(&AWARE_G(user_error_handlers));

if (AWARE_G(user_error_handler)) {
zval_dtor(AWARE_G(user_error_handler));
FREE_ZVAL(AWARE_G(user_error_handler));
//zval_dtor(AWARE_G(user_error_handler));
//zval_ptr_dtor(&AWARE_G(user_error_handler));
//FREE_ZVAL(AWARE_G(user_error_handler));
}

if (zend_hash_find(EG(function_table), "set_error_handler", sizeof("set_error_handler"), (void **)&orig_set_error_handler) == SUCCESS) {
Expand Down
4 changes: 2 additions & 2 deletions aware_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ void php_aware_storage_store(php_aware_storage_module *mod, const char *uuid, zv
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to connect the storage module (%s)", mod->name);
return;
}

if (mod->store(uuid, event, error_filename, error_lineno TSRMLS_CC) == AwareOperationFailed) {
if (mod->store(uuid, event, error_filename, error_lineno TSRMLS_CC,type,AWARE_G(appname),AWARE_G(source_baseurl)) == AwareOperationFailed) {
php_aware_original_error_cb(E_WARNING TSRMLS_CC, "Failed to store the event %s (%s)", uuid, mod->name);
}

Expand Down
10 changes: 9 additions & 1 deletion php_aware.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@

#include <sys/resource.h>

#define PHP_5_3_X_API_NO 20090626
#define PHP_5_3_X_API_NO 20090626
#define PHP_5_4_X_API_NO 20100525
#define PHP_5_5_X_API_NO 20121212
#define PHP_5_6_X_API_NO 20131226


/* Original error callback */
typedef void (*php_aware_orig_error_cb_t)(int, const char *, const uint, const char *, va_list);

Expand Down Expand Up @@ -89,7 +96,8 @@ ZEND_BEGIN_MODULE_GLOBALS(aware)
HashTable module_error_reporting; /* hashtable containing error reporting levels for different storage modules */

php_aware_serialize_cache s_cache; /* serialize cache, repeated serializations are stored here */

char *source_baseurl; /* base URL of your code repo, for displaying a link to the file when reporting the error */
char *appname; /* report the appname in which the err was triggered */
char *error_page; /* Display pretty error page on fatal if set */

ZEND_END_MODULE_GLOBALS(aware)
Expand Down
2 changes: 1 addition & 1 deletion php_aware_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ typedef enum _AwareModuleRegisterStatus {
*/
#define PHP_AWARE_CONNECT_ARGS TSRMLS_D
#define PHP_AWARE_GET_ARGS const char *uuid, zval *event TSRMLS_DC
#define PHP_AWARE_STORE_ARGS const char *uuid, zval *event, const char *error_filename, long error_lineno TSRMLS_DC
#define PHP_AWARE_STORE_ARGS const char *uuid, zval *event, const char *error_filename, long error_lineno TSRMLS_DC, long type, const char *appname, const char *source_baseurl
#define PHP_AWARE_GET_LIST_ARGS long start, long limit, zval *events TSRMLS_DC
#define PHP_AWARE_DELETE_ARGS const char *uuid TSRMLS_DC
#define PHP_AWARE_DISCONNECT_ARGS TSRMLS_D
Expand Down
Loading