From 2520f38555c9afcd6bec50116807f8bf97311d43 Mon Sep 17 00:00:00 2001
From: johannst <johannes.stoelp@gmail.com>
Date: Fri, 25 Oct 2019 20:42:48 +0200
Subject: added some notes about debugging

---
 debug.txt | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 gdb.txt   | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 101 insertions(+), 5 deletions(-)
 create mode 100644 debug.txt

diff --git a/debug.txt b/debug.txt
new file mode 100644
index 0000000..5a3c105
--- /dev/null
+++ b/debug.txt
@@ -0,0 +1,48 @@
+.:: debugging ::.
+--------------------------------------------------------------------------------
+
+# print syscalls of process with all threads (-f) of running process
+strace -f -p <pid>
+# only trace certain syscalls
+strace -f -p <pid> -e trace=open,socket
+# trace signals delivered to process
+strace -f -p <pid> -e signal
+
+# dump stack of process and all threads
+pstack <pid>
+
+# print file flags
+# +fg    print file flag abbreviations
+lsof +fg -p <pid>
+
+# print process virt mem map
+# compared to /proc/<>/maps it shows the size of the mappings
+pmap <pid>
+
+
+# get supported events
+perf list
+
+perf stat -p <pid>
+perf stat -p <pid> -I <ms>
+perf stat -p <pid> -e cycles,faults,cache-misses,context-switches
+
+# -K    hide kernel threads
+perf top -F 99 -p <pid> -K
+
+perf record -F 99 -p <pid>
+perf record -F 99 -p <pid> --call-graph dwarf
+perf record -F 99 -p <pid> -e instructions,cpu-cycles,faults,cache-misses,context-switches
+
+
+# print supported events
+ophelp
+
+operf -p <pid>
+
+opreport -c
+opreport -l
+
+--------------------------------------------------------------------------------
+vim:sts=2:et:tw=80:cc=80:fo+=t:ft=help
+
diff --git a/gdb.txt b/gdb.txt
index 47ecdce..37b6b48 100644
--- a/gdb.txt
+++ b/gdb.txt
@@ -8,6 +8,7 @@
     |prompt|
     |user_commands|
     |hooks|
+    |flows|
 
 # gdb(1)                                                                   *gdb*
 =========
@@ -30,7 +31,7 @@
                             specify which process to follow on fork(2)
 
       sharedlibrary [regex]
-                            load symbols of shared lib, if regex then only symbols
+                            load symbols of shared lib, if REGEX then only symbols
                             for matching libs
 
       break <sym> thread <tnum>
@@ -41,20 +42,34 @@
                             so 'rbreak foo' matches barfoobar()
 
       command [bp_list]     define commands to run after breakpoint hit if
-                            bp_list not supplied attach command to last
+                            BP_LIST not supplied attach command to last
                             created bp
 
-                            bp_list: space separates list, eg 'command 2 5-8'
+                            BP_LIST: space separates list, eg 'command 2 5-8'
                             to run command for bp 2,5,6,7,8
 
       info functions [regex]
-                            list functions according to regex, if regex empty,
+                            list functions according to REGEX, if REGEX empty,
                             list all
 
       info variables [regex]
-                            list variables according to regex, if regex empty,
+                            list variables according to REGEX, if REGEX empty,
                             list all
 
+      info handle [signal]
+                            list bevahior for SIGNAL, if SIGNAL empty list all signals
+
+      handle signal <action>
+                            configure how gdb handles SIGNAL which is ment for the debugee
+                            <action>:
+                              stop/nostop       catch signal in gdb and break
+                              print/noprint     print message when gdb catches signal
+                              pass/nopass       pass signal down to debugee
+                              noignore/ignore   synonyms for
+
+      catch signal <signal>
+                            create a catchpoint for SIGNAL
+
 
     user commands:                                               *user_commands*
       define <cmd>                  defines user command <cmd> to be run
@@ -78,6 +93,39 @@
         # cmds                      executing <cmd>
       end
 
+
+    flows:                                                               *flows*
+      # catch SIGSEGV and execute some actions once it happends
+      - script:
+        catch signal SIGSEGV
+        command
+          bt
+          ct
+        end
+
+      # quickly execute gdb command on running process, eg get backtrace from
+      # thread 1
+      - cmd:
+        $> gdb -ex 'thread 1' -ex 'bt' -p <PID>
+
+      # script gdb for automating debugging sessions
+      - script: run.gdb
+        set pagination off
+
+        break mmap
+        command
+        info reg rdi rsi rdx
+        bt
+        c
+        end
+
+        #initial drop
+        c
+
+      - cmd:
+        $> gdb -p <PID> -x ./run.gdb  --batch &> run.log
+
+
 --------------------------------------------------------------------------------
 vim:sts=2:et:tw=80:cc=80:fo+=t:ft=help
 
-- 
cgit v1.2.3