Skip to content

debugging state transitions and events gdb helper #47

@mo-krauti

Description

@mo-krauti

Thank you for the library!
I created a little python helper for gdb to allow easier event and state transitioning debugging.

import re

import gdb


EVENT_REGEX = re.compile("^.*const ([a-zA-Z0-9]*) .*$") # matches event name
TRANSITION_STATE_REGEX = re.compile("^.*transit<([a-zA-Z0-9]*)>.*$") # matches state to which sm transitions
TRANSITION_FSM_REGEX = re.compile("^.*tinyfsm::Fsm<([a-zA-Z0-9]*)>::transit.*$") # matches sm which transitions

def gdb_cmd_extract_regex(cmd: str, regex: re.Pattern) -> str:
    match = regex.match(gdb.execute(cmd, to_string=True).split("\n")[0])
    if match:
        return match.group(1)
    else:
        return "error not found"

def handle_sm_event():
    event = gdb_cmd_extract_regex("p event", EVENT_REGEX)
    print(f"send_event({event})")
    gdb.execute("c")
    
def handle_sm_transition():
    sm_name = gdb_cmd_extract_regex("frame", TRANSITION_FSM_REGEX)
    state_name = gdb_cmd_extract_regex("frame", TRANSITION_STATE_REGEX)
    print(f"{sm_name} transitioning to {state_name}")
    gdb.execute("c")

def stop_handler(event):
    if isinstance(event,gdb.SignalEvent):
        print("event stop signal %s" %(event.stop_signal))
        if event.stop_signal == 'SIGINT':
            gdb.execute("c")
    elif isinstance(event,gdb.BreakpointEvent):
        match event.breakpoints[0].location:
            case "send_event":
                handle_sm_event()
            case "transit":
                handle_sm_transition()
            case _:
                print(f"unhandled breakpoint at {event.breakpoints[0].location}")

def main():
    gdb.execute("b transit")
    gdb.execute("b send_event")
    gdb.events.stop.connect (stop_handler)
    gdb.execute("run")

if __name__ == "__main__":
    main()

Run

gdb --command debug_helper.py executable

I thought it might help someone else :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions