Skip to content
/ server Public

MDEV-36676 Add debug function to print MEM_ROOT name#4703

Open
longjinvan wants to merge 1 commit intoMariaDB:mainfrom
longjinvan:MDEV-36676
Open

MDEV-36676 Add debug function to print MEM_ROOT name#4703
longjinvan wants to merge 1 commit intoMariaDB:mainfrom
longjinvan:MDEV-36676

Conversation

@longjinvan
Copy link

@longjinvan longjinvan commented Feb 27, 2026

Description

MEM_ROOTs used to have names stored inside the structure. Now these names are stored in Performance Schema, making it difficult to retrieve them during debugging.

Add dbug_print_memroot_name() function that retrieves and prints the name of a MEM_ROOT from Performance Schema. The function uses DBUG_PRINT to output the MEM_ROOT name, address, and PSI key. The function is debug-only (wrapped in #ifndef DBUG_OFF) with zero production overhead.

How can this PR be tested?

A new test file test_memroot_name.c has been added to validate the function. The test is automatically built in Debug mode and verifies that dbug_print_memroot_name() correctly handles NULL pointers, registered PSI keys, PSI_NOT_INSTRUMENTED, and invalid keys.

Standalone unit test:

cd build
cmake ../mariadb-server -G Ninja -DCMAKE_BUILD_TYPE=Debug
ninja test_memroot_name
./mysys/test_memroot_name
cat /tmp/test_memroot_name.trace

Note: In standalone mode, Performance Schema is not initialized, so PSI name resolution is unavailable and keys will show as 0. The test validates edge case handling and crash safety.

End-to-end test within a running server (verifies full PSI name resolution):

# Add a temporary call in server code, e.g. in dispatch_command() in sql/sql_parse.cc:
#   dbug_print_memroot_name(thd->mem_root);

ninja mariadbd
./sql/mariadbd --no-defaults --datadir=/tmp/test_datadir \
  --debug=d,info:t:o,/tmp/mysqld.trace \
  --skip-grant-tables --performance-schema=ON \
  --port=3307 --socket=/tmp/test_mysql.sock &

mysql --socket=/tmp/test_mysql.sock -e "SELECT 1"
grep "MEM_ROOT" /tmp/mysqld.trace

Expected output: MEM_ROOT 'memory/sql/thd::main_mem_root' (0x...) [key=7]

Standalone test output

Testing dbug_print_memroot_name() function
===========================================
Debug trace output: /tmp/test_memroot_name.trace

Test 1: NULL MEM_ROOT
  Expected trace: info: MEM_ROOT: NULL
  OK (no crash)

Test 2: MEM_ROOT with registered PSI key
  Standalone: key will be 0 (PSI noop), no name resolution
  In server:  expect 'memory/test/test_memroot' with actual key
  psi_key=0
  OK

Test 3: MEM_ROOT with PSI_NOT_INSTRUMENTED
  Expected trace: MEM_ROOT (addr) [key=0]
  psi_key=0 OK

Test 4: MEM_ROOT with invalid key (9999)
  Expected trace: MEM_ROOT (addr) [key=9999] (no name)
  psi_key=9999 OK (no crash)

Test 5: MEM_ROOT with second registered key + allocation
  Allocation OK, psi_key=0

===========================================
All tests passed.

Trace file (test_memroot_name.trace):

info: MEM_ROOT: NULL
info: MEM_ROOT (0x7ffd9d770cf0) [key=0]
info: MEM_ROOT (0x7ffd9d770d30) [key=0]
info: MEM_ROOT (0x7ffd9d770d70) [key=9999]
info: MEM_ROOT (0x7ffd9d770db0) [key=0]

End-to-end server test (grep "MEM_ROOT" /tmp/mysqld.trace):

| | info: MEM_ROOT 'memory/sql/thd::main_mem_root' (0x7fa7fc007090) [key=7]
| | info: MEM_ROOT 'memory/sql/thd::main_mem_root' (0x7fa7fc007090) [key=7]
| | info: MEM_ROOT 'memory/sql/thd::main_mem_root' (0x7fa7fc007090) [key=7]

Basing the PR against the correct MariaDB version

This is a new feature and the PR is based against the latest MariaDB development branch

PR quality check

✅ I have checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
✅ For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

Copyright

All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc.

Add dbug_print_memroot_name function that retrieves and prints the name
of a MEM_ROOT from Performance Schema.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer Amazon Web
Services, Inc.
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Feb 27, 2026
Copy link
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution. This is a preliminary review.

Several things to fix in addition to the actual comments below:

  • remove the license reference from the commit message
  • make the commit message compliant with CODING_STANDARDS.md
  • click on the CLA bot button on the PR github page, pick your license and say "accept". This should clear the CLA bot.

#ifndef DBUG_OFF
extern void dbug_print_memroot_name(MEM_ROOT *root);
#else
#define dbug_print_memroot_name(A) do {} while(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't it just be an empty define, similar to e.g. DBUG_ENTER?


if (!root)
{
DBUG_PRINT("info", ("MEM_ROOT: NULL"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would print to the standard output. The primary use of this is when you're on a debugger console and trying to get the name of the memroot quickly without tweaking the P_S functions. So I'd expect this to print to standard output and not via (the possibly turned off) DBUG_PRINT functions.

Ditto for all the rest of the DBUG_PRINTs in this function.

@@ -0,0 +1,142 @@
/* Copyright (c) 2025, MariaDB Corporation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please convert that to a proper unit test and move it into unittest/. If you go with my advice to move it into PFS, you might need to create a subdirectory of unittest/ for pfs.

@@ -715,3 +715,42 @@ LEX_STRING lex_string_casedn_root(MEM_ROOT *root, CHARSET_INFO *cs,
res.str[res.length]= '\0';
return res;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By defining the function here you're making mysys depend on pfs and thus creating a circular dependency since pfs already depends on mysys.

I'd move the whole function out of mysys to pfs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

3 participants