From 7ec8ae12c4b214e39f27e0a93556750505f9cf95 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 9 Apr 2023 11:59:50 +0100 Subject: [PATCH] ext/pgsql: pg_trace allow to refine its trace mode via 2 new constants. - PGSQL_TRACE_SUPPRESS_TIMESTAMPS. - PGSQL_TRACE_REGRESS_MODE to have a more verbose and observable output to check possible regressions. Close GH-11041 --- NEWS | 2 ++ UPGRADING | 12 ++++++++---- ext/pgsql/pgsql.c | 16 +++++++++++++++- ext/pgsql/pgsql.stub.php | 16 +++++++++++++++- ext/pgsql/pgsql_arginfo.h | 9 ++++++++- ext/pgsql/tests/pg_trace.phpt | 26 ++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 ext/pgsql/tests/pg_trace.phpt diff --git a/NEWS b/NEWS index 1a099ff0e20..92fe365e3ab 100644 --- a/NEWS +++ b/NEWS @@ -124,6 +124,8 @@ PHP NEWS (David Carlier) . Added GH-9344, pipeline mode support. (David Carlier) . pg_cancel use thread safe PQcancel api instead. (David Carlier) + . pg_trace new PGSQL_TRACE_SUPPRESS_TIMESTAMPS/PGSQL_TRACE_REGRESS_MODE + contants support. (David Carlier) - Phar: . Fix memory leak in phar_rename_archive(). (stkeke) diff --git a/UPGRADING b/UPGRADING index 665f6b3f418..c9067e02884 100644 --- a/UPGRADING +++ b/UPGRADING @@ -214,11 +214,15 @@ PHP 8.3 UPGRADE NOTES - PCNTL: . SIGINFO +- PGSQL: + . PGSQL_TRACE_SUPPRESS_TIMESTAMPS. + . PGSQL_TRACE_REGRESS_MODE. + - Posix: - . POSIX_SC_ARG_MAX - . POSIX_SC_PAGESIZE - . POSIX_SC_NPROCESSORS_CONF - . POSIX_SC_NPROCESSORS_ONLN + . POSIX_SC_ARG_MAX. + . POSIX_SC_PAGESIZE. + . POSIX_SC_NPROCESSORS_CONF. + . POSIX_SC_NPROCESSORS_ONLN. - Sockets: . SO_ATTACH_REUSEPORT_CBPF (Linux only). diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 9eab2342002..7f912320398 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -2115,13 +2115,14 @@ PHP_FUNCTION(pg_trace) { char *z_filename, *mode = "w"; size_t z_filename_len, mode_len; + zend_long trace_mode = 0; zval *pgsql_link = NULL; PGconn *pgsql; FILE *fp = NULL; php_stream *stream; pgsql_link_handle *link; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|sO!", &z_filename, &z_filename_len, &mode, &mode_len, &pgsql_link, pgsql_link_ce) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|sO!l", &z_filename, &z_filename_len, &mode, &mode_len, &pgsql_link, pgsql_link_ce, &trace_mode) == FAILURE) { RETURN_THROWS(); } @@ -2147,6 +2148,19 @@ PHP_FUNCTION(pg_trace) } php_stream_auto_cleanup(stream); PQtrace(pgsql, fp); + if (trace_mode > 0) { +#ifdef PQTRACE_REGRESS_MODE + if (!(trace_mode & (PQTRACE_SUPPRESS_TIMESTAMPS|PQTRACE_REGRESS_MODE))) { + zend_argument_value_error(4, "must be PGSQL_TRACE_SUPPRESS_TIMESTAMPS and/or PGSQL_TRACE_REGRESS_MODE"); + RETURN_THROWS(); + } else { + PQsetTraceFlags(pgsql, trace_mode); + } +#else + zend_argument_value_error(4, "cannot set as trace is unsupported"); + RETURN_THROWS(); +#endif + } RETURN_TRUE; } /* }}} */ diff --git a/ext/pgsql/pgsql.stub.php b/ext/pgsql/pgsql.stub.php index 35efd2f33d7..e095350543c 100644 --- a/ext/pgsql/pgsql.stub.php +++ b/ext/pgsql/pgsql.stub.php @@ -412,6 +412,20 @@ namespace { * @cvalue PGSQL_DML_STRING */ const PGSQL_DML_STRING = UNKNOWN; +#ifdef PQTRACE_SUPPPRESS_TIMESTAMPS + /** + * @var int + * @cvalue PQTRACE_SUPPRESS_TIMESTAMPS + */ + const PGSQL_TRACE_SUPPRESS_TIMESTAMPS = UNKNOWN; +#endif +#ifdef PQTRACE_REGRESS_MODE + /** + * @var int + * @cvalue PQTRACE_REGRESS_MODE + */ + const PGSQL_TRACE_REGRESS_MODE = UNKNOWN; +#endif #ifdef LIBPQ_HAS_PIPELINING /** @@ -662,7 +676,7 @@ namespace { */ function pg_getlastoid(PgSql\Result $result): string|int|false {} - function pg_trace(string $filename, string $mode = "w", ?PgSql\Connection $connection = null): bool {} + function pg_trace(string $filename, string $mode = "w", ?PgSql\Connection $connection = null, int $trace_mode = 0): bool {} function pg_untrace(?PgSql\Connection $connection = null): bool {} diff --git a/ext/pgsql/pgsql_arginfo.h b/ext/pgsql/pgsql_arginfo.h index 01ffe5c3df6..2b8e7cd17ae 100644 --- a/ext/pgsql/pgsql_arginfo.h +++ b/ext/pgsql/pgsql_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: c4ff82a0df3e65eae3abbb8c9910978f28bf6ac4 */ + * Stub hash: f18a73443942daa2b3695e8750c8daaea6b96194 */ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_pg_connect, 0, 1, PgSql\\Connection, MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, connection_string, IS_STRING, 0) @@ -198,6 +198,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pg_trace, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, mode, IS_STRING, 0, "\"w\"") ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, connection, PgSql\\Connection, 1, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, trace_mode, IS_LONG, 0, "0") ZEND_END_ARG_INFO() #define arginfo_pg_untrace arginfo_pg_close @@ -808,6 +809,12 @@ static void register_pgsql_symbols(int module_number) REGISTER_LONG_CONSTANT("PGSQL_DML_EXEC", PGSQL_DML_EXEC, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_DML_ASYNC", PGSQL_DML_ASYNC, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_DML_STRING", PGSQL_DML_STRING, CONST_PERSISTENT); +#if defined(PQTRACE_SUPPPRESS_TIMESTAMPS) + REGISTER_LONG_CONSTANT("PGSQL_TRACE_SUPPRESS_TIMESTAMPS", PQTRACE_SUPPRESS_TIMESTAMPS, CONST_PERSISTENT); +#endif +#if defined(PQTRACE_REGRESS_MODE) + REGISTER_LONG_CONSTANT("PGSQL_TRACE_REGRESS_MODE", PQTRACE_REGRESS_MODE, CONST_PERSISTENT); +#endif #if defined(LIBPQ_HAS_PIPELINING) REGISTER_LONG_CONSTANT("PGSQL_PIPELINE_SYNC", PGRES_PIPELINE_SYNC, CONST_PERSISTENT); #endif diff --git a/ext/pgsql/tests/pg_trace.phpt b/ext/pgsql/tests/pg_trace.phpt new file mode 100644 index 00000000000..0917959bbef --- /dev/null +++ b/ext/pgsql/tests/pg_trace.phpt @@ -0,0 +1,26 @@ +--TEST-- +pg_trace +--EXTENSIONS-- +pgsql +--SKIPIF-- + +--FILE-- +getMessage() . PHP_EOL; +} +var_dump(pg_trace($tracefile, 'w', $db, 0)); +$res = pg_query($db, 'select 1'); + +?> +--EXPECT-- +pg_trace(): Argument #4 ($trace_mode) cannot set as trace is unsupported +bool(true)