PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/mysqlnd/mysqlnd_debug.c

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