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