Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An example of debug script debugging (GDB + PyDev) #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions misc/gdb_python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.exe
*.o
22 changes: 22 additions & 0 deletions misc/gdb_python/Makefile
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions misc/gdb_python/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void main(void)
{
int i = 0;
for (; i < 10;) {
i++;
}
}
22 changes: 22 additions & 0 deletions misc/gdb_python/main.exe-gdb.py
Original file line number Diff line number Diff line change
@@ -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")
11 changes: 11 additions & 0 deletions misc/gdb_python/readme.md
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions misc/gdb_python/settings.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CFLAGS+=-g -O0 -no-pie

progs:=main

debug: main.exe
gdb -iex "set auto-load safe-path /" main.exe