1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef THREAD_HANDLER_H
6#define THREAD_HANDLER_H
7
8
9#include <Referenceable.h>
10#include <util/OpenHashTable.h>
11
12#include "Breakpoint.h"
13#include "DebugEvent.h"
14#include "ImageDebugInfoProvider.h"
15#include "Thread.h"
16
17
18class BreakpointManager;
19class DebuggerInterface;
20class StackFrame;
21class Statement;
22class Worker;
23
24
25class ThreadHandler : public BReferenceable, private ImageDebugInfoProvider,
26	private BreakpointClient {
27public:
28								ThreadHandler(Thread* thread, Worker* worker,
29									DebuggerInterface* debuggerInterface,
30									BreakpointManager* breakpointManager);
31								~ThreadHandler();
32
33			void				Init();
34
35			thread_id			ThreadID() const	{ return fThread->ID(); }
36			Thread*				GetThread() const	{ return fThread; }
37
38			status_t			SetBreakpointAndRun(target_addr_t address);
39									// team lock held
40
41			// All Handle*() methods are invoked in team debugger thread,
42			// looper lock held.
43			bool				HandleThreadDebugged(
44									ThreadDebuggedEvent* event);
45			bool				HandleDebuggerCall(
46									DebuggerCallEvent* event);
47			bool				HandleBreakpointHit(
48									BreakpointHitEvent* event);
49			bool				HandleWatchpointHit(
50									WatchpointHitEvent* event);
51			bool				HandleSingleStep(
52									SingleStepEvent* event);
53			bool				HandleExceptionOccurred(
54									ExceptionOccurredEvent* event);
55
56			void				HandleThreadAction(uint32 action);
57
58			void				HandleThreadStateChanged();
59			void				HandleCpuStateChanged();
60			void				HandleStackTraceChanged();
61
62private:
63	// ImageDebugInfoProvider
64	virtual	status_t			GetImageDebugInfo(Image* image,
65									ImageDebugInfo*& _info);
66
67			bool				_HandleThreadStopped(CpuState* cpuState,
68									uint32 stoppedReason,
69									const BString& stoppedReasonInfo
70										= BString());
71
72			void				_SetThreadState(uint32 state,
73									CpuState* cpuState, uint32 stoppedReason,
74									const BString& stoppedReasonInfo);
75
76			Statement*			_GetStatementAtInstructionPointer(
77									StackFrame* frame);
78
79			void				_StepFallback();
80			bool				_DoStepOver(CpuState* cpuState);
81
82			status_t			_InstallTemporaryBreakpoint(
83									target_addr_t address);
84			void				_UninstallTemporaryBreakpoint();
85			void				_ClearContinuationState();
86			void				_RunThread(target_addr_t instructionPointer);
87			void				_SingleStepThread(
88									target_addr_t instructionPointer);
89
90
91			bool				_HandleBreakpointHitStep(CpuState* cpuState);
92			bool				_HandleSingleStepStep(CpuState* cpuState);
93
94private:
95			Thread*				fThread;
96			Worker*				fWorker;
97			DebuggerInterface*	fDebuggerInterface;
98			BreakpointManager*	fBreakpointManager;
99			uint32				fStepMode;
100			Statement*			fStepStatement;
101			target_addr_t		fBreakpointAddress;
102			target_addr_t		fPreviousInstructionPointer;
103			target_addr_t		fPreviousFrameAddress;
104			bool				fSingleStepping;
105
106public:
107			ThreadHandler*		fNext;
108};
109
110
111struct ThreadHandlerHashDefinition {
112	typedef thread_id		KeyType;
113	typedef	ThreadHandler	ValueType;
114
115	size_t HashKey(thread_id key) const
116	{
117		return (size_t)key;
118	}
119
120	size_t Hash(const ThreadHandler* value) const
121	{
122		return HashKey(value->ThreadID());
123	}
124
125	bool Compare(thread_id key, ThreadHandler* value) const
126	{
127		return value->ThreadID() == key;
128	}
129
130	ThreadHandler*& GetLink(ThreadHandler* value) const
131	{
132		return value->fNext;
133	}
134};
135
136typedef BOpenHashTable<ThreadHandlerHashDefinition> ThreadHandlerTable;
137
138
139#endif	// THREAD_HANDLER_H
140