C - Debugging Tools Flashcards
- breakpoint
a point in the program where it will stop executing
- Watchpoint
a point where the program will stop executing when the specified memory location is modified
- catchpoint
special breakpoint that stops program execution when a certian kind of event occurs
- memory leak
a program error of repeatedly allocating memory, using it, and neglecting to free it
- valgrind
programming tool for memory debugging, memory leak detection, and profiling.
commands 1: run
run [arglist]
start your program (with arglist, if specified).
basic commands 2. continue
c
continue running your program (after stopping, like at a breakpoint)
basic commands 3. finish
finish
continue execution until the current function returns
basic commands 4. break
break [[ func | line | *addr)}] [if cond] [thread thread]
sets a breakpoint at a specified location.
break 200 // sets breakpoint at 200
basic commands 5. next
next [expr]
executes the next function call, and stops when the function call finishes executing.
basic commands 6. step/step [count]
step [expr]
steps forward, even into the first executable statement in a function call.
step 5 will execute step 5 times
basic commands 7. display/undisplay
display [Expression]
by default, displays variable values at each step state,
undisplay removes a variable from the list of variables to be displayed automatically
basic commands 8. print
print [Expression]
print $[Previous value number]
print {[Type]}[Address]
print [First element]@[Element count]
print /[Format] [Expression]
prints the value of a given expression
basic commands 9. list
list
list [ file_name:]line_expression
list begin,end
list begin,
list ,end
list +[num]
list -[num]
list *address
displays lines of source code specified by the following:
position of the program counter,
last line listed (if multiple list commands listed)
the line numbers specified as the parameters to the list command
https://www.irya.unam.mx/computo/sites/manuales/fce12/debugger/cl/commandref/common_cmd/cmd_list.htm
basic commands 10. watch
watch lvalue
sets a write watchpoint on the specified expression
(watch variableName)
basic commands 11. quit
quit
exits the debugger
Advanced commands 1 ptype
ptype [name]
prints detailed description of a type, or the type of the last value in the command history
Advanced commands 2. catch
catch [signal_ID]
catches and handles the specified signal.
debugger does not make an entry in the beakpoint table for catch command,
a catch that has already being caught, does not create an additional breakpoint for that signal.
catch without parameter lists all signals currently being handled.
https://www.irya.unam.mx/computo/sites/manuales/fce12/debugger/cl/commandref/idb_mode/cmd_catch.htm
vanced commands 3. clear
clear [{func | line | *addr}] [if cond] [thread thread]
clear a breakpoint at the specified location.
if none specified, removes the breakpoint from the next instruction to be executed.
Advanced commands 4. up
up [num]
move (backwards) to the previous stack frame, or one of the frames preceeding it
Advanced commands 5. down
opposite of up, moving to the next stack frames.
Advanced Commands 6. where/backtrace
where [num] [thread { thread_id, … | all | * } ]
show the current stack trace of currently active functions.
backtrace [full] [num]
prints backtrace of stack frames
- until
until [line]
continues the debugee past the current line until the specified source line in the current stack frame.
- call
call expression (parmlist)
call a function in the dubuggee
specify the function as if you were calling it from within the appication
- x (examining memory)
x [/nfu] [addr]
prints memory
- disable/enable
disable [breakpoints][ {ID … | ID-range …} ]
disables one or more breakpoints
enable [breakpoints] [ {ID … | ID-range …} ]
- set/show args
set args [arguments] [IO_redirection]
specifies arguments for debuggee program
show args
show arguments and input/output redirections.
- set var
set variable variable = [expression]
set variables mid-debug
Q1 How would you compile a source file with debug information?
gcc -g -o fileName Exercises.c
./fileName
gdb -tui fileName
Q2 What is the difference between step and next?
step will step once line through the code, even into function calls.
next will skim through the entire next function call if there is one.
!3 what is the difference between a breakpoint, watchpoint and catchpoint?
a break point will pause the program when reached.
watchpoint stops the program when the value of an expression changes.
catchpoint stops when a certain kind of event occurs, like when an exception is thrown or a library is loaded.
Q4 What is the difference between until and step?
step will step once line through the code, even into function calls.
until will skip/step through several lines of code until the specified in a stack frame
Valgrind 1: alias vlg=’valgrind –leak-check=yes –track-origins=yes’
leak-check: searches for memory leaks when the program finishes. by default, says how many leaks occured. set to yes, each leak is shown in detail and/or counted as an error
track-origins: if an uninitialised value is encountered, it describes:
if from heap block, shows where the block was allocated.
if from stack allocations, tells which function allocated the value.
if from client request or other sources, just where they came from.
alias vlg=’valgrind –leak-check=yes –track-origins=yes’
Valgrind 2: Do an alocation of memory and don’t free it
int *ptr2 = (int *)malloc(sizeof(int));
lost 4 bytes in 1 block.
Valgrind 3: Create an array of size 10 and try to read its 11th member
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
printf(“%d”, array[10);
valgrind doesn’t seem to recognize the issue
Valgrind 4: Free a pointer, then dereference that pointer
free(ptr2);
int x = *ptr2;
invalid read of size 4,
in reverse order, displays where and by who the address was assigned,
free’d,
alloc’d
Valgrind 5: perform an if statement based on a memory address that was not initialized. for example, an array allocated with malloc() but not initialized, and then if *arr …
int *ptr2 = (int *)malloc(sizeof(int));
if (ptr2[0]){printf(“oopsie, stinky.”);}
conditional jump or move depends on uninitialised values,
uninitialised value was created a heap allocation,
4 bytes are definitely lost in los record 1 of 1 at x malloc by y main
Valgrind 6:
indeed, tested and checked, all functions in 2s2 are fine
Core dump 1: read man core dump
Code dump 2: Write a program that loops infinitely. run it.
while(1){printf(“A”);
Core dump 3: send a SIGABRT signall to the process running your program. set ulimit to allow for core-file to be generated
kill SIGABRT <pid></pid>
Core dump 4: debug the generated file
suffering.
ulimit -c unlimited
strl+\
Extra: Fix the thing
include <stdio.h></stdio.h>
#include <stdlib.h>
#include <assert.h></assert.h></stdlib.h>
void whatSouldWeFixHere(int *arr, size_t arr_length)
{
int i = 0;
int *p;
assert(arr); p = (int *)malloc(sizeof(int) * arr_length); for(i = 0; i < (int)arr_length; ++i) { p[arr_length - 1 - i] = arr[i]; } for(i = 0; i < (int)arr_length; ++i) { arr[i] = p[i]; } free(p);/*added free(p)*/ }
int main(int argc, char *argv[])
{
int i = 0;
int arr[] = {0, 1, 2, 3, 4, 5};
whatSouldWeFixHere(arr, sizeof(arr)/sizeof(int)); for(i = 0; i < sizeof(arr)/sizeof(int); ++i) /*less than, not less than or equal to.*/ { printf("%d,", arr[i]); } return 0; }