From 2dc96fd8c690d206ef396c7bf9be14cef5963225 Mon Sep 17 00:00:00 2001 From: Efimov Vasily Date: Fri, 2 Oct 2020 17:55:49 +0300 Subject: [PATCH] misc: an example of debugging (with PyDev) of debug script (GDB Python API) Signed-off-by: Efimov Vasily --- misc/gdb_python/.gitignore | 2 ++ misc/gdb_python/Makefile | 22 ++++++++++++++++++++++ misc/gdb_python/main.c | 7 +++++++ misc/gdb_python/main.exe-gdb.py | 22 ++++++++++++++++++++++ misc/gdb_python/readme.md | 11 +++++++++++ misc/gdb_python/settings.mak | 6 ++++++ 6 files changed, 70 insertions(+) create mode 100644 misc/gdb_python/.gitignore create mode 100644 misc/gdb_python/Makefile create mode 100644 misc/gdb_python/main.c create mode 100755 misc/gdb_python/main.exe-gdb.py create mode 100644 misc/gdb_python/readme.md create mode 100644 misc/gdb_python/settings.mak diff --git a/misc/gdb_python/.gitignore b/misc/gdb_python/.gitignore new file mode 100644 index 000000000..774f00871 --- /dev/null +++ b/misc/gdb_python/.gitignore @@ -0,0 +1,2 @@ +*.exe +*.o diff --git a/misc/gdb_python/Makefile b/misc/gdb_python/Makefile new file mode 100644 index 000000000..5ff3b714a --- /dev/null +++ b/misc/gdb_python/Makefile @@ -0,0 +1,22 @@ +# Default target. +all: + +# User can redefine macros & rules in settings.mak above its inclusion. + +__progs=$(foreach p,$(progs),$(p).exe) + +__PROG_FILES=$(foreach f,$(1) $($(1)),$(f).o) + +__progs_files=$(foreach p,$(progs),$(call __PROG_FILES,$p)) + +clean: + rm -f $(__progs) $(__progs_files) + +%.exe: + $(CC) $(CFLAGS) $(filter-out Makefile settings.mak,$^) -o $@ + +include settings.mak + +$(foreach f,$(__progs_files) $(__progs),$(eval $(f):settings.mak Makefile)) +$(foreach p,$(progs), $(eval $(p).exe: $(call __PROG_FILES,$p) )) +all: $(__progs) diff --git a/misc/gdb_python/main.c b/misc/gdb_python/main.c new file mode 100644 index 000000000..1e1849205 --- /dev/null +++ b/misc/gdb_python/main.c @@ -0,0 +1,7 @@ +void main(void) +{ + int i = 0; + for (; i < 10;) { + i++; + } +} diff --git a/misc/gdb_python/main.exe-gdb.py b/misc/gdb_python/main.exe-gdb.py new file mode 100755 index 000000000..63efde494 --- /dev/null +++ b/misc/gdb_python/main.exe-gdb.py @@ -0,0 +1,22 @@ +# Find out which of system python is backing gdb. +# Then: sudo [python] -m pip install pydevd +# import sys +# print(sys.path) + +# A PyDev Debug Server must be active. +# E.g.: Launch Eclipse with PyDev installed, then Pydev -> Start Debug Server +# and open "Debug" perspective. +import pydevd + +# Execution of this script is paused after this command with corresponding +# PyDev GUI reaction. +pydevd.settrace() + +class MyBr(gdb.Breakpoint): + + def stop(self): + i = gdb.parse_and_eval("i") + # You may set a breakpoint at this line (in Eclipse PyDev editor). + return i > 5 + +MyBr("main.c:5") diff --git a/misc/gdb_python/readme.md b/misc/gdb_python/readme.md new file mode 100644 index 000000000..737e22cde --- /dev/null +++ b/misc/gdb_python/readme.md @@ -0,0 +1,11 @@ +# HowTo + +Read [main.exe-gdb.py](main.exe-gdb.py), then: + +```bash +make debug +``` + +# References +- https://www.sourceware.org/gdb/onlinedocs/gdb/Python-API.html +- https://www.pydev.org/manual_adv_remote_debugger.html diff --git a/misc/gdb_python/settings.mak b/misc/gdb_python/settings.mak new file mode 100644 index 000000000..024b528cf --- /dev/null +++ b/misc/gdb_python/settings.mak @@ -0,0 +1,6 @@ +CFLAGS+=-g -O0 -no-pie + +progs:=main + +debug: main.exe + gdb -iex "set auto-load safe-path /" main.exe