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

ECE2220Slides(1.3 Intro Debugging)

This document provides an introduction to GDB debugging, detailing how to compile programs with debugging information using the -g switch in gcc. It includes examples of debugging commands, the differences in executable sizes with and without debugging symbols, and several GDB command examples for managing breakpoints and inspecting variables. Additionally, it illustrates debugging through sample code snippets and the resulting outputs when using GDB.

Uploaded by

a02spam9
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ECE2220Slides(1.3 Intro Debugging)

This document provides an introduction to GDB debugging, detailing how to compile programs with debugging information using the -g switch in gcc. It includes examples of debugging commands, the differences in executable sizes with and without debugging symbols, and several GDB command examples for managing breakpoints and inspecting variables. Additionally, it illustrates debugging through sample code snippets and the resulting outputs when using GDB.

Uploaded by

a02spam9
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

ECE 2220 - System Programming Chapter 1.

3 - Introduction - GDB Debugging 1

1.3 GDB Debugging


A program must be compiled with certain information to allow this
debugging. In gcc, you must include the –g switch.
To debug the program debug1_g, we run gdb with the program as
its argument.

apollo13% gcc debug1.c –g –o debug1_g


apollo13% gdb debug1_g
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to redistribute it.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
Find the GDB manual and other documentation resources online
at: <http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Reading symbols from debug1_g...done.
(gdb)
WJR, Clemson University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 2

Debugging Info
Now let’s look at compiling the program without using the the –g
switch.
This time, when we run gdb, we find that there are no symbols to be
loaded.
apollo13% gcc debug1.c –o debug1_no_g
apollo13% gdb debug1_no_g
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to redistribute it.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
Find the GDB manual and other documentation resources online
at: <http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Reading symbols from debug1_no_g...(no debugging symbols found)
...done.
(gdb)
WJR, Clemson University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 3

Debugging Info (2)


If we now perform a listing of all files in my directory starting with
“debug1” we see that the executable file with debug information
is more than a thousand bytes larger.

apollo13% ls -all debug1*


-rw------- 1 breid cuuser 146 Jan 29 2016 debug1.c
-rwx------ 1 breid cuuser 9.5K Jul 12 17:04 debug1_g*
-rwx------ 1 breid cuuser 8.4K Jul 12 17:13 debug1_no_g*
apollo13%

WJR, Clemson University, 2010-2018


ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 4

Debugging Info (3)


Examine the difference in these two executables…

WJR, Clemson University, 2010-2018


ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 5

Common gdb Commands


run (or r) – Start the program’s execution.
break n (or b n) – Stop before a certain instruction number n.
clear n – Remove a breakpoint at line n.
print var (or p var) – Display the value of a variable.
The following codes preceded by a ‘/’ can be used to print in the
following formats.
o – octal, x – hexadecimal, u – unsigned decimal, t – binary
f – floating point, a – address, c – char, s - string
display var – Display variable value at each break.
step (or s) – Executes one more line in program.
next (or n) – Executes one more line but does not step into a
function. (Equivalent to “Step Over” in some debuggers.)
continue (or c) – Continues running to next breakpoint.
WJR, Clemson University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 6

Common gdb Commands (2)


where – Displays where the program is paused.
[Enter] – Perform previous command.
quit (or q) – Exit gdb.

Note: To debug a program with command line arguments, type in


the arguments you wish to use as you run the program in
gdb.
(gdb) run 1 2 “arg 3”

gdb is much more complex than described here. Please familiarized


yourself with it with the Linux manual and online at
https://www.gnu.org/software/gdb/.

WJR, Clemson University, 2010-2018


ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 7

gdb Example 1
debug1.c
1 #include <stdio.h> apollo13% debug1_g
2 j = 5432
3 void main(void) j = 603
4 { int i, j; j = 75
j = 10
5 j = 1
6 j = 54321; j = 0
7 for (i=10;i>=0;i--) jj == 00
8 { j = j/i; j = 0
9 printf(“j=%d\n",j); j = 0
Floating exception (core dumped)
10 } apollo13%
11 }

WJR, Clemson University, 2010-2018


ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 8

gdb Example 1 (2)


1 #include <stdio.h> (gdb) break 1
2 Breakpoint 1 at 0x400535: file
debug1.c, line 1.
3 void main(void) (gdb) run
4 { int i, j; Starting program:
5 /users/breid/ece2220/debug1_g
6 j = 54321; Breakpoint 1, main () at
7 for (i=10;i>=0;i--) debug1.c:6
8 { j = j/i; 6 j = 54321;
(gdb) print j
9 printf(“j=%d\n",j); $1 = 0
10 } (gdb) step
11 } 7 for (i= 10; i>=0; i--)
(gdb) print j
$2 = 54321
(gdb) print /t j
$3 = 1101010000110001
(gdb) step
8 { j = j/i;
(gdb) print i
$4 = 10
WJR, Clemson(gdb)
University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 9

gdb Example 1 (3)


(gdb) break 8
1 #include <stdio.h>
Breakpoint 2 at 0x400545: file
2 debug1.c, line 8.
3 void main(void) (gdb) display i
4 { int i, j; 1: i = 10
5 (gdb) continue
6 j = 54321; Continuing.
7 for (i=10;i>=0;i--) j = 5432
8 { j = j/i; Breakpoint 2, main () at
9 printf(“j=%d\n",j); debug1.c:8
10 } 8 { j = j/i;
11 } 1: i = 9
(gdb)
Continuing.
j = 603

Breakpoint 2, main () at
debug1.c:8
8 { j = j/i;
1: i = 8
WJR, Clemson(gdb)
University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 10

gdb Example 1 (4)


(gdb)
1 #include <stdio.h>
Continuing.
2 j = 0
3 void main(void)Breakpoint 2, main () at debug1.c:8
4 { int i, j; 8 { j = j/i;
5 1: i = 1
6 j = 54321; (gdb)
7 Continuing.
for (i=10;i>=0;i--)
j = 0
8 { j = j/i;
Breakpoint 2, main () at debug1.c:8
9 printf(“j=%d\n",j);
8 { j = j/i;
10 } 1: i = 0
11 } (gdb)
Continuing.

Program received signal SIGFPE, Arithmetic


exception.
0x0000000000400549 in main () at debug1.c:8
8 { j = j/i;
1: i = 0
(gdb)
WJR, Clemson University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 11

gdb Example 2
debug2.c
1 #include <stdio.h>
apollo13% debug2
2
3 void main(void) Segmentation fault (core dumped)
4 { int i, j, k[5];
5
6 j = 54321;
7
8 for (i=10; i>=0; i--)
9 { k[j] = j/i;
10 printf(“k[j]=%d\n",k[j]);
11 }
12 }

WJR, Clemson University, 2010-2018


ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 12

gdb Example 2 (2)


3 void main(void)
4 { int i, j, k[5];
5
6 j = 54321;
7
8 for (i=10; i>=0; i--)
9 { k[j] = j/i;
10 printf(“k[j]=%d\n",k[j]);
11 }
12 } (gdb) break 6
Breakpoint 1 at 0x400535: file debug2.c, line
6.
(gdb) break 9
Breakpoint 2 at
display i 0x400545: file debug2.c, line
9. symbol "i" in current context.
No
(gdb)

WJR, Clemson University, 2010-2018


ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 13

gdb Example 2 (3)


3 void main(void)
4 { int i, j, k[5];
5
6 j = 54321;
7
8 for (i=10; i>=0; i--)
9 { k[j] = j/i;
10 printf(“k[j]=%d\n",k[j]);
11 }
12 } (gdb) run
Starting program: /users/breid/ece222/1_intro/debug2
Breakpoint 1, main () at debug2.c:6
6 j = 54321;
(gdb) display i
1: i = 0
(gdb) display j
2: j = 0
(gdb) display k
3: k = {4195712, 0, 4195392, 0, -8432}
(gdb) WJR, Clemson University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 14

gdb Example 2 (4)


6 j = 54321;
7
8 for (i=10; i>=0; i--)
9 { k[j] = j/i;
10 printf(“k[j]=%d\n",k[j]);
11 }
12 } (gdb) continue
Continuing.
Breakpoint 2, main () at debug2.c:9
9 { k[j] = j/i;
3: k = {4195712, 0, 4195392, 0, -8432}
2: j = 54321
1: i = 10
(gdb) step
Program received signal SIGBUS, Bus error.
0x0000000000400553 in main () at debug2.c:9
9 { k[j] = j/i;
3: k = {4195712, 0, 4195392, 0, -8432}
2: j = 54321
1: i = 10
(gdb) WJR, Clemson University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 15

gdb Example 3
debug3.c
1 #include <stdio.h>
apollo13% debug3_g
2
3 void main(void) i=4, j=618330
4 { int i, j; i=4, j=618335
i=4, j=618340
5 i=4, j=618345
6 for (i=0; i<10; i++) i=4, j=618350
7 { j += i; i=4, j=618355
8 if (j > 10) i=4, j=618360
9 { i--; i=4, j=618365
10 i=4, j=618370
}
i=4, j=618375
11 printf("i=%d, j=%d \n", i, j); i=4, j=618380
12 } i=4, j=618385
13 } ^C4, j=618390
apollo13%

WJR, Clemson University, 2010-2018


ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 16

gdb Example 3 (2)


3 void main(void)
4 { int i, j;
5
6 for (i=0; i<10; i++)
7 { j += i;
8 if (j > 10)
9 { i--;
10 }
11 printf("i=%d, j=%d \n", i, j);
12 }(gdb) break 11
Breakpoint 1 at 0x40054e: file debug3.c, line 11.
13 }
(gdb) run
Starting program: /users/breid/ece2220/debug3
Breakpoint 1, main () at debug3.c:11
11 printf("i=%d, j=%d \n", i, j);
(gdb) display i
1: i = 0
(gdb) display j
2: j = 0
(gdb) WJR, Clemson University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 17

gdb Example 3 (3)


(gdb) continue
Continuing.
3 void main(void) i=0, j=0
4 { int i, j; Breakpoint 1, main () at debug3.c:11
5 11 printf("i=%d, j=%d \n", i, j);
2: j = 1
6 for (i=0; i<10; i++)
1: i = 1
7 { j += i; (gdb) continue
8 if (j > 10)Continuing.
9 { i--; i=1, j=1
10 } Breakpoint 1, main () at debug3.c:11
11 11 j=%d \n",printf("i=%d,
printf("i=%d, i, j); j=%d \n", i, j);
2: j = 3
12 } 1: i = 2
13 } (gdb) continue
Continuing.
i=2, j=3
Breakpoint 1, main () at debug3.c:11
11 printf("i=%d, j=%d \n", i, j);
2: j = 6
1: i = 3
(gdb)
WJR, Clemson University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 18

gdb Example 3 (4)


(gdb) continue
Continuing.
3 void main(void) i=3, j=6
4 { int i, j; Breakpoint 1, main () at debug3.c:11
5 11 printf("i=%d, j=%d \n", i, j);
2: j = 10
6 for (i=0; i<10; i++)
1: i = 4
7 { j += i; (gdb) continue
8 if (j > 10)Continuing.
9 { i--; i=4, j=10
10 } Breakpoint 1, main () at debug3.c:11
11 11 j=%d \n",printf("i=%d,
printf("i=%d, i, j); j=%d \n", i, j);
2: j = 15
12 } 1: i = 4
13 } (gdb) continue
Continuing.
i=4, j=15
Breakpoint 1, main () at debug3.c:11
11 printf("i=%d, j=%d \n", i, j);
2: j = 20
1: i = 4
(gdb)
WJR, Clemson University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 19

Need for gdb


What if the program code looked more like this?

for (i=0; i<MaxLoopValue; i++)


{ j += i;
if (j > MaxIncValue)
{ i -= Decrement;
}
}
Why not just insert a bunch of printf statements
to analyze the code?

Inserting code in a program changes the program, and in some


instances may even fix a problem, or seem to fix a problem which
comes right back when the code is removed.

Learn to be a gdb-er, not a“printf-er!”


WJR, Clemson University, 2010-2018
ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 20

IDEs
Integrated Development Environments are programs which contain
compilers, editors, and debuggers all in one package.
• Sun’s Net Beans
• Code::Blocks
• Eclipse CDT
• CodeLite
• Bluefish
• Atom
• JetBrains’ CLion
• Microsoft’s Visual Studio
• Bloodshed’s Dev C++
https://www.tecmint.com/best-linux-ide-editors-source-code-editors/

WJR, Clemson University, 2010-2018


ECE 2220 - System Programming Chapter 1.3 - Introduction - GDB Debugging 21

Shell Emulation
CYGWIN can be used to emulate a Linux
shell on a windows machine.

WJR, Clemson University, 2010-2018

You might also like