05-EE485 24spring Lecture5-Debuggers
05-EE485 24spring Lecture5-Debuggers
05-EE485 24spring Lecture5-Debuggers
Lecture 5: Debuggers
(Reference: The ART OF DEBUGGING with GDB, DDD, and ECLIPSE (TAD))
https://www.oreilly.com/library/view/the-art-of/9781593271749/
Introduction to GNU Debugger
Today’s topic
General strategy with debugging with GDB:
Insertion sorting
2
Typical Steps for Debugging with GDB
(gdb)continue
next command steps over the function, that is, executes the next line without stepping into the
function
4
Typical Steps for Debugging with GDB (cont.)
5
Other Useful Tips
6
Debugging "insertsort.c” (from TAD)
#include <stdio.h>
#include <stdlib.h>
// insertion sort, several errors
// usage: insert_sort num1 num2 num3 ...,
// where the numi are the numbers to be sorted
7
Debugging "insertsort.c” (from TAD)
8
Debugging "insertsort.c” (from TAD)
void process_data()
{
for (num_y = 0; num_y < num_inputs; num_y++)
// insert new y in the proper place
// among y[0],...,y[num_y-1]
insert(x[num_y]);
}
void print_results()
{ int i;
9
Insertion Sort
Supposed to do followings
Get the integer input from the command line $ ./insertsort 10 15 7 50 1
1
Sort them by insertion sort 7
10
Print out the result to stdout 15
50
Insertion sort?
You have a sorted array (initially it’s empty)
For each new input value, add it to the correct position in the sorted array
Repeat the second step until you process all input values
10
Build & Run the Program
11
Build & Run the Program
Stopped at process_data().
12
(3) Stop at insert() when num_y == 1
13
Debugging Insertsort
14
Debugging Insertsort
(7) Stop at insert() when new_y == 10
(gdb) break insert if new_y == 10 27 void insert(int new_y)
(gdb) run 15 10 28 {
Breakpoint 1, insert (new_y=10) at insertsort.c:30 29 int j;
30 if (num_y == 0) { // y empty so far 30 if (num_y == 0) { // y empty so
far
(8) A bit more investigation with next/print 31 y[0] = new_y;
32 return;
(gdb) next 33 }
36 for (j = 0; j < num_y; j++) { …
(gdb) next 36 for (j = 0; j < num_y; j++) {
37 if (new_y < y[j]) { 37 if (new_y < y[j]) {
(gdb) print y[0] …
$3 = 15 40 scoot_over(j);
(gdb) next 41 y[j] = new_y;
40 scoot_over(j); 42 return;
(gdb) print y 43 }
$4 = {15, 0, 0, 0, 0, 0, 0, 0, 0, 0} 44 }
45 }
OK. A bug in scoot_over()! After scoot_over(),
15 should have been moved like
y[] = {0, 15, 0, 0, 0, 0, 0, 0, 0, 0}
15
Debugging Insertsort
16
Debugging Insertsort
17
Debugging Insertsort
b=break
18
(11) Look at the loop
(gdb) n
36 for (j = 0; j < num_y; j++) {
(gdb) n n=next
37 if (new_y < y[j]) {
(gdb) p y p=print
$1 = {10, 15, 0, 0, 0, 0, 0, 0, 0, 0}
(gdb) n
36 for (j = 0; j < num_y; j++) {
(gdb) n
36 for (j = 0; j < num_y; j++) {
(gdb) n
45 }
Found a problem! Not storing new_y when new_y >= all elements!
Fix: y[num_y]=new_y after line 44. Now all bugs are fixed!
19
More on Breakpoints
Various breakpoints
(gdb) break 30 // stop at line 30 of the current file
(gdb) break fileA.c:40 // stop at line 40 on fileA.c
(gdb) break func if expr // stop at func() if expr is true
20
list/where/up/down
21
Resuming Execution
22
Gdb with core in eelab machines
23
Assignment for Lecture 5
Fix all bugs in insertsort.c and submit the bug-free file to KLMS (no screenshot or JPG file allowed)
Use gcc instead of gcc209 for this assignment (Since gcc209 is safer, you can’t compile the code with gcc209
due to a compiling error)
Name the file as YourStudentID-insert.c (e.g., 20101234-insert.c), and upload the file to KLMS
24