Skip to content

Commit 0750def

Browse files
committed
add gdb stack-deltas command
1 parent 47adf86 commit 0750def

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

gdb/.gdbinit

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ source ~/.gdb-cyrus-dashboard
44
# Bring in typeof
55
source ~/.tromey-gdb-helpers/gdbhelpers/typeof.py
66

7+
set auto-load safe-path /
8+
79
# Tweak some defaults
810
set history save on
911
set history size 10000
@@ -150,3 +152,50 @@ class PrintEnumCmd(gdb.Command):
150152

151153
PrintEnumCmd()
152154
end
155+
156+
python
157+
import gdb
158+
159+
class StackDeltaCommand(gdb.Command):
160+
"""Compute and display the stack delta for each frame in the backtrace."""
161+
162+
def __init__(self):
163+
super(StackDeltaCommand, self).__init__("stack-deltas", gdb.COMMAND_USER)
164+
165+
def invoke(self, arg, from_tty):
166+
# Get the newest (currently selected) frame
167+
frame = gdb.newest_frame()
168+
169+
if frame is None:
170+
print("No stack frames found.")
171+
return
172+
173+
deltas = []
174+
sp_values = []
175+
176+
# Traverse frames and collect $sp values
177+
while frame is not None:
178+
try:
179+
sp_val = int(frame.read_register("sp"))
180+
sp_values.append((frame, sp_val))
181+
except ValueError:
182+
print(f"Could not read $sp for frame: {frame.name()}")
183+
break
184+
frame = frame.older()
185+
186+
# Compute deltas between adjacent frames
187+
print("Stack deltas (in bytes):")
188+
for i in range(len(sp_values) - 1):
189+
frame, sp = sp_values[i]
190+
_, next_sp = sp_values[i + 1]
191+
delta = sp - next_sp
192+
print(f"Frame {i} ({frame.name()}): delta = {delta}")
193+
194+
# Print the last frame (no delta)
195+
if sp_values:
196+
last_frame, _ = sp_values[-1]
197+
print(f"Frame {len(sp_values)-1} ({last_frame.name()}): delta = N/A")
198+
199+
200+
StackDeltaCommand()
201+
end

0 commit comments

Comments
 (0)