cvs: pecl(PECL_4_3) /apd Makefile.in apd.dsp apd_lib.c apd_lib.h opcode.c package.xml php_apd.c php_apd.h pprofp win32compat.c win32compat.h

From: Date: Wed, 10 Mar 2004 21:55:36 +0000
Subject: cvs: pecl(PECL_4_3) /apd Makefile.in apd.dsp apd_lib.c apd_lib.h opcode.c package.xml php_apd.c php_apd.h pprofp win32compat.c win32compat.h
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-1048@lists.php.net to get a copy of this message
gschlossnagle Wed Mar 10 16:55:36 2004 EDT Modified files: (Branch: PECL_4_3) /pecl/apd Makefile.in apd.dsp apd_lib.c apd_lib.h opcode.c package.xml php_apd.c php_apd.h pprofp win32compat.c win32compat.h Log: Move from HEAD earlier was a bad commit. Correctly reverted now.

http://cvs.php.net/diff.php/pecl/apd/Makefile.in?r1=1.1.1.1.6.1&r2=1.1.1.1.6.2&ty=u Index: pecl/apd/Makefile.in diff -u pecl/apd/Makefile.in:1.1.1.1.6.1 pecl/apd/Makefile.in:1.1.1.1.6.2 --- pecl/apd/Makefile.in:1.1.1.1.6.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/Makefile.in Wed Mar 10 16:55:35 2004 @@ -8,7 +8,7 @@ PHP_SRC_DIR = LTLIBRARY_NAME = libapd.la -LTLIBRARY_SOURCES = php_apd.c apd_lib.c opcode.c apd_array.c apd_summary.c +LTLIBRARY_SOURCES = php_apd.c apd_lib.c opcode.c apd_stack.c LTLIBRARY_SHARED_NAME = php_apd.la LTLIBRARY_SHARED_LIBADD = $(TRACE_SHARED_LIBADD) http://cvs.php.net/diff.php/pecl/apd/apd.dsp?r1=1.4.2.1&r2=1.4.2.2&ty=u Index: pecl/apd/apd.dsp diff -u pecl/apd/apd.dsp:1.4.2.1 pecl/apd/apd.dsp:1.4.2.2 --- pecl/apd/apd.dsp:1.4.2.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/apd.dsp Wed Mar 10 16:55:35 2004 @@ -43,7 +43,7 @@ # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "APD_EXPORTS" /YX /FD /c -# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\..\\" /I "..\..\main" /I "..\..\Zend" /I "..\..\TSRM" /I "..\..\win32" /D "WIN32" /D "PHP_EXPORTS" /D "COMPILE_DL_APD" /D ZTS=1 /D HAVE_APD=1 /D ZEND_DEBUG=0 /D "NDEBUG" /D "_WINDOWS" /D "ZEND_WIN32" /D "PHP_WIN32" /YX /FD /c +# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\.." /I "..\..\main" /I "..\..\Zend" /I "..\..\TSRM" /I "..\..\win32" /D "WIN32" /D "PHP_EXPORTS" /D "COMPILE_DL_APD" /D ZTS=1 /D HAVE_APD=1 /D ZEND_DEBUG=0 /D "NDEBUG" /D "_WINDOWS" /D "ZEND_WIN32" /D "PHP_WIN32" /YX /FD /c # ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 # ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 # ADD BASE RSC /l 0x407 /d "NDEBUG" @@ -70,7 +70,7 @@ # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "APD_EXPORTS" /YX /FD /GZ /c -# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "..\..\\" /I "..\..\main" /I "..\..\Zend" /I "..\..\TSRM" /I "..\..\win32" /D ZEND_DEBUG=1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "PHP_EXPORTS" /D "COMPILE_DL_APD" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_APD=1 /YX /FD /GZ /c +# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "..\.." /I "..\..\main" /I "..\..\Zend" /I "..\..\TSRM" /I "..\..\win32" /D ZEND_DEBUG=1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "PHP_EXPORTS" /D "COMPILE_DL_APD" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_APD=1 /YX /FD /GZ /c # ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32 # ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32 # ADD BASE RSC /l 0x407 /d "_DEBUG" @@ -93,15 +93,11 @@ # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" # Begin Source File -SOURCE=.\apd_array.c -# End Source File -# Begin Source File - SOURCE=.\apd_lib.c # End Source File # Begin Source File -SOURCE=.\apd_summary.c +SOURCE=.\apd_stack.c # End Source File # Begin Source File @@ -121,11 +117,11 @@ # PROP Default_Filter "h;hpp;hxx;hm;inl" # Begin Source File -SOURCE=.\apd_array.h +SOURCE=.\apd_lib.h # End Source File # Begin Source File -SOURCE=.\apd_lib.h +SOURCE=.\apd_stack.h # End Source File # Begin Source File @@ -137,14 +133,6 @@ # End Source File # Begin Source File -SOURCE=.\php_sockets.h -# End Source File -# Begin Source File - -SOURCE=.\php_sockets_win.h -# End Source File -# Begin Source File - SOURCE=.\win32compat.h # End Source File # End Group http://cvs.php.net/diff.php/pecl/apd/apd_lib.c?r1=1.10.4.1&r2=1.10.4.2&ty=u Index: pecl/apd/apd_lib.c diff -u pecl/apd/apd_lib.c:1.10.4.1 pecl/apd/apd_lib.c:1.10.4.2 --- pecl/apd/apd_lib.c:1.10.4.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/apd_lib.c Wed Mar 10 16:55:35 2004 @@ -2,7 +2,7 @@ +----------------------------------------------------------------------+ | APD Profiler & Debugger +----------------------------------------------------------------------+ - | Copyright (c) 2001-2003 Community Connect Inc. + | Copyright (c) 2001-2002 Community Connect Inc. +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | @@ -33,6 +33,159 @@ #undef DEBUG +/* apd_emalloc: malloc that dies on failure */ +void* apd_emalloc(size_t n) +{ + void* p = emalloc(n); + if (p == NULL) { + apd_eprint("apd_emalloc: malloc failed to allocate %u bytes:", n); + } + return p; +} + +/* apd_erealloc: realloc that dies on failure */ +void* apd_erealloc(void* p, size_t n) +{ + p = erealloc(p, n); + if (p == NULL) { + apd_eprint("apd_erealloc: realloc failed to allocate %u bytes:", n); + } + return p; +} + +/* apd_efree: free that bombs when given a null pointer */ +void apd_efree(void* p) +{ + if (p == NULL) { + apd_eprint("apd_efree: attempt to free null pointer"); + } + efree(p); +} + +/* apd_estrdup: strdup that dies on failure */ +char* apd_estrdup(const char* s) +{ + int len; + char* dup; + + if (s == NULL) { + return NULL; + } + len = strlen(s); + dup = (char*) emalloc(len+1); + if (dup == NULL) { + apd_eprint("apd_estrdup: malloc failed to allocate %u bytes:", len+1); + } + memcpy(dup, s, len); + dup[len] = '\0'; + return dup; +} + +/* apd_copystr: copy string of known length */ +char* apd_copystr(const char* s, int len) +{ + char* dup; + + if (s == NULL) { + return NULL; + } + dup = (char*) emalloc(len+1); + if (dup == NULL) { + apd_eprint("apd_estrdup: malloc failed to allocate %u bytes:", len+1); + } + memcpy(dup, s, len); + dup[len] = '\0'; + return dup; +} + +/* apd_eprint: print error message and exit */ +void apd_eprint(char *fmt, ...) +{ + va_list args; + + fflush(stdout); + + va_start(args, fmt); + va_end(args); + + if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') { + } + exit(2); +} + +/* apd_dprint: print messages if DEBUG is defined */ +void apd_dprint(char *fmt, ...) +{ +#ifdef DEBUG + va_list args; + + printf("DEBUG: "); + + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + + if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') { + printf(" %s", strerror(errno)); + } +#endif +} + +/* apd_strcat: like strcat, but automatically expands dest string */ +void apd_strcat(char** dst, int* curSize, const char* src) +{ + int dstLen; + int srcLen; + + srcLen = strlen(src); + + if (*dst == 0) { + *curSize = srcLen+1; + *dst = (char*) emalloc(*curSize); + strcpy(*dst, src); + return; + } + + dstLen = strlen(*dst); + if (dstLen + srcLen + 1 > *curSize) { + while (dstLen + srcLen + 1 > *curSize) { + if(*curSize == 0) { + *curSize = 1; + } + else { + *curSize *= 2; + } + } + *dst = erealloc(*dst, *curSize); + } + + strcat(*dst, src); +} + +/* apd_strncat: like strcat, but automatically expands dest string */ +void apd_strncat(char** dst, int* curSize, const char* src, int srcLen) +{ + int dstLen; + + if (*dst == 0) { + *curSize = srcLen+1; + *dst = (char*) emalloc(*curSize); + strncpy(*dst, src, srcLen); + return; + } + + dstLen = strlen(*dst); + + if (dstLen + srcLen + 1 > *curSize) { + while (dstLen + srcLen + 1 > *curSize) { + *curSize *= 2; + } + *dst = erealloc(*dst, *curSize); + } + + strncat(*dst, src, dstLen + srcLen); +} + /* apd_sprintf: safe, automatic sprintf */ char* apd_sprintf(const char* fmt, ...) { @@ -40,17 +193,17 @@ va_list args; va_start(args, fmt); - newStr = apd_sprintf_real(fmt, args); - va_end(args); - return newStr; + newStr = apd_sprintf_real(fmt, args); + va_end(args); + return newStr; } /* apd_sprintf_real: the meat of the safe, automatic sprintf */ char* apd_sprintf_real(const char* fmt, va_list args) { - char* newStr; - int size = 1; - newStr = (char*) emalloc(size); + char* newStr; + int size = 1; + newStr = (char*) apd_emalloc(size); for (;;) { int n = vsnprintf(newStr, size, fmt, args); if (n > -1 && n < size) { @@ -62,13 +215,146 @@ else { size = n+1; } - newStr = (char*) erealloc(newStr, size); + newStr = (char*) apd_erealloc(newStr, size); } va_end(args); return newStr; } +char* apd_sprintcatf(char** dst, const char* fmt, ...) +{ + char* tmpStr; + int dstLen; + int curLen, size , curSize; + va_list args; + + size = 1; + tmpStr = (char*) apd_emalloc(size); + + + va_start(args, fmt); + for(;;) { + int n = vsnprintf(tmpStr, size, fmt, args); + if( n > -1 && n < size) { + break; + } + if( n < 0 ) { + if(size == 0) { + size = 1; + } + else { + size *= 2; + } + } + else { + size = n+1; + } + tmpStr = (char*) apd_erealloc(tmpStr, size); + } + va_end(args); + if(*dst == NULL) { + *dst = tmpStr; + return tmpStr; + } + curSize = strlen(*dst) + 1; + apd_strcat(dst, &curSize , tmpStr); + return *dst; +} + +char* apd_strtac(char **dst, char *src) +{ + int dstLen, srcLen; + char *tmpStr; + + if(*dst == NULL) { + *dst = (char *) apd_emalloc(strlen(src) + 1); + strcpy(*dst,src); + return *dst; + } + dstLen = strlen(*dst); + srcLen = strlen(src); + tmpStr = (char*) apd_emalloc(dstLen + srcLen +1); + memcpy(tmpStr, src, srcLen); + strcat(tmpStr, *dst); + apd_efree(*dst); + *dst = tmpStr; + return *dst; +} + +char* apd_indent(char **dst, int spaces) +{ + char *tmpStr; + int dstLen; + + if(spaces == 0) { + return *dst; + } + if(*dst == NULL) { + dstLen = 0; + } + else { + dstLen = strlen(*dst); + } + tmpStr = (char*) apd_emalloc(spaces + dstLen + 1); + memset(tmpStr, ' ', spaces); + tmpStr[spaces] = '\0'; + if(dstLen == 0) + { + *dst = tmpStr; // FIXME: memory leak + return *dst; + } + strcat(tmpStr, *dst); + apd_efree(*dst); + *dst = tmpStr; + return *dst; +} + +int __apd_dump_regular_resources(zval *arr TSRMLS_DC) { + Bucket *p; + HashTable *ht; + + if(array_init(arr) == FAILURE) { + fprintf(stderr, "failed\n"); + return 0; + } + ht = &(EG(regular_list)); + p = ht->pListHead; + while(p != NULL) { + list_entry *le; + char *resource_name = NULL; + le = p->pData; + resource_name = zend_rsrc_list_get_rsrc_type(p->h TSRMLS_CC); + if (resource_name != NULL) { + add_index_string(arr, p->h, zend_rsrc_list_get_rsrc_type(p->h TSRMLS_CC), 1); + } else { + resource_name = apd_emalloc(256); + snprintf(resource_name, 255, "APD: unknown resource type %d", p->h); + add_index_string(arr, p->h, resource_name, 1); + apd_efree(resource_name); + } + p = p->pListNext; + } + return 0; +} + +int __apd_dump_persistent_resources(zval *arr TSRMLS_DC) { + Bucket *p; + HashTable *ht; + if(array_init(arr) == FAILURE) { + return 0; + } + ht = &(EG(persistent_list)); + p = ht->pListHead; + while( p != NULL) { + list_entry *le; + le =p->pData; + add_next_index_stringl(arr, p->arKey, p->nKeyLength, 1); + p = p->pListNext; + } + return 0; +} + void timevaldiff(struct timeval *a, struct timeval *b, struct timeval *result) { result->tv_sec = a->tv_sec - b->tv_sec; @@ -78,13 +364,7 @@ result->tv_usec += 1000000; } } + -/** - * Local Variables: - * indent-tabs-mode: t - * c-basic-offset: 4 - * tab-width: 4 - * End: - * vim600:fdm=marker - * vim:noet:sw=4:ts=4 - */ + + http://cvs.php.net/diff.php/pecl/apd/apd_lib.h?r1=1.8.4.1&r2=1.8.4.2&ty=u Index: pecl/apd/apd_lib.h diff -u pecl/apd/apd_lib.h:1.8.4.1 pecl/apd/apd_lib.h:1.8.4.2 --- pecl/apd/apd_lib.h:1.8.4.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/apd_lib.h Wed Mar 10 16:55:35 2004 @@ -28,8 +28,40 @@ /* wrappers for memory allocation routines */ +extern void* apd_emalloc (size_t n); +extern void* apd_erealloc(void* p, size_t n); +extern void apd_efree (void* p); +extern char* apd_estrdup (const char* s); +extern char* apd_copystr (const char* s, int len); + +/* simple display facility */ + +extern void apd_eprint(char *fmt, ...); +extern void apd_dprint(char *fmt, ...); + +/* simple timer facility */ + +extern void apd_timerstart (void); +extern void apd_timerstop (void); +extern double apd_timerreport(void); + +/* string routines */ + +extern void apd_strcat (char** dst, int* curSize, const char* src); +extern void apd_strncat(char** dst, int* curSize, const char* src, int srcLen); extern char* apd_sprintf(const char* fmt, ...); extern char* apd_sprintf_real(const char* fmt, va_list args); +/* sprintf's to the end of a string. reallocates automatically */ +extern char* apd_sprintcatf(char** dst, const char* fmt, ...); +/* indents a string by the specified amount */ +extern char* apd_indent(char **dst, int spaces); +extern char* apd_strtac(char **dst, char *src); + + +/* resource access routines */ + +extern int __apd_dump_regular_resources(zval *array TSRMLS_DC); +extern int __apd_dump_persistent_resources(zval *array TSRMLS_DC); /* timeval functions */ extern void timevaldiff(struct timeval* a, struct timeval* b, struct timeval* result); http://cvs.php.net/diff.php/pecl/apd/opcode.c?r1=1.2.6.1&r2=1.2.6.2&ty=u Index: pecl/apd/opcode.c diff -u pecl/apd/opcode.c:1.2.6.1 pecl/apd/opcode.c:1.2.6.2 --- pecl/apd/opcode.c:1.2.6.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/opcode.c Wed Mar 10 16:55:35 2004 @@ -2,7 +2,7 @@ +----------------------------------------------------------------------+ | APD Profiler & Debugger +----------------------------------------------------------------------+ - | Copyright (c) 2001-2003 Community Connect Inc. + | Copyright (c) 2001-2002 Community Connect Inc. +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | @@ -140,12 +140,4 @@ return opcodes[op]; } -/** - * Local Variables: - * indent-tabs-mode: t - * c-basic-offset: 4 - * tab-width: 4 - * End: - * vim600:fdm=marker - * vim:noet:sw=4:ts=4 - */ + http://cvs.php.net/diff.php/pecl/apd/package.xml?r1=1.10.4.1&r2=1.10.4.2&ty=u Index: pecl/apd/package.xml diff -u pecl/apd/package.xml:1.10.4.1 pecl/apd/package.xml:1.10.4.2 --- pecl/apd/package.xml:1.10.4.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/package.xml Wed Mar 10 16:55:35 2004 @@ -16,7 +16,7 @@ </maintainer> </maintainers> <release> - <version>0.9</version> + <version>0.4p2</version> <date>2002-11-25</date> <state>stable</state> <notes> @@ -31,6 +31,8 @@ <file role="src">apd.dsp</file> <file role="src">apd_lib.c</file> <file role="src">apd_lib.h</file> + <file role="src">apd_stack.c</file> + <file role="src">apd_stack.h</file> <file role="src">build</file> <file role="src">config.m4</file> <file role="src">opcode.c</file> http://cvs.php.net/diff.php/pecl/apd/php_apd.c?r1=1.54.2.1&r2=1.54.2.2&ty=u Index: pecl/apd/php_apd.c diff -u pecl/apd/php_apd.c:1.54.2.1 pecl/apd/php_apd.c:1.54.2.2 --- pecl/apd/php_apd.c:1.54.2.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/php_apd.c Wed Mar 10 16:55:36 2004 @@ -1,8 +1,8 @@ /* +----------------------------------------------------------------------+ - | APD Profiler & Debugger | + | APD Profiler & Debugger +----------------------------------------------------------------------+ - | Copyright (c) 2001-2003 Community Connect Inc. | + | Copyright (c) 2001-2002 Community Connect Inc. +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | @@ -14,24 +14,23 @@ +----------------------------------------------------------------------+ | Authors: Daniel Cowgill <dcowgill@communityconnect.com> | | George Schlossnagle <george@lethargy.org> | - | Sterling Hughes <sterling@php.net> | +----------------------------------------------------------------------+ */ -#include "php_apd.h" #ifdef PHP_WIN32 #include "win32compat.h" # define APD_IS_INVALID_SOCKET(a) (a == INVALID_SOCKET) #else #include <sys/time.h> -#include <sys/resource.h> #include <sys/times.h> #include <unistd.h> # define APD_IS_INVALID_SOCKET(a) (a < 0) #endif +#include "php_apd.h" #include "apd_lib.h" #include "opcode.h" +#include "apd_stack.h" #include "zend_API.h" #include "zend_hash.h" #include "zend_alloc.h" @@ -49,9 +48,9 @@ #define PF_INET AF_INET #endif -/** - * Tracng calls to zend_compile_file - */ +// --------------------------------------------------------------------------- +// Tracng calls to zend_compile_file +// --------------------------------------------------------------------------- #undef TRACE_ZEND_COMPILE /* define to trace all calls to zend_compile_file */ ZEND_DLEXPORT zend_op_array* apd_compile_file(zend_file_handle* TSRMLS_DC); ZEND_DLEXPORT zend_op_array* (*old_compile_file)(zend_file_handle* TSRMLS_DC); @@ -64,103 +63,37 @@ /* This comes from php install tree. */ #include "ext/standard/info.h" -/* List of exported functions. */ +///* List of exported functions. */ +//static unsigned char a2_arg_force_ref[] = { 2, BYREF_NONE, BYREF_FORCE }; function_entry apd_functions[] = { + PHP_FE(apd_callstack, NULL) + PHP_FE(apd_cluck, NULL) + PHP_FE(apd_croak, NULL) + PHP_FE(apd_dump_regular_resources, NULL) + PHP_FE(apd_dump_persistent_resources, NULL) PHP_FE(override_function, NULL) PHP_FE(rename_function, NULL) + PHP_FE(dump_function_table, NULL) + PHP_FE(apd_set_session_trace, NULL) PHP_FE(apd_set_pprof_trace, NULL) - PHP_FE(apd_set_browser_trace, NULL) PHP_FE(apd_set_session_trace_socket, NULL) + PHP_FE(apd_set_session, NULL) PHP_FE(apd_breakpoint, NULL) PHP_FE(apd_continue, NULL) PHP_FE(apd_echo, NULL) {NULL, NULL, NULL} }; -void apd_pprof_fprintf(const char* fmt, ...); - -long diff_times(struct timeval a, struct timeval b) -{ - long retval = (a.tv_sec - b.tv_sec) * 1000000L; - retval += (a.tv_usec - b.tv_usec); - - return retval; -} - -/** - * Output to be analyzed by pprof - */ -static void -apd_pprof_output_header(void) -{ - TSRMLS_FETCH(); - - apd_pprof_fprintf("#Pprof [APD] v0.9\n"); - apd_pprof_fprintf("caller=%s\n",zend_get_executed_filename(TSRMLS_C)); - apd_pprof_fprintf("\nEND_HEADER\n"); -} - -static void -apd_pprof_output_file_reference(int id, const char *filename) -{ - apd_pprof_fprintf("! %d %s\n", id, filename); -} - -static void -apd_pprof_output_elapsed_time(int usert, int systemt, int realt) -{ - apd_pprof_fprintf("@ %ld %ld %ld\n", usert, systemt, realt); -} - -static void -apd_pprof_output_declare_function(int index, const char *fname, int type) -{ - apd_pprof_fprintf("& %d %s %d\n", index, fname, type); -} - -static void -apd_pprof_output_enter_function(int index, int filenum, int linenum) -{ - apd_pprof_fprintf("+ %d %d %d\n", index, filenum, linenum); -} - -static void -apd_pprof_output_exit_function(int index, int mem) -{ - if (mem == -1) { - apd_pprof_fprintf("- %d\n", index); - } else { - apd_pprof_fprintf("- %d %d\n", index, mem); - } -} - -static void -apd_pprof_output_footer(void) -{ - struct rusage end_ru; - struct timeval end_clock; - TSRMLS_FETCH(); - - if (!APD_GLOBALS(pprof_file)) { - return; - } - - gettimeofday(&end_clock, NULL); - getrusage(RUSAGE_SELF, &end_ru); +// Declare global structure. +int print_indent; +ZEND_DECLARE_MODULE_GLOBALS(apd); - apd_pprof_fprintf("END_TRACE\n"); - apd_pprof_fprintf("total_user=%ld\ntotal_sys=%ld\ntotal_wall=%ld\n", - diff_times(end_ru.ru_utime, APD_GLOBALS(first_ru).ru_utime), - diff_times(end_ru.ru_stime, APD_GLOBALS(first_ru).ru_stime), - diff_times(end_clock, APD_GLOBALS(first_clock))); - apd_pprof_fprintf("END_FOOTER\n"); -} +// --------------------------------------------------------------------------- +// apd_dump_fprintf - Outputer - either to file or socket +// --------------------------------------------------------------------------- -/* --------------------------------------------------------------------------- - apd_dump_fprintf - Outputer - either to file or socket - --------------------------------------------------------------------------- */ void apd_dump_fprintf(const char* fmt, ...) { va_list args; @@ -174,9 +107,9 @@ fprintf(APD_GLOBALS(dump_file), "%s", newStr); } else if ( APD_GLOBALS(dump_sock_id) > 0) { #ifndef PHP_WIN32 - write(APD_GLOBALS(dump_sock_id), newStr, strlen (newStr) + 1); + write(APD_GLOBALS(dump_sock_id), newStr, strlen (newStr) + 1); #else - send(APD_GLOBALS(dump_sock_id), newStr, strlen (newStr) + 1, 0); + send(APD_GLOBALS(dump_sock_id), newStr, strlen (newStr) + 1, 0); #endif } efree(newStr); @@ -189,32 +122,31 @@ TSRMLS_FETCH(); if(!APD_GLOBALS(pproftrace)) { - zend_error(E_ERROR, "pproftrace is unset"); + zend_error(E_ERROR, "pproftrace is unset"); return; } - va_start(args, fmt); newStr = apd_sprintf_real(fmt, args); va_end(args); if (APD_GLOBALS(pprof_file) != NULL) { fprintf(APD_GLOBALS(pprof_file), newStr); } - /* - else if ( APD_GLOBALS(prof_sock_id) > 0) { - #ifndef PHP_WIN32 - write(APD_GLOBALS(pprof_sock_id), newStr, strlen (newStr) + 1); - #else - send(APD_GLOBALS(pprof_sock_id), newStr, strlen (newStr) + 1, 0); - #endif - } - */ +/* + else if ( APD_GLOBALS(prof_sock_id) > 0) { +#ifndef PHP_WIN32 + write(APD_GLOBALS(pprof_sock_id), newStr, strlen (newStr) + 1); +#else + send(APD_GLOBALS(pprof_sock_id), newStr, strlen (newStr) + 1, 0); +#endif + } +*/ efree(newStr); } -/* --------------------------------------------------------------------------------- - Interactive Mode - --------------------------------------------------------------------------------- */ +// --------------------------------------------------------------------------------- +// Interactive Mode +// --------------------------------------------------------------------------------- /* interactive execution - \n continues, otherwise the data is eval'ed */ @@ -278,151 +210,436 @@ -/* --------------------------------------------------------------------------- - Call Tracing (Application-specific Code) - --------------------------------------------------------------------------- */ +// --------------------------------------------------------------------------- +// Call Tracing (Application-specific Code) +// --------------------------------------------------------------------------- -char *apd_get_active_function_name(zend_op_array *op_array TSRMLS_DC) +typedef enum { CALL_ARG_VAR, CALL_ARG_REF, CALL_ARG_LITERAL } CallArgType; + +typedef struct summary_t { + int index; + int calls; + int totalTime; +} summary_t; + +typedef struct CallArg CallArg; +struct CallArg { + CallArgType type; // argument type + char* strVal; // string representation + char* argName; // argument name +}; + +typedef struct CallStackEntry CallStackEntry; +struct CallStackEntry { + char* functionName; // name of the function + int numArgs; // number of parameters + CallArg* args; // parameter list + char *filename; // location of call + int lineNum; // location of call + int type; // internal or user function +}; + +typedef struct apd_stack_t CallStack; + +static char* boolToString(int v) { - char *funcname = NULL; - int curSize = 0; - zend_execute_data *execd; - char *tmpfname; - char *classname; - int classnameLen; - int tmpfnameLen; - - execd = EG(current_execute_data); - if(execd) { - tmpfname = execd->function_state.function->common.function_name; - if(tmpfname) { - tmpfnameLen = strlen(tmpfname); - if(execd->object) { - classname = Z_OBJCE(*execd->object)->name; - classnameLen = strlen(classname); - funcname = (char *)emalloc(classnameLen + tmpfnameLen + 3); - snprintf(funcname, classnameLen + tmpfnameLen + 3, "%s->%s", - classname, tmpfname); - } - else if(execd->function_state.function->common.scope) { - classname = execd->function_state.function->common.scope->name; - classnameLen = strlen(classname); - funcname = (char *)emalloc(classnameLen + tmpfnameLen + 3); - snprintf(funcname, classnameLen + tmpfnameLen + 3, "%s::%s", - classname, tmpfname); - } - else { - funcname = estrdup(tmpfname); - } - } - else { - switch (execd->opline->op2.u.constant.value.lval) { - case ZEND_EVAL: - funcname = estrdup("eval"); - break; - case ZEND_INCLUDE: - funcname = estrdup("include"); - break; - case ZEND_REQUIRE: - funcname = estrdup("require"); - break; - case ZEND_INCLUDE_ONCE: - funcname = estrdup("include_once"); - break; - case ZEND_REQUIRE_ONCE: - funcname = estrdup("require_once"); - break; - default: - funcname = estrdup("???"); - break; - } - } - } - else { - funcname = estrdup("???"); + return v ? estrdup("true") : estrdup("false"); +} + +CallStackEntry *mkCallStackEntry(const char *func, const char *filename, int linenum, int type) +{ + CallStackEntry* entry; + entry = (CallStackEntry*) emalloc(sizeof(CallStackEntry)); + entry->functionName = estrdup(func); + entry->numArgs = 0; + entry->args = NULL; + entry->filename = estrdup(filename); + entry->lineNum = linenum; + return entry; +} + +static CallArg* mkCallArgVal(zend_op* curOp) +{ + CallArg* arg; + + assert(curOp->opcode == ZEND_SEND_VAL); + arg = (CallArg*) emalloc(sizeof(CallArg)); + arg->type = CALL_ARG_LITERAL; + + if (curOp->op1.op_type != IS_CONST) { + arg->strVal = estrdup("??"); + return arg; + } + + switch (curOp->op1.u.constant.type) { + case IS_NULL: + arg->strVal = estrdup("(null constant)"); + break; + case IS_LONG: + arg->strVal = apd_sprintf("%ld", curOp->op1.u.constant.value.lval); + break; + case IS_DOUBLE: + arg->strVal = apd_sprintf("%g", curOp->op1.u.constant.value.dval); + break; + case IS_STRING: + arg->strVal = apd_sprintf("'%s'", curOp->op1.u.constant.value.str.val); + break; + case IS_BOOL: + arg->strVal = curOp->op1.u.constant.value.lval + ? estrdup("true") + : estrdup("false"); + break; + case IS_CONSTANT: + arg->strVal = estrdup(curOp->op1.u.constant.value.str.val); + break; + default: + arg->strVal = estrdup("???"); + break; } - return funcname; + + return arg; } -static void trace_function_entry(HashTable *func_table, const char *fname, int type, const char *filename, int linenum) +static CallArg* mkCallArgVar(zend_op* curOp, CallArgType type) { - int i = 0; - int *function_index; - int *filenum; - TSRMLS_FETCH(); + CallArg* arg; + zend_op* prvOp; + int curSize; - if (!APD_GLOBALS(pproftrace)) { - return; + assert(curOp->opcode == ZEND_SEND_VAR || curOp->opcode == ZEND_SEND_REF); + arg = (CallArg*) emalloc(sizeof(CallArg)); + arg->type = type; + arg->strVal = 0; + curSize = 0; + + prvOp = curOp - 1; + + while (prvOp->opcode != ZEND_EXT_FCALL_BEGIN) { + // Check for unrecognized opcodes. + if (prvOp->opcode != ZEND_FETCH_R && prvOp->opcode != ZEND_FETCH_W) { + apd_strcat(&arg->strVal, &curSize, "??"); + break; + } + + if (prvOp->op1.op_type == IS_CONST) { + apd_strcat(&arg->strVal, &curSize, "$"); + apd_strncat(&arg->strVal, &curSize, prvOp->op1.u.constant.value.str.val, prvOp->op1.u.constant.value.str.len); + break; + } + else { + apd_strcat(&arg->strVal, &curSize, "$"); + } + prvOp--; } - - if (zend_hash_find(APD_GLOBALS(file_summary), (char *) filename, strlen(filename) + 1, (void *) &filenum) == FAILURE) { - filenum = (int *) emalloc(sizeof(int)); - *filenum = ++APD_GLOBALS(file_index); - APD_GLOBALS(output).file_reference(*filenum, filename); - zend_hash_add(APD_GLOBALS(file_summary), (char *) filename, strlen(filename) + 1, filenum, sizeof(int), NULL); + + if (prvOp->opcode == ZEND_EXT_FCALL_BEGIN) { + apd_strcat(&arg->strVal, &curSize, "??"); } + return arg; +} - if (zend_hash_find(APD_GLOBALS(function_summary), (char *) fname, strlen(fname)+1, (void *) &function_index) == SUCCESS) { - APD_GLOBALS(output).enter_function(*function_index, *filenum, linenum); - } else { - function_index = (int *) emalloc(sizeof(int)); - *function_index = ++APD_GLOBALS(function_index); - zend_hash_add(APD_GLOBALS(function_summary), (char *) fname, strlen(fname)+1, function_index, sizeof(int), NULL); - APD_GLOBALS(output).declare_function(*function_index, fname, type); - APD_GLOBALS(output).enter_function(*function_index, *filenum, linenum); + +static void freeCallStackEntry(CallStackEntry* entry) +{ + int i; + + for (i = 0; i < entry->numArgs; i++) { + efree(entry->args[i].strVal); } + if(entry->args) { + efree(entry->args); + } + efree(entry->functionName); + efree(entry->filename); + efree(entry); } +static void initializeTracer() +{ + CallStack* stack; + TSRMLS_FETCH(); + + stack = apd_stack_create(); + APD_GLOBALS(stack) = (void*) stack; +} -static void trace_function_exit(char *fname) +static void shutdownTracer() { - struct rusage wall_ru; - struct timeval clock; - long utime, stime, rtime; - int *function_index; - int allocated = -1; + CallStack *stack; TSRMLS_FETCH(); - if (!APD_GLOBALS(pproftrace)) { - return; - } + stack = (CallStack*) APD_GLOBALS(stack); + apd_stack_destroy(stack); +} -#if MEMORY_LIMIT - allocated = AG(memory_limit); +#if ZEND_EXTENSION_API_NO > 20020429 +char *apd_get_active_function_name(zend_op_array *op_array TSRMLS_DC) +{ + char *funcname = NULL; + int curSize = 0; + zend_execute_data *execd; + execd = EG(current_execute_data); + + if(execd) { + if(execd->function_state.function->common.function_name) { + if(execd->ce) { + funcname = estrdup(execd->ce->name); + apd_strcat(&funcname, &curSize, "->"); + apd_strcat(&funcname, &curSize, execd->function_state.function->common.function_name); + } + else if(execd->object.ptr) { + funcname = estrdup(execd->object.ptr->value.obj.ce->name); + apd_strcat(&funcname, &curSize, "->"); + apd_strcat(&funcname, &curSize, execd->function_state.function->common.function_name); + } + else { + funcname = estrdup(execd->function_state.function->common.function_name); + } + } + else { + switch (execd->opline->op2.u.constant.value.lval) { + case ZEND_EVAL: + funcname = estrdup("eval"); + break; + case ZEND_INCLUDE: + funcname = estrdup("include"); + break; + case ZEND_REQUIRE: + funcname = estrdup("require"); + break; + case ZEND_INCLUDE_ONCE: + funcname = estrdup("include_once"); + break; + case ZEND_REQUIRE_ONCE: + funcname = estrdup("require_once"); + break; + default: + funcname = estrdup("???"); + break; + } + } + } + else { + funcname = estrdup("???"); + } + return funcname; +} +#else +char *apd_get_active_function_name(zend_op_array *op_array TSRMLS_DC) +{ + char *fname = NULL; + zval **object_ptr_ptr; + if(zend_hash_find(EG(active_symbol_table), "this", sizeof("this"), + (void **) &object_ptr_ptr)!=FAILURE) + { + char *tmp; + zend_class_entry *ce; + zend_function **dummy; + tmp = get_active_function_name(TSRMLS_C); + ce = Z_OBJCE_PP(object_ptr_ptr); + if(zend_hash_exists(&(ce->function_table), tmp, strlen(tmp) + 1)) { + tmp = apd_sprintf("%s::%s", ce->name, tmp); + } + else { + } + fname = strdup(tmp); + apd_efree(tmp); + } + else { + if(EG(function_state_ptr)) { + fname = strdup(get_active_function_name(TSRMLS_C)); + } + else { + fname = strdup("main"); + } + } + return fname; +} #endif - - gettimeofday(&clock, NULL); - getrusage(RUSAGE_SELF, &wall_ru); - if (APD_GLOBALS(function_index) > 1) { - utime = diff_times(wall_ru.ru_utime, APD_GLOBALS(last_ru).ru_utime); - stime = diff_times(wall_ru.ru_stime, APD_GLOBALS(last_ru).ru_stime); - rtime = diff_times(clock, APD_GLOBALS(last_clock)); - if(utime || stime || rtime) { - APD_GLOBALS(output).elapsed_time(utime, stime, rtime); - } - } - - APD_GLOBALS(last_ru) = wall_ru; - APD_GLOBALS(last_clock) = clock; - if (zend_hash_find(APD_GLOBALS(function_summary), fname, strlen(fname) + 1, (void *) &function_index) == SUCCESS) { - APD_GLOBALS(output).exit_function(*function_index, allocated); - } else { - function_index = (int *) emalloc(sizeof(int)); - *function_index = ++APD_GLOBALS(function_index); - zend_hash_add(APD_GLOBALS(function_summary), fname, strlen(fname)+1, function_index, sizeof(int), NULL); - APD_GLOBALS(output).exit_function(*function_index, allocated); - } +static void traceFunctionEntry( + HashTable * func_table, + const char* fname, + int type, + const char *filename, + int linenum) +{ + CallStack* stack; + CallStackEntry* entry; + int filenum; + + int i = 0; + TSRMLS_FETCH(); + stack = (CallStack*) APD_GLOBALS(stack); + entry = mkCallStackEntry(fname, filename, linenum, type); + apd_stack_push(stack, entry); + if(APD_GLOBALS(pproftrace)) { + summary_t *summaryStats; + int *filenum; + if ( zend_hash_find(APD_GLOBALS(file_summary), (char *) filename, strlen(filename) + 1, (void *) &filenum) == FAILURE ) { + filenum = (int *) emalloc(sizeof(int)); + *filenum = ++APD_GLOBALS(file_index); + apd_pprof_fprintf("! %d %s\n", *filenum, filename); + zend_hash_add(APD_GLOBALS(file_summary), (char *) filename, + strlen(filename) + 1, filenum, sizeof(int), NULL); + } + if ( zend_hash_find(APD_GLOBALS(summary), (char *) fname, strlen(fname) + 1, (void *) &summaryStats) == SUCCESS ) + { + apd_pprof_fprintf("+ %d %d %d\n", summaryStats->index, *filenum, linenum); + } else { + summaryStats = (summary_t *) emalloc(sizeof(summary_t)); + summaryStats->calls = 1; + summaryStats->index = ++APD_GLOBALS(index); + summaryStats->totalTime = 0; + zend_hash_add(APD_GLOBALS(summary), (char *) fname, strlen(fname) + 1, summaryStats, sizeof(summary_t), NULL); + apd_pprof_fprintf("& %d %s %d\n", summaryStats->index, fname, type); + apd_pprof_fprintf("+ %d %d %d\n", summaryStats->index, *filenum, linenum); + } + } +} + +static void traceFunctionExit(char *fname) +{ + CallStack* stack; + CallStackEntry* entry; + char* line = NULL; + char* tmp; + struct timeval now; + struct timeval elapsed; + struct timeval profelapsed; + struct timeval diff; + TSRMLS_FETCH(); + + stack = (CallStack*) APD_GLOBALS(stack); + entry = (CallStackEntry *) apd_stack_pop(stack); + if( APD_GLOBALS(pproftrace)) + { + struct tms walltimes; + clock_t clock; + summary_t *summaryStats; + + clock = times(&walltimes); + + if (APD_GLOBALS(index) > 1) { + if( (walltimes.tms_utime - APD_GLOBALS(lasttms).tms_utime) || + (walltimes.tms_stime - APD_GLOBALS(lasttms).tms_stime) || + (clock - APD_GLOBALS(lastclock))) + { + apd_pprof_fprintf("@ %d %d %d\n", + walltimes.tms_utime - APD_GLOBALS(lasttms).tms_utime, + walltimes.tms_stime - APD_GLOBALS(lasttms).tms_stime, + clock - APD_GLOBALS(lastclock)); + } + } + APD_GLOBALS(lasttms) = walltimes; + APD_GLOBALS(lastclock) = clock; + if ( zend_hash_find(APD_GLOBALS(summary), fname, + strlen(fname) + 1, + (void *) &summaryStats) == SUCCESS ) + { + #if MEMORY_LIMIT + apd_pprof_fprintf("- %d %d\n", summaryStats->index, AG(allocated_memory)); + #else + apd_pprof_fprintf("- %d\n", summaryStats->index); + #endif + } else { + summaryStats = (summary_t *) emalloc(sizeof(summary_t)); + summaryStats->calls = 1; + summaryStats->index = ++APD_GLOBALS(index); + summaryStats->totalTime = 0; + zend_hash_add(APD_GLOBALS(summary), fname, strlen(fname) + + 1, summaryStats, sizeof(summary_t), NULL); + #if MEMORY_LIMIT + apd_pprof_fprintf("- %d %d\n", summaryStats->index, AG(allocated_memory)); + #else + apd_pprof_fprintf("- %d\n", summaryStats->index); + #endif + } + } + freeCallStackEntry(entry); +} + +// -------------------------------------------------------------------------- +// Error Tracing +// -------------------------------------------------------------------------- + +void (*old_zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args); + +static void apd_error_cb(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args) +{ + char buffer[1024]; + char *error_type_str; + char *tmp; + char *line = NULL; + int curSize; + + TSRMLS_FETCH(); + + if(APD_GLOBALS(bitmask) & ERROR_TRACE) { + switch(type) { + case E_ERROR: + case E_CORE_ERROR: + case E_COMPILE_ERROR: + case E_USER_ERROR: + error_type_str = "Fatal Error"; + break; + case E_WARNING: + case E_CORE_WARNING: + case E_COMPILE_WARNING: + case E_USER_WARNING: + error_type_str = "Warning"; + break; + case E_PARSE: + error_type_str = "Parse error"; + break; + case E_NOTICE: + error_type_str = "Warning"; + break; + case E_USER_NOTICE: + error_type_str = "Notice"; + break; + default: + error_type_str = "Unknown error"; + break; + } + vsnprintf(buffer, sizeof(buffer) - 1, format, args); + line = estrdup(""); + apd_indent(&line, 2*print_indent); + if(APD_GLOBALS(bitmask) & TIMING_TRACE) + { + struct timeval now; + struct timeval elapsed; + + gettimeofday(&now, NULL); + timevaldiff(&now, &APD_GLOBALS(req_begin), &elapsed); + tmp = apd_sprintf("(%3d.%06d): ", elapsed.tv_sec, elapsed.tv_usec); + curSize = strlen(tmp); + apd_strcat(&tmp, &curSize, line); + efree(line); + line = tmp; + } + tmp = apd_sprintf("%s at %s:%s() line %d error_string \"%s\"\n", + error_type_str, error_filename, + EG(active_op_array)->function_name?EG(active_op_array)->function_name:"main", + error_lineno, buffer); + curSize = strlen(line) ; + apd_strcat(&line, &curSize, tmp); + efree(tmp); + if(APD_GLOBALS(bitmask)) { + apd_dump_fprintf( "%s", line); + efree(line); + } + } + old_zend_error_cb(type, error_filename, error_lineno, format, args); } -/* -------------------------------------------------------------------------- - Error Tracing - --------------------------------------------------------------------------- - Module Entry - --------------------------------------------------------------------------- */ +// --------------------------------------------------------------------------- +// Module Entry +// --------------------------------------------------------------------------- + zend_module_entry apd_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 - STANDARD_MODULE_HEADER, + STANDARD_MODULE_HEADER, #endif "APD", apd_functions, @@ -434,7 +651,7 @@ NULL, NULL, #if ZEND_MODULE_API_NO >= 20010901 - NO_VERSION_YET, /* extension version number (string) */ + NO_VERSION_YET, /* extension version number (string) */ #endif STANDARD_MODULE_PROPERTIES_EX }; @@ -444,75 +661,68 @@ ZEND_GET_MODULE(apd) #endif -/* --------------------------------------------------------------------------- - PHP Configuration Functions - --------------------------------------------------------------------------- */ +// --------------------------------------------------------------------------- +// PHP Configuration Functions +// --------------------------------------------------------------------------- static PHP_INI_MH(set_dumpdir) { - struct stat sb; - if (new_value == NULL || stat(new_value, &sb)) { - APD_GLOBALS(dumpdir) = NULL; - } - else { - APD_GLOBALS(dumpdir) = new_value; - } - return SUCCESS; + struct stat sb; + if (new_value == NULL || stat(new_value, &sb)) { + APD_GLOBALS(dumpdir) = NULL; + } + else { + APD_GLOBALS(dumpdir) = new_value; + } + return SUCCESS; } PHP_INI_BEGIN() - PHP_INI_ENTRY("apd.dumpdir", NULL, PHP_INI_ALL, set_dumpdir) + PHP_INI_ENTRY("apd.dumpdir", NULL, PHP_INI_ALL, set_dumpdir) + STD_PHP_INI_ENTRY("apd.bitmask", "0", PHP_INI_ALL, OnUpdateInt, + bitmask, zend_apd_globals, apd_globals) PHP_INI_END() -/* --------------------------------------------------------------------------- - Module Startup and Shutdown Function Definitions - --------------------------------------------------------------------------- */ - -static void php_apd_init_globals(zend_apd_globals *apd_globals) -{ - memset(apd_globals, 0, sizeof(zend_apd_globals)); - - apd_globals->function_summary = (HashTable *) malloc(sizeof(HashTable)); - apd_globals->file_summary = (HashTable *) malloc(sizeof(HashTable)); - - zend_hash_init(apd_globals->function_summary, 0, NULL, NULL, 1); - zend_hash_init(apd_globals->file_summary, 0, NULL, NULL, 1); - -} +// --------------------------------------------------------------------------- +// Module Startup and Shutdown Function Definitions +// --------------------------------------------------------------------------- -static void php_apd_free_globals(zend_apd_globals *apd_globals) -{ - free(apd_globals->function_summary); - free(apd_globals->file_summary); +static void php_apd_init_globals(zend_apd_globals *apd_globals) { + struct tms walltimes; + memset(apd_globals, 0, sizeof(zend_apd_globals)); + } PHP_MINIT_FUNCTION(apd) { - ZEND_INIT_MODULE_GLOBALS(apd, php_apd_init_globals, php_apd_free_globals); + ZEND_INIT_MODULE_GLOBALS(apd, php_apd_init_globals, NULL); REGISTER_INI_ENTRIES(); - REGISTER_LONG_CONSTANT("APD_AF_UNIX", AF_UNIX, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("APD_AF_INET", AF_INET, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("APD_FUNCTION_TRACE", FUNCTION_TRACE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("APD_ARGS_TRACE", ARGS_TRACE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("APD_ASSIGNMENT_TRACE", ASSIGNMENT_TRACE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("APD_STATEMENT_TRACE", STATEMENT_TRACE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("APD_MEMORY_TRACE", MEMORY_TRACE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("APD_TIMING_TRACE", TIMING_TRACE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("APD_SUMMARY_TRACE", SUMMARY_TRACE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("APD_ERROR_TRACE", ERROR_TRACE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("APD_PROF_TRACE", PROF_TRACE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_AF_UNIX", AF_UNIX, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_AF_INET", AF_INET, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_FUNCTION_TRACE", FUNCTION_TRACE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_ARGS_TRACE", ARGS_TRACE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_ASSIGNMENT_TRACE", ASSIGNMENT_TRACE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_STATEMENT_TRACE", STATEMENT_TRACE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_MEMORY_TRACE", MEMORY_TRACE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_TIMING_TRACE", TIMING_TRACE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_SUMMARY_TRACE", SUMMARY_TRACE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_ERROR_TRACE", ERROR_TRACE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("APD_PROF_TRACE", PROF_TRACE, CONST_CS | CONST_PERSISTENT); #ifdef TRACE_ZEND_COMPILE /* we can trace the time to compile things. */ - /* gettimeofday(&APD_GLOBALS(last_call), NULL); */ + gettimeofday(&APD_GLOBALS(req_begin), NULL); +// gettimeofday(&APD_GLOBALS(last_call), NULL); old_compile_file = zend_compile_file; zend_compile_file = apd_compile_file; #endif old_execute = zend_execute; zend_execute = apd_execute; - /* if the zend_execute_internal pointer exists, use it to trace 'built-ins*/ +/* if the zend_execute_internal pointer exists, use it to trace 'built-ins*/ #if ZEND_EXTENSION_API_NO>=20020731 - zend_execute_internal = apd_execute_internal; + zend_execute_internal = apd_execute_internal; #endif + old_zend_error_cb = zend_error_cb; + zend_error_cb = apd_error_cb; return SUCCESS; } @@ -520,87 +730,112 @@ ZEND_API void apd_execute(zend_op_array *op_array TSRMLS_DC) { char *fname = NULL; - char *tmp; + char *tmp; void **p; int argCount; + apd_stack_t* argStack; zval **object_ptr_ptr; - - fname = apd_get_active_function_name(op_array TSRMLS_CC); - trace_function_entry(EG(function_table), fname, ZEND_USER_FUNCTION, - zend_get_executed_filename(TSRMLS_C), - zend_get_executed_lineno(TSRMLS_C)); + fname = apd_get_active_function_name(op_array TSRMLS_CC); + traceFunctionEntry( EG(function_table), fname, ZEND_USER_FUNCTION, + zend_get_executed_filename(TSRMLS_C), + zend_get_executed_lineno(TSRMLS_C)); old_execute(op_array TSRMLS_CC); - trace_function_exit(fname); + traceFunctionExit(fname); efree(fname); - apd_interactive(); + apd_interactive(); } +#if ZEND_EXTENSION_API_NO > 20020429 ZEND_API void apd_execute_internal(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC) { char *fname = NULL; - char *tmp; + char *tmp; void **p; int argCount; + apd_stack_t* argStack; zval **object_ptr_ptr; - - fname = apd_get_active_function_name(EG(current_execute_data)->op_array TSRMLS_CC); - trace_function_entry(EG(function_table), fname, ZEND_INTERNAL_FUNCTION, - zend_get_executed_filename(TSRMLS_C), - zend_get_executed_lineno(TSRMLS_C)); - execute_internal(execute_data_ptr, return_value_used TSRMLS_CC); - trace_function_exit(fname); + fname = apd_get_active_function_name(EG(current_execute_data)->op_array TSRMLS_CC); + traceFunctionEntry( EG(function_table), fname, ZEND_INTERNAL_FUNCTION, + zend_get_executed_filename(TSRMLS_C), + zend_get_executed_lineno(TSRMLS_C)); + ((zend_internal_function *) execute_data_ptr->function_state.function)->handler(execute_data_ptr->opline->extended_value, execute_data_ptr->Ts[execute_data_ptr->opline->result.u.var].var.ptr, execute_data_ptr->object.ptr, return_value_used TSRMLS_CC); + traceFunctionExit(fname); efree(fname); - apd_interactive(); + apd_interactive(); } +#endif PHP_RINIT_FUNCTION(apd) { - APD_GLOBALS(output).header = apd_pprof_output_header; - APD_GLOBALS(output).footer = apd_pprof_output_footer; - APD_GLOBALS(output).file_reference = apd_pprof_output_file_reference; - APD_GLOBALS(output).elapsed_time = apd_pprof_output_elapsed_time; - APD_GLOBALS(output).declare_function = apd_pprof_output_declare_function; - APD_GLOBALS(output).enter_function = apd_pprof_output_enter_function; - APD_GLOBALS(output).exit_function = apd_pprof_output_exit_function; - + gettimeofday(&APD_GLOBALS(req_begin), NULL); +// gettimeofday(&APD_GLOBALS(last_call), NULL); +// APD_GLOBALS(last_mem_header) = AG(head)->pLast; +// APD_GLOBALS(last_pmem_header) = AG(phead)->pLast; APD_GLOBALS(dump_file) = stderr; APD_GLOBALS(dump_sock_id) = 0; APD_GLOBALS(bitmask) = 0; + APD_GLOBALS(summary) = (HashTable*) emalloc(sizeof(HashTable)); + APD_GLOBALS(file_summary) = (HashTable*) emalloc(sizeof(HashTable)); + APD_GLOBALS(file_index) = 0; APD_GLOBALS(interactive_mode) = 0; APD_GLOBALS(ignore_interactive) = 0; - gettimeofday(&APD_GLOBALS(last_clock), NULL); - getrusage(RUSAGE_SELF, &APD_GLOBALS(last_ru)); - APD_GLOBALS(first_ru) = APD_GLOBALS(last_ru); - APD_GLOBALS(first_clock) = APD_GLOBALS(last_clock); - - /** - * These values must start from 1 because they are stored in the hashtable, and - * Zend will try to free them, and die freeing a NULL pointer otherwise. - */ - APD_GLOBALS(file_index) = 1; - APD_GLOBALS(function_index) = 1; - + APD_GLOBALS(lastclock) = times(&APD_GLOBALS(lasttms)); + memcpy(&APD_GLOBALS(firsttms), &APD_GLOBALS(lasttms), sizeof(struct tms)); + APD_GLOBALS(firstclock) = APD_GLOBALS(lastclock); + gettimeofday(&APD_GLOBALS(lasttime), NULL); + zend_hash_init(APD_GLOBALS(summary), 0, NULL, NULL, 0); + zend_hash_init(APD_GLOBALS(file_summary), 0, NULL, NULL, 0); + initializeTracer(); return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(apd) { - APD_GLOBALS(output).footer(); - if (APD_GLOBALS(pprof_file)) { - fclose(APD_GLOBALS(pprof_file)); - } - - if (APD_GLOBALS(dump_sock_id)) { - close(APD_GLOBALS(dump_sock_id)); - /* bit academic - but may as well */ - APD_GLOBALS(dump_sock_id)=0; - } - - zend_hash_clean(APD_GLOBALS(function_summary)); - zend_hash_clean(APD_GLOBALS(file_summary)); - + if(APD_GLOBALS(bitmask)) + { + struct timeval req_end, elapsed; + time_t starttime; + + starttime = time(0); + gettimeofday(&req_end, NULL); + timevaldiff(&req_end, &APD_GLOBALS(req_begin), &elapsed); + apd_dump_fprintf("(%3d.%06d): RSHUTDOWN called - end of trace\n", elapsed.tv_sec, elapsed.tv_usec); + apd_dump_fprintf("---------------------------------------------------------------------------\n"); + apd_dump_fprintf("Process Pid (%d)\n", getpid()); + if(APD_GLOBALS(bitmask) & SUMMARY_TRACE) { + printUnsortedSummary(elapsed TSRMLS_CC); + } + apd_dump_fprintf("---------------------------------------------------------------------------\n"); + apd_dump_fprintf("Trace Ended at %s", ctime(&starttime)); + apd_dump_fprintf("---------------------------------------------------------------------------\n"); + } + shutdownTracer(); + if (APD_GLOBALS(dump_file)) { + fclose(APD_GLOBALS(dump_file)); + } + if(APD_GLOBALS(pprof_file)) { + struct tms endtp; + clock_t endclock; + endclock = times(&endtp); + apd_pprof_fprintf("END_TRACE\n"); + apd_pprof_fprintf("total_user=%d\ntotal_sys=%d\ntotal_wall=%d\n", + endtp.tms_utime - APD_GLOBALS(firsttms).tms_utime, + endtp.tms_stime - APD_GLOBALS(firsttms).tms_stime, + endclock - APD_GLOBALS(firstclock)); + apd_pprof_fprintf("END_FOOTER\n"); + fclose(APD_GLOBALS(pprof_file)); + } + if (APD_GLOBALS(dump_sock_id)) { + close(APD_GLOBALS(dump_sock_id)); + /* bit academic - but may as well */ + APD_GLOBALS(dump_sock_id)=0; + } + zend_hash_destroy(APD_GLOBALS(summary)); + zend_hash_destroy(APD_GLOBALS(file_summary)); + efree(APD_GLOBALS(summary)); + efree(APD_GLOBALS(file_summary)); return SUCCESS; } @@ -613,9 +848,179 @@ } -/* --------------------------------------------------------------------------- - PHP Extension Functions - --------------------------------------------------------------------------- */ +// --------------------------------------------------------------------------- +// PHP Extension Functions +// --------------------------------------------------------------------------- + +PHP_FUNCTION(apd_callstack) +{ + CallStack* stack; + void** elements; + int numElements; + int i; + + if (ZEND_NUM_ARGS() != 0) { + WRONG_PARAM_COUNT; + } + + stack = (CallStack*) APD_GLOBALS(stack); + elements = apd_stack_toarray(stack); + numElements = apd_stack_getsize(stack); + + if (array_init(return_value) != SUCCESS) { + assert(0); // XXX: handle failure + } + + for (i = numElements-2; i >= 0; i--) { + CallStackEntry* stackEntry; + zval* phpEntry; + zval* args; + int j; + + stackEntry = (CallStackEntry*) elements[i]; + + MAKE_STD_ZVAL(phpEntry); + if (array_init(phpEntry) != SUCCESS) { + assert(0); // XXX: handle failure + } + + add_index_string(phpEntry, 0, stackEntry->functionName, 1); + add_index_string(phpEntry, 1, stackEntry->filename, 1); + add_index_long (phpEntry, 2, stackEntry->lineNum); + + MAKE_STD_ZVAL(args); + if (array_init(args) != SUCCESS) { + assert(0); // XXX: handle failure + } + + for (j = 0; j < stackEntry->numArgs; j++) { + add_index_string(args, j, stackEntry->args[j].strVal, 1); + } + zend_hash_index_update(Z_ARRVAL_P(phpEntry), 3, + &args, sizeof(zval*), NULL); + + zend_hash_index_update(Z_ARRVAL_P(return_value), numElements - i - 2, + &phpEntry, sizeof(zval*), NULL); + +// add_index_string(return_value, i, stackEntry->functionName, 1); + } + +// RETURN_TRUE; +} + +PHP_FUNCTION(apd_cluck) +{ + CallStack* stack; + void** elements; + int numElements; + int i; + char *errstr = NULL, *delimiter = NULL; + int errstr_len = 0, delimiter_len = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &errstr, &errstr_len, &delimiter, &delimiter_len) == FAILURE) { + return; + } + zend_printf("%s at %s line %d%s", + errstr ? errstr : "clucked", + zend_get_executed_filename(TSRMLS_C), + zend_get_executed_lineno(TSRMLS_C), + delimiter ? delimiter : "<BR />\n"); + + stack = (CallStack*) APD_GLOBALS(stack); + elements = apd_stack_toarray(stack); + numElements = apd_stack_getsize(stack); + + + for (i = numElements-2; i >= 0; i--) { + CallStackEntry* stackEntry; + zval* phpEntry; + zval* args; + int j; + + stackEntry = (CallStackEntry*) elements[i]; + + zend_printf("%s(", stackEntry->functionName); + for (j = 0; j < stackEntry->numArgs; j++) { + if (j < stackEntry->numArgs - 1) { + zend_printf("%s,", stackEntry->args[j].strVal); + } + else { + zend_printf("%s", stackEntry->args[j].strVal); + } + } + zend_printf(") called at %s line %d%s", + stackEntry->filename, + stackEntry->lineNum, + delimiter ? delimiter : "<BR />\n"); + } + +// RETURN_TRUE; +} + +PHP_FUNCTION(apd_croak) +{ + CallStack* stack; + void** elements; + int numElements; + int i; + char *errstr = NULL, *delimiter = NULL; + int errstr_len = 0, delimiter_len = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &errstr, &errstr_len, &delimiter, &delimiter_len) == FAILURE) { + return; + } + stack = (CallStack*) APD_GLOBALS(stack); + elements = apd_stack_toarray(stack); + numElements = apd_stack_getsize(stack); + + zend_printf("%s at %s line %d%s", + errstr ? errstr : "croaked", + zend_get_executed_filename(TSRMLS_C), + zend_get_executed_lineno(TSRMLS_C), + delimiter ? delimiter : "<BR />\n"); + + for (i = numElements-2; i >= 0; i--) { + CallStackEntry* stackEntry; + zval* phpEntry; + zval* args; + int j; + + stackEntry = (CallStackEntry*) elements[i]; + + zend_printf("%s(", stackEntry->functionName); + for (j = 0; j < stackEntry->numArgs; j++) { + if (j < stackEntry->numArgs - 1) { + zend_printf("%s,", stackEntry->args[j].strVal); + } + else { + zend_printf("%s", stackEntry->args[j].strVal); + } + } + zend_printf(") called at %s line %d%s", + stackEntry->filename, + stackEntry->lineNum, + delimiter ? delimiter : "<BR />\n"); + } + zend_bailout(); +} + +PHP_FUNCTION(apd_dump_regular_resources) +{ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) { + return; + } + + __apd_dump_regular_resources(return_value TSRMLS_CC); +} + +PHP_FUNCTION(apd_dump_persistent_resources) +{ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) { + return; + } + + __apd_dump_persistent_resources(return_value TSRMLS_CC); +} #define TEMP_OVRD_FUNC_NAME "__overridden__" PHP_FUNCTION(override_function) @@ -626,10 +1031,10 @@ if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &z_function_name, &z_function_args, - &z_function_code) == FAILURE) - { - ZEND_WRONG_PARAM_COUNT(); - } + &z_function_code) == FAILURE) + { + ZEND_WRONG_PARAM_COUNT(); + } convert_to_string_ex(z_function_name); convert_to_string_ex(z_function_args); @@ -642,7 +1047,7 @@ + Z_STRLEN_PP(z_function_code); eval_code = (char *) emalloc(eval_code_length); sprintf(eval_code, "function " TEMP_OVRD_FUNC_NAME "(%s){%s}", - Z_STRVAL_PP(z_function_args), Z_STRVAL_PP(z_function_code)); + Z_STRVAL_PP(z_function_args), Z_STRVAL_PP(z_function_code)); eval_name = zend_make_compiled_string_description("runtime-created override function" TSRMLS_CC); retval = zend_eval_string(eval_code, NULL, eval_name TSRMLS_CC); efree(eval_code); @@ -652,20 +1057,20 @@ zend_function *func; if (zend_hash_find(EG(function_table), TEMP_OVRD_FUNC_NAME, - sizeof(TEMP_OVRD_FUNC_NAME), (void **) &func) == FAILURE) - { - zend_error(E_ERROR, "%s() temporary function name not present in global function_table", get_active_function_name(TSRMLS_C)); - RETURN_FALSE; - } + sizeof(TEMP_OVRD_FUNC_NAME), (void **) &func) == FAILURE) + { + zend_error(E_ERROR, "%s() temporary function name not present in global function_table", get_active_function_name(TSRMLS_C)); + RETURN_FALSE; + } function_add_ref(func); zend_hash_del(EG(function_table), Z_STRVAL_PP(z_function_name), - Z_STRLEN_PP(z_function_name) + 1); + Z_STRLEN_PP(z_function_name) + 1); if(zend_hash_add(EG(function_table), Z_STRVAL_PP(z_function_name), - Z_STRLEN_PP(z_function_name) + 1, func, sizeof(zend_function), - NULL) == FAILURE) - { - RETURN_FALSE; - } + Z_STRLEN_PP(z_function_name) + 1, func, sizeof(zend_function), + NULL) == FAILURE) + { + RETURN_FALSE; + } RETURN_TRUE; } else { @@ -675,145 +1080,211 @@ PHP_FUNCTION(rename_function) { - zval **z_orig_fname, **z_new_fname; - zend_function *func, *dummy_func; - - if( ZEND_NUM_ARGS() != 2 || - zend_get_parameters_ex(2, &z_orig_fname, &z_new_fname) == FAILURE ) - { - ZEND_WRONG_PARAM_COUNT(); - } - - convert_to_string_ex(z_orig_fname); - convert_to_string_ex(z_new_fname); + zval **z_orig_fname, **z_new_fname; + zend_function *func, *dummy_func; - if(zend_hash_find(EG(function_table), Z_STRVAL_PP(z_orig_fname), - Z_STRLEN_PP(z_orig_fname) + 1, (void **) &func) == FAILURE) - { - zend_error(E_WARNING, "%s(%s, %s) failed: %s does not exist!", - get_active_function_name(TSRMLS_C), - Z_STRVAL_PP(z_orig_fname), Z_STRVAL_PP(z_new_fname), - Z_STRVAL_PP(z_orig_fname)); - RETURN_FALSE; - } - if(zend_hash_find(EG(function_table), Z_STRVAL_PP(z_new_fname), - Z_STRLEN_PP(z_new_fname) + 1, (void **) &dummy_func) == SUCCESS) - { - zend_error(E_WARNING, "%s(%s, %s) failed: %s already exists!", - get_active_function_name(TSRMLS_C), - Z_STRVAL_PP(z_orig_fname), Z_STRVAL_PP(z_new_fname), - Z_STRVAL_PP(z_new_fname)); - RETURN_FALSE; - } - if(zend_hash_add(EG(function_table), Z_STRVAL_PP(z_new_fname), - Z_STRLEN_PP(z_new_fname) + 1, func, sizeof(zend_function), - NULL) == FAILURE) - { - zend_error(E_WARNING, "%s() failed to insert %s into EG(function_table)", - get_active_function_name(TSRMLS_C), - Z_STRVAL_PP(z_new_fname)); - RETURN_FALSE; - } - if(zend_hash_del(EG(function_table), Z_STRVAL_PP(z_orig_fname), - Z_STRLEN_PP(z_orig_fname) + 1) == FAILURE) - { - zend_error(E_WARNING, "%s() failed to remove %s from function table", - get_active_function_name(TSRMLS_C), - Z_STRVAL_PP(z_orig_fname)); - zend_hash_del(EG(function_table), Z_STRVAL_PP(z_new_fname), - Z_STRLEN_PP(z_new_fname) + 1); - RETURN_FALSE; - } + if( ZEND_NUM_ARGS() != 2 || + zend_get_parameters_ex(2, &z_orig_fname, &z_new_fname) == FAILURE ) + { + ZEND_WRONG_PARAM_COUNT(); + } + + convert_to_string_ex(z_orig_fname); + convert_to_string_ex(z_new_fname); + + if(zend_hash_find(EG(function_table), Z_STRVAL_PP(z_orig_fname), + Z_STRLEN_PP(z_orig_fname) + 1, (void **) &func) == FAILURE) + { + zend_error(E_WARNING, "%s(%s, %s) failed: %s does not exist!", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_orig_fname), Z_STRVAL_PP(z_new_fname), + Z_STRVAL_PP(z_orig_fname)); + RETURN_FALSE; + } + if(zend_hash_find(EG(function_table), Z_STRVAL_PP(z_new_fname), + Z_STRLEN_PP(z_new_fname) + 1, (void **) &dummy_func) == SUCCESS) + { + zend_error(E_WARNING, "%s(%s, %s) failed: %s already exists!", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_orig_fname), Z_STRVAL_PP(z_new_fname), + Z_STRVAL_PP(z_new_fname)); + RETURN_FALSE; + } + if(zend_hash_add(EG(function_table), Z_STRVAL_PP(z_new_fname), + Z_STRLEN_PP(z_new_fname) + 1, func, sizeof(zend_function), + NULL) == FAILURE) + { + zend_error(E_WARNING, "%s() failed to insert %s into EG(function_table)", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_new_fname)); + RETURN_FALSE; + } + if(zend_hash_del(EG(function_table), Z_STRVAL_PP(z_orig_fname), + Z_STRLEN_PP(z_orig_fname) + 1) == FAILURE) + { + zend_error(E_WARNING, "%s() failed to remove %s from function table", + get_active_function_name(TSRMLS_C), + Z_STRVAL_PP(z_orig_fname)); + zend_hash_del(EG(function_table), Z_STRVAL_PP(z_new_fname), + Z_STRLEN_PP(z_new_fname) + 1); + RETURN_FALSE; + } + RETURN_TRUE; +} + +PHP_FUNCTION(dump_function_table) +{ + Bucket *p; + HashTable *hash; + + hash = EG(function_table); + p = hash->pListHead; + while(p != NULL) { + printf("key (%s) length (%d)\n", p->arKey, p->nKeyLength); + p = p->pListNext; + } RETURN_TRUE; } -void apd_pprof_header(TSRMLS_D) { - char *fname = "main"; - char *filename; - int linenum, *filenum, *fnum; - - APD_GLOBALS(output).header(); - - filename = zend_get_executed_filename(TSRMLS_C); - linenum = zend_get_executed_lineno(TSRMLS_C); - - fnum = (int *) emalloc(sizeof(int)); - *fnum = APD_GLOBALS(function_index)++; - zend_hash_add(APD_GLOBALS(function_summary), fname, strlen(fname)+1, fnum, sizeof(int), NULL); - - filenum = (int *) emalloc(sizeof(int)); - *filenum = APD_GLOBALS(file_index)++; - zend_hash_add(APD_GLOBALS(file_summary), (char *) filename, strlen(filename)+1, filenum, sizeof(int), NULL); +void apd_dump_session_start() { + time_t starttime; + TSRMLS_FETCH(); - APD_GLOBALS(output).file_reference(*filenum, filename); - APD_GLOBALS(output).declare_function(*fnum, fname, ZEND_USER_FUNCTION); - APD_GLOBALS(output).enter_function(*fnum, *filenum, linenum); + starttime = time(0); + apd_dump_fprintf("\n\nAPD - Advanced PHP Debugger Trace File\n"); + apd_dump_fprintf("---------------------------------------------------------------------------\n"); + apd_dump_fprintf("Process Pid (%d)\n", getpid()); + apd_dump_fprintf("Trace Begun at %s", ctime(&starttime)); + apd_dump_fprintf("---------------------------------------------------------------------------\n"); + apd_dump_fprintf("( 0.000000): apd_set_session_trace called at %s:%d\n", zend_get_executed_filename(TSRMLS_C), zend_get_executed_lineno(TSRMLS_C)); } -PHP_FUNCTION(apd_set_browser_trace) -{ - if (ZEND_NUM_ARGS() > 1) { - ZEND_WRONG_PARAM_COUNT(); - } - - APD_GLOBALS(pproftrace) = 1; - - APD_GLOBALS(output).header = apd_summary_output_header; - APD_GLOBALS(output).footer = apd_summary_output_footer; - APD_GLOBALS(output).file_reference = apd_summary_output_file_reference; - APD_GLOBALS(output).elapsed_time = apd_summary_output_elapsed_time; - APD_GLOBALS(output).declare_function = apd_summary_output_declare_function; - APD_GLOBALS(output).enter_function = apd_summary_output_enter_function; - APD_GLOBALS(output).exit_function = apd_summary_output_exit_function; - apd_pprof_header(TSRMLS_C); -} -PHP_FUNCTION(apd_set_pprof_trace) +PHP_FUNCTION(apd_set_session_trace) { int issock =0; int socketd = 0; int path_len; char *dumpdir; char *path; - zval **z_dumpdir; + zval **z_bitmask, **z_dumpdir; - if(ZEND_NUM_ARGS() > 1 ) + if(ZEND_NUM_ARGS() > 2 || ZEND_NUM_ARGS() == 0) + { + ZEND_WRONG_PARAM_COUNT(); + } + if(ZEND_NUM_ARGS() == 1) + { + if(zend_get_parameters_ex(1, &z_bitmask) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } - if(ZEND_NUM_ARGS() == 0) - { - if(APD_GLOBALS(dumpdir)) { - dumpdir = APD_GLOBALS(dumpdir); - } - else { - zend_error(E_WARNING, "%s() no dumpdir specified", - get_active_function_name(TSRMLS_C)); - RETURN_FALSE; - } - APD_GLOBALS(pproftrace) = 1; + if(APD_GLOBALS(dumpdir)) { + dumpdir = APD_GLOBALS(dumpdir); + } + else { + zend_error(E_WARNING, "%s() no dumpdir specified", + get_active_function_name(TSRMLS_C)); + RETURN_FALSE; } + convert_to_long(*z_bitmask); + APD_GLOBALS(bitmask) = Z_LVAL_PP(z_bitmask); + } else { - if(zend_get_parameters_ex(1, &z_dumpdir) == FAILURE) - { - ZEND_WRONG_PARAM_COUNT(); - } - APD_GLOBALS(pproftrace) = 1; + if(zend_get_parameters_ex(2, &z_bitmask, &z_dumpdir) == FAILURE) + { + ZEND_WRONG_PARAM_COUNT(); + } + convert_to_long(*z_bitmask); + APD_GLOBALS(bitmask) = Z_LVAL_PP(z_bitmask); convert_to_string_ex(z_dumpdir); dumpdir = Z_STRVAL_PP(z_dumpdir); } - - path_len = strlen(dumpdir) + 1 + strlen("pprof.") + 5; + path_len = strlen(dumpdir) + 1 + strlen("apd_dump_") + 5; path = (char *) emalloc((path_len + 1)* sizeof(char) ); - snprintf(path, path_len + 1, "%s/pprof.%05d", dumpdir, getpid()); - if((APD_GLOBALS(pprof_file) = fopen(path, "a")) == NULL) { + snprintf(path, path_len + 1, "%s/apd_dump_%05d", dumpdir, getpid()); + if((APD_GLOBALS(dump_file) = fopen(path, "a")) == NULL) { zend_error(E_ERROR, "%s() failed to open %s for tracing", get_active_function_name(TSRMLS_C), path); - } + } efree(path); + apd_dump_session_start(); +} + +void apd_pprof_header(TSRMLS_D) { + summary_t *summaryStats; + char *fname = "main"; + char *filename; + int linenum, *filenum; + filename = zend_get_executed_filename(TSRMLS_C); + linenum = zend_get_executed_lineno(TSRMLS_C); + apd_pprof_fprintf("#Pprof [APD] v0.9\n"); +#ifdef _SC_CLK_TCK + apd_pprof_fprintf("hz=%d\n", sysconf(_SC_CLK_TCK)); +#elif defined(CLOCKS_PER_SEC) /* 1000 on windows */ + apd_pprof_fprintf("hz=%d\n", CLOCKS_PER_SEC); +#endif + apd_pprof_fprintf("caller=%s\n",zend_get_executed_filename(TSRMLS_C)); + apd_pprof_fprintf("\nEND_HEADER\n"); + summaryStats = (summary_t *) emalloc(sizeof(summary_t)); + summaryStats->calls = 1; + summaryStats->index = 1; + summaryStats->totalTime = 0; + APD_GLOBALS(index) = 1; + zend_hash_add(APD_GLOBALS(summary), fname, strlen(fname) + 1, summaryStats, sizeof(summary_t), NULL); + filenum = (int *) emalloc(sizeof(int)); + *filenum = ++APD_GLOBALS(file_index); + apd_pprof_fprintf("! %d %s\n", *filenum, filename); + zend_hash_add(APD_GLOBALS(file_summary), (char *) filename, + strlen(filename) + 1, filenum, sizeof(int), NULL); + apd_pprof_fprintf("& %d %s %d\n", summaryStats->index, fname, ZEND_USER_FUNCTION); + apd_pprof_fprintf("+ %d %d %d\n", summaryStats->index, *filenum, linenum); +} - apd_pprof_header(TSRMLS_C); +PHP_FUNCTION(apd_set_pprof_trace) +{ + int issock =0; + int socketd = 0; + int path_len; + char *dumpdir; + char *path; + zval **z_dumpdir; + + if(ZEND_NUM_ARGS() > 1 ) + { + ZEND_WRONG_PARAM_COUNT(); + } + if(ZEND_NUM_ARGS() == 0) + { + if(APD_GLOBALS(dumpdir)) { + dumpdir = APD_GLOBALS(dumpdir); + } + else { + zend_error(E_WARNING, "%s() no dumpdir specified", + get_active_function_name(TSRMLS_C)); + RETURN_FALSE; + } + APD_GLOBALS(pproftrace) = 1; + } + else { + if(zend_get_parameters_ex(1, &z_dumpdir) == FAILURE) + { + ZEND_WRONG_PARAM_COUNT(); + } + APD_GLOBALS(pproftrace) = 1; + + convert_to_string_ex(z_dumpdir); + dumpdir = Z_STRVAL_PP(z_dumpdir); + } + path_len = strlen(dumpdir) + 1 + strlen("pprof.") + 5; + path = (char *) emalloc((path_len + 1)* sizeof(char) ); + snprintf(path, path_len + 1, "%s/pprof.%05d", dumpdir, getpid()); + if((APD_GLOBALS(pprof_file) = fopen(path, "a")) == NULL) { + zend_error(E_ERROR, "%s() failed to open %s for tracing", get_active_function_name(TSRMLS_C), path); + } + efree(path); + apd_pprof_header(TSRMLS_C); } @@ -822,182 +1293,296 @@ PHP_FUNCTION(apd_set_session_trace_socket) { - - char *address; - int address_len; - - int sock_domain; - int sock_port; - int bitmask; - int connect_result; + + char *address; + int address_len; + + int sock_domain; + unsigned short sock_port; + int bitmask; + int connect_result; - struct sockaddr_in si; - struct sockaddr_un su; + struct sockaddr_in si; + struct sockaddr_un su; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, - "slll", &address,&address_len, &sock_domain, &sock_port,&bitmask) == FAILURE) - { - return; - } - APD_GLOBALS(bitmask) = bitmask; - APD_GLOBALS(dump_file)=NULL; - - /* now for the socket stuff */ - /* is it already connected ? */ - if (APD_GLOBALS(dump_sock_id) > 0) { - RETURN_TRUE; - } - - if (sock_domain != AF_UNIX && sock_domain != AF_INET) { - php_error(E_WARNING, "%s() invalid socket domain [%d] specified for argument 2, assuming AF_INET", get_active_function_name(TSRMLS_C), sock_domain); - sock_domain = AF_INET; - } - - APD_GLOBALS(dump_sock_id) = socket(sock_domain, SOCK_STREAM, 0); - if (APD_IS_INVALID_SOCKET(APD_GLOBALS(dump_sock_id))) { - /* this may not work = it assumes that 0 would be an invalid socket! */ - APD_GLOBALS(dump_sock_id)=0; - RETURN_FALSE; - } - /* now connect it */ - switch(sock_domain) { - case AF_UNIX: - { - su.sun_family = AF_UNIX; - strncpy (su.sun_path, address, sizeof (su.sun_path)); - if (connect(APD_GLOBALS(dump_sock_id), (struct sockaddr *) &su, SUN_LEN(&su)) < 0) - { - php_error(E_WARNING, "%s() failed to connect to [%s]", get_active_function_name(TSRMLS_C), address); - APD_GLOBALS(dump_sock_id)=0; - RETURN_FALSE; - } - break; - } - case AF_INET: - { - struct hostent *hp; - si.sin_family = AF_INET; - si.sin_port = htons (sock_port); - hp = gethostbyname (address); - if (hp == NULL) - { - php_error(E_WARNING, "%s() failed to get host by name [%s]", get_active_function_name(TSRMLS_C), address); - APD_GLOBALS(dump_sock_id)=0; - RETURN_FALSE; - } - si.sin_addr = *(struct in_addr *) hp->h_addr; - connect_result = connect(APD_GLOBALS(dump_sock_id), (struct sockaddr *) &si, sizeof (si)); - if (0 > connect_result) - { - php_error(E_WARNING, "%s() failed to connect to [%s:%d] %d", get_active_function_name(TSRMLS_C), address,sock_port,connect_result); - APD_GLOBALS(dump_sock_id)=0; - RETURN_FALSE; - } - break; - } - } - apd_summary_output_header(); - RETURN_TRUE; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, + "slll", &address,&address_len, &sock_domain, &sock_port,&bitmask) == FAILURE) + { + return; + } + APD_GLOBALS(bitmask) = bitmask; + APD_GLOBALS(dump_file)=NULL; + + /* now for the socket stuff */ + /* is it already connected ? */ + if (APD_GLOBALS(dump_sock_id) > 0) { + RETURN_TRUE; + } + + if (sock_domain != AF_UNIX && sock_domain != AF_INET) { + php_error(E_WARNING, "%s() invalid socket domain [%d] specified for argument 2, assuming AF_INET", get_active_function_name(TSRMLS_C), sock_domain); + sock_domain = AF_INET; + } + + APD_GLOBALS(dump_sock_id) = socket(sock_domain, SOCK_STREAM, 0); + if (APD_IS_INVALID_SOCKET(APD_GLOBALS(dump_sock_id))) { + /* this may not work = it assumes that 0 would be an invalid socket! */ + APD_GLOBALS(dump_sock_id)=0; + RETURN_FALSE; + } + /* now connect it */ + switch(sock_domain) { + case AF_UNIX: + { + su.sun_family = AF_UNIX; + strncpy (su.sun_path, address, sizeof (su.sun_path)); + if (connect(APD_GLOBALS(dump_sock_id), (struct sockaddr *) &su, SUN_LEN(&su)) < 0) + { + php_error(E_WARNING, "%s() failed to connect to [%s]", get_active_function_name(TSRMLS_C), address); + APD_GLOBALS(dump_sock_id)=0; + RETURN_FALSE; + } + break; + } + case AF_INET: + { + struct hostent *hp; + si.sin_family = AF_INET; + si.sin_port = htons (sock_port); + hp = gethostbyname (address); + if (hp == NULL) + { + php_error(E_WARNING, "%s() failed to get host by name [%s]", get_active_function_name(TSRMLS_C), address); + APD_GLOBALS(dump_sock_id)=0; + RETURN_FALSE; + } + si.sin_addr = *(struct in_addr *) hp->h_addr; + connect_result = connect(APD_GLOBALS(dump_sock_id), (struct sockaddr *) &si, sizeof (si)); + if (0 > connect_result) + { + php_error(E_WARNING, "%s() failed to connect to [%s:%d] %d", get_active_function_name(TSRMLS_C), address,sock_port,connect_result); + APD_GLOBALS(dump_sock_id)=0; + RETURN_FALSE; + } + break; + } + } + apd_dump_session_start(); + RETURN_TRUE; +} + +/* {{{ proto bool apd_set_session(int mask) + Changes or sets the session trace mask*/ + +PHP_FUNCTION(apd_set_session) +{ + int bitmask; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, + "l", &bitmask) == FAILURE) { + return; + } + APD_GLOBALS(bitmask) = bitmask; + RETURN_TRUE; } - -/* }}} */ /* {{{ proto void apd_breakpoint() Starts interactive mode*/ PHP_FUNCTION(apd_breakpoint) { - int bitmask; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, - "l", &bitmask) == FAILURE) { - return; - } - APD_GLOBALS(bitmask) = bitmask; - APD_GLOBALS(interactive_mode) = 1; - RETURN_TRUE; + int bitmask; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, + "l", &bitmask) == FAILURE) { + return; + } + APD_GLOBALS(bitmask) = bitmask; + APD_GLOBALS(interactive_mode) = 1; + RETURN_TRUE; } - -/* }}} */ -/* {{{ proto void apd_continue() +/* {{{ proto void apd_breakpoint() Stops interactive mode - normally called by interactive interpreter! */ PHP_FUNCTION(apd_continue) { - int bitmask; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, - "l", &bitmask) == FAILURE) { - return; - } - APD_GLOBALS(bitmask) = bitmask; - APD_GLOBALS(interactive_mode) = 0; - RETURN_TRUE; + int bitmask; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, + "l", &bitmask) == FAILURE) { + return; + } + APD_GLOBALS(bitmask) = bitmask; + APD_GLOBALS(interactive_mode) = 0; + RETURN_TRUE; } - -/* }}} */ /* {{{ proto apd_echo(string output) sends string to debugger - normally called by interactive interpreter!*/ - + PHP_FUNCTION(apd_echo) -{ - char *str; - int str_len; - +{ + + char *str; + int str_len; + - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str,&str_len) == FAILURE) { - return; - } - if (str_len > 0) { - /* php_error(E_WARNING, "apd echoing %s", str); */ - if ( APD_GLOBALS(dump_sock_id) > 0) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str,&str_len) == FAILURE) { + return; + } + if (str_len > 0) { +// php_error(E_WARNING, "apd echoing %s", str); + if ( APD_GLOBALS(dump_sock_id) > 0) { #ifndef PHP_WIN32 - /* Unix bit */ - write(APD_GLOBALS(dump_sock_id), str, str_len); - write(APD_GLOBALS(dump_sock_id), "\n", 2); + /* Unix bit */ + write(APD_GLOBALS(dump_sock_id), str, str_len); + write(APD_GLOBALS(dump_sock_id), "\n", 2); #else - /* Win32 bit */ - send(APD_GLOBALS(dump_sock_id), str, str_len, 0); - send(APD_GLOBALS(dump_sock_id), "\n", 2, 0); + /* Win32 bit */ + send(APD_GLOBALS(dump_sock_id), str, str_len, 0); + send(APD_GLOBALS(dump_sock_id), "\n", 2, 0); #endif - } - apd_dump_fprintf("%s\n",str); - /* efree(str); */ - } - RETURN_TRUE; + } + apd_dump_fprintf("%s\n",str); +// efree(str); + } + RETURN_TRUE; } - -/* }}} */ // --------------------------------------------------------------------------- // Zend Extension Functions // --------------------------------------------------------------------------- +static const char* getOpTypeName(int optype) +{ + switch (optype) { + case IS_CONST: return "IS_CONST"; + case IS_TMP_VAR: return "IS_TMP_VAR"; + case IS_VAR: return "IS_VAR"; + case IS_UNUSED: return "IS_UNUSED"; + default: return "Unkown"; + } +} + +static const char* getDatatypeName(int type) +{ + switch (type) { + case IS_NULL: return "IS_NULL"; + case IS_LONG: return "IS_LONG"; + case IS_DOUBLE: return "IS_DOUBLE"; + case IS_STRING: return "IS_STRING"; + case IS_ARRAY: return "IS_ARRAY"; + case IS_OBJECT: return "IS_OBJECT"; + case IS_BOOL: return "IS_BOOL"; + case IS_RESOURCE: return "IS_RESOURCE"; + case IS_CONSTANT: return "IS_CONSTANT"; + case IS_CONSTANT_ARRAY: return "IS_CONSTANT_ARRAY"; + default: assert(0); + } +} +static void printZval(FILE* out, zval* zv) +{ + fprintf(out, "\t\t\ttype = %s\n", getDatatypeName(zv->type)); + fprintf(out, "\t\t\tis_ref = %d\n", zv->is_ref); + fprintf(out, "\t\t\trefcount = %d\n", zv->refcount); + fprintf(out, "\t\t\tvalue = "); + switch (zv->type) { + case IS_NULL: fprintf(out, "(null)"); break; + case IS_LONG: fprintf(out, "%ld", zv->value.lval); break; + case IS_DOUBLE: fprintf(out, "%g", zv->value.dval); break; + case IS_STRING: fprintf(out, "%s", zv->value.str.val); break; + case IS_ARRAY: fprintf(out, "(array)"); break; + case IS_OBJECT: fprintf(out, "(object)"); break; + case IS_BOOL: fprintf(out, "%d", zv->value.lval); break; + case IS_RESOURCE: fprintf(out, "(resource)"); break; + case IS_CONSTANT: fprintf(out, "%s", zv->value.str.val); break; + case IS_CONSTANT_ARRAY: fprintf(out, "(constant array)"); break; + default: assert(0); + } + fprintf(out, "\n"); +} + +static void printZnode(FILE* out, const char* name, znode* zn) +{ + fprintf(out, "\t\t%s = %s\n", name, getOpTypeName(zn->op_type)); + switch (zn->op_type) { + case IS_CONST: + printZval(out, &zn->u.constant); + break; + case IS_TMP_VAR: + case IS_VAR: + case IS_UNUSED: + default: + break; + } +} + +static void dumpOpArray(zend_op_array* op_array) +{ + int i; + + if (!op_array) { + return; + } + + fprintf(stderr, "op_array (%d opcodes)\n", op_array->last); + fprintf(stderr, "function_name: %s\n", op_array->function_name); + fprintf(stderr, "last: %u\n", op_array->last); + fprintf(stderr, "size: %u\n", op_array->size); + fprintf(stderr, "T: %u\n", op_array->T); + fprintf(stderr, "last_brk_cont: %u\n", op_array->last_brk_cont); + fprintf(stderr, "current_brk_cont: %u\n", op_array->current_brk_cont); +#if 0 + fprintf(stderr, "start_op_number: %d\n", op_array->start_op_number); + fprintf(stderr, "end_op_number: %d\n", op_array->end_op_number); + fprintf(stderr, "last_executed_op_number: %d\n", op_array->last_executed_op_number); +#endif + for (i = 0; i < op_array->last; i++) { + printZnode(stderr, "result", &op_array->opcodes[i].result); + printZnode(stderr, "op1 ", &op_array->opcodes[i].op1); + printZnode(stderr, "op2 ", &op_array->opcodes[i].op2); + } +} + +void printUnsortedSummary(struct timeval elapsed TSRMLS_DC) +{ + Bucket *p; + summary_t* summary; + uint i; + + apd_dump_fprintf("%% time usecs usecs/call calls function\n"); + apd_dump_fprintf("----- ----- ---------- ----- --------\n"); + p = APD_GLOBALS(summary)->pListHead; + while(p != NULL) { + summary = (summary_t*) p->pData; + apd_dump_fprintf("%3.2f %10d %10d %8d %s\n", ((float)summary->totalTime)/((float)(100000*elapsed.tv_sec + elapsed.tv_usec)) * 100.0, summary->totalTime, summary->totalTime/summary->calls, summary->calls, p->arKey); + p = p->pListNext; + } +} + ZEND_DLEXPORT void onStatement(zend_op_array *op_array) { - TSRMLS_FETCH(); - if (APD_GLOBALS(ignore_interactive) == 1) return; - if(APD_GLOBALS(bitmask) & STATEMENT_TRACE) { - apd_dump_fprintf("statement: %s:%d\n", - zend_get_executed_filename(TSRMLS_C), - zend_get_executed_lineno(TSRMLS_C)); - apd_interactive(); - } + TSRMLS_FETCH(); + if (APD_GLOBALS(ignore_interactive) == 1) return; + if(APD_GLOBALS(bitmask) & STATEMENT_TRACE) { + apd_dump_fprintf("statement: %s:%d\n", + zend_get_executed_filename(TSRMLS_C), + zend_get_executed_lineno(TSRMLS_C)); + apd_interactive(); + } } -/* --------------------------------------------------------------------------- - Zend Extension Support - --------------------------------------------------------------------------- */ +// --------------------------------------------------------------------------- +// Zend Extension Support +// --------------------------------------------------------------------------- int apd_zend_startup(zend_extension *extension) { TSRMLS_FETCH(); - CG(extended_info) = 1; /* XXX: this is ridiculous */ + CG(extended_info) = 1; // XXX: this is ridiculous return zend_startup_module(&apd_module_entry); } ZEND_DLEXPORT void apd_zend_shutdown(zend_extension *extension) { - /* Do nothing. */ + // Do nothing. } #ifndef ZEND_EXT_API @@ -1008,7 +1593,7 @@ ZEND_DLEXPORT zend_extension zend_extension_entry = { "Advanced PHP Debugger (APD)", APD_VERSION, - "George Schlossnagle", + "Daniel Cowgill and George Schlossnagle", "http://pear.php.net/", "", apd_zend_startup, @@ -1017,7 +1602,7 @@ NULL, // deactivate_func_t NULL, // message_handler_func_t NULL, // op_array_handler_func_t - NULL, // statement_handler_func_t + NULL, // statement_handler_func_t NULL, // fcall_begin_handler_func_t NULL, // fcall_end_handler_func_t NULL, // op_array_ctor_func_t @@ -1030,12 +1615,3 @@ #endif }; -/** - * Local Variables: - * indent-tabs-mode: t - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600:fdm=marker - * vim:noet:sw=4:ts=4 - */ http://cvs.php.net/diff.php/pecl/apd/php_apd.h?r1=1.19.4.1&r2=1.19.4.2&ty=u Index: pecl/apd/php_apd.h diff -u pecl/apd/php_apd.h:1.19.4.1 pecl/apd/php_apd.h:1.19.4.2 --- pecl/apd/php_apd.h:1.19.4.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/php_apd.h Wed Mar 10 16:55:36 2004 @@ -1,8 +1,8 @@ /* +----------------------------------------------------------------------+ - | APD Profiler & Debugger | + | APD Profiler & Debugger +----------------------------------------------------------------------+ - | Copyright (c) 2001-2003 Community Connect Inc. | + | Copyright (c) 2001-2002 Community Connect Inc. +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | @@ -14,25 +14,21 @@ +----------------------------------------------------------------------+ | Authors: Daniel Cowgill <dcowgill@communityconnect.com> | | George Schlossnagle <george@lethargy.org> | - | Sterling Hughes <sterling@php.net> | +----------------------------------------------------------------------+ */ #ifndef PHP_APD_H #define PHP_APD_H -#include "php.h" - #if PHP_WIN32 #include "config.w32.h" #else #include "php_config.h" #endif +#include "php.h" #include "php_ini.h" #include "php_globals.h" -#include "ext/standard/php_string.h" -#include "apd_array.h" #include "zend.h" #include "zend_API.h" @@ -49,7 +45,6 @@ #include <netinet/tcp.h> #include <sys/time.h> #include <sys/times.h> -#include <sys/resource.h> #include <unistd.h> #else /* windows */ /* these are from ext/socket -- probably worth just copying the files into apd? */ @@ -69,15 +64,24 @@ // --------------------------------------------------------------------------- /* Declarations of functions to be exported. */ +PHP_FUNCTION(apd_callstack); +PHP_FUNCTION(apd_cluck); +PHP_FUNCTION(apd_croak); +PHP_FUNCTION(apd_dump_regular_resources); +PHP_FUNCTION(apd_dump_persistent_resources); PHP_FUNCTION(override_function); PHP_FUNCTION(rename_function); +PHP_FUNCTION(dump_function_table); +PHP_FUNCTION(apd_set_session_trace); PHP_FUNCTION(apd_set_pprof_trace); -PHP_FUNCTION(apd_set_browser_trace); PHP_FUNCTION(apd_set_session_trace_socket); +PHP_FUNCTION(apd_set_session); PHP_FUNCTION(apd_breakpoint); PHP_FUNCTION(apd_continue); PHP_FUNCTION(apd_echo); +PHP_FUNCTION(apd_get_active_symbols); +PHP_FUNCTION(apd_get_function_table); PHP_MINIT_FUNCTION(apd); PHP_RINIT_FUNCTION(apd); @@ -89,7 +93,7 @@ extern zend_module_entry apd_module_entry; #define apd_module_ptr &apd_module_entry -#define APD_VERSION "0.9" +#define APD_VERSION "0.4p2" #define FUNCTION_TRACE 1 #define ARGS_TRACE 2 @@ -101,103 +105,31 @@ #define ERROR_TRACE 128 #define PROF_TRACE 256 -typedef struct { - int id; - char *filename; -} apd_file_entry_t; - -typedef struct _apd_fcall { - int line; - int file; - long usertime; - long systemtime; - long realtime; - long cumulative; - int memory; - int calls; - void *entry; - - struct _apd_fcall *next; - struct _apd_fcall *prev; -} apd_fcall_t; - -typedef struct _apd_coverage { - apd_fcall_t *head; - apd_fcall_t *tail; - - int size; -} apd_coverage_t; - -typedef struct { - apd_coverage_t coverage; - char *name; - int id; - int type; -} apd_function_entry_t; - - -typedef struct { - apd_array_t functions; - apd_array_t files; - zend_llist call_list; - char *caller; -} apd_summary_t; - -void apd_summary_output_header(void); -void apd_summary_output_file_reference(int, const char *); -void apd_summary_output_elapsed_time(int, int, int); -void apd_summary_output_declare_function(int, const char *, int); -void apd_summary_output_enter_function(int, int, int); -void apd_summary_output_exit_function(int, int); -void apd_summary_output_footer(void); - - -typedef void (*apd_output_header_func_t)(void); -typedef void (*apd_output_file_reference_func_t)(int, const char *); -typedef void (*apd_output_elapsed_time_func_t)(int, int, int); -typedef void (*apd_output_declare_function_func_t)(int, const char *, int); -typedef void (*apd_output_function_enter_func_t)(int, int, int); -typedef void (*apd_output_function_exit_func_t)(int, int); -typedef void (*apd_output_footer_func_t)(void); - -typedef struct { - apd_output_header_func_t header; - apd_output_footer_func_t footer; - apd_output_file_reference_func_t file_reference; - apd_output_elapsed_time_func_t elapsed_time; - apd_output_declare_function_func_t declare_function; - apd_output_function_enter_func_t enter_function; - apd_output_function_exit_func_t exit_function; -} apd_output_handlers; - ZEND_BEGIN_MODULE_GLOBALS(apd) void* stack; - HashTable* function_summary; + HashTable* summary; HashTable* file_summary; char* dumpdir; /* directory for dumping seesion traces to */ FILE* dump_file; /* FILE for dumping session traces to */ FILE* pprof_file; /* File for profiling output */ int dump_sock_id; /* Socket for dumping data to */ - struct timeval first_clock; - struct timeval last_clock; - struct rusage first_ru; - struct rusage last_ru; - int function_index; /* current index of functions for pprof tracing */ - int file_index; /* current index of functions for pprof tracing */ - long bitmask; /* Bitmask for determining what gets logged */ - long pproftrace; /* Flag for whether we are doing profiling */ + struct timeval req_begin; /* Time the request began */ + struct timeval lasttime; /* Last time recorded */ + clock_t firstclock; /* Last time recorded */ + clock_t lastclock; /* Last time recorded */ + struct tms firsttms; /* Last time recorded */ + struct tms lasttms; /* Last time recorded */ + int index; /* current index of functions for pprof tracing */ + int file_index; /* current index of functions for pprof tracing */ + long bitmask; /* Bitmask for determining what gets logged */ + long pproftrace; /* Flag for whether we are doing profiling */ void* last_mem_header; /* tail of persistent zend_mem_header list */ void* last_pmem_header; /* tail of persistent zend_mem_header list */ - int interactive_mode; /* is interactive mode on */ + int interactive_mode; /* is interactive mode on */ int ignore_interactive; /* ignore interactive mode flag for executing php from the debugger*/ int allocated_memory; - apd_output_handlers output; - apd_summary_t summary; ZEND_END_MODULE_GLOBALS(apd) -/* Declare global structure. */ -ZEND_DECLARE_MODULE_GLOBALS(apd); - #ifdef ZTS #define APD_GLOBALS(v) TSRMG(apd_globals_id, zend_apd_globals *, v) #else @@ -207,13 +139,3 @@ #define phpext_apd_ptr apd_module_ptr #endif - -/** - * Local Variables: - * indent-tabs-mode: t - * c-basic-offset: 4 - * tab-width: 4 - * End: - * vim600:fdm=marker - * vim:noet:sw=4:ts=4 - */ http://cvs.php.net/diff.php/pecl/apd/pprofp?r1=1.11.4.1&r2=1.11.4.2&ty=u Index: pecl/apd/pprofp diff -u pecl/apd/pprofp:1.11.4.1 pecl/apd/pprofp:1.11.4.2 --- pecl/apd/pprofp:1.11.4.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/pprofp Wed Mar 10 16:55:36 2004 @@ -21,6 +21,9 @@ } parse_info('HEADER', $DATA, $cfg); +if(!array_key_exists('hz', $cfg)) { + print "File does not appear to be a pprof trace file\n"; +} $callstack = array(); $calls = array(); @@ -66,7 +69,7 @@ array_push($callstack, $index_cur); if(array_key_exists('T', $opt)) { if(array_key_exists('c', $opt)) { - printf("%2.02f ", $rtotal/1000000); + printf("%2.02f ", $rtotal/$cfg['hz']); } print str_repeat(" ", $indent_cur).$symbol_hash[$index_cur]."\n"; if(array_key_exists('m', $opt)) { @@ -82,7 +85,7 @@ $repstr = ' ('.++$repcnt.'x)'; } if(array_key_exists('c', $opt)) { - printf("%2.02f ", $rtotal/1000000); + printf("%2.02f ", $rtotal/$cfg['hz']); } print str_repeat(" ", $indent_last).$symbol_hash[$index_last].$repstr."\n"; if(array_key_exists('m', $opt)) { @@ -155,7 +158,7 @@ Total Elapsed Time = %4.2f Total System Time = %4.2f Total User Time = %4.2f -", $cfg['caller'], $rtotal/1000000, $stotal/1000000, $utotal/1000000); +", $cfg['caller'], $rtotal/$cfg['hz'], $stotal/$cfg['hz'], $utotal/$cfg['hz']); print "\n Real User System secs/ cumm %Time (excl/cumm) (excl/cumm) (excl/cumm) Calls call s/call Memory Usage Name @@ -173,12 +176,12 @@ if ($l++ < $opt['O']) { $pcnt = 100*($stimes[$j] + $utimes[$j])/($utotal + $stotal + $itotal); $c_pcnt = 100* ($c_stimes[$j] + $c_utimes[$j])/($utotal + $stotal + $itotal); - $rsecs = $rtimes[$j]/1000000; - $ssecs = $stimes[$j]/1000000; - $usecs = $utimes[$j]/1000000; - $c_rsecs = $c_rtimes[$j]/1000000; - $c_ssecs = $c_stimes[$j]/1000000; - $c_usecs = $c_utimes[$j]/1000000; + $rsecs = $rtimes[$j]/$cfg['hz']; + $ssecs = $stimes[$j]/$cfg['hz']; + $usecs = $utimes[$j]/$cfg['hz']; + $c_rsecs = $c_rtimes[$j]/$cfg['hz']; + $c_ssecs = $c_stimes[$j]/$cfg['hz']; + $c_usecs = $c_utimes[$j]/$cfg['hz']; $ncalls = $calls[$j]; if(array_key_exists('z', $opt)) { $percall = ($usecs + $ssecs)/$ncalls; http://cvs.php.net/diff.php/pecl/apd/win32compat.c?r1=1.4.2.1&r2=1.4.2.2&ty=u Index: pecl/apd/win32compat.c diff -u pecl/apd/win32compat.c:1.4.2.1 pecl/apd/win32compat.c:1.4.2.2 --- pecl/apd/win32compat.c:1.4.2.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/win32compat.c Wed Mar 10 16:55:36 2004 @@ -1,3 +1,6 @@ +#include "win32compat.h" + + /***************************************************************************** * * * DH_TIME.C * @@ -12,7 +15,7 @@ * * *****************************************************************************/ -/* $Id: win32compat.c,v 1.4.2.1 2004/03/10 16:23:17 gschlossnagle Exp $ */ +/* $Id: win32compat.c,v 1.4.2.2 2004/03/10 21:55:36 gschlossnagle Exp $ */ #include "win32compat.h" @@ -22,6 +25,7 @@ * - Added patch by "Vanhanen, Reijo" <Reijo.Vanhanen@helsoft.fi> * Improves accuracy of msec */ + int FiletimeToTimeval(FILETIME ft, struct timeval *time_Info) { __int64 ff; @@ -166,38 +170,3 @@ */ return GetTickCount(); } - -int getrusage(int processes, struct rusage *rusage) -{ - HANDLE cp; - FILETIME CreationTime, ExitTime, KernelTime, UserTime; - /* If times fails, a -1 is returned and errno is set to indicate the error. */ - - if (rusage == (struct rusage *) NULL) { - errno = EFAULT; - return -1; - } - - memset(rusage, 0, sizeof(rusage)); - cp = GetCurrentProcess(); - GetProcessTimes(cp, &CreationTime, &ExitTime, &KernelTime, &UserTime); - - switch (processes) { - case RUSAGE_SELF: - FiletimeToTimeval(UserTime,&rusage->ru_utime); - FiletimeToTimeval(KernelTime,&rusage->ru_stime); - break; - /* - XXX no support for children times yet - case RUSAGE_CHILDREN: - u = tms.tms_cutime; - s = tms.tms_cstime; - break; - */ - default: - errno = EINVAL; - return -1; - } - return 0; -} - http://cvs.php.net/diff.php/pecl/apd/win32compat.h?r1=1.4.2.1&r2=1.4.2.2&ty=u Index: pecl/apd/win32compat.h diff -u pecl/apd/win32compat.h:1.4.2.1 pecl/apd/win32compat.h:1.4.2.2 --- pecl/apd/win32compat.h:1.4.2.1 Wed Mar 10 11:23:17 2004 +++ pecl/apd/win32compat.h Wed Mar 10 16:55:36 2004 @@ -15,13 +15,13 @@ * * *****************************************************************************/ -/* $Id: win32compat.h,v 1.4.2.1 2004/03/10 16:23:17 gschlossnagle Exp $ */ +/* $Id: win32compat.h,v 1.4.2.2 2004/03/10 21:55:36 gschlossnagle Exp $ */ /* Include stuff ************************************************************ */ #define WIN32_LEAN_AND_MEAN #include <windows.h> -#include <winsock2.h> +#include <winsock.h> #include <time.h> /* Struct stuff ************************************************************* */ @@ -39,35 +39,9 @@ clock_t tms_cstime; /* System CPU time of dead children. */ }; -#define RUSAGE_SELF 0 -#define RUSAGE_CHILDREN 1 - -struct rusage -{ - /* This data type stores various resource usage statistics. It has the - following members, and possibly others: */ - - struct timeval ru_utime; /* Time spent executing user instructions. */ - struct timeval ru_stime; /*Time spent in operating system code on behalf of processes. */ - long int ru_maxrss; /* The maximum resident set size used, in kilobytes. That is, the maximum number of kilobytes of physical memory that processes used simultaneously. */ - long int ru_ixrss; /* An integral value expressed in kilobytes times ticks of execution, which indicates the amount of memory used by text that was shared with other processes. */ - long int ru_idrss; /* An integral value expressed the same way, which is the amount of unshared memory used for data. */ - long int ru_isrss; /* An integral value expressed the same way, which is the amount of unshared memory used for stack space. */ - long int ru_minflt; /* The number of page faults which were serviced without requiring any I/O. */ - long int ru_majflt; /* The number of page faults which were serviced by doing I/O. */ - long int ru_nswap; /* The number of times processes was swapped entirely out of main memory. */ - long int ru_inblock; /* The number of times the file system had to read from the disk on behalf of processes. */ - long int ru_oublock; /* The number of times the file system had to write to the disk on behalf of processes. */ - long int ru_msgsnd; /* Number of IPC messages sent. */ - long int ru_msgrcv; /* Number of IPC messages received. */ - long int ru_nsignals; /* Number of signals received. */ - long int ru_nvcsw; /* The number of times processes voluntarily invoked a context switch (usually to wait for some service). */ - long int ru_nivcsw; /* The number of times an involuntary context switch took place (because a time slice expired, or another process of higher priority was scheduled). */ -}; /* Prototype stuff ********************************************************** */ extern int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info); -extern clock_t times(struct tms *__buffer); -extern int getrusage(int processes, struct rusage *rusage); +extern clock_t times (struct tms *__buffer); #endif \ No newline at end of file
« previous php.pecl.cvs (#1048) next »