Skip to content

[#514] Print line numbers in pgmoneta_backtrace - #521

Merged
jesperpedersen merged 1 commit into
pgmoneta:mainfrom
silver-ymz:feat/enhance-pgmoneta_backtrace
Apr 2, 2025
Merged

jesperpedersen merged 1 commit into
pgmoneta:mainfrom
silver-ymz:feat/enhance-pgmoneta_backtrace

Conversation

@silver-ymz

Copy link
Copy Markdown
Contributor

implement #514

This PR introduces an enhanced stack backtrace feature that provides detailed debugging information, including function names, filenames, and line numbers. When pgmoneta_backtrace is called, it generates a formatted stack trace that is particularly useful for debugging and troubleshooting.

Example Output

The following example demonstrates the output when pgmoneta_backtrace is invoked from the version function in src/main.c:206:

#0  0x7c00d33dff2a in pgmoneta_backtrace
	at /home/silver/code/pgmoneta/src/libpgmoneta/utils.c:4543
#1  0x5dc62da7fe25 in version
	at /home/silver/code/pgmoneta/src/main.c:206
#2  0x5dc62da7c1e0 in main
	at /home/silver/code/pgmoneta/src/main.c:301

Details

  • The implementation leverages the libbacktrace to capture stack trace information.
  • When symbolic information is unavailable, the backtrace will fallback to displaying only the memory addresses.

@jesperpedersen
jesperpedersen self-requested a review March 14, 2025 16:49
@jesperpedersen jesperpedersen added the enhancement Improvement to an existing feature label Mar 14, 2025
@jesperpedersen

Copy link
Copy Markdown
Member

@silver-ymz It looks like libbacktrace isn't available in any repositories

Can you borrow ideas ?

Comment thread src/include/utils.h Outdated
pgmoneta_backtrace(void);

#endif
#ifdef DEBUG

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remove

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed now

Comment thread src/libpgmoneta/utils.c Outdated
return 0;
}

#ifdef DEBUG

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remove

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed now

@silver-ymz

Copy link
Copy Markdown
Contributor Author

It looks like libbacktrace isn't available in any repositories

Can you borrow ideas ?

It seems that only debian and arch have packaged it. I will update the CI and documentation soon.

@jesperpedersen

Copy link
Copy Markdown
Member

@silver-ymz You have to make sure that your solution works with RockyLinux

@silver-ymz
silver-ymz force-pushed the feat/enhance-pgmoneta_backtrace branch from 33ddfea to e47824a Compare March 14, 2025 18:09
@silver-ymz
silver-ymz force-pushed the feat/enhance-pgmoneta_backtrace branch from e47824a to 540166e Compare March 21, 2025 10:49
@silver-ymz

Copy link
Copy Markdown
Contributor Author

@jesperpedersen Could you help to review it if you have time? Thanks.

@jesperpedersen

Copy link
Copy Markdown
Member

You either have to find a solution without libbacktrace or include some of its ideas into your solution.

Did you try an alternative with addr2line ?

@silver-ymz

Copy link
Copy Markdown
Contributor Author

I find 2 alternative solutions.

  1. Using CMake’s ExternalProject_Add feature to include libbacktrace:
    This approach would automate the process of downloading and building libbacktrace during the initial build. It’s convenient for users as it eliminates the need to build libbacktrace separately beforehand. However, it does require an internet connection during the first build.

  2. Using popen to execute addr2line externally:
    It avoids adding new dependencies to the project. addr2line doesn't provide a library function, but we can use popen to execute the external program.

Do you think the second one is better?

@jesperpedersen

Copy link
Copy Markdown
Member

Yes, but for 1) how are you going to build a .RPM for that ? So, you would need to create a subdirectory with the basic necessary files under its license, and then use its API. I guess the key takeaway is that you can't compile external repositories.

For 2) it is a question of we can get the information that we need.

So, for now you will have to work on both solutions so we can compare.

@silver-ymz

Copy link
Copy Markdown
Contributor Author

for 1) how are you going to build a .RPM for that ? So, you would need to create a subdirectory with the basic necessary files under its license, and then use its API. I guess the key takeaway is that you can't compile external repositories.

ExternalProject_Add automatically creates a directory in the build directory. We don't need to include any additional files, except for the changes to CMakeLists.txt

@jesperpedersen

Copy link
Copy Markdown
Member

No, you have to do src/libbacktrace/ in that case, and we don't need all the files

@jesperpedersen

Copy link
Copy Markdown
Member

@silver-ymz Are you ok on this ?

@silver-ymz
silver-ymz force-pushed the feat/enhance-pgmoneta_backtrace branch 2 times, most recently from fe48463 to 5c6006b Compare April 1, 2025 09:23
@silver-ymz

silver-ymz commented Apr 1, 2025 •

Copy link
Copy Markdown
Contributor Author

Now it uses addr2line. Could you review it again? @jesperpedersen

Example Output:

#0  0x781226def0f0 in pgmoneta_backtrace
    at /home/silver/code/pgmoneta/src/libpgmoneta/utils.c:4641
#1  0x5e99d8925556 in version
    at /home/silver/code/pgmoneta/src/main.c:208
#2  0x5e99d89217df in main
    at /home/silver/code/pgmoneta/src/main.c:317

Sorry for not replying for a long time. I've been a bit busy lately.

Comment thread src/libpgmoneta/utils.c Outdated
static bool
calculate_offset(uint64_t addr, uint64_t* offset, char** filepath)
{
FILE* fp = fopen("/proc/self/maps", "r");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

#ifdef HAVE_LINUX

Comment thread src/libpgmoneta/utils.c Outdated
if (fp == NULL)
{
pgmoneta_log_error("Failed to open /proc/self/maps: %s", strerror(errno));
return 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use a goto error

Comment thread src/libpgmoneta/utils.c Outdated
return 1;
}

char line[256];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Variables in the beginning of a function

Comment thread src/libpgmoneta/utils.c Outdated
char* start = strtok(line, "-");
char* end = strtok(NULL, " ");
strtok(NULL, " "); // skip the next token
char* base_offset = strtok(NULL, " ");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Variables in the beginning

Comment thread src/libpgmoneta/utils.c Outdated
{
fclose(fp);
uint64_t base_offset_value = strtoul(base_offset, NULL, 16);
*offset = addr - start_addr + base_offset_value;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can use pgmoneta_append

Comment thread src/libpgmoneta/utils.c Outdated
}
}
}
fclose(fp);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if (fp != NULL)

Comment thread src/libpgmoneta/utils.c Outdated
*offset = addr - start_addr + base_offset_value;
*filepath = malloc(strlen(filepath_ptr) + 1);
memcpy(*filepath, filepath_ptr, strlen(filepath_ptr) + 1);
return 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Move outside

Comment thread src/libpgmoneta/utils.c Outdated
uint64_t end_addr = strtoul(end, NULL, 16);
if (addr >= start_addr && addr < end_addr)
{
fclose(fp);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Move outside

Comment thread src/libpgmoneta/utils.c Outdated
continue;
}

char cmd[256];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The same comments here...

@jesperpedersen

Copy link
Copy Markdown
Member

Do

int
function_name(input_parameters, output_parameters)
{
    // Variables

   // Code

  // Cleanup
  return 0;

error:

  // Cleanup
  return 1;

@jesperpedersen

Copy link
Copy Markdown
Member

It is ok to only support Linux for now

@silver-ymz
silver-ymz force-pushed the feat/enhance-pgmoneta_backtrace branch from 5c6006b to 6724a0e Compare April 1, 2025 12:47
@silver-ymz

Copy link
Copy Markdown
Contributor Author

All suggestions have been adopted. Could you review again?

Comment thread src/include/utils.h
#include <message.h>
#include <workers.h>

#include <stdlib.h>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why ?

@silver-ymz silver-ymz Apr 1, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

clangd hints me it's an unused header.

Comment thread src/libpgmoneta/utils.c Outdated
}

#ifdef DEBUG
#ifdef HAVE_LINUX

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Need to be inside the functions

Comment thread src/libpgmoneta/utils.c Outdated
end_addr = strtoul(end, NULL, 16);
if (addr >= start_addr && addr < end_addr)
{
goto success;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do a bool success instead, and put it in the while

Comment thread src/libpgmoneta/utils.c Outdated
}
}
}
goto error;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No,

if (!success)
{
    goto error;
}

Comment thread src/libpgmoneta/utils.c Outdated
}
goto error;

success:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remove this label

Comment thread src/libpgmoneta/utils.c Outdated
{
printf("%s\n", strings[i]);
pgmoneta_log_error("Failed to get backtrace: %s", strerror(errno));
return 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

goto error

Comment thread src/libpgmoneta/utils.c
}

snprintf(cmd, sizeof(cmd), "addr2line -e %s -fC 0x%lx", filepath, offset);
free(filepath);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Add file_path = NULL;

Comment thread src/libpgmoneta/utils.c Outdated
{
found_main = true;
}
printf("#%d 0x%lx in %s\n", i - 1, addr, buffer);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Build up a string using pgmoneta_append and then pgmoneta_log_debug it

Comment thread src/libpgmoneta/utils.c
buffer[strlen(buffer) - 1] = '\0'; // Remove trailing newline
printf("\tat %s\n", buffer);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Down to here

Comment thread src/libpgmoneta/utils.c
return 0;
}

#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Non-Linux should return 1

@silver-ymz
silver-ymz force-pushed the feat/enhance-pgmoneta_backtrace branch from 6724a0e to 28da67d Compare April 1, 2025 14:04
@silver-ymz

Copy link
Copy Markdown
Contributor Author

Updated now. Could you review again?

Comment thread src/libpgmoneta/utils.c
break;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Where is the return 0; ?

Comment thread src/libpgmoneta/utils.c Outdated
{
found_main = true;
}
log_str_len = snprintf(log_str, sizeof(log_str), "#%d 0x%lx in %s\n", i - 1, addr, buffer);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use pgmoneta_append(s) - you can do

DEBUG: Backtrace
...
...
...

so basically

pgmoneta_append(log_str, "Backtrace:\n") to begin with

Comment thread src/libpgmoneta/utils.c Outdated
}

pgmoneta_log_debug("%s", log_str);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

free(log_str);

Use clang/Debug so you get feedback from ASAN

@silver-ymz
silver-ymz force-pushed the feat/enhance-pgmoneta_backtrace branch from 28da67d to 1f0cdd8 Compare April 1, 2025 17:04
@silver-ymz

Copy link
Copy Markdown
Contributor Author

Updated now. Could you review again?

@jesperpedersen

Copy link
Copy Markdown
Member
[ 41%] Building C object src/CMakeFiles/pgmoneta.dir/libpgmoneta/utils.c.o
src/libpgmoneta/utils.c: In function ‘pgmoneta_backtrace’:
src/libpgmoneta/utils.c:4712:63: error: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size between 227 and 245 [-Werror=format-truncation=]
 4712 |       snprintf(log_buffer, sizeof(log_buffer), "#%d  0x%lx in %s\n", i - 1, addr, buffer);
      |                                                               ^~                  ~~~~~~
src/libpgmoneta/utils.c:4712:7: note: ‘snprintf’ output between 13 and 286 bytes into a destination of size 256
 4712 |       snprintf(log_buffer, sizeof(log_buffer), "#%d  0x%lx in %s\n", i - 1, addr, buffer);
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/libpgmoneta/utils.c:4723:57: error: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size 252 [-Werror=format-truncation=]
 4723 |          snprintf(log_buffer, sizeof(log_buffer), "\tat %s\n", buffer);
      |                                                         ^~     ~~~~~~
src/libpgmoneta/utils.c:4723:10: note: ‘snprintf’ output between 6 and 261 bytes into a destination of size 256
 4723 |          snprintf(log_buffer, sizeof(log_buffer), "\tat %s\n", buffer);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Use pgmoneta_append instead of snprintf

@silver-ymz
silver-ymz force-pushed the feat/enhance-pgmoneta_backtrace branch from 1f0cdd8 to de9ef92 Compare April 2, 2025 07:37
@silver-ymz

Copy link
Copy Markdown
Contributor Author

Updated now. Could you review again?

@jesperpedersen
jesperpedersen merged commit de9ef92 into pgmoneta:main Apr 2, 2025
@jesperpedersen

Copy link
Copy Markdown
Member

Merged.

Thanks for your contribution !

@jesperpedersen

Copy link
Copy Markdown
Member

@silver-ymz Please, port this patch to pgagroal and pgexporter

@silver-ymz
silver-ymz deleted the feat/enhance-pgmoneta_backtrace branch April 3, 2025 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants