乐闻世界logo
搜索文章和话题

Ask GDB to list all functions in a program

1个答案

1

When using GDB (GNU Debugger) for debugging, if you want to list all functions in your program, you can use several different methods. First, ensure that the debugging information for the program is loaded.

Method 1: Using info functions

The most straightforward method is to use the info functions command in the GDB command line. This command lists all available function names in the program, including both static and non-static functions. For example:

bash
(gdb) info functions

This will display output similar to the following:

shell
All defined functions: File main.c: void print_hello(); int main(); File utils.c: int add(int, int); int subtract(int, int);

This example shows that main.c contains the print_hello() and main() functions, while utils.c contains the add() and subtract() functions.

Method 2: Using the nm Tool

Although not executed directly within GDB, you can also use the nm command in a Linux system to list all symbols in the program, including functions. This is particularly useful for binary files without debugging information. For example:

bash
nm -C your_program

Here, the -C option tells nm to demangle the actual names of the symbols, which helps you more easily identify each function. The output will include the address, type (e.g., "T" for a symbol defined in the text (code) section), and symbol name.

Method 3: Using objdump

Similar to nm, the objdump command can be used to view function information contained in the compiled program. Use the following command:

bash
objdump -t your_program | grep ' F '

This command filters out all functions (entries marked with 'F'). The information provided is similar to nm.

Conclusion

Typically, info functions is the most straightforward method in GDB to view all defined functions, as it is fully integrated within the debugging environment. However, if you are examining binary files without debugging information or need to analyze symbols outside of GDB, nm and objdump are very useful tools.

2024年6月29日 12:07 回复

你的答案