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

Generated by: LCOV version 1.10

Generated at Sun, 22 May 2016 10:52:57 +0000 (3 days ago)

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