Skip to content

Instantly share code, notes, and snippets.

@aidanhs
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aidanhs/a25a6131155834b4c483 to your computer and use it in GitHub Desktop.
Save aidanhs/a25a6131155834b4c483 to your computer and use it in GitHub Desktop.
golang gdb script hackery
--- runtime-gdb.py 2015-05-05 17:03:21.520510629 +0100
+++ runtime-gdb-hacks.py 2015-05-05 17:05:24.416677753 +0100
@@ -369,6 +369,41 @@
ptr = ptr[linkfield]
+class AidanCmd(gdb.Command):
+ "List all goroutines."
+
+ def __init__(self):
+ gdb.Command.__init__(self, "info aidan", gdb.COMMAND_STACK, gdb.COMPLETE_NONE)
+
+ def invoke(self, _arg, _from_tty):
+ vp = gdb.lookup_type('void').pointer()
+ sv = SliceValue(gdb.parse_and_eval("'runtime.allgs'"))
+ print("num: " + str(sv.len))
+ for ptr in sv:
+ if ptr['atomicstatus'] == 6: # 'gdead'
+ continue
+ s = ' '
+ if ptr['m']:
+ s = '*'
+ pc = ptr['sched']['pc'].cast(vp)
+ # python2 will not cast pc (type void*) to an int cleanly
+ # instead python2 and python3 work with the hex string representation
+ # of the void pointer which we can parse back into an int.
+ # int(pc) will not work.
+ try:
+ #python3 / newer versions of gdb
+ pc = int(pc)
+ except gdb.error:
+ # str(pc) can return things like
+ # "0x429d6c <runtime.gopark+284>", so
+ # chop at first space.
+ pc = int(str(pc).split(None, 1)[0], 16)
+ blk = gdb.block_for_pc(pc)
+ goid = ptr['goid']
+ print(s, ptr['goid'], "{0:8s}".format(sts[int(ptr['atomicstatus'])]), blk.function)
+ GoroutineCmd.invoke(self, str(goid) + ' bt', None)
+
+
class GoroutinesCmd(gdb.Command):
"List all goroutines."
@@ -378,7 +413,9 @@
def invoke(self, _arg, _from_tty):
# args = gdb.string_to_argv(arg)
vp = gdb.lookup_type('void').pointer()
- for ptr in SliceValue(gdb.parse_and_eval("'runtime.allgs'")):
+ sv = SliceValue(gdb.parse_and_eval("'runtime.allgs'"))
+ print("num: " + str(sv.len))
+ for ptr in sv:
if ptr['atomicstatus'] == 6: # 'gdead'
continue
s = ' '
@@ -499,6 +536,7 @@
GoLenFunc()
GoCapFunc()
DTypeFunc()
+AidanCmd()
GoroutinesCmd()
GoroutineCmd()
GoIfaceCmd()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment