File: mysqli_debug_mysqlnd_only.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (127 lines) | stat: -rw-r--r-- 3,856 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
--TEST--
mysqli_debug() - mysqlnd only control strings
--EXTENSIONS--
mysqli
--SKIPIF--
<?php
require_once 'skipifconnectfailure.inc';

if (!function_exists('mysqli_debug'))
    die("skip mysqli_debug() not available");

if (!defined('MYSQLI_DEBUG_TRACE_ENABLED'))
    die("skip: can't say for sure if mysqli_debug works");

if (defined('MYSQLI_DEBUG_TRACE_ENABLED') && !MYSQLI_DEBUG_TRACE_ENABLED)
    die("skip: debug functionality not enabled");
?>
--FILE--
<?php
    require_once 'table.inc';

    function try_control_string($link, $control_string, $trace_file, $offset) {

        @unlink($trace_file);
        if (true !== ($tmp = @mysqli_debug($control_string))) {
            printf("[%03d][control string '%s'] Expecting boolean/true, got %s/%s.\n",
                $offset + 1,
                $control_string,
                gettype($tmp),
                $tmp);
            return false;
        }

        if (!$res = mysqli_query($link, 'SELECT * FROM test')) {
            printf("[%03d][control string '%s'] [%d] %s.\n",
                $offset + 2,
                $control_string,
                mysqli_errno($link),
                mysqli_error($link));
            return false;
        }
        while (mysqli_fetch_assoc($res))
            ;
        mysqli_free_result($res);

        clearstatcache();
        if (!file_exists($trace_file)) {
            printf("[%03d][control string '%s'] Trace file has not been written.\n",
                $offset + 3,
                $control_string,
                gettype($tmp),
                $tmp);
            return false;
        }

        return trim(substr(file_get_contents($trace_file), 0, 100024));
    }

    $memory_funcs = array(
        '_mysqlnd_ecalloc',
        '_mysqlnd_emalloc',
        '_mysqlnd_palloc_free_thd_cache_reference',
        '_mysqlnd_pecalloc',
        '_mysqlnd_pefree',
        '_mysqlnd_pemalloc',
        '_mysqlnd_perealloc',
    );
    $trace_file = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, 'mysqli_debug_phpt.trace');

    $trace = try_control_string($link, 't:m:O,' . $trace_file, $trace_file, 10);
    if (!strstr($trace, 'SELECT * FROM test') && !strstr($trace, 'mysql_real_query'))
        printf("[015] SELECT query cannot be found in trace. Trace contents seems wrong.\n");

    $lines_trace = explode("\n", $trace);
    $functions_trace = array();
    foreach ($lines_trace as $line) {
        $line = trim($line);
        if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) {
            $functions_trace[$matches[1]] = $matches[1];
        }
    }

    $found = 0;
    foreach ($memory_funcs as $name)
        if (isset($functions_trace[$name]))
            $found++;

    if ($found < 1) {
        printf("[016] Only %d memory functions have been found, expecting at least %d.\n",
            $found, 1);
        var_dump($trace);
    }

    $trace = try_control_string($link, 't:O,' . $trace_file, $trace_file, 20);
    if (!strstr($trace, 'SELECT * FROM test') && !strstr($trace, 'mysql_real_query'))
        printf("[025] SELECT query cannot be found in trace. Trace contents seems wrong.\n");

    $lines_trace = explode("\n", $trace);
    $functions_trace = array();
    foreach ($lines_trace as $line) {
        $line = trim($line);
        if (preg_match("@^[|\s]*>([\w:]+)@ism", $line, $matches)) {
            $functions_trace[$matches[1]] = $matches[1];
        }
    }

    $found = 0;
    foreach ($memory_funcs as $name)
        if (isset($functions_trace[$name]))
            $found++;

    if ($found > 2) {
        printf("[026] More than %d memory functions have been recorded, that's strange.\n",
            $found);
        var_dump($trace);
    }

    mysqli_close($link);
    @unlink($trace_file);
    print "done!";
?>
--CLEAN--
<?php
require_once 'clean_table.inc';
?>
--EXPECT--
done!