This Blog will show useful debugger tool
Introduction
Hello every Dear friend.I am wsyingang. In this article,I will show some debug tools.I will written this blog with English,because I does not find a suitable software for typewriting.
Debugger tool for c/c++
As we know ,GDB is the most popular debug tool for c++/c . Now, have a try.
Env info
Operating System:Ubuntu
Demo code
#include<bits/stdc++.h>
using namespace std;
int add(int a,int b){
cout<<"in add"<<endl;
int c=a+b;
int d=a-b;
return c;
}
int main(){
int a;
a=10;
int b;
b=11;
int ans=add(a,b);
cout<<"ans==="<<ans<<endl;
int sum=1;
for(int i=1;i<=10;i++){
sum+=i;
}
cout<<"sum=="<<sum<<endl;
}
Compile Source Code
compile code with g++ compiler,if you have not this tool on your os,you can install like this:
sudo apt install g++
g++ -g -o test demo.cpp
now,we get the target :test
Start gdb
gdb test
if everything work well,you will get this info:
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...
Attention the last line,Reading symbols from test.
Some action
List source code
gdb) list
1 #include<bits/stdc++.h>
2 using namespace std;
3 int add(int a,int b){
4 cout<<"in add"<<endl;
5 int c=a+b;
6 int d=a-b;
7 return c;
8 }
9 int main(){
10 int a;
(gdb)
list command will list source code.Maybe you want look the spec function,you can exec command:list func_name
press enter will show you last code,which not appear.
run
runcommand will run this program.For Example
(gdb) run
Starting program: /home/yg/code-repo/gdb-learn/test
in add
ans===21
sum==56
[Inferior 1 (process 9399) exited normally]
(gdb)
breakpoint
For debugging breakpoint is very important.You cant set breakpoint with this format:
b line_number
for example
(gdb) b 6
Breakpoint 1 at 0x55555555520e: file demo.cpp, line 6.
Then,run this program
(gdb) run
Starting program: /home/yg/code-repo/gdb-learn/test
in add
Breakpoint 1, add (a=10, b=11) at demo.cpp:6
6 int d=a-b;
(gdb)
This process will stop at line 6
continue
continue command let process go to next breakpoint.Now we set two breakpoints
(gdb) b 6
Note: breakpoint 1 also set at pc 0x55555555520e.
Breakpoint 2 at 0x55555555520e: file demo.cpp, line 6.
(gdb) b 20
Breakpoint 3 at 0x5555555552a0: file demo.cpp, line 20.
at line 6 and line 20.Then run this program
(gdb) run
Starting program: /home/yg/code-repo/gdb-learn/test
in add
Breakpoint 1, add (a=10, b=11) at demo.cpp:6
6 int d=a-b;
Now,It reach first breakpoint,and continue
(gdb) continue
Continuing.
ans===21
Breakpoint 3, main () at demo.cpp:20
20 cout<<"sum=="<<sum<<endl;
it reach second breakpoint.
print /whatis
When you are debugging,you want watch some variable.You can use print and whatis to detect variable.
For Example
(gdb) run
Starting program: /home/yg/code-repo/gdb-learn/test
in add
Breakpoint 1, add (a=10, b=11) at demo.cpp:6
6 int d=a-b;
(gdb) print d
$1 = 21845
(gdb)
Now,you maybe curious about the value of d.It is a strange value,not equal a-b.In fact at line 6,a break point is alive,so d is not assign value at this time.
step step command will let process exec next step,if next step.
next next command will let process exec next command.
(gdb) next
7 return c;
(gdb) print d
$4 = -1
(gdb)
if exec next/step command,then print d ,you will find d is equal to a-b.
backtrace
(gdb) backtrace
#0 add (a=10, b=11) at demo.cpp:7
#1 <function called from gdb>
#2 add (a=10, b=11) at demo.cpp:6
#3 0x0000555555555245 in main () at demo.cpp:14
(gdb)
this command will show you current stack.
quit
if you want finish debug use quit command.
Summary
gdb is a very useful tool for debugging.This above introduction is only small part.You can find more by yourself
Debugger for Go
If you are go programer,you can use gdb for debugging as well.But there as other options.
delve is a easy-to-use debug tool for golang.
How to Install
you can follow the documentation doc.
If everything work well,you will get the binary executable file,at you $GOPATH/bin
for example
yg@ubuntu:~/go/bin$ ls | grep dlv
dlv
yg@ubuntu:~/go/bin$
you can add an soft link for this file like this:
yg@ubuntu:/usr/local/bin$ ln -s ~/go/bin/dlv dlv
yg@ubuntu:/usr/local/bin$ ll
total 8
drwxr-xr-x 2 root root 4096 Nov 7 01:22 ./
drwxr-xr-x 11 root root 4096 Aug 15 00:02 ../
lrwxrwxrwx 1 root root 19 Nov 7 01:22 dlv -> /home/yg/go/bin/dlv*
then you can use dlv at everywhere.
Debugger for Java
JDB
a gdb likely tool for java debug
Arthas
Arthas is a Java Diagnostic tool open sourced by Alibaba.