A method and apparatus for capturing and logging activities of a state machine prior to error. The state machine receives a packet and processes the packet. A state machine monitor is provided for each state machine. The state machine monitor logs activities of its associated state machine in a trace buffer when the state machine changes its state. The changes of the states in the state machine are traced to create a record in the trace buffer. Optionally, a snooper may be provided to determine whether a predetermined condition exists in the received packet. The snooper attaches a trigger tag to the received packet when the predetermined condition exists. The state machine monitor stops tracing the changes of the states of the state machine when the state machine monitor detects the trigger tag in a packet forwarded to its associated state machine.