0% found this document useful (0 votes)
83 views

Linux GDB Commands

1. The gcc command is used to compile C code into executable programs and libraries. Common gcc options include -o to specify the output file name, -O levels for optimization, and -g to include debugging symbols. 2. GDB is a debugger program. Key GDB commands include backtrace to print the call stack, next/step to step through code, break to set breakpoints, and print to examine variables. 3. Debugging tools like GDB allow examining programs line-by-line and monitoring variables to debug bugs. Techniques include setting breakpoints, stepping through code, and inspecting the call stack and local/global variables.

Uploaded by

Bipin Jaiswal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as XLS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Linux GDB Commands

1. The gcc command is used to compile C code into executable programs and libraries. Common gcc options include -o to specify the output file name, -O levels for optimization, and -g to include debugging symbols. 2. GDB is a debugger program. Key GDB commands include backtrace to print the call stack, next/step to step through code, break to set breakpoints, and print to examine variables. 3. Debugging tools like GDB allow examining programs line-by-line and monitoring variables to debug bugs. Techniques include setting breakpoints, stepping through code, and inspecting the call stack and local/global variables.

Uploaded by

Bipin Jaiswal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as XLS, PDF, TXT or read online on Scribd
You are on page 1/ 10

Sno Command Description1

# gcc hello.c -o hello


1 gcc
# ./hello

# gcc -O3 main.c -o main


2 gcc 03

# gcc -pg main.c -o main


# ./main
3 gcc -pg
# gprof main gmon.out

# gcc -c program1.c
program2.c library.c
# ld program1.o library.o -o
4 gcc -c program1
# ld program2.o library.o -o
program2

5 Gcc Gcc -g

6 gdb (gdb) backtrace


7 (gdb) print j
8 (gdb) list
9 (gdb) cont
10 (gdb) set width
11 (gdb) break

12 (gdb) run

13 (gdb) n

14 (gdb) s

15 (gdb) bt
16 (gdb) p
17 (gdb) l (list)
18 (gdb) c
19 (gdb) quit
20 (gdb) program

21 (gdb) program core

22 (gdb) program 1234


23 (gdb) –args
24 (gdb) –args gcc -02 -c foo.c

25 (gdb) -h

26 (gdb) help command

27 (gdb) apropos args

28 (gdb) set environment

29 (gdb) kill

30 (gdb) thread threadno


31 (gdb) info threads
32 (gdb) -s file
33 (gdb) -e file

34 (gdb) -se file


35 (gdb) -c file
36 (gdb) thread apply

Debugging programs with


37 Multiple processes
multiple proocesses

38 info program

39 watchpoints

40 watch expr
41 rwatch expr

42 awatch expr
43 Stack Examining the stack

44 frame args

45 select-frame
46 bt
47 bt n
48 bt -n

49 frame n

50 f addr
51 up n
52 down n
53 info f

54 f

55 info f addr
56 info args
57 info locals

58 info catch

59 info line linespec


60 Disassemble disas 0x32c4 0x32e4
61 Examining memory x
62 x addr

63 info registers

64 info float
65 info vector

66 Trace tracepoints
67 trace

68 tr foo.c:121
69 tr +2
70 tr my_function
71 tr *my_function
72 tr *0x2117c4
73 delete trace 1 2 3
74 delete
75 actions

76 tstart
77 tstop
78 tstatus

79 Remote debugging target type parameters


80 help target

81 target

82 run

83 attach
84 help target name
85 target exec program
86 target core filename
87 target remote dev

88 target nrom dev

89 load filename

90 set endian big


91 set endian auto

92 Gdbserver Gdbserver
On the target Target> gdbserver comm
93
machine program [args ...]

Target> gdbserver
94
/dev/com1 emacs foo.txt
Target> gdbserver
95
host:2345 emacs foo.txt
On the GDB host
96 (gdb) --baud
machine
(gdb) Target remote
97
/dev/ttyb
(gdb) target remote the-
98
target:2345
99
100
Description 2
The option -o specifies the name of the executable to be
produced, which traditionally has no extension.

gcc offers three levels of optimization: -O1 (or -O), -O2, and
-O3.

Use the gnu profiler gprof, which measures the time spent
in each routine during execution.
The gcc option -pg compiles in the hooks necessary for the
gnu profiler to perform its measurements. When run, the
executable produces the file gmon.out containing the
profile informaton; gprof reads this file to produce a report
containing the profile information. Very useful!

You can use the -c option to have gcc compile files into an
objects rather than executables. Object files need not
contain a main routine, so you can collect libraries of
routines which may be useful to multiple programs into
object files. You can then use ld to link the required object
files together into an executable, without having to
recompile the libraries.

To request debugging information, specify the -g option


when u run the compiler.

This option is used to tell GDB to use a narrower display


width than usual.
Sets breakpoint at the relevant subroutine
Starts running excutable under GDB control as long as
control does not reach the breakpoint.We can also trigger
the breakpoint so that gdb suspends execution and
displays info about the context where it stops.
To advance execution to the next line of the current
function. This command is also used to avoid falling into
the subroutine.

Step goes to the next line to be executed in any subroutine.


This command displays a stack frame for each active
subroutine.
To examine the variables and print their values.
To display 10 lines of source surrounding the current line.
To allow the program to continue executing normally.
End gdb session
Start gdb with one argument specifying an executable
program.
Start gdb with both an executable program and a core file
specified.
If u want to debug a running process specify a process id
as the second argument.
This option stops option processing
This will cause gdb to debug gcc, and to set gcc’s
command-line arguments to ‘-02 -c foo.c’
To display all available options and briefly describe their
use.
GDB displays a paragraph on how to use that command.
Searches through all the GDB commands for the regular
expression specified in args.
To change parts of the environment that affect your
program.
Kill the child process in which your program is running
under gdb. This command is useful if u wish to debug a
core dump instead of a running process. GDB ignores any
core dump file while your program is running.
A command to switch among threads.
A command to inquire about existing threads. Display a
summary of all threads currently in your program.
Read symbol table from file file
Use file file as the executable file to execute when
appropriate.
Read symbol table from file file and use it as the
executable file.
Use file file as a core dump to examine.
The thread apply command allows you to apply a
command to one or more threads.
When a program forks gdb will continue to debug the
parent process and the child process will run
unimpeded.However if u want to debug the child process,
put a call to sleep in the code which the child executes
after the fork. While the child is sleeping, use the ps
program to get its process ID, then tell gdb to attach to the
child process.
Display information about the status of your program.
Used to stop execution whenever the value of an
expression changes. Watchpoints may be implemented in
software or hardware. Hardware watchpints execute every
quickly, and the debugger reports a change in value at the
exact location where the change occurs.
Set a watchpoint for an expression.
Set a watchpoint that will break when watch expr is read by
the program.
Set a watchpoint that will break when expr is either read or
written into by the program.
Each time your program performs a function call,
information about the call is generated. The information is
saved in a block of data called a stack frame. The stack
frames are allocated in a region of memory called the call
stack.
The frame command allows u to move from one stack
frame to another, and to print the stack frame u select. Args
may be either the address of the frame or the stack frame
number. Without an argument, frame prints the current
stack frame.
The select-frame command allows u to move from one
stack frame to another without printing the frame.
Print a backtrace of the entire stack.
Print only the innermost n frames.
Print only the outermost n farmes.
Select frame number n. Frame 0 is the innermost(currently
executing) frame, frame one is the frame that called the
innermost one, and so on. The highest-numbered frame is
the one for main.
Select the frame at address addr.
Move n frames up the stack.
Move n frames down the stack.
Prints a verbose description of the selected stack frame.
Prints a brief description of the currently selected stack
frame.
Print a verbose descrription of the frame at address
addr.without selecting that frame.
Print the arguments of the selected frame.
Print the local variables of the selected frame.
Print a list of all the exception handlers that are active in
the current satck frame at the current point of execution.

Print the starting and ending addresses of the compiled


code for the source line linespec.
Dumps a range of memory as machine instructions.
Use the x command to examine memory.
Addr is an expression giving the address where u want to
start displaying memory.
Print the names and values of all registers, including
floating-point registers.
Display hardware-dependent information about the floating
point unit.
Display information about the vector unit.
If the program’s correctness depends on its real-time
behavior, it is useful to observe the program’s behavior
without interrupting it. Using GDB’s trace commands, u can
specify locations in the program called tracepoints, to
evaluate when those tracepoints are reached. The
tracepoint facility is currently available only for remote
targets.
The trace command defines a tracepoint, which is a point in
the target program where the debugger will briefly stop,
collect some data, and then allow the program to continue.

A source file and line number


2 lines forward
First source line of function
Exact start add of function
An address
Remove three tracepoints
Remove all tracepoints
This command will prompt for a list of actions to be taken
when the tracepoint is hit.
It starts the trace experiment, and begins collecting data.
It ends the trace experiment and stops collecting data.
This command displays the status of the current trace data
collection.
Connects the GDB host environment to a target machine or
process.
displays the name of all targets available.

target is the execution environment occupied by your


program.You can use the target command to specify one of
the target types configured for GDB.Three classes of
targets: processes,core files, and executable files.

when u type run, your executable file becomes an active


process taraget.
to specify as a target a process that is already running use
the attach command.
describe a particular target.
an executable file
a core dump file
remote serial target, the argument dev specifies what serial
device to use for the connection.
NetROM ROM emulator.This target only supports
downloading.

make filename(an executable) available for debugging on


the remote system-by downloading or dynamic linking.
instruct gdb to assume the target is big-endian.
instruct gdb to use the byte order associated with the
executable.
Is a control program which allows u to connect your
program with a remote GDB via target remote. It is much
smaller than GDB , it is easier to port than all of GDB, so u
may be able to get started more quickly in a new system by
using gdbserver. GDB and gdbserver communicate via
either a serial line or TCP connection.
Gdbserver does not need your program’s symbol table, so
u can strip the program if necessary to save space.
Gdbserver waits passively for the host to communicate
with.
To debug Emacs with the argument ‘foo.txt’ and
communicate with GDB over the serial port ‘/dev/com1’
Communicating with host gdb via TCP.

If the serial line is running at anything other than 9600 bps.


To establish communication with gdbserver using serial
device.
Communicates via a TCP connection to port 2345 on host
‘the-target’

You might also like