= *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 ./<program>`.
[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.