Talk C chestnuts together (125th back: C language instance -- display file name and row number)

Source: Internet
Author: User

Talk C chestnuts together (125th back: C language instance -- display file name and row number)

Hello, everyone. The built-in macro example we mentioned in the previous review is as follows:Show file name and row number. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

We used a built-in macro to display the file name and row number in the previous step. In this case, we will introduce another method to display the file name and row number: Use the addr2line tool to display the row number of the file name.

Addr2line is a debugging tool.To convert an address to a program file name or line number based on the debugging information in the executable file. This tool has multiple options. The commonly used options are "e" and "f ".

For example, addr2line-e a. out 0x12345678 will display the file name and row number corresponding to the address 0x12345678. Example: addr2line-f-e. the function name corresponding to the address 0x12345678 will be displayed on the basis of the original 0x12345678. Of course, these two options can be used together to display the file name, function name, and row number at the same time.

It is not our style to say that we do not practice it. Let's take a practical example to illustrate how to use the addr2line tool to display the file name and row number through code.

In this example, we first run the func function, and then display the func address of the function in the main function.

int main(){    func();    printf("address of func:%p \n",func);    return 0;}

The function func is defined as follows. For simplicity, only one row of statements is output in the function, indicating that the function is running.

void func(){    printf("func is running \n");}

Compile the program and add debugging information. The specific command is as follows:

Gcc-g Ex082_addr2line.c-o s // compile the executable file named s

Run the file to obtain the following results:

./S // run the executable file func is running address of func: 0x804844d // the address of the function is displayed

Then we can view the file name, row number, and function name through the address. The specific command is as follows:

Addr2line-f-e s 0x804844d // use both the f and e options. Note that f must be run based on e, therefore, f displays the function name/home/talk8/Ex082_addr2line.c: 8 // In front of e and displays the file name and row number corresponding to address 0x804844d.

The readers will not write code in the text, and the completed Code will be put into my resources. You can download and use it. From the code
To see that the func function is located in row 8th of the file.

At last, let's compare the advantages and disadvantages of the two display methods:

Advantages of built-in macros: the file name and line number can be directly displayed when the program is running, without debugging or other third-party tools. Disadvantages of built-in macros: You need to add a built-in macro to the running program. Otherwise, the file name and row number are not displayed. In addition, it is a headache to add a built-in macro to the program, this needs to be added based on the needs of the program. Advantages of the addr2line tool: When debugging a program, the file name and line number are directly displayed through an address (the address is usually obtained in the dump file ), the most important thing is that we do not need to add any content to the code. Disadvantage of the addr2line tool: You need to use the "g" option of gcc to add debugging information during compilation, which will increase the compilation time and increase the size of the target file. In addition, it needs to use the address in the program to display the file name and row number. If there is no valid address, it will be powerless.

Based on my experience, the display of file names and line numbers is mainly to quickly find errors in the program. If errors in the program are easy to reproduce, you can add a built-in macro to the program as necessary, compile the program, and run the compiled program. When an error occurs, the file name and row number are output. In this way, you can quickly locate the file where the error occurred and the location of the error in the file. If errors in the program are not easy to reproduce, you can use the addr2line tool. When an error occurs in the program, generate a dump file, and find the address when the error occurs, with this tool, you can get the specific location of the file with errors and errors in the file. In addition, if there is no dump file, you can use gdb for debugging, and then use the gdb info command to view the address of the relevant function.

In actual work, various errors may occur in the programs we write. Therefore, we hope that you can master these two methods to display file names and row numbers. I also used different methods in different situations based on my shared experience to quickly find errors in the program.

Let's talk about the example of displaying file names and row numbers. I want to know what examples will be provided later, and I will try again.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.