Skip to content
/ server Public

MDEV-38753: Debugging help: print which MEM_ROOT the object is on#4615

Open
spetrunia wants to merge 2 commits into10.6from
10.6-MDEV-38753
Open

MDEV-38753: Debugging help: print which MEM_ROOT the object is on#4615
spetrunia wants to merge 2 commits into10.6from
10.6-MDEV-38753

Conversation

@spetrunia
Copy link
Member

Add two functions intended for use from debugger:
bool dbug_is_mem_on_mem_root(MEM_ROOT *mem_root, void *ptr);
const char *dbug_which_mem_root(THD *thd, void *ptr);

Also, collect declarations of all other functions intended for use from debugger in sql/sql_debug.h

@vuvova
Copy link
Member

vuvova commented Feb 4, 2026

How sql_debug.cc is different from sql_test.cc ? The latter contains debug-only functions like print_where(), print_sjm(), etc.

Copy link
Member

@DaveGosselin-MariaDB DaveGosselin-MariaDB left a comment

Choose a reason for hiding this comment

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

Mostly cosmetic but a couple of nontrivial suggestions.

#ifndef DBUG_OFF

/* Check if ptr points to memory on the mem_root */

Choose a reason for hiding this comment

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

Consider removing this blank line to have the function immediately follow the comment (consistent with dbug_which_mem_root below)

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought most functions actually do the reverse - have a blank line between the comment and function.
Do I change it in dbug_which_mem_root instead?

sql/sql_test.cc Outdated

bool dbug_is_mem_on_mem_root(const MEM_ROOT *mem_root, void *ptr)
{
const USED_MEM *ptrs[]={mem_root->free, mem_root->used};

Choose a reason for hiding this comment

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

As I understand, there can be no MEM_ROOT covering address 0x0, so consider adding a shortcut here when ptr is NULL:

if (!ptr)
  return false;

Copy link
Member Author

Choose a reason for hiding this comment

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

No MEM_ROOT covers the NULL pointer, but the current code already handles it:

(gdb) p dbug_is_mem_on_mem_root(thd->mem_root, 0)
$1 = false

We are in debugger, performance is not a concern... why add shortcuts?

for (const USED_MEM *block= *p; block; block=block->next)
{
const char *start= (const char*)block;
const char *end= start + block->size - block->left;

Choose a reason for hiding this comment

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

It seems to me that a valid block's size should never be zero (or less), but I wouldn't be surprised if a zero-length block is possible for some special case. Consider asserting that it is greater-than zero here. (similar thoughts apply to block->left)

In any case, there's an implicit assumption here that end > start so we should avoid ambiguity in the case that somehow end <= start is possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added an assert that end >= start.

Add two functions intended for use from debugger:
  bool dbug_is_mem_on_mem_root(MEM_ROOT *mem_root, void *ptr);
  const char *dbug_which_mem_root(THD *thd, void *ptr);

Also, collect declarations of all other functions intended for use
from debugger in sql/sql_test.h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants