330 likes | 483 Vues
Debugging. Data Display Debugger (DDD). References. Project Homepage http://www.gnu.org/software/ddd/ Manual URL http://www.gnu.org/manual/ddd/. Debugger. Computer program Capabilities Run a program step by step Stopping at some kind of event Tracking the values of some variables
E N D
Debugging Data Display Debugger (DDD)
References • Project Homepage • http://www.gnu.org/software/ddd/ • Manual URL • http://www.gnu.org/manual/ddd/
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.
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
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; • }
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
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
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.
Run the program • Select menu ‘Program->Run’. • In ‘Run with Arguments’ dialog, enter arguments for the program to be executed.
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.
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.
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’
‘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
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
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…’.
Reference • Project Homepage • http://www.gnu.org/software/make/ • Manual URL • http://www.gnu.org/software/make/manual/make.html
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.
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.
Simple Makefile • A simple makefile consists of "rules" with the following shape: targets : prerequisites commands • 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.
Simplest Makefile for sample #Simplest Makefile for sample sample: sample.c gcc -g -o sample.c clean: rm sample Rule Rule
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’
Makefile for sample #Simplest Makefile for sample. #sample.o is intermediate file. sample: sample.o gcc -o sample.o sample.o: sample.c gcc -g -c sample.c Why we need the intermediate file, which is ‘sample.o’?
Simple Makefile edit: main.c kbd.c command.c display.c gcc -o main.c kbd.c command.c display.c clean: rm edit
Simple Makefile edit : main.o kbd.o command.o display.o gcc -o edit main.o kbd.o command.o display.o main.o : main.c defs.h gcc -c main.c kbd.o : kbd.c defs.h command.h gcc -c kbd.c command.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 main.o kbd.o command.o display.o
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)
Simple Makefile objects = main.o kbd.o command.o display.o edit : $(objects) gcc -o edit $(objects) main.o : main.c defs.h gcc -c main.c kbd.o : kbd.c defs.h command.h gcc -c kbd.c command.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)
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.o gcc -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.
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
Simple Makefile objects = main.o kbd.o command.o display.o CFLAGS = -g CC = gcc edit : $(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)
Simple Makefile objects = main.o kbd.o command.o display.o CFLAGS = -g CC = gcc edit : $(objects) $(CC) -o edit $(objects) $(objects) : defs.h kbd.o command.o files.o : command.h display.o insert.o search.o files.o : buffer.h
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!