Debugging
description
Transcript of Debugging
![Page 1: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/1.jpg)
Debugging
Data Display Debugger (DDD)
![Page 2: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/2.jpg)
References
• Project Homepage– http://www.gnu.org/software/ddd/
• Manual URL– http://www.gnu.org/manual/ddd/
![Page 3: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/3.jpg)
Debugger
• Computer program• Capabilities
– Run a program step by step– Stopping at some kind of event– Tracking the values of some variables– Modify the state of the program while it’s running.
• Difficulty– Track down runtime problems in complex multi-
threaded or distributed systems.• Changes the internal timing of a software program.
![Page 4: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/4.jpg)
Data Display Debugger (DDD)
• A graphical user interface (GUI) for command-line debuggers.
• Under the GUI, you can use following debuggers– GDB, DBX, Ladebug, or XDB: debug
executable binaries– JDB: debug Java byte code programs– PYDB: debug Python programs– Perl debugger: debug Perl programs– BASHDB: debug Bash programs
![Page 5: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/5.jpg)
A sample DDD session• static void shell_sort(int a[], int size)• {• int i, j;• int h = 1;
• do {• h = h * 3 + 1;• } while (h <= size);
• do {• h /= 3;• for (i = h; i < size; i++)• {• int v = a[i];• for (j = i; j >= h && a[j - h] > v; j -= h)• a[j] = a[j - h];• if (i != j)• a[j] = v;• }• } while (h != 1);• }
• int main(int argc, char *argv[])• {• int *a;• int i;• a = (int *)malloc((argc - 1) * sizeof(int));• for (i = 0; i < argc - 1; i++)• a[i] = atoi(argv[i + 1]);• shell_sort(a, argc-1);• for (i = 0; i < argc - 1; i++)• printf("%d ", a[i]);• printf("\n");• free(a);• return 0;• }
![Page 6: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/6.jpg)
A sample DDD session
• $ gcc -o sample sample.c
• $ ./sample 8 7 5 4 1 3
• 1 3 4 5 7 8
• $ ./sample 8000000000 7000000000 5000000000 1000000000 4000000000
• 1000000000 1913000000 4000000000 5000000000 7000000000
![Page 7: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/7.jpg)
Compiling for debugging• Need to generate debugging information
when compile a program.– Stored in the object file
• Debugging information– Describes the data type of each variable or
function and the correspondence between source line numbers and addresses in the executable code.
– Specify ‘-g’ option when compiling
• For our case: – gcc -g -o sample sample.c
![Page 8: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/8.jpg)
Launching: ddd sample
![Page 9: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/9.jpg)
Breakpoint
• Makes your program stop whenever a certain point in the program is reached.
• Typically, breakpoints are set before running the program.
• Put the cursor on the left of the source line, and click on the ‘Break’ button.
![Page 10: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/10.jpg)
Run the program
• Select menu ‘Program->Run’.
• In ‘Run with Arguments’ dialog, enter arguments for the program to be executed.
![Page 11: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/11.jpg)
Command Tools
• ‘Run’: Run the program without argument / Run the program again with the same arguments.
• ‘Interrupt’: Interrupt the running program.• ‘Step’: Step into a subroutine/sub-function which
has debugging information.• ‘Stepi’: Similar with ‘Step’, but it executes one
machine instruction.• ‘Next’: Execute a source line without stepping
into the subroutine.• ‘Nexti’: Similar with ‘Next’, also executes one
machine instruction.
![Page 12: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/12.jpg)
Command Tools
• ‘Until’: Continue until a greater line in the current function is reached– This is useful to avoid single stepping through
a loop more than once.
• ‘Finish’: Continue running until the current function returns.
• ‘Continue’: Resume execution.• ‘Kill’: Kill the process of the debugged
program.
![Page 13: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/13.jpg)
Command Tools
• ‘Up’: Select the function stack frame that called this one.
• ‘Down’: Select the function stack frame that was called by this one.
• ‘Undo’: Undo the most recent action.• ‘Redo’: Redo the most recently undone.• ‘Edit’: Edit the source code by a test editor, by
default it is VIM.• ‘Make’: Recompile the source code.
– Needs ‘Makefile’
![Page 14: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/14.jpg)
‘Makefile’ for sample.c
#Makefile for sample.c
sample: sample.o
gcc -o sample sample.o
sample.o: sample.c
gcc -g -c sample.c
clean:
rm sample sample.o
![Page 15: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/15.jpg)
Examine data
• To examine a simple variable– Move the mouse pointer on its name.– Enter variable name to argument field, and click on
the ‘Print’ button.• Show once
– Enter variable name to argument field, and click on the ‘Display’ button.
• The value will be shown in the Data Window
• To examine an entire array– Enter ‘a[0]@(argc - 1)’ to argument field, and print or
display
![Page 16: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/16.jpg)
Assignment to variables
• Change the values of arbitrary variables during program execution.– Select the variable in the source window, and
click on the ‘Set’ button.– Enter the variable name in the argument field,
and click on the ‘Set’ button.– If the variable is on the Data window, just
select the variable, and click on the ‘Set’ button, or right click on the variable and select ‘Set Value…’.
![Page 17: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/17.jpg)
Makefile
![Page 18: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/18.jpg)
Reference
• Project Homepage– http://www.gnu.org/software/make/
• Manual URL– http://www.gnu.org/software/make/manual/make.html
![Page 19: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/19.jpg)
Why we need a makefile
• Make enables the end user to build and install/deinstall your package without knowing the details of how that is done.
• Make figures out automatically which files it needs to update, based on which source files have changed.
• Make is not limited to any particular language. • Make can do anything else you want to do often enough
to make it worth while writing down how to do it. • Makefile tells make what to do.
– Tells make how to compile and link a program.
![Page 20: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/20.jpg)
How to use ‘Makefile’
• $ make
• ‘make’ is system program
• ‘make’ will automatically search ‘Makefile’ or ‘makefile’ in the current directory
• ‘make’ will interpret it and execute it.
![Page 21: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/21.jpg)
Simple Makefile
• A simple makefile consists of "rules" with the following shape:
targets : prerequisitescommands
• target:– usually the name of a file that is generated by a
program.– the name of an action to carry out, such as `clean‘.
• prerequisite: a file that is used as input to create the target.
• commands: an action that make carries out.
![Page 22: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/22.jpg)
Simplest Makefile for sample
#Simplest Makefile for sample
sample: sample.c
gcc -g -o sample.c
clean:
rm sample
Rule
Rule
![Page 23: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/23.jpg)
How ‘make’ Processes a Makefile
• By default, ‘make’ starts with the first target.– ‘sample’
• Before processing the rule ‘sample’, ‘make’ will process on the prerequisites first.
• ‘make’ will recompile ‘edit’ if– one or more its prerequisites are newer than
‘sample’.– no ‘sample’ file exists.
• If you want to process a specific rule, such as ‘clean’– ‘make clean’
![Page 24: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/24.jpg)
Makefile for sample
#Simplest Makefile for sample.#sample.o is intermediate file.sample: sample.o
gcc -o sample.o
sample.o: sample.cgcc -g -c sample.c
Why we need the intermediate file, which is ‘sample.o’?
![Page 25: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/25.jpg)
Simple Makefile
edit: main.c kbd.c command.c display.c
gcc -o main.c kbd.c command.c display.c
clean:
rm edit
![Page 26: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/26.jpg)
Simple Makefileedit : main.o kbd.o command.o display.o gcc -o
edit main.o kbd.o command.o display.omain.o : main.c defs.h
gcc -c main.ckbd.o : kbd.c defs.h command.h
gcc -c kbd.ccommand.o : command.c defs.h command.h
gcc -c command.cdisplay.o : display.c defs.h buffer.h
gcc -c display.cclean :
rm edit main.o kbd.o command.o display.o
![Page 27: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/27.jpg)
Variable
edit : main.o kbd.o command.o display.o
cc -o edit main.o kbd.o command.o display.o
• A name defined in a makefile to represent a string of text, called the variable's value.
• Use ‘=’ to assign a value to a variable.– objects = main.o kbd.o command.o display.o\
insert.o search.o files.o utils.o
• Usage: $(var_name)– $(objects)
![Page 28: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/28.jpg)
Simple Makefileobjects = main.o kbd.o command.o display.oedit : $(objects)
gcc -o edit $(objects)main.o : main.c defs.h
gcc -c main.ckbd.o : kbd.c defs.h command.h
gcc -c kbd.ccommand.o : command.c defs.h command.h
gcc -c command.c
display.o : display.c defs.h buffer.h gcc -c display.c
clean : rm edit $(objects)
![Page 29: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/29.jpg)
Implicit Rule• If you either write a rule with no command lines, or
don't write a rule at all– ‘make’ will figure out which implicit rule to use based on
which kind of source file exists or can be made.– For example
foo: foo.ogcc -o foo foo.o
foo.o: foo.h– ‘name.o’ is made automatically from ‘name.c’ with a
command of the form ‘$(CC) -c $(CPPFLAGS) $(CFLAGS)’ in C programs
– In our case, we should set ‘CFLAGS = -g’, such that can be used for debugging.
![Page 30: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/30.jpg)
Implicit Rule (Example)
sample: sample.o
gcc -o sample.o
sample.o: sample.c
gcc -g -c sample.c
CC = gcc
CFLAGS = -g
sample: sample.o
gcc -o sample.o
![Page 31: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/31.jpg)
Simple Makefileobjects = main.o kbd.o command.o display.oCFLAGS = -gCC = gccedit : $(objects)
$(CC) -o edit $(objects) main.o : defs.h kbd.o : defs.h command.h command.o : defs.h command.h display.o : defs.h buffer.h clean :
rm edit $(objects)
![Page 32: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/32.jpg)
Simple Makefile
objects = main.o kbd.o command.o display.oCFLAGS = -gCC = gccedit : $(objects)
$(CC) -o edit $(objects) $(objects) : defs.h kbd.o command.o files.o : command.hdisplay.o insert.o search.o files.o : buffer.h
![Page 33: Debugging](https://reader036.fdocuments.net/reader036/viewer/2022070405/56813f63550346895daa351d/html5/thumbnails/33.jpg)
Lab Exercise
• Debug the quick sorting program.
• Extract the file using ‘tar -xvf sorting.tar’.
• Make a ‘Makefile’ to compile the source files.
• The algorithm is some kind of quick sort algorithm, but it does not work correctly.
• Find the bug!