Use of GDB debugging and debugging scripts

Source: Internet
Author: User
Tags gcov
I. GDB debugging

1.1. GDB Overview

GDB is a powerful Unix program debugging tool released by the GNU open-source organization. Maybe you prefer the graphical interface, such as Vc, BCB, and other ide debugging, but if you are running software on Unix/Linux platforms, you will find that the gdb debugging tool has more powerful functions than the visual debugger of VC and BCB. This is the so-called "have an inch, have a small size and have a short size.

In general, GDB helps you complete the following four functions:

1. Start your program and run it as needed according to your custom requirements.
2. The program to be debugged can be stopped at the breakpoint you specified. (The breakpoint can be a conditional expression)
3. When the program is stopped, you can check what happens in your program.
4. dynamically change the execution environment of your program.

From the above point of view, GDB is similar to a general debugging tool and basically completes these functions. However, in details, you will find that GDB is a powerful debugging tool, you may be used to graphical debugging tools, but sometimes command line debugging tools have functions that cannot be completed by graphical tools. Let's look at it one by one.

1.2.gdb example

Use a simple judgment to test: the 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 judge whether X is smaller than 4. If it is smaller than 4, the output "X is less than 4". If it is larger than 4, the output "X is biger than 4" is boring, but we can use it for GDB testing!

Note: you must use the-G option during compilation. I use GDB-G3 gdbtest. C-o gdbtest.

Debug with GDB:

# GDB gdbtest <------- start gdbgnu GDB (GDB) 7.5-ubuntucopyright (c) 2012 Free Software Foundation, Inc. license gplv3 +: gnu gpl Version 3 or later 

In the above GDB debugging test, we can change the value of X to 5, and then the output of the program to X is biger than 4. Very interesting and powerful, right?


1.3.gdb Summary of more knowledge points (continuous collection)

1.3.1 how do I pass parameters in GDB debugging?

We still use examples to demonstrate:

The sample code 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 this example, the values of the first parameter plus 1 and the second parameter minus 1 are printed respectively.

We use GDB for debugging. After debugging, we use set ARGs 111 1.
Method setting parameters

# Gcc-G3 test. c-o Test # GDB test <------- normal start of the program gnu gdb (GDB) 7.5-ubuntucopyright (c) 2012 Free Software Foundation, Inc. license gplv3 +: gnu gpl Version 3 or later 

Or we can use GDB -- args./test 111 1
Method

gdb --args ./test 111 1GNU gdb (GDB) 7.5-ubuntuCopyright (C) 2012 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later 

Ii. Use of GDB debugging scripts

Next we will use the gdb script to debug the gdbtest. c file in the first chapter. In fact, we only need to put the required operations in a file, for example, gdbtest. Sh.

break 5runset x=5cq

So how can we use it? In fact, it is very simple. We don't need to use GDB gdbtest directly, but use GDB./gdbtest-command = gdbtest. Sh.

In fact, there is another way to add the file information to be debugged in the script. The content of gdbtest. Sh is as follows:

File gdbtest <----- specify the target file as gdbtestbreak 5 runset x = 5cq

The command we used for debugging is simple. Use GDB-x gdbtest. Sh directly!



Iii. Use of gcov

3.1 Gcov What is it?
  • Gcov is GCC coverage
  • Is a tool for testing code coverage.
  • Is a command-line console Program
  • With the release of GCC, GCC is used to test the statement coverage and branch coverage of C/C ++ files;
  • Working with profiling tool (such as GPROF), you can estimate which code in the program is the most time-consuming;

Note: The program summary analysis tool is used to analyze code performance.


3.2 gcovWhat can we do?

Gcov statistics:

  • Execution frequency of each line of code
  • Actually, which code has been executed?
  • Time consumed by each section code (execution time)

Therefore, gcov can help you optimize the code. Of course, this optimization action should still be completed by developers.


3.3 gcov usage

We continue to use the gdbtest. c file in chapter 1, and use gcc-G3-fprofile-arcs-ftest-coverage gdbtest. C during gcov compilation.

# Lsgdbtest. c gdbtest. sh # gcc-G3-fprofile-arcs-ftest-coverage gdbtest. c <-------- use the-fprofile-arcs-ftest-coverage parameter to add gcov information. In fact, the-G parameter is not used. In my example, GDB debugging is required, so # is added #. /. outx is less than 4 # gcov gdbtestfile 'gdbtest. c' Number of executed rows: 83.33% (6 rows in total) 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" indicates the number of times this line runs-: 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"); <----------- "####" indicates that this row is not run> Row 1: 9 :}## GDB. /. out-command = gdbtest. sh <-------- use the above script for debugging. In fact, our goal is to run else and see the difference! Gnu gdb (GDB) 7.5-ubuntucopyright (c) 2012 Free Software Foundation, Inc. license gplv3 +: gnu gpl Version 3 or later 

Note:

[1] Chen Hao's column: "using GDB debugging tools"

1. http://blog.csdn.net/haoel/article/details/2879

2. http://blog.csdn.net/haoel/article/details/2880

Http://blog.csdn.net/haoel/article/details/2881

4. http://blog.csdn.net/haoel/article/details/2882

5. http://blog.csdn.net/haoel/article/details/2883

6. http://blog.csdn.net/haoel/article/details/2884

7. http://blog.csdn.net/haoel/article/details/2885

[2] http://blog.csdn.net/zhujinghao09/article/details/8461543

[3] http://blog.csdn.net/ganggexiongqi/article/details/8846001

[4] http://blog.csdn.net/yukin_xue/article/details/7653482

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.