mysqli_debug_mysqlnd_only.phpt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. --TEST--
  2. mysqli_debug() - mysqlnd only control strings
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifemb.inc');
  7. require_once('skipifconnectfailure.inc');
  8. require_once('connect.inc');
  9. if (!function_exists('mysqli_debug'))
  10. die("skip mysqli_debug() not available");
  11. if (!defined('MYSQLI_DEBUG_TRACE_ENABLED'))
  12. die("skip: can't say for sure if mysqli_debug works");
  13. if (defined('MYSQLI_DEBUG_TRACE_ENABLED') && !MYSQLI_DEBUG_TRACE_ENABLED)
  14. die("skip: debug functionality not enabled");
  15. if (!$IS_MYSQLND)
  16. die("skip mysqlnd only test");
  17. ?>
  18. --FILE--
  19. <?php
  20. require_once('connect.inc');;
  21. require_once('table.inc');
  22. function try_control_string($link, $control_string, $trace_file, $offset) {
  23. @unlink($trace_file);
  24. if (true !== ($tmp = @mysqli_debug($control_string))) {
  25. printf("[%03d][control string '%s'] Expecting boolean/true, got %s/%s.\n",
  26. $offset + 1,
  27. $control_string,
  28. gettype($tmp),
  29. $tmp);
  30. return false;
  31. }
  32. if (!$res = mysqli_query($link, 'SELECT * FROM test')) {
  33. printf("[%03d][control string '%s'] [%d] %s.\n",
  34. $offset + 2,
  35. $control_string,
  36. mysqli_errno($link),
  37. mysqli_error($link));
  38. return false;
  39. }
  40. while ($row = mysqli_fetch_assoc($res))
  41. ;
  42. mysqli_free_result($res);
  43. clearstatcache();
  44. if (!file_exists($trace_file)) {
  45. printf("[%03d][control string '%s'] Trace file has not been written.\n",
  46. $offset + 3,
  47. $control_string,
  48. gettype($tmp),
  49. $tmp);
  50. return false;
  51. }
  52. return trim(substr(file_get_contents($trace_file), 0, 100024));
  53. }
  54. $memory_funcs = array(
  55. '_mysqlnd_ecalloc',
  56. '_mysqlnd_emalloc',
  57. '_mysqlnd_palloc_free_thd_cache_reference',
  58. '_mysqlnd_pecalloc',
  59. '_mysqlnd_pefree',
  60. '_mysqlnd_pemalloc',
  61. '_mysqlnd_perealloc',
  62. );
  63. $trace_file = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, 'mysqli_debug_phpt.trace');
  64. $trace = try_control_string($link, 't:m:O,' . $trace_file, $trace_file, 10);
  65. if (!strstr($trace, 'SELECT * FROM test') && !strstr($trace, 'mysql_real_query'))
  66. printf("[015] SELECT query cannot be found in trace. Trace contents seems wrong.\n");
  67. $lines_trace = explode("\n", $trace);
  68. $functions_trace = array();
  69. foreach ($lines_trace as $k => $line) {
  70. $line = trim($line);
  71. if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) {
  72. $functions_trace[$matches[1]] = $matches[1];
  73. }
  74. }
  75. $found = 0;
  76. foreach ($memory_funcs as $k => $name)
  77. if (isset($functions_trace[$name]))
  78. $found++;
  79. if ($found < (count($memory_funcs) - 3))
  80. printf("[016] Only %d memory functions have been found, expecting at least %d.\n",
  81. $found, count($memory_funcs) - 3);
  82. $trace = try_control_string($link, 't:O,' . $trace_file, $trace_file, 20);
  83. if (!strstr($trace, 'SELECT * FROM test') && !strstr($trace, 'mysql_real_query'))
  84. printf("[025] SELECT query cannot be found in trace. Trace contents seems wrong.\n");
  85. $lines_trace = explode("\n", $trace);
  86. $functions_trace = array();
  87. foreach ($lines_trace as $k => $line) {
  88. $line = trim($line);
  89. if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) {
  90. $functions_trace[$matches[1]] = $matches[1];
  91. }
  92. }
  93. $found = 0;
  94. foreach ($memory_funcs as $k => $name)
  95. if (isset($functions_trace[$name]))
  96. $found++;
  97. if ($found > 2)
  98. printf("[026] More than %d memory functions have been recorded, that's strange.\n",
  99. $found);
  100. mysqli_close($link);
  101. @unlink($trace_file);
  102. print "done!";
  103. ?>
  104. --CLEAN--
  105. <?php
  106. require_once("clean_table.inc");
  107. ?>
  108. --EXPECTF--
  109. done!