Developing debugging applications using GNU GCC and GDB

Source: Internet
Author: User
Tags terminates


Use of GCC commands

When using GCC to compile a program, the compilation process can be subdivided into four phases:

    1. Pretreatment (pre-processing)
    2. Compiling (compiling)
    3. Compilation (assembling)
    4. Links (linking)

If you do not add any parameters, GCC performs all operations by default and generates the executable file directly.

The corresponding parameters of the above four stages are:

1.-E only performs preprocessing

2.–s only compile, not assemble and link

3.–c compiled assembly not linked

4.–o Link to target file

GCC Common options
-C notifies GCC to unlink step, which is to compile the source and generate the target file at the end;

-DMACRO defines the specified macro so that it can be inspected by the #ifdef in the source code;

-E is delivered to standard output without the output of the compiled preprocessor;

-G3 obtains detailed information about the debugger, which cannot be used in conjunction with the-o option;

-idirectory Add the specified directory at the beginning of the containing file search path;

-llibrary prompts the linker to include the specified library when creating the final executable file;

-O,-o2,-o3 the optimization state is turned on, this option cannot be used in conjunction with the-G option;

-S requires the compiler to generate output from the source code assembler program;

-V to start all alerts;

-wall cancels the compile operation when an alert occurs, and the alert is considered an error;

-werror Cancel the compile operation when the alarm occurs, that is, the alarm is treated as an error;

-W disables all alarms.

Use of GDB Debugging tools

Basic GDB Command:

---------------------------------------------------------------------
Command Shorthand function
---------------------------------------------------------------------
File loads the executable files that you want to debug.
Kill K Terminates the program being debugged.
List L lists part of the source code that generated the execution file.
Next N executes a line of source code but does not enter the inside of the function.
Step S executes a line of source code and goes inside the function.
Continue c continue with the program until the next interrupt or the end of the program.
Run R executes the program that is currently being debugged.
Quit Q terminates gdb.
Watch allows you to monitor the value of a variable regardless of when it is changed.
Catch to set the snap point.
Thread T to view the threads information for the currently running program.
Break B Sets a breakpoint in the code, which causes the program to be suspended when it is executed here.
Make enables you to regenerate executables without having to exit gdb.
The shell allows you to execute the Unixshell command without leaving GDB.
Print p Prints the contents of the data.
Examine x print memory contents.
BACKTRACEBT View all the information for the function call stack.

Example analysis

Code:

#define _gnu_source#include <string.h> #include <stdio.h> #include <stdlib.h> #define INFO (addr) printf ("%30s @%12p\n", #addr, &addr) int global_var = 1;static int static_global_var = 2;intlocal_function (int var) {
   int Local_function_var = var;        static int static_local_function_var = 6;        info (static_local_function_var);        info (local_function_var);        return Local_function_var;} Intmain (int argc, char *argv[]) {        local_function (0);        int main_function_var = 3;        static int static_main_var = 4;        info (global_var);        info (static_global_var);        info (static_main_var);        info (main_function_var);        info (main);        info (local_function);        return 0;}

Compile the adjustable target file and run:

[Email protected]:~/workspace/test$ gcc-o test test.c[email protected]:~/workspace/test$./test    Static_local_ Function_var @    0x60102c           local_function_var @ 0x7fffc2487a3c                    global_var @     0x601020            static_ Global_var @     0x601024               static_main_var @     0x601028            main_function_var @ 0x7fffc2487a6c                          main @     0x400541                local_function @     0x4004f4

[Email protected]:~/workspace/test$ gcc-o test-g test.c

[Email Protected]:~/workspace/test$./test

Static_local_function_var @ 0x60102c

Local_function_var @ 0x7fff97a0869c

Global_var @ 0x601020

Static_global_var @ 0x601024

Static_main_var @ 0x601028

Main_function_var @ 0x7fff97a086cc

Main @ 0x400541

Local_function @ 0x4004f4

[Email protected]:~/workspace/test$ gdb./test

GNU gdb (UBUNTU/LINARO7.4-2012.04-0UBUNTU2) 7.4-2012.04

Copyright (C) Free Softwarefoundation, Inc.

License gplv3+: GNU GPL version 3 or later

This was free software:you was free tochange and redistribute it.

There is NO WARRANTY, to the extentpermitted by law. Type "Showcopying"

and "Show warranty" for details.

This GDB is configured as "X86_64-linux-gnu".

For bugs reporting instructions, please see:

Reading symbols From/home/jxdong/workspace/test/test...done.

(GDB) b 32

Breakpoint 1 at 0x400608:file test.c, Line32.

(GDB) Run

Starting Program:/home/jxdong/workspace/test/test

Static_local_function_var @ 0x60102c

Local_function_var @ 0x7fffffffe24c

Global_var @ 0x601020

Static_global_var @ 0x601024

Static_main_var @ 0x601028

Main_function_var @ 0x7fffffffe27c

Main @ 0x400541

Local_function @ 0x4004f4

Breakpoint 1, Main (argc=1,argv=0x7fffffffe368) at test.c:32

return 0;

(GDB) bt full

#0 Main (argc=1, argv=0x7fffffffe368) at test.c:32

Main_function_var = 3

Static_main_var = 4

(GDB) Info proc

Process 32545

CmdLine = '/home/jxdong/workspace/test/test '

CWD = '/home/jxdong/workspace/test '

EXE = '/home/jxdong/workspace/test/test '

(GDB)

Process 32545

CmdLine = '/home/jxdong/workspace/test/test '

CWD = '/home/jxdong/workspace/test '

EXE = '/home/jxdong/workspace/test/test '

(GDB) Info proc Map

Process 32545

Mapped Address spaces:

Start Addr End Addr Size Offset objfile

0x400000 0x401000 0x1000 0x0/home/jxdong/workspace/test/test

0x600000 0x601000 0x1000 0x0/home/jxdong/workspace/test/test

0x601000 0x602000 0x1000 0x1000/home/jxdong/workspace/test/test

0x7ffff7a1a000 0x7ffff7bcf000 0x1b5000 0x0/lib/x86_64-linux-gnu/libc-2.15.so

0x7ffff7bcf000 0x7ffff7dcf000 0x200000 0x1b5000/lib/x86_64-linux-gnu/libc-2.15.so

0x7ffff7dcf000 0x7ffff7dd3000 0x4000 0x1b5000/lib/x86_64-linux-gnu/libc-2.15.so

0x7ffff7dd3000 0x7ffff7dd5000 0x2000 0x1b9000/lib/x86_64-linux-gnu/libc-2.15.so

0x7ffff7dd5000 0x7ffff7dda000 0x5000 0x0

0x7ffff7dda000 0x7ffff7dfc000 0x22000 0x0/lib/x86_64-linux-gnu/ld-2.15.so

0x7ffff7fe9000 0x7ffff7fec000 0x3000 0x0

0x7ffff7ff8000 0x7ffff7ffb000 0x3000 0x0

0x7ffff7ffb000 0x7ffff7ffc000 0x1000 0x0 [VDSO]

0x7ffff7ffc000 0x7ffff7ffd000 0x1000 0x22000/lib/x86_64-linux-gnu/ld-2.15.so

0x7ffff7ffd000 0x7ffff7fff000 0x2000 0x23000/lib/x86_64-linux-gnu/ld-2.15.so

0x7ffffffde000 0x7ffffffff000 0x21000 0x0 [Stack]

0xffffffffff600000 0xffffffffff601000 0x1000 0x0 [Vsyscall]

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.