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 327 0.9 %
Date: 2014-07-21 Functions: 1 11 9.1 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:13 +0000 (8 days ago)

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