The use of GDB debugging and debugging scripts under Linux gcc/g++

Source: Internet
Author: User
Tags create directory gcov

GDB debugging and the use of debug scripts
Return Script Blackstone
First, GDB debugging

1.1. GDB Overview

GDB is a powerful UNIX program debugging tool released by the GNU Open source organization. Perhaps, you prefer that graphical interface, such as VC, BCB and other IDE debugging, but if you are in the Unix/linux platform to do software, you will find GDB this debugging tool has more powerful features than VC, BCB graphical debugger. The so-called "inch, the ruler is short" is this truth.

In general, GDB is the main help you complete the following four aspects of the function:

1, start your program, you can follow your custom requirements to run the program at will.
2. Allow the program to be debugged to stop at the breakpoint where you have specified the adjustment. (Breakpoint can be a conditional expression)
3. When the program is stopped, you can check what happened in your program at this time.
4. Dynamically change the execution environment of your program.

From the above, GDB and the General debugging tool is not the same, basically is to complete these functions, but in the details, you will find GDB this debugging tool is powerful, you may be more accustomed to the graphical debugging tools, but sometimes, the command line debugging tools have a graphical tool can not be done. Let's see.

1.2.GDB Use Example

Use a simple judgment to test: file name gdbtest.c

#include "stdio.h"
int main ()
{
int x=3;
if (x<4)
printf ("X is less than 4\n");
Else
printf ("X is biger than 4\n");
}


The program is very simple, set x=3, and then determine whether X is smaller than 4, if the output is smaller than 4 "x is less than 4", if it is larger than 4, then output "X is biger than 4", the program is very boring, but we can use to do GDB test!

Note: When compiling, you need to use the-G option, I am using: gcc-g-o gdbtest gdbtest.c

Debugging with GDB:

GdB./gdbtest <-------start GDB
GNU gdb (GDB) 7.5-ubuntu
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I686-linux-gnu".
For bugs reporting instructions, please see:
Reading symbols From/home/long/gdbtest...done.
(GDB) L <-------l command equals list, starting from the first line
Code
1 #include "stdio.h"
2 int Main ()
3 {
4 int x=3;
5 if (x<4)
6 printf ("X is less than 4\n");
7 Else
8 printf ("X is biger than 4\n");
9}
(GDB) b 5 <-------B Quite with break set breakpoints at line 5th of the source program.
Breakpoint 1 at 0x8048935:file gdbtest.c, line 5.
(GDB) Run <-------runs the program, or you can use the abbreviated R
Starting program:/home/long/gdbtest

Breakpoint 1, Main () at Gdbtest.c:5 <-------actually stop at the first breakpoint, in line 5th
5 if (x<4)
(GDB) Info B <-------view breakpoint information
Num Type Disp Enb Address What
1 breakpoint Keep Y 0x08048935 in main at Gdbtest.c:5
Breakpoint already hit 1 time
(GDB) Print x <-------prints the value of x (print can also use its shorthand p), at which point x equals 3 of the value assigned above
$ = 3
(gdb) Print &x <-------The address of the printing x
$ = (int *) 0xbffff21c
(GDB) x/4x 0xbffff21c <-------View the value of 4*4 bytes starting from 0xbffff21c
0xbffff21c:0x00000003 0x0804a000 0x00000000 0x00000000 <-------x is an int value of 4 bytes
, so the value of x equals 0x00000003, and we can see that at this point x equals 3.
(GDB) Set X=5 <-------we set x=5
(GDB) Print x <-------prints the x value, you can see that x has been changed to 5.
$5
(GDB) x/4x 0xbffff21c
0xbffff21c:0x00000005 0x0804a000 0x00000000 0x00000000
(GDB) n <-------single statement execution, next command shorthand.
8 printf ("X is biger than 4\n");
(GDB) C <-------continue to run the program, continue command shorthand.
Continuing.
Profiling:/home/zhouyl:cannot Create Directory
Profiling:/home/zhouyl/nicholclass/error_test/gdb/gdbtest.gcda:skip
X is biger than 4[inferior 1 (process 9265) exited with code] <-------program Output x is biger than 4 because at this point x has been changed to 5
(GDB) Q <-------exit GDB
#


In the GDB debug test above, we can change the value of X to 5, then the output of the program becomes X is biger than 4. It's fun and powerful, isn't it?


1.3.GDB More Knowledge points Summary (continuous collection)

1.3.1 GDB debugging How to pass parameters?

We still use examples to demonstrate:

The code for the sample is simple: test.c

#include <stdio.h>
int main (int argc, char **argv)
{
int i=0;
I=atoi (argv[1]);
i = i + 1;
printf ("The value after add the first arg is:%d\n", i);
I=atoi (argv[2]);
i = i-1;
printf ("The value after minus the second arg is:%d\n", i);
return 0;
}


In the example, we print the first parameter plus 1 and the second parameter minus 1 for each one.

We use GDB to debug:

#gcc-G-O test test.c
#gdb./test <-------normal Start dispensing procedure
GNU gdb (GDB) 7.5-ubuntu
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I686-linux-gnu".
For bugs reporting instructions, please see:
Reading symbols From/tmp/test...done.
(GDB) Set args 111 1 <-------Pass parameters to the program when debugging
(GDB) Run
Starting program:/tmp/test 111 1
The value after add the first arg is:112
The value after minus the second Arg is:0
[Inferior 1 (process 9667) exited normally]
(GDB) Q
#



Second, the use of GDB debug script

Here we use GDB script debugging in the first chapter of the gdbtest.c file, in fact, it is very simple we just put the required operations into a file, such as called gdbtest.sh

Break 5
Run
Set x=5
C
Q

So how do we use it? In fact, it's very simple, we use GDB without direct gdb gdbtest./gdbtest-command=gdbtest.sh

In fact there is a way, we directly in the script to add the file information to debug, at this time the gdbtest.sh content is:

File Gdbtest
Break 5
Run
Set x=5
C
Q

And we debug the use of the command is simple, directly use Gdb-x gdbtest.sh can!



Third, the use of Gcov

What is 3.1 Gcov?

Gcov is GCC Coverage

is a tool for testing code coverage

is a command-line way of the console program

With the GCC release, together with GCC to implement the statement overlay and the branch coverage test of C + + file;

Working with the program Profiling Tools (profiling tool, such as gprof), you can estimate which piece of code in the program is the most time-consuming;

Note: The program profiling tool is a tool for analyzing code performance.


What can 3.2 Gcov do?

Gcov can be counted:

How often each line of code executes

Actually, what code really is executed?

Time spent on each piece of code (section Code) (Execution time)

Therefore, Gcov can help you optimize the code, of course, this optimization action should be a developer to complete.


3.3 Gcov Use

We continue to use the Gdbtest.c file in chapter One, using Gcov at compile time, using Gcc-g3-fprofile-arcs-ftest-coverage gdbtest.c


#ls
Gdbtest.c gdbtest.sh
#gcc-g3-fprofile-arcs-ftest-coverage gdbtest.c <--------add Gcov information using the-fprofile-arcs-ftest-coverage parameter, you can actually not use the-G parameter , I need to use GDB debugging in my example, so I added a
#./a.out
X is less than 4
#gcov Gdbtest
File ' gdbtest.c '
Number of rows executed: 83.33% (total 6 rows)
Creating ' Gdbtest.c.gcov '

# Cat Gdbtest.c.gcov
-: 0:source:gdbtest.c
-: 0:graph:gdbtest.gcno
-: 0:DATA:GDBTEST.GCDA
-: 0:runs:1
-: 0:programs:1
-: 1: #include "stdio.h"
1:2:int Main () <-----------"1" for bank run Times
-: 3:{
1:4: int x=3;
1:5: if (x<4)
1:6: printf ("X is less than 4\n");
-: 7:else
#####: 8:printf ("x is biger than 4\n"); <-----------"#####" for this line not shipped > line
1:9:}
#
#gdb./a.out-command=gdbtest.sh <--------Use the script above to debug it
The real purpose is to run else, and then look at the difference!
GNU gdb (GDB) 7.5-ubuntu
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I686-linux-gnu".
For bugs reporting instructions, please see:
Reading symbols From/home/long/gcovtest/a.out...done.
Breakpoint 1 at 0x80489dd:file gdbtest.c, line 5.
Breakpoint 1, Main () at Gdbtest.c:5
5 if (x<4)
X is biger than 4
[Inferior 1 (Process 10165) exited with code 01]
#gcov Gdbtest <-----------run multiple times a.out or
Use the script, and then want to look back at the latest test code coverage, need to re-gcov Gdbtest
File ' gdbtest.c '
Number of rows executed: 100% (total 6 rows)
Creating ' Gdbtest.c.gcov '

#cat Gdbtest.c.gcov
-: 0:source:gdbtest.c
-: 0:graph:gdbtest.gcno
-: 0:DATA:GDBTEST.GCDA
-: 0:runs:2
-: 0:programs:1
-: 1: #include "stdio.h"
2:2:int Main ()
-: 3:{
2:4: int x=3;
2:5: if (x<4)
1:6: printf ("X is less than 4\n"); <-----------run with a script, this > row is not executed, so it runs 1 times
-: 7:else <-----------In fact, the bank else is running > once, but when Gcov statistics, we put together the next line of print time!
1:8: printf ("x is biger than 4\n");
2:9:}
#



The use of GDB debugging and debugging scripts under Linux gcc/g++

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.