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

Ask GDB to list all functions in a program

1个答案

1

When debugging a program with GDB (GNU Debugger), you can view all functions in the program using various commands. One commonly used command is info functions, which lists all functions in the program, including static functions if they are present in the debugging information.

How to Use the info functions Command

  1. Start GDB: First, you need a compiled program that includes debugging information. For example, if you have a program example.c, you can compile it using the following command:
bash
gcc -g example.c -o example
  1. Start Debugging with GDB: Launch your program using GDB:
bash
gdb ./example
  1. List All Functions: At the GDB prompt, enter info functions to list all visible function names:
shell
(gdb) info functions

This command will display all functions, including those defined in your program and those linked from libraries. If you're interested in specific functions, you can filter the output using regular expressions, for example:

shell
(gdb) info functions main

This command will list all functions containing "main".

Practical Application Example

Suppose you are debugging a simple program that includes several functions for mathematical operations. In your example.c file, you might have functions like add, subtract, and multiply. Using the info functions command in GDB, you will see output similar to the following:

shell
All defined functions: File example.c: int add(int, int); int subtract(int, int); int multiply(int, int); ...

This command helps you quickly understand the program structure, especially when dealing with large or complex codebases.

Summary

info functions is a powerful GDB command for viewing all functions defined in the program. It is very helpful for understanding and debugging the overall structure of the program. Of course, to fully utilize this feature, ensure that you compile your program with the -g option to generate the necessary debugging information.

2024年7月23日 11:06 回复

你的答案