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