C - Debugging Tools Flashcards

1
Q
  1. breakpoint
A

a point in the program where it will stop executing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Watchpoint
A

a point where the program will stop executing when the specified memory location is modified

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. catchpoint
A

special breakpoint that stops program execution when a certian kind of event occurs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. memory leak
A

a program error of repeatedly allocating memory, using it, and neglecting to free it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. valgrind
A

programming tool for memory debugging, memory leak detection, and profiling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

commands 1: run

A

run [arglist]
start your program (with arglist, if specified).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

basic commands 2. continue

A

c
continue running your program (after stopping, like at a breakpoint)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

basic commands 3. finish

A

finish

continue execution until the current function returns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

basic commands 4. break

A

break [[ func | line | *addr)}] [if cond] [thread thread]
sets a breakpoint at a specified location.

break 200 // sets breakpoint at 200

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

basic commands 5. next

A

next [expr]

executes the next function call, and stops when the function call finishes executing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

basic commands 6. step/step [count]

A

step [expr]

steps forward, even into the first executable statement in a function call.

step 5 will execute step 5 times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

basic commands 7. display/undisplay

A

display [Expression]

by default, displays variable values at each step state,

undisplay removes a variable from the list of variables to be displayed automatically

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

basic commands 8. print

A

print [Expression]
print $[Previous value number]
print {[Type]}[Address]
print [First element]@[Element count]
print /[Format] [Expression]

prints the value of a given expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

basic commands 9. list

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

basic commands 10. watch

A

watch lvalue
sets a write watchpoint on the specified expression

(watch variableName)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

basic commands 11. quit

A

quit

exits the debugger

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Advanced commands 1 ptype

A

ptype [name]
prints detailed description of a type, or the type of the last value in the command history

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Advanced commands 2. catch

A

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

19
Q

vanced commands 3. clear

A

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.

20
Q

Advanced commands 4. up

A

up [num]
move (backwards) to the previous stack frame, or one of the frames preceeding it

21
Q

Advanced commands 5. down

A

opposite of up, moving to the next stack frames.

22
Q

Advanced Commands 6. where/backtrace

A

where [num] [thread { thread_id, … | all | * } ]

show the current stack trace of currently active functions.

backtrace [full] [num]

prints backtrace of stack frames

23
Q
  1. until
A

until [line]

continues the debugee past the current line until the specified source line in the current stack frame.

24
Q
  1. call
A

call expression (parmlist)
call a function in the dubuggee

specify the function as if you were calling it from within the appication

25
Q
  1. x (examining memory)
A

x [/nfu] [addr]

prints memory

26
Q
  1. disable/enable
A

disable [breakpoints][ {ID … | ID-range …} ]
disables one or more breakpoints
enable [breakpoints] [ {ID … | ID-range …} ]

27
Q
  1. set/show args
A

set args [arguments] [IO_redirection]
specifies arguments for debuggee program

show args
show arguments and input/output redirections.

28
Q
  1. set var
A

set variable variable = [expression]
set variables mid-debug

29
Q

Q1 How would you compile a source file with debug information?

A

gcc -g -o fileName Exercises.c
./fileName

gdb -tui fileName

30
Q

Q2 What is the difference between step and next?

A

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.

31
Q

!3 what is the difference between a breakpoint, watchpoint and catchpoint?

A

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.

32
Q

Q4 What is the difference between until and step?

A

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

33
Q

Valgrind 1: alias vlg=’valgrind –leak-check=yes –track-origins=yes’

A

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’

34
Q

Valgrind 2: Do an alocation of memory and don’t free it

A

int *ptr2 = (int *)malloc(sizeof(int));

lost 4 bytes in 1 block.

35
Q

Valgrind 3: Create an array of size 10 and try to read its 11th member

A

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

36
Q

Valgrind 4: Free a pointer, then dereference that pointer

A

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

37
Q

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 …

A

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

38
Q

Valgrind 6:

A

indeed, tested and checked, all functions in 2s2 are fine

39
Q

Core dump 1: read man core dump

A
40
Q

Code dump 2: Write a program that loops infinitely. run it.

A

while(1){printf(“A”);

41
Q

Core dump 3: send a SIGABRT signall to the process running your program. set ulimit to allow for core-file to be generated

A

kill SIGABRT <pid></pid>

42
Q

Core dump 4: debug the generated file

A

suffering.

ulimit -c unlimited
strl+\

43
Q

Extra: Fix the thing

A

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; }