(gdb) set solib-search-path <my directories having so files separated by :>
(gdb) show solib-search-path
http://visualgdb.com/gdbreference/commands/set_solib-search-path
You can use the set environment command in gdb to change the environment gdb uses to launch the program you want to debug. Extract from the gdb help:
(gdb) help set environment
Set environment variable value to give the program.
Arguments are VAR VALUE where VAR is variable name and VALUE is value.
VALUES of environment variables are uninterpreted strings.
This does not affect the program until the next "run" command.
Example with LD_LIBRARY_PATH:
(gdb) set environment LD_LIBRARY_PATH /home/paceholder/projects/geo/lib/debug
If you want to follow the child process instead of the parent process, use the command set follow-fork-mode.
set follow-fork-mode mode
Set the debugger response to a program call of fork or vfork. A call to fork or vfork creates a new process. The mode argument can be:
parent
The original process is debugged after a fork. The child process runs unimpeded. This is the default.
child
The new process is debugged after a fork. The parent process runs unimpeded.
Write function in .gdbinit
.gdbinit example
root@ns# cat ~/.gdbinit
define myloop_print
set $total = $arg0
set $i = 1
while ($i < 2)
if $total == 2
set $i = $i + $total
end
set $i = $i + 1
end
print $i
end
Load GDB and run the script
Example for running gdbscript
root@ns# gdb
GNU gdb 6.1.1 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "amd64-marcel-freebsd".
(gdb) myloop_print 2
$1 = 4
(gdb) myloop_print 1
$2 = 2
Useful link: https://stackoverflow.com/questions/20481815/how-to-loop-in-a-gdb-script-till-program-is-finished
Use below command after attaching process with gdb
(gdb) generate-core-file
Useful link:https://stackoverflow.com/questions/4610005/gdb-creating-a-core-file
x/100x <address> -> print the memory value for address
x/100x <address> -100 -> print the memory value for address starting from -100 position
info register -> prints all registers
p $rsp -> print register rsp
set $rsp=<address> -> modify value of register rsp
disassemble -> shows disassembly code
x/I <function name>-> prints function
attach <pid> -> attach process
watch <memory address> -> breaks to the code which changes the address
layout asm -> opens an UI with assembly code.. useful for walkthrough of assembly code
layout src -> useful for walkthrough of source code. Source code path should be already set.
dir <source code path> -> add the source code search path in the gdb list
source script -> loads gdb script
Method to notice who is changing the memory address
use watch
Get source code in the machine where you do debugging using mount
Refer https://www.digitalocean.com/community/tutorials/how-to-set-up-an-nfs-mount-on-ubuntu-16-04
Configure source code machine (10.106.102.141)
Install nfs in ubuntu machine
root#apt-get install nfs-kernel-server
root#apt-get install nfs-common
Enable remote access via mount
root@ubuntu:~/ws1/rs_120/usr.src# cat /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/root/ 10.102.53.248(rw,no_root_squash,sync,no_subtree_check)
root#/etc/init.d/nfs-kernel-server restart
Mount source code to the machine where you do GDB(10.102.53.248)
root#mount -t nfs 10.106.102.141:/root/ /var/code/
root# mount | grep 10.106.102.141
10.106.102.141:/root on /var/code type nfs4 (rw,relatime,vers=4.0,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.102.53.248,local_lock=none,addr=10.106.102.141)
root@ubuntu:~/personal/sslcrash/assert32bit# ls /var/code/
emacs op personal pid_file rb.log ws1
Configure GDB
Get test binary
root@ubuntu:/var/code/ws1/mytest# cat test.c
#include<stdio.h>
int main()
{
printf("Hello world");
return 0;
}
root@ubuntu:/var/code/ws1/mytest# gcc -ggdb3 test.c -o test
root@ubuntu:/var/code/ws1/mytest# ls
test test.c
root@ubuntu:/var/code/ws1/mytest# cd ../../
root@ubuntu:/var/code# cp ws1/mytest/test .
root@ubuntu:/var/code# file test
test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=182c25a6f444b7aa7d1b8b7c6d11cadb6bbb1e75, not stripped
Enable source code walkthrough in GDB
root@ubuntu:/var/code/ws1/mytest# cd /root/personal/testdir/
root@ubuntu:~/personal/testdir# ls
test
root@ubuntu:~/personal# gdb testdir/test
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 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 testdir/test...done.
(gdb) help dir
Add directory DIR to beginning of search path for source files.
Forget cached info on source file locations and line positions.
DIR can also be $cwd for the current working directory, or $cdir for the
directory in which the source file was compiled into object code.
With no argument, reset the search path to $cdir:$cwd, the default.
(gdb) dir /var/code/ws1/mytest/
Source directories searched: /var/code/ws1/mytest:$cdir:$cwd
(gdb) show dir
Source directories searched: /var/code/ws1/mytest:$cdir:$cwd
(gdb) layout src
Screenshot of debugging
http://stackoverflow.com/questions/16254546/gdb-can-not-open-shared-object-file
https://sourceware.org/gdb/onlinedocs/gdb/Forks.html