diff --git a/GDB-Commands-Scripts-Workflow b/GDB-Commands-Scripts-Workflow.adoc similarity index 100% rename from GDB-Commands-Scripts-Workflow rename to GDB-Commands-Scripts-Workflow.adoc diff --git a/GDB-Commands-Scripts-Workflow.html b/GDB-Commands-Scripts-Workflow.html deleted file mode 100644 index 0fc721d..0000000 --- a/GDB-Commands-Scripts-Workflow.html +++ /dev/null @@ -1,534 +0,0 @@ - - - - - - - -GDB Commands, Scripts and Workflow - - - - - - -
-
-

Breakpoints and Commands — save time!

-
-

In order to display some specific information when we reach a breakpoint, -we can achieve this by (gdb)command command in gdb.

-
-
-
-
(gdb) command 1
-Type commands for breakpoint(s) 1, one per line.
-End with a line saying just "end".
->print arr
->print size
->end
-
-
-
-

Here, the argument to "command" is the index of the breakpoint to which -these print commands will be associated to.

-
-
-
-

define your own commands

-
-

In order to define bunch of command give it a name like a alias -we can use define <alias>.

-
-
-
-
(gdb) define log
-Type commands for definition of "log".
-End with a line saying just "end".
->info args
->info frame
->bt
->end
-
-
-
-
-
Temporary breakpoint 1, main () at array.c:34
-34      int main () {
-(gdb) log
-No arguments.
-Stack level 0, frame at 0x7fffffffe230:
- rip = 0x555555555217 in main (array.c:34); saved rip = 0x7ffff7c2a1ca
- source language c.
- Arglist at 0x7fffffffe220, args:
- Locals at 0x7fffffffe220, Previous frame's sp is 0x7fffffffe230
- Saved registers:
-  rbp at 0x7fffffffe220, rip at 0x7fffffffe228
-#0  main () at array.c:34
-
-
-
-

To redefine the same alias like in the above example 'log' we can use -the same command define log

-
-
-
-

gdb scripts

-
-

It is like a .vimrc for gdb.

-
-
-

It is named as .gdbinit and it is defined in home (~/) directory -and, if we want, can be overridden by defining the .gdbinit -in the current directory.

-
-
-

In this script, we can define command to define aliases and -print some message, etc.

-
-
-
-

Shell commands within GDB (shell, make and pipe)

- -
-
- - - - - \ No newline at end of file diff --git a/GDB-Extra-Features.adoc b/GDB-Extra-Features.adoc new file mode 100644 index 0000000..dc1c61d --- /dev/null +++ b/GDB-Extra-Features.adoc @@ -0,0 +1,90 @@ += *Inspecting the Virtual Table for Inheritance (C++)* + +In C++, To inspect whether a pointer to a allocated object is of type +the pointer is declared to a particular class or it is object of a class +which is dervied from the class of which the type of the pointer is. + +To check it, we can use the virtualy pointer table of derived class +which can help us in such information. + +We can use command: +[source,] +---- +info vtbl +---- +[source,] +---- +(gdb) info vtbl obj2 +vtable for 'Base' @ 0x555555557d48 (subobject @ 0x55555556b6e0): +[0]: 0x5555555552e2 +(gdb) +---- + +Code: +[source,] +---- +#include + +class Base { + +public: + Base () { + std::cout << "base\n"; + } + + virtual void VirtualMember () { + + } +}; + +class Derived : public Base { + +public: + Derived () { + std::cout << "derived\n"; + } + + void VirtualMember () { + + } +}; + + +int main () { + + Base *obj = new Base; + Base *obj2 = new Derived; + + + return 0; +} +---- + +=== *GDB Command Debug Levels* + +There are different level of debugging information that we can achieve +using the different type of `-g` flag during compilation. + +This is mainly to get _macro_ information as these are not added to +debug symbol by default. + + +Different levels of Debugging symbols: + +1. -g0 +2. -g1 +3. -g2 (default) +4. -g3 +5. -ggdb3 (highest level specifically for GDB debugger) + +To see the information for all the macro, use command: +[source,] +---- +info macros +---- + +To see the information for particular macro, use command: +[source,] +---- +info macro +---- diff --git a/Other-useful-Debugging-Tools.adoc b/Other-useful-Debugging-Tools.adoc new file mode 100644 index 0000000..0aa1c48 --- /dev/null +++ b/Other-useful-Debugging-Tools.adoc @@ -0,0 +1,316 @@ += *Other useful Debugging Tools* + +=== strace and ltrace + + +*strace* + +To trace out the system calls that are being used in program, directly or indirectly, we can use this tool to analyze the program. +[source,] +---- +strace ./add +execve("./add", ["./add"], 0x7ffc2a332960 /* 26 vars */) = 0 +brk(NULL) = 0x5aba508de000 +mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f750ab18000 +access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) +openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 +fstat(3, {st_mode=S_IFREG|0644, st_size=27723, ...}) = 0 +mmap(NULL, 27723, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f750ab11000 +close(3) = 0 +openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 +read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\243\2\0\0\0\0\0"..., 832) = 832 +pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784 +fstat(3, {st_mode=S_IFREG|0755, st_size=2125328, ...}) = 0 +pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784 +mmap(NULL, 2170256, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f750a800000 +mmap(0x7f750a828000, 1605632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x28000) = 0x7f750a828000 +mmap(0x7f750a9b0000, 323584, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b0000) = 0x7f750a9b0000 +mmap(0x7f750a9ff000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1fe000) = 0x7f750a9ff000 +mmap(0x7f750aa05000, 52624, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f750aa05000 +close(3) = 0 +mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f750ab0e000 +arch_prctl(ARCH_SET_FS, 0x7f750ab0e740) = 0 +set_tid_address(0x7f750ab0ea10) = 8992 +set_robust_list(0x7f750ab0ea20, 24) = 0 +rseq(0x7f750ab0f060, 0x20, 0, 0x53053053) = 0 +mprotect(0x7f750a9ff000, 16384, PROT_READ) = 0 +mprotect(0x5aba1a564000, 4096, PROT_READ) = 0 +mprotect(0x7f750ab50000, 8192, PROT_READ) = 0 +prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0 +munmap(0x7f750ab11000, 27723) = 0 +fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0), ...}) = 0 +getrandom("\xf5\x11\x15\x82\x1c\x35\xd7\xb0", 8, GRND_NONBLOCK) = 8 +brk(NULL) = 0x5aba508de000 +brk(0x5aba508ff000) = 0x5aba508ff000 +write(1, "addAndSquare(0,0)=0\n", 20addAndSquare(0,0)=0 +) = 20 +write(1, "addAndSquare(0,1)=1\n", 20addAndSquare(0,1)=1 +) = 20 +write(1, "addAndSquare(0,2)=4\n", 20addAndSquare(0,2)=4 +) = 20 +write(1, "addAndSquare(0,3)=9\n", 20addAndSquare(0,3)=9 +) = 20 +write(1, "addAndSquare(0,4)=16\n", 21addAndSquare(0,4)=16 +) = 21 +write(1, "addAndSquare(0,5)=25\n", 21addAndSquare(0,5)=25 +) = 21 +write(1, "addAndSquare(1,0)=1\n", 20addAndSquare(1,0)=1 +) = 20 +write(1, "addAndSquare(1,1)=4\n", 20addAndSquare(1,1)=4 +) = 20 +write(1, "addAndSquare(1,2)=9\n", 20addAndSquare(1,2)=9 +) = 20 +write(1, "addAndSquare(1,3)=16\n", 21addAndSquare(1,3)=16 +) = 21 +write(1, "addAndSquare(1,4)=25\n", 21addAndSquare(1,4)=25 +) = 21 +write(1, "addAndSquare(1,5)=36\n", 21addAndSquare(1,5)=36 +) = 21 +write(1, "addAndSquare(2,0)=4\n", 20addAndSquare(2,0)=4 +) = 20 +write(1, "addAndSquare(2,1)=9\n", 20addAndSquare(2,1)=9 +) = 20 +write(1, "addAndSquare(2,2)=16\n", 21addAndSquare(2,2)=16 +) = 21 +write(1, "addAndSquare(2,3)=25\n", 21addAndSquare(2,3)=25 +) = 21 +write(1, "addAndSquare(2,4)=36\n", 21addAndSquare(2,4)=36 +) = 21 +write(1, "addAndSquare(2,5)=49\n", 21addAndSquare(2,5)=49 +) = 21 +write(1, "addAndSquare(3,0)=9\n", 20addAndSquare(3,0)=9 +) = 20 +write(1, "addAndSquare(3,1)=16\n", 21addAndSquare(3,1)=16 +) = 21 +write(1, "addAndSquare(3,2)=25\n", 21addAndSquare(3,2)=25 +) = 21 +write(1, "addAndSquare(3,3)=36\n", 21addAndSquare(3,3)=36 +) = 21 +write(1, "addAndSquare(3,4)=49\n", 21addAndSquare(3,4)=49 +) = 21 +write(1, "addAndSquare(3,5)=64\n", 21addAndSquare(3,5)=64 +) = 21 +write(1, "addAndSquare(4,0)=16\n", 21addAndSquare(4,0)=16 +) = 21 +write(1, "addAndSquare(4,1)=25\n", 21addAndSquare(4,1)=25 +) = 21 +write(1, "addAndSquare(4,2)=36\n", 21addAndSquare(4,2)=36 +) = 21 +write(1, "addAndSquare(4,3)=49\n", 21addAndSquare(4,3)=49 +) = 21 +write(1, "addAndSquare(4,4)=64\n", 21addAndSquare(4,4)=64 +) = 21 +write(1, "addAndSquare(4,5)=81\n", 21addAndSquare(4,5)=81 +) = 21 +write(1, "addAndSquare(5,0)=25\n", 21addAndSquare(5,0)=25 +) = 21 +write(1, "addAndSquare(5,1)=36\n", 21addAndSquare(5,1)=36 +) = 21 +write(1, "addAndSquare(5,2)=49\n", 21addAndSquare(5,2)=49 +) = 21 +write(1, "addAndSquare(5,3)=64\n", 21addAndSquare(5,3)=64 +) = 21 +write(1, "addAndSquare(5,4)=81\n", 21addAndSquare(5,4)=81 +) = 21 +write(1, "addAndSquare(5,5)=100\n", 22addAndSquare(5,5)=100 +) = 22 +exit_group(0) = ? ++++ exited with 0 +++ +---- + +To get summary of the system calls and their usage or contribution to the programe, we can use `strace` command `strace -c ./`. +[source,] +---- +strace -c ./add +addAndSquare(0,0)=0 +addAndSquare(0,1)=1 +addAndSquare(0,2)=4 +addAndSquare(0,3)=9 +addAndSquare(0,4)=16 +addAndSquare(0,5)=25 +addAndSquare(1,0)=1 +addAndSquare(1,1)=4 +addAndSquare(1,2)=9 +addAndSquare(1,3)=16 +addAndSquare(1,4)=25 +addAndSquare(1,5)=36 +addAndSquare(2,0)=4 +addAndSquare(2,1)=9 +addAndSquare(2,2)=16 +addAndSquare(2,3)=25 +addAndSquare(2,4)=36 +addAndSquare(2,5)=49 +addAndSquare(3,0)=9 +addAndSquare(3,1)=16 +addAndSquare(3,2)=25 +addAndSquare(3,3)=36 +addAndSquare(3,4)=49 +addAndSquare(3,5)=64 +addAndSquare(4,0)=16 +addAndSquare(4,1)=25 +addAndSquare(4,2)=36 +addAndSquare(4,3)=49 +addAndSquare(4,4)=64 +addAndSquare(4,5)=81 +addAndSquare(5,0)=25 +addAndSquare(5,1)=36 +addAndSquare(5,2)=49 +addAndSquare(5,3)=64 +addAndSquare(5,4)=81 +addAndSquare(5,5)=100 +% time seconds usecs/call calls errors syscall +------ ----------- ----------- --------- --------- ---------------- + 71.38 0.002342 65 36 write + 9.17 0.000301 37 8 mmap + 2.96 0.000097 32 3 mprotect + 2.77 0.000091 30 3 fstat + 2.10 0.000069 23 3 brk + 2.01 0.000066 33 2 pread64 + 1.55 0.000051 25 2 openat + 1.31 0.000043 21 2 close + 1.28 0.000042 42 1 getrandom + 1.10 0.000036 36 1 munmap + 0.76 0.000025 25 1 1 access + 0.70 0.000023 23 1 read + 0.70 0.000023 23 1 set_robust_list + 0.67 0.000022 22 1 arch_prctl + 0.67 0.000022 22 1 set_tid_address + 0.64 0.000021 21 1 rseq + 0.21 0.000007 7 1 prlimit64 + 0.00 0.000000 0 1 execve +------ ----------- ----------- --------- --------- ---------------- +100.00 0.003281 47 69 1 total +---- + +*ltrace* + +Just like strace to trace the usage of library in program, we can use `ltrace` tools. +[source,] +---- +ltrace ./add +printf("addAndSquare(%d,%d)=%d\n", 0, 0, 0addAndSquare(0,0)=0 +) = 20 +printf("addAndSquare(%d,%d)=%d\n", 0, 1, 1addAndSquare(0,1)=1 +) = 20 +printf("addAndSquare(%d,%d)=%d\n", 0, 2, 4addAndSquare(0,2)=4 +) = 20 +printf("addAndSquare(%d,%d)=%d\n", 0, 3, 9addAndSquare(0,3)=9 +) = 20 +printf("addAndSquare(%d,%d)=%d\n", 0, 4, 16addAndSquare(0,4)=16 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 0, 5, 25addAndSquare(0,5)=25 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 1, 0, 1addAndSquare(1,0)=1 +) = 20 +printf("addAndSquare(%d,%d)=%d\n", 1, 1, 4addAndSquare(1,1)=4 +) = 20 +printf("addAndSquare(%d,%d)=%d\n", 1, 2, 9addAndSquare(1,2)=9 +) = 20 +printf("addAndSquare(%d,%d)=%d\n", 1, 3, 16addAndSquare(1,3)=16 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 1, 4, 25addAndSquare(1,4)=25 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 1, 5, 36addAndSquare(1,5)=36 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 2, 0, 4addAndSquare(2,0)=4 +) = 20 +printf("addAndSquare(%d,%d)=%d\n", 2, 1, 9addAndSquare(2,1)=9 +) = 20 +printf("addAndSquare(%d,%d)=%d\n", 2, 2, 16addAndSquare(2,2)=16 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 2, 3, 25addAndSquare(2,3)=25 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 2, 4, 36addAndSquare(2,4)=36 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 2, 5, 49addAndSquare(2,5)=49 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 3, 0, 9addAndSquare(3,0)=9 +) = 20 +printf("addAndSquare(%d,%d)=%d\n", 3, 1, 16addAndSquare(3,1)=16 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 3, 2, 25addAndSquare(3,2)=25 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 3, 3, 36addAndSquare(3,3)=36 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 3, 4, 49addAndSquare(3,4)=49 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 3, 5, 64addAndSquare(3,5)=64 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 4, 0, 16addAndSquare(4,0)=16 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 4, 1, 25addAndSquare(4,1)=25 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 4, 2, 36addAndSquare(4,2)=36 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 4, 3, 49addAndSquare(4,3)=49 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 4, 4, 64addAndSquare(4,4)=64 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 4, 5, 81addAndSquare(4,5)=81 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 5, 0, 25addAndSquare(5,0)=25 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 5, 1, 36addAndSquare(5,1)=36 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 5, 2, 49addAndSquare(5,2)=49 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 5, 3, 64addAndSquare(5,3)=64 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 5, 4, 81addAndSquare(5,4)=81 +) = 21 +printf("addAndSquare(%d,%d)=%d\n", 5, 5, 100addAndSquare(5,5)=100 +) = 22 ++++ exited (status 0) +++ +---- + +Summary: +[source,] +---- +ltrace -c ./add +addAndSquare(0,0)=0 +addAndSquare(0,1)=1 +addAndSquare(0,2)=4 +addAndSquare(0,3)=9 +addAndSquare(0,4)=16 +addAndSquare(0,5)=25 +addAndSquare(1,0)=1 +addAndSquare(1,1)=4 +addAndSquare(1,2)=9 +addAndSquare(1,3)=16 +addAndSquare(1,4)=25 +addAndSquare(1,5)=36 +addAndSquare(2,0)=4 +addAndSquare(2,1)=9 +addAndSquare(2,2)=16 +addAndSquare(2,3)=25 +addAndSquare(2,4)=36 +addAndSquare(2,5)=49 +addAndSquare(3,0)=9 +addAndSquare(3,1)=16 +addAndSquare(3,2)=25 +addAndSquare(3,3)=36 +addAndSquare(3,4)=49 +addAndSquare(3,5)=64 +addAndSquare(4,0)=16 +addAndSquare(4,1)=25 +addAndSquare(4,2)=36 +addAndSquare(4,3)=49 +addAndSquare(4,4)=64 +addAndSquare(4,5)=81 +addAndSquare(5,0)=25 +addAndSquare(5,1)=36 +addAndSquare(5,2)=49 +addAndSquare(5,3)=64 +addAndSquare(5,4)=81 +addAndSquare(5,5)=100 +% time seconds usecs/call calls function +------ ----------- ----------- --------- -------------------- +100.00 0.034095 947 36 printf +------ ----------- ----------- --------- -------------------- +100.00 0.034095 36 total +---- + += *Static Analysis tools* + +We can use cppcheck, which is static analysis tool. +Or we can use `-Weffc++` flag with `g++` for static analysis. \ No newline at end of file diff --git a/Valgrind.adoc b/Valgrind.adoc new file mode 100644 index 0000000..e69de29