PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/mysqlnd - mysqlnd_debug.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 3 310 1.0 %
Date: 2014-11-22 Functions: 1 11 9.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 2006-2014 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Georg Richter <georg@mysql.com>                             |
      16             :   |          Andrey Hristov <andrey@mysql.com>                           |
      17             :   |          Ulf Wendel <uwendel@mysql.com>                              |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #include "php.h"
      24             : #include "mysqlnd.h"
      25             : #include "mysqlnd_priv.h"
      26             : #include "mysqlnd_debug.h"
      27             : 
      28             : static const char * const mysqlnd_debug_default_trace_file = "/tmp/mysqlnd.trace";
      29             : static const char * const mysqlnd_debug_empty_string = "";
      30             : 
      31             : #ifdef ZTS 
      32             : #define MYSQLND_ZTS(self) TSRMLS_D = (self)->TSRMLS_C
      33             : #else
      34             : #define MYSQLND_ZTS(self)
      35             : #endif
      36             : 
      37             : 
      38             : /* {{{ mysqlnd_debug::open */
      39             : static enum_func_status
      40           0 : MYSQLND_METHOD(mysqlnd_debug, open)(MYSQLND_DEBUG * self, zend_bool reopen)
      41             : {
      42             :         MYSQLND_ZTS(self);
      43             : 
      44           0 :         if (!self->file_name) {
      45           0 :                 return FAIL;
      46             :         }
      47             : 
      48           0 :         self->stream = php_stream_open_wrapper(self->file_name,
      49             :                                                                                    reopen == TRUE || self->flags & MYSQLND_DEBUG_APPEND? "ab":"wb",
      50             :                                                                                    REPORT_ERRORS, NULL);
      51           0 :         return self->stream? PASS:FAIL;
      52             : }
      53             : /* }}} */
      54             : 
      55             : 
      56             : /* {{{ mysqlnd_debug::log */
      57             : static enum_func_status
      58           0 : MYSQLND_METHOD(mysqlnd_debug, log)(MYSQLND_DEBUG * self,
      59             :                                                                    unsigned int line, const char * const file,
      60             :                                                                    unsigned int level, const char * type, const char * message)
      61             : {
      62             :         char pipe_buffer[512];
      63             :         enum_func_status ret;
      64             :         int i;
      65             :         char * message_line;
      66             :         unsigned int message_line_len;
      67           0 :         unsigned int flags = self->flags;
      68             :         char pid_buffer[10], time_buffer[30], file_buffer[200],
      69             :                  line_buffer[6], level_buffer[7];
      70             :         MYSQLND_ZTS(self);
      71             : 
      72           0 :         if (!self->stream && FAIL == self->m->open(self, FALSE)) {
      73           0 :                 return FAIL;
      74             :         }
      75             : 
      76           0 :         if (level == -1) {
      77           0 :                 level = zend_stack_count(&self->call_stack);
      78             :         }
      79           0 :         i = MIN(level, sizeof(pipe_buffer) / 2  - 1);
      80           0 :         pipe_buffer[i*2] = '\0';
      81           0 :         for (;i > 0;i--) {
      82           0 :                 pipe_buffer[i*2 - 1] = ' ';
      83           0 :                 pipe_buffer[i*2 - 2] = '|';
      84             :         }
      85             : 
      86             : 
      87           0 :         if (flags & MYSQLND_DEBUG_DUMP_PID) {
      88           0 :                 snprintf(pid_buffer, sizeof(pid_buffer) - 1, "%5u: ", self->pid);
      89           0 :                 pid_buffer[sizeof(pid_buffer) - 1 ] = '\0';
      90             :         }
      91           0 :         if (flags & MYSQLND_DEBUG_DUMP_TIME) {
      92             :                 /* The following from FF's DBUG library, which is in the public domain */
      93             : #if defined(PHP_WIN32)
      94             :                 /* FIXME This doesn't give microseconds as in Unix case, and the resolution is
      95             :                 in system ticks, 10 ms intervals. See my_getsystime.c for high res */
      96             :                 SYSTEMTIME loc_t;
      97             :                 GetLocalTime(&loc_t);
      98             :                 snprintf(time_buffer, sizeof(time_buffer) - 1,
      99             :                                  /* "%04d-%02d-%02d " */
     100             :                                  "%02d:%02d:%02d.%06d ",
     101             :                                  /*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
     102             :                                  loc_t.wHour, loc_t.wMinute, loc_t.wSecond, loc_t.wMilliseconds);
     103             :                 time_buffer[sizeof(time_buffer) - 1 ] = '\0';
     104             : #else
     105             :                 struct timeval tv;
     106             :                 struct tm *tm_p;
     107           0 :                 if (gettimeofday(&tv, NULL) != -1) {
     108           0 :                         if ((tm_p= localtime((const time_t *)&tv.tv_sec))) {
     109           0 :                                 snprintf(time_buffer, sizeof(time_buffer) - 1,
     110             :                                                  /* "%04d-%02d-%02d " */
     111             :                                                  "%02d:%02d:%02d.%06d ",
     112             :                                                  /*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
     113             :                                                  tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec,
     114           0 :                                                  (int) (tv.tv_usec));
     115           0 :                                 time_buffer[sizeof(time_buffer) - 1 ] = '\0';
     116             :                         }
     117             :                 }
     118             : #endif
     119             :         }
     120           0 :         if (flags & MYSQLND_DEBUG_DUMP_FILE) {
     121           0 :                 snprintf(file_buffer, sizeof(file_buffer) - 1, "%14s: ", file);
     122           0 :                 file_buffer[sizeof(file_buffer) - 1 ] = '\0';
     123             :         }
     124           0 :         if (flags & MYSQLND_DEBUG_DUMP_LINE) {
     125           0 :                 snprintf(line_buffer, sizeof(line_buffer) - 1, "%5u: ", line);
     126           0 :                 line_buffer[sizeof(line_buffer) - 1 ] = '\0';
     127             :         }
     128           0 :         if (flags & MYSQLND_DEBUG_DUMP_LEVEL) {
     129           0 :                 snprintf(level_buffer, sizeof(level_buffer) - 1, "%4u: ", level);
     130           0 :                 level_buffer[sizeof(level_buffer) - 1 ] = '\0';
     131             :         }
     132             : 
     133           0 :         message_line_len = mnd_sprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
     134             :                                                                 flags & MYSQLND_DEBUG_DUMP_PID? pid_buffer:"",
     135             :                                                                 flags & MYSQLND_DEBUG_DUMP_TIME? time_buffer:"",
     136             :                                                                 flags & MYSQLND_DEBUG_DUMP_FILE? file_buffer:"",
     137             :                                                                 flags & MYSQLND_DEBUG_DUMP_LINE? line_buffer:"",
     138             :                                                                 flags & MYSQLND_DEBUG_DUMP_LEVEL? level_buffer:"",
     139             :                                                                 pipe_buffer, type? type:"", message);
     140             : 
     141           0 :         ret = php_stream_write(self->stream, message_line, message_line_len)? PASS:FAIL;
     142           0 :         mnd_sprintf_free(message_line);
     143           0 :         if (flags & MYSQLND_DEBUG_FLUSH) {
     144           0 :                 self->m->close(self);
     145           0 :                 self->m->open(self, TRUE);
     146             :         }
     147           0 :         return ret;
     148             : }
     149             : /* }}} */
     150             : 
     151             : 
     152             : /* {{{ mysqlnd_debug::log_va */
     153             : static enum_func_status
     154           0 : MYSQLND_METHOD(mysqlnd_debug, log_va)(MYSQLND_DEBUG *self,
     155             :                                                                           unsigned int line, const char * const file,
     156             :                                                                           unsigned int level, const char * type,
     157             :                                                                           const char *format, ...)
     158             : {
     159             :         char pipe_buffer[512];
     160             :         int i;
     161             :         enum_func_status ret;
     162             :         char * message_line, *buffer;
     163             :         unsigned int message_line_len;
     164             :         va_list args;
     165           0 :         unsigned int flags = self->flags;
     166             :         char pid_buffer[10], time_buffer[30], file_buffer[200],
     167             :                  line_buffer[6], level_buffer[7];
     168             :         MYSQLND_ZTS(self);
     169             : 
     170           0 :         if (!self->stream && FAIL == self->m->open(self, FALSE)) {
     171           0 :                 return FAIL;
     172             :         }
     173             : 
     174           0 :         if (level == -1) {
     175           0 :                 level = zend_stack_count(&self->call_stack);
     176             :         }
     177           0 :         i = MIN(level, sizeof(pipe_buffer) / 2  - 1);
     178           0 :         pipe_buffer[i*2] = '\0';
     179           0 :         for (;i > 0;i--) {
     180           0 :                 pipe_buffer[i*2 - 1] = ' ';
     181           0 :                 pipe_buffer[i*2 - 2] = '|';
     182             :         }
     183             : 
     184             : 
     185           0 :         if (flags & MYSQLND_DEBUG_DUMP_PID) {
     186           0 :                 snprintf(pid_buffer, sizeof(pid_buffer) - 1, "%5u: ", self->pid);
     187           0 :                 pid_buffer[sizeof(pid_buffer) - 1 ] = '\0';
     188             :         }
     189           0 :         if (flags & MYSQLND_DEBUG_DUMP_TIME) {
     190             :                 /* The following from FF's DBUG library, which is in the public domain */
     191             : #if defined(PHP_WIN32)
     192             :                 /* FIXME This doesn't give microseconds as in Unix case, and the resolution is
     193             :                 in system ticks, 10 ms intervals. See my_getsystime.c for high res */
     194             :                 SYSTEMTIME loc_t;
     195             :                 GetLocalTime(&loc_t);
     196             :                 snprintf(time_buffer, sizeof(time_buffer) - 1,
     197             :                                  /* "%04d-%02d-%02d " */
     198             :                                  "%02d:%02d:%02d.%06d ",
     199             :                                  /*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
     200             :                                  loc_t.wHour, loc_t.wMinute, loc_t.wSecond, loc_t.wMilliseconds);
     201             :                 time_buffer[sizeof(time_buffer) - 1 ] = '\0';
     202             : #else
     203             :                 struct timeval tv;
     204             :                 struct tm *tm_p;
     205           0 :                 if (gettimeofday(&tv, NULL) != -1) {
     206           0 :                         if ((tm_p= localtime((const time_t *)&tv.tv_sec))) {
     207           0 :                                 snprintf(time_buffer, sizeof(time_buffer) - 1,
     208             :                                                  /* "%04d-%02d-%02d " */
     209             :                                                  "%02d:%02d:%02d.%06d ",
     210             :                                                  /*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
     211             :                                                  tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec,
     212           0 :                                                  (int) (tv.tv_usec));
     213           0 :                                 time_buffer[sizeof(time_buffer) - 1 ] = '\0';
     214             :                         }
     215             :                 }
     216             : #endif
     217             :         }
     218           0 :         if (flags & MYSQLND_DEBUG_DUMP_FILE) {
     219           0 :                 snprintf(file_buffer, sizeof(file_buffer) - 1, "%14s: ", file);
     220           0 :                 file_buffer[sizeof(file_buffer) - 1 ] = '\0';
     221             :         }
     222           0 :         if (flags & MYSQLND_DEBUG_DUMP_LINE) {
     223           0 :                 snprintf(line_buffer, sizeof(line_buffer) - 1, "%5u: ", line);
     224           0 :                 line_buffer[sizeof(line_buffer) - 1 ] = '\0';
     225             :         }
     226           0 :         if (flags & MYSQLND_DEBUG_DUMP_LEVEL) {
     227           0 :                 snprintf(level_buffer, sizeof(level_buffer) - 1, "%4u: ", level);
     228           0 :                 level_buffer[sizeof(level_buffer) - 1 ] = '\0';
     229             :         }
     230             : 
     231           0 :         va_start(args, format);
     232           0 :         mnd_vsprintf(&buffer, 0, format, args);
     233           0 :         va_end(args);
     234             : 
     235           0 :         message_line_len = mnd_sprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
     236             :                                                                 flags & MYSQLND_DEBUG_DUMP_PID? pid_buffer:"",
     237             :                                                                 flags & MYSQLND_DEBUG_DUMP_TIME? time_buffer:"",
     238             :                                                                 flags & MYSQLND_DEBUG_DUMP_FILE? file_buffer:"",
     239             :                                                                 flags & MYSQLND_DEBUG_DUMP_LINE? line_buffer:"",
     240             :                                                                 flags & MYSQLND_DEBUG_DUMP_LEVEL? level_buffer:"",
     241             :                                                                 pipe_buffer, type? type:"", buffer);
     242           0 :         mnd_sprintf_free(buffer);
     243           0 :         ret = php_stream_write(self->stream, message_line, message_line_len)? PASS:FAIL;
     244           0 :         mnd_sprintf_free(message_line);
     245             : 
     246           0 :         if (flags & MYSQLND_DEBUG_FLUSH) {
     247           0 :                 self->m->close(self);
     248           0 :                 self->m->open(self, TRUE);
     249             :         }
     250           0 :         return ret;
     251             : }
     252             : /* }}} */
     253             : 
     254             : 
     255             : /* FALSE - The DBG_ calls won't be traced, TRUE - will be traced */
     256             : /* {{{ mysqlnd_debug::func_enter */
     257             : static zend_bool
     258           0 : MYSQLND_METHOD(mysqlnd_debug, func_enter)(MYSQLND_DEBUG * self,
     259             :                                                                                   unsigned int line, const char * const file,
     260             :                                                                                   const char * const func_name, unsigned int func_name_len)
     261             : {
     262           0 :         if ((self->flags & MYSQLND_DEBUG_DUMP_TRACE) == 0 || self->file_name == NULL) {
     263           0 :                 return FALSE;
     264             :         }
     265           0 :         if ((uint) zend_stack_count(&self->call_stack) >= self->nest_level_limit) {
     266           0 :                 return FALSE;
     267             :         }
     268             : 
     269           0 :         if ((self->flags & MYSQLND_DEBUG_TRACE_MEMORY_CALLS) == 0 && self->skip_functions) {
     270           0 :                 const char ** p = self->skip_functions;
     271           0 :                 while (*p) {
     272           0 :                         if (*p == func_name) {
     273           0 :                                 zend_stack_push(&self->call_stack, &mysqlnd_debug_empty_string);
     274             : #ifndef MYSQLND_PROFILING_DISABLED
     275           0 :                                 if (self->flags & MYSQLND_DEBUG_PROFILE_CALLS) {
     276           0 :                                         uint64_t some_time = 0;
     277           0 :                                         zend_stack_push(&self->call_time_stack, &some_time);
     278             :                                 }
     279             : #endif
     280           0 :                                 return FALSE;
     281             :                         }
     282           0 :                         p++;
     283             :                 }
     284             :         }
     285             : 
     286           0 :         zend_stack_push(&self->call_stack, &func_name);
     287             : #ifndef MYSQLND_PROFILING_DISABLED
     288           0 :         if (self->flags & MYSQLND_DEBUG_PROFILE_CALLS) {
     289           0 :                 uint64_t some_time = 0;
     290           0 :                 zend_stack_push(&self->call_time_stack, &some_time);
     291             :         }
     292             : #endif
     293             : 
     294           0 :         if (zend_hash_num_elements(&self->not_filtered_functions) &&
     295           0 :                 0 == zend_hash_str_exists(&self->not_filtered_functions, func_name, strlen(func_name)))
     296             :         {
     297           0 :                 return FALSE;
     298             :         }
     299             : 
     300           0 :         self->m->log_va(self, line, file, zend_stack_count(&self->call_stack) - 1, NULL, ">%s", func_name);
     301           0 :         return TRUE;
     302             : }
     303             : /* }}} */
     304             : 
     305             : #ifndef MYSQLND_PROFILING_DISABLED
     306             : struct st_mysqlnd_dbg_function_profile {
     307             :         uint64_t calls;
     308             :         uint64_t min_own;
     309             :         uint64_t max_own;
     310             :         uint64_t avg_own;
     311             :         uint64_t own_underporm_calls;
     312             :         uint64_t min_in_calls;
     313             :         uint64_t max_in_calls;
     314             :         uint64_t avg_in_calls;
     315             :         uint64_t in_calls_underporm_calls;
     316             :         uint64_t min_total;
     317             :         uint64_t max_total;
     318             :         uint64_t avg_total;     
     319             :         uint64_t total_underporm_calls;
     320             : };
     321             : #define PROFILE_UNDERPERFORM_THRESHOLD 10
     322             : #endif
     323             : 
     324             : /* {{{ mysqlnd_debug::func_leave */
     325             : static enum_func_status
     326           0 : MYSQLND_METHOD(mysqlnd_debug, func_leave)(MYSQLND_DEBUG * self, unsigned int line, const char * const file, uint64_t call_time)
     327             : {
     328             :         char **func_name;
     329           0 :         uint64_t * parent_non_own_time_ptr = NULL, * mine_non_own_time_ptr = NULL;
     330           0 :         uint64_t mine_non_own_time = 0;
     331           0 :         zend_bool profile_calls = self->flags & MYSQLND_DEBUG_PROFILE_CALLS? TRUE:FALSE;
     332             : 
     333           0 :         if ((self->flags & MYSQLND_DEBUG_DUMP_TRACE) == 0 || self->file_name == NULL) {
     334           0 :                 return PASS;
     335             :         }
     336           0 :         if ((uint) zend_stack_count(&self->call_stack) >= self->nest_level_limit) {
     337           0 :                 return PASS;
     338             :         }
     339             : 
     340           0 :         func_name = zend_stack_top(&self->call_stack);
     341             : 
     342             : #ifndef MYSQLND_PROFILING_DISABLED
     343           0 :         if (profile_calls) {
     344           0 :                 mine_non_own_time_ptr = zend_stack_top(&self->call_time_stack);
     345           0 :                 mine_non_own_time = *mine_non_own_time_ptr;
     346           0 :                 zend_stack_del_top(&self->call_time_stack); /* callee - removing ourselves */
     347             :         }
     348             : #endif
     349             : 
     350           0 :         if ((*func_name)[0] == '\0') {
     351             :                 ; /* don't log that function */
     352           0 :         } else if (!zend_hash_num_elements(&self->not_filtered_functions) ||
     353           0 :                            1 == zend_hash_str_exists(&self->not_filtered_functions, (*func_name), strlen((*func_name))))
     354             :         {
     355             : #ifndef MYSQLND_PROFILING_DISABLED
     356           0 :                 if (FALSE == profile_calls) {
     357             : #endif
     358           0 :                         self->m->log_va(self, line, file, zend_stack_count(&self->call_stack) - 1, NULL, "<%s", *func_name);
     359             : 
     360             : #ifndef MYSQLND_PROFILING_DISABLED
     361             :                 } else {
     362           0 :                         struct st_mysqlnd_dbg_function_profile f_profile_stack = {0};
     363           0 :                         struct st_mysqlnd_dbg_function_profile * f_profile = NULL;
     364           0 :                         uint64_t own_time = call_time - mine_non_own_time;
     365           0 :                         uint func_name_len = strlen(*func_name);
     366             : 
     367           0 :                         self->m->log_va(self, line, file, zend_stack_count(&self->call_stack) - 1, NULL, "<%s (total=%u own=%u in_calls=%u)",
     368             :                                                 *func_name, (unsigned int) call_time, (unsigned int) own_time, (unsigned int) mine_non_own_time
     369             :                                         );
     370             : 
     371           0 :                         if ((f_profile = zend_hash_str_find_ptr(&self->function_profiles, *func_name, func_name_len)) != NULL) {
     372             :                                 /* found */
     373           0 :                                         if (f_profile) {
     374           0 :                                         if (mine_non_own_time < f_profile->min_in_calls) {
     375           0 :                                                 f_profile->min_in_calls = mine_non_own_time;
     376           0 :                                         } else if (mine_non_own_time > f_profile->max_in_calls) {
     377           0 :                                                 f_profile->max_in_calls = mine_non_own_time;
     378             :                                         }
     379           0 :                                         f_profile->avg_in_calls = (f_profile->avg_in_calls * f_profile->calls + mine_non_own_time) / (f_profile->calls + 1);
     380             : 
     381           0 :                                         if (own_time < f_profile->min_own) {
     382           0 :                                                 f_profile->min_own = own_time;
     383           0 :                                         } else if (own_time > f_profile->max_own) {
     384           0 :                                                 f_profile->max_own = own_time;
     385             :                                         }
     386           0 :                                         f_profile->avg_own = (f_profile->avg_own * f_profile->calls + own_time) / (f_profile->calls + 1);
     387             : 
     388           0 :                                         if (call_time < f_profile->min_total) {
     389           0 :                                                 f_profile->min_total = call_time;
     390           0 :                                         } else if (call_time > f_profile->max_total) {
     391           0 :                                                 f_profile->max_total = call_time;
     392             :                                         }
     393           0 :                                         f_profile->avg_total = (f_profile->avg_total * f_profile->calls + call_time) / (f_profile->calls + 1);
     394             : 
     395           0 :                                         ++f_profile->calls;
     396           0 :                                         if (f_profile->calls > PROFILE_UNDERPERFORM_THRESHOLD) {
     397           0 :                                                 if (f_profile->avg_in_calls < mine_non_own_time) {
     398           0 :                                                         f_profile->in_calls_underporm_calls++;
     399             :                                                 }
     400           0 :                                                 if (f_profile->avg_own < own_time) {
     401           0 :                                                         f_profile->own_underporm_calls++;
     402             :                                                 }
     403           0 :                                                 if (f_profile->avg_total < call_time) {
     404           0 :                                                         f_profile->total_underporm_calls++;
     405             :                                                 }
     406             :                                         }
     407             :                                 }
     408             :                         } else {
     409             :                                 /* add */
     410           0 :                                 f_profile = &f_profile_stack;
     411           0 :                                 f_profile->min_in_calls = f_profile->max_in_calls = f_profile->avg_in_calls = mine_non_own_time;
     412           0 :                                 f_profile->min_total = f_profile->max_total = f_profile->avg_total = call_time;
     413           0 :                                 f_profile->min_own = f_profile->max_own = f_profile->avg_own = own_time;
     414           0 :                                 f_profile->calls = 1;
     415           0 :                                 zend_hash_str_add_mem(&self->function_profiles, *func_name, func_name_len, f_profile, sizeof(struct st_mysqlnd_dbg_function_profile));
     416             :                         }
     417           0 :                         if ((uint) zend_stack_count(&self->call_time_stack)) {
     418           0 :                                 uint64_t parent_non_own_time = 0;
     419             : 
     420           0 :                                 parent_non_own_time_ptr = zend_stack_top(&self->call_time_stack);
     421           0 :                                 parent_non_own_time = *parent_non_own_time_ptr;
     422           0 :                                 parent_non_own_time += call_time;
     423           0 :                                 zend_stack_del_top(&self->call_time_stack); /* the caller */
     424           0 :                                 zend_stack_push(&self->call_time_stack, &parent_non_own_time); /* add back the caller */
     425             :                         }
     426             :                 }
     427             : #endif
     428             :         }
     429             : 
     430           0 :         return zend_stack_del_top(&self->call_stack) == SUCCESS? PASS:FAIL;
     431             : }
     432             : /* }}} */
     433             : 
     434             : 
     435             : /* {{{ mysqlnd_debug::close */
     436             : static enum_func_status
     437           0 : MYSQLND_METHOD(mysqlnd_debug, close)(MYSQLND_DEBUG * self)
     438             : {
     439             :         MYSQLND_ZTS(self);
     440           0 :         if (self->stream) {
     441             : #ifndef MYSQLND_PROFILING_DISABLED
     442           0 :                 if (!(self->flags & MYSQLND_DEBUG_FLUSH) && (self->flags & MYSQLND_DEBUG_PROFILE_CALLS)) {
     443             :                         struct st_mysqlnd_dbg_function_profile * f_profile;
     444           0 :                         zend_string     *string_key = NULL;
     445             : 
     446           0 :                         self->m->log_va(self, __LINE__, __FILE__, 0, "info : ", 
     447             :                                         "number of functions: %d", zend_hash_num_elements(&self->function_profiles));
     448           0 :                         ZEND_HASH_FOREACH_STR_KEY_PTR(&self->function_profiles, string_key, f_profile) {
     449           0 :                                 self->m->log_va(self, __LINE__, __FILE__, -1, "info : ",
     450             :                                                 "%-40s\tcalls=%5llu  own_slow=%5llu  in_calls_slow=%5llu  total_slow=%5llu"
     451             :                                                 "   min_own=%5llu  max_own=%7llu  avg_own=%7llu   "
     452             :                                                 "   min_in_calls=%5llu  max_in_calls=%7llu  avg_in_calls=%7llu"
     453             :                                                 "   min_total=%5llu  max_total=%7llu  avg_total=%7llu"
     454             :                                                 ,string_key->val
     455             :                                                 ,(uint64_t) f_profile->calls
     456             :                                                 ,(uint64_t) f_profile->own_underporm_calls
     457             :                                                 ,(uint64_t) f_profile->in_calls_underporm_calls
     458             :                                                 ,(uint64_t) f_profile->total_underporm_calls
     459             : 
     460             :                                                 ,(uint64_t) f_profile->min_own
     461             :                                                 ,(uint64_t) f_profile->max_own
     462             :                                                 ,(uint64_t) f_profile->avg_own
     463             :                                                 ,(uint64_t) f_profile->min_in_calls
     464             :                                                 ,(uint64_t) f_profile->max_in_calls
     465             :                                                 ,(uint64_t) f_profile->avg_in_calls
     466             :                                                 ,(uint64_t) f_profile->min_total
     467             :                                                 ,(uint64_t) f_profile->max_total
     468             :                                                 ,(uint64_t) f_profile->avg_total
     469             :                                                 );
     470             :                         } ZEND_HASH_FOREACH_END();
     471             :                 }
     472             : #endif  
     473             : 
     474           0 :                 php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE);
     475           0 :                 self->stream = NULL;
     476             :         }
     477             :         /* no DBG_RETURN please */
     478           0 :         return PASS;
     479             : }
     480             : /* }}} */
     481             : 
     482             : 
     483             : /* {{{ mysqlnd_res_meta::free */
     484             : static enum_func_status
     485           0 : MYSQLND_METHOD(mysqlnd_debug, free)(MYSQLND_DEBUG * self)
     486             : {
     487           0 :         if (self->file_name && self->file_name != mysqlnd_debug_default_trace_file) {
     488           0 :                 efree(self->file_name);
     489           0 :                 self->file_name = NULL;
     490             :         }
     491           0 :         zend_stack_destroy(&self->call_stack);
     492           0 :         zend_stack_destroy(&self->call_time_stack);
     493           0 :         zend_hash_destroy(&self->not_filtered_functions);
     494           0 :         zend_hash_destroy(&self->function_profiles);
     495           0 :         free(self);
     496           0 :         return PASS;
     497             : }
     498             : /* }}} */
     499             : 
     500             : enum mysqlnd_debug_parser_state
     501             : {
     502             :         PARSER_WAIT_MODIFIER,
     503             :         PARSER_WAIT_COLON,
     504             :         PARSER_WAIT_VALUE
     505             : };
     506             : 
     507             : 
     508             : /* {{{ mysqlnd_res_meta::set_mode */
     509             : static void
     510           0 : MYSQLND_METHOD(mysqlnd_debug, set_mode)(MYSQLND_DEBUG * self, const char * const mode)
     511             : {
     512             :         unsigned int mode_len, i;
     513           0 :         enum mysqlnd_debug_parser_state state = PARSER_WAIT_MODIFIER;
     514             : 
     515           0 :         mode_len = mode? strlen(mode) : 0;
     516             : 
     517           0 :         self->flags = 0;
     518           0 :         self->nest_level_limit = 0;
     519           0 :         if (self->file_name && self->file_name != mysqlnd_debug_default_trace_file) {
     520           0 :                 efree(self->file_name);
     521           0 :                 self->file_name = NULL;
     522             :         }
     523           0 :         if (zend_hash_num_elements(&self->not_filtered_functions)) {
     524           0 :                 zend_hash_destroy(&self->not_filtered_functions);
     525           0 :                 zend_hash_init(&self->not_filtered_functions, 0, NULL, NULL, 0);
     526             :         }
     527             : 
     528           0 :         for (i = 0; i < mode_len; i++) {
     529           0 :                 switch (mode[i]) {
     530             :                         case 'O':
     531             :                         case 'A':
     532           0 :                                 self->flags |= MYSQLND_DEBUG_FLUSH;
     533             :                         case 'a':
     534             :                         case 'o':
     535           0 :                                 if (mode[i] == 'a' || mode[i] == 'A') {
     536           0 :                                         self->flags |= MYSQLND_DEBUG_APPEND;
     537             :                                 }
     538           0 :                                 if (i + 1 < mode_len && mode[i+1] == ',') {
     539           0 :                                         unsigned int j = i + 2;
     540             : #ifdef PHP_WIN32
     541             :                                         if (i+4 < mode_len && mode[i+3] == ':' && (mode[i+4] == '\\' || mode[i+5] == '/')) {
     542             :                                                 j = i + 5;
     543             :                                         }
     544             : #endif
     545           0 :                                         while (j < mode_len) {
     546           0 :                                                 if (mode[j] == ':') {
     547           0 :                                                         break;
     548             :                                                 }
     549           0 :                                                 j++;
     550             :                                         }
     551           0 :                                         if (j > i + 2) {
     552           0 :                                                 self->file_name = estrndup(mode + i + 2, j - i - 2);
     553             :                                         }
     554           0 :                                         i = j;
     555             :                                 } else {
     556           0 :                                         if (!self->file_name)
     557           0 :                                                 self->file_name = (char *) mysqlnd_debug_default_trace_file;
     558             :                                 }
     559           0 :                                 state = PARSER_WAIT_COLON;
     560           0 :                                 break;
     561             :                         case ':':
     562             : #if 0
     563             :                                 if (state != PARSER_WAIT_COLON) {
     564             :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Consecutive semicolons at position %u", i);
     565             :                                 }
     566             : #endif
     567           0 :                                 state = PARSER_WAIT_MODIFIER;
     568           0 :                                 break;
     569             :                         case 'f': /* limit output to these functions */
     570           0 :                                 if (i + 1 < mode_len && mode[i+1] == ',') {
     571           0 :                                         unsigned int j = i + 2;
     572           0 :                                         i++;
     573           0 :                                         while (j < mode_len) {
     574           0 :                                                 if (mode[j] == ':') {
     575             :                                                         /* function names with :: */
     576           0 :                                                         if ((j + 1 < mode_len) && mode[j+1] == ':') {
     577           0 :                                                                 j += 2;
     578           0 :                                                                 continue;
     579             :                                                         }
     580             :                                                 }
     581           0 :                                                 if (mode[j] == ',' || mode[j] == ':') {
     582           0 :                                                         if (j > i + 2) {
     583             :                                                                 char func_name[1024];
     584           0 :                                                                 unsigned int func_name_len = MIN(sizeof(func_name) - 1, j - i - 1);
     585           0 :                                                                 memcpy(func_name, mode + i + 1, func_name_len);
     586           0 :                                                                 func_name[func_name_len] = '\0'; 
     587             : 
     588           0 :                                                                 zend_hash_str_add_empty_element(&self->not_filtered_functions,
     589             :                                                                                                                         func_name, func_name_len);
     590           0 :                                                                 i = j;
     591             :                                                         }
     592           0 :                                                         if (mode[j] == ':') {
     593           0 :                                                                 break;
     594             :                                                         }
     595             :                                                 }
     596           0 :                                                 j++;
     597             :                                         }
     598           0 :                                         i = j;
     599             :                                 } else {
     600             : #if 0
     601             :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING,
     602             :                                                                          "Expected list of functions for '%c' found none", mode[i]);
     603             : #endif
     604             :                                 }
     605           0 :                                 state = PARSER_WAIT_COLON;
     606           0 :                                 break;
     607             :                         case 'D':
     608             :                         case 'd':
     609             :                         case 'g':
     610             :                         case 'p':
     611             :                                 /* unsupported */
     612           0 :                                 if ((i + 1) < mode_len && mode[i+1] == ',') {
     613           0 :                                         i+= 2;
     614           0 :                                         while (i < mode_len) {
     615           0 :                                                 if (mode[i] == ':') {
     616           0 :                                                         break;
     617             :                                                 }
     618           0 :                                                 i++;
     619             :                                         }
     620             :                                 }
     621           0 :                                 state = PARSER_WAIT_COLON;
     622           0 :                                 break;
     623             :                         case 'F':
     624           0 :                                 self->flags |= MYSQLND_DEBUG_DUMP_FILE;
     625           0 :                                 state = PARSER_WAIT_COLON;
     626           0 :                                 break;
     627             :                         case 'i':
     628           0 :                                 self->flags |= MYSQLND_DEBUG_DUMP_PID;
     629           0 :                                 state = PARSER_WAIT_COLON;
     630           0 :                                 break;
     631             :                         case 'L':
     632           0 :                                 self->flags |= MYSQLND_DEBUG_DUMP_LINE;
     633           0 :                                 state = PARSER_WAIT_COLON;
     634           0 :                                 break;
     635             :                         case 'n':
     636           0 :                                 self->flags |= MYSQLND_DEBUG_DUMP_LEVEL;
     637           0 :                                 state = PARSER_WAIT_COLON;
     638           0 :                                 break;
     639             :                         case 't':
     640           0 :                                 if (mode[i+1] == ',') {
     641           0 :                                         unsigned int j = i + 2;
     642           0 :                                         while (j < mode_len) {
     643           0 :                                                 if (mode[j] == ':') {
     644           0 :                                                         break;
     645             :                                                 }
     646           0 :                                                 j++;
     647             :                                         }
     648           0 :                                         if (j > i + 2) {
     649           0 :                                                 char *value_str = estrndup(mode + i + 2, j - i - 2);
     650           0 :                                                 self->nest_level_limit = atoi(value_str);
     651           0 :                                                 efree(value_str);
     652             :                                         }
     653           0 :                                         i = j;
     654             :                                 } else {
     655           0 :                                         self->nest_level_limit = 200; /* default value for FF DBUG */
     656             :                                 }
     657           0 :                                 self->flags |= MYSQLND_DEBUG_DUMP_TRACE;
     658           0 :                                 state = PARSER_WAIT_COLON;
     659           0 :                                 break;
     660             :                         case 'T':
     661           0 :                                 self->flags |= MYSQLND_DEBUG_DUMP_TIME;
     662           0 :                                 state = PARSER_WAIT_COLON;
     663           0 :                                 break;
     664             :                         case 'N':
     665             :                         case 'P':
     666             :                         case 'r':
     667             :                         case 'S':
     668           0 :                                 state = PARSER_WAIT_COLON;
     669           0 :                                 break;
     670             :                         case 'm': /* mysqlnd extension - trace memory functions */
     671           0 :                                 self->flags |= MYSQLND_DEBUG_TRACE_MEMORY_CALLS;
     672           0 :                                 state = PARSER_WAIT_COLON;
     673           0 :                                 break;
     674             :                         case 'x': /* mysqlnd extension - profile calls */
     675           0 :                                 self->flags |= MYSQLND_DEBUG_PROFILE_CALLS;
     676           0 :                                 state = PARSER_WAIT_COLON;
     677           0 :                                 break;                          
     678             :                         default:
     679           0 :                                 if (state == PARSER_WAIT_MODIFIER) {
     680             : #if 0
     681             :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unrecognized format '%c'", mode[i]);
     682             : #endif
     683           0 :                                         if (i+1 < mode_len && mode[i+1] == ',') {
     684           0 :                                                 i+= 2;
     685           0 :                                                 while (i < mode_len) {
     686           0 :                                                         if (mode[i] == ':') {
     687           0 :                                                                 break;
     688             :                                                         }
     689           0 :                                                         i++;
     690             :                                                 }
     691             :                                         }
     692           0 :                                         state = PARSER_WAIT_COLON;
     693             :                                 } else if (state == PARSER_WAIT_COLON) {
     694             : #if 0
     695             :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Colon expected, '%c' found", mode[i]);
     696             : #endif
     697             :                                 }
     698             :                                 break;
     699             :                 }
     700             :         }
     701           0 : }
     702             : /* }}} */
     703             : 
     704             : MYSQLND_CLASS_METHODS_START(mysqlnd_debug)
     705             :         MYSQLND_METHOD(mysqlnd_debug, open),
     706             :         MYSQLND_METHOD(mysqlnd_debug, set_mode),
     707             :         MYSQLND_METHOD(mysqlnd_debug, log),
     708             :         MYSQLND_METHOD(mysqlnd_debug, log_va),
     709             :         MYSQLND_METHOD(mysqlnd_debug, func_enter),
     710             :         MYSQLND_METHOD(mysqlnd_debug, func_leave),
     711             :         MYSQLND_METHOD(mysqlnd_debug, close),
     712             :         MYSQLND_METHOD(mysqlnd_debug, free),
     713             : MYSQLND_CLASS_METHODS_END;
     714             : 
     715             : 
     716             : /* {{{ mysqlnd_debug_init */
     717             : PHPAPI MYSQLND_DEBUG *
     718           0 : mysqlnd_debug_init(const char * skip_functions[] TSRMLS_DC)
     719             : {
     720           0 :         MYSQLND_DEBUG *ret = calloc(1, sizeof(MYSQLND_DEBUG));
     721             : #ifdef ZTS
     722             :         ret->TSRMLS_C = TSRMLS_C;
     723             : #endif
     724           0 :         ret->nest_level_limit = 0;
     725           0 :         ret->pid = getpid();
     726           0 :         zend_stack_init(&ret->call_stack, sizeof(char *));
     727           0 :         zend_stack_init(&ret->call_time_stack, sizeof(uint64_t));
     728           0 :         zend_hash_init(&ret->not_filtered_functions, 0, NULL, NULL, 0);
     729           0 :         zend_hash_init(&ret->function_profiles, 0, NULL, NULL, 0);
     730             : 
     731           0 :         ret->m = & mysqlnd_mysqlnd_debug_methods;
     732           0 :         ret->skip_functions = skip_functions;
     733             : 
     734           0 :         return ret;
     735             : }
     736             : /* }}} */
     737             : 
     738             : 
     739             : /* {{{ _mysqlnd_debug */
     740           0 : PHPAPI void _mysqlnd_debug(const char * mode TSRMLS_DC)
     741             : {
     742             : #if PHP_DEBUG
     743             :         MYSQLND_DEBUG * dbg = MYSQLND_G(dbg);
     744             :         if (!dbg) {
     745             :                 struct st_mysqlnd_plugin_trace_log * trace_log_plugin = mysqlnd_plugin_find("debug_trace");
     746             :                 if (trace_log_plugin) {
     747             :                         dbg = trace_log_plugin->methods.trace_instance_init(mysqlnd_debug_std_no_trace_funcs TSRMLS_CC);
     748             :                         if (!dbg) {
     749             :                                 return;
     750             :                         }
     751             :                         MYSQLND_G(dbg) = dbg;
     752             :                 }
     753             :         }
     754             :         if (dbg) {
     755             :                 dbg->m->close(dbg);
     756             :                 dbg->m->set_mode(dbg, mode);
     757             :                 while (zend_stack_count(&dbg->call_stack)) {
     758             :                         zend_stack_del_top(&dbg->call_stack);
     759             :                 }
     760             :                 while (zend_stack_count(&dbg->call_time_stack)) {
     761             :                         zend_stack_del_top(&dbg->call_time_stack);
     762             :                 }
     763             :         }
     764             : #endif
     765           0 : }
     766             : /* }}} */
     767             : 
     768             : 
     769             : static struct st_mysqlnd_plugin_trace_log mysqlnd_plugin_trace_log_plugin =
     770             : {
     771             :         {
     772             :                 MYSQLND_PLUGIN_API_VERSION,
     773             :                 "debug_trace",
     774             :                 MYSQLND_VERSION_ID,
     775             :                 MYSQLND_VERSION,
     776             :                 "PHP License 3.01",
     777             :                 "Andrey Hristov <andrey@mysql.com>,  Ulf Wendel <uwendel@mysql.com>, Georg Richter <georg@mysql.com>",
     778             :                 {
     779             :                         NULL, /* no statistics , will be filled later if there are some */
     780             :                         NULL, /* no statistics */
     781             :                 },
     782             :                 {
     783             :                         NULL /* plugin shutdown */
     784             :                 }
     785             :         },
     786             :         {/* methods */
     787             :                 mysqlnd_debug_init,
     788             :         }
     789             : };
     790             : 
     791             : 
     792             : /* {{{ mysqlnd_debug_trace_plugin_register */
     793             : void
     794       20507 : mysqlnd_debug_trace_plugin_register(TSRMLS_D)
     795             : {
     796       20507 :         mysqlnd_plugin_register_ex((struct st_mysqlnd_plugin_header *) &mysqlnd_plugin_trace_log_plugin TSRMLS_CC);
     797       20507 : }
     798             : /* }}} */
     799             : 
     800             : 
     801             : /*
     802             :  * Local variables:
     803             :  * tab-width: 4
     804             :  * c-basic-offset: 4
     805             :  * End:
     806             :  * vim600: noet sw=4 ts=4 fdm=marker
     807             :  * vim<600: noet sw=4 ts=4
     808             :  */

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:18 +0000 (3 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.