Created
April 8, 2016 09:59
-
-
Save antoinealb/17520f0d6fe977b85086a451dbc79d0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gdb | |
from collections import namedtuple | |
THREAD_STATES = [ | |
"READY", "CURRENT", "WTSTART", "SUSPENDED", "QUEUED", "WTSEM", "WTMTX", | |
"WTCOND", "SLEEPING", "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSGQ", | |
"SNDMSG", "WTMSG", "FINAL"] | |
Thread = namedtuple('Thread', ('r13', 'stklimit', 'name', 'state')) | |
def get_thread(thread): | |
void_p = gdb.lookup_type('void').pointer() | |
# Grab stack pointer | |
r13 = thread['p_ctx']['r13'].cast(void_p) | |
stklimit = thread['p_stklimit'] | |
name = thread['p_name'].string() | |
if not name: | |
name = "<no name>" | |
state = THREAD_STATES[int(thread['p_state'])] | |
return Thread(r13=r13, stklimit=stklimit, name=name, state=state) | |
def chibios_get_threads(): | |
""" Create a list of ChibiosThreads for all threads currently in | |
the system | |
""" | |
# Walk the thread registry | |
rlist_p = gdb.parse_and_eval('&ch.rlist') | |
rlist_as_thread = rlist_p.cast(gdb.lookup_type('thread_t').pointer()) | |
newer = rlist_as_thread.dereference()['p_newer'] | |
older = rlist_as_thread.dereference()['p_older'] | |
while newer != rlist_as_thread: | |
yield get_thread(newer.dereference()) | |
previous = newer | |
newer = newer.dereference()['p_newer'] | |
older = newer.dereference()['p_older'] | |
if older != previous: | |
raise gdb.GdbError('Rlist pointer invalid--corrupt list?') | |
class ThreadsCommand(gdb.Command): | |
thread_format = "'{name:20s}'" | |
def __init__ (self): | |
super(ThreadsCommand, self).__init__ ("chthreads", gdb.COMMAND_USER) | |
def invoke(self, arg, from_tty): | |
print("Name \tStack pointer\tStack limit\tState\t") | |
for t in chibios_get_threads(): | |
print("{:20s}\t0x{:08x}\t0x{:08x}\t{}".format(t.name, long(t.r13), long(t.stklimit), t.state)) | |
ThreadsCommand() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment