1/*
2 * Copyright 2012-2016, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef DEBUG_REPORT_GENERATOR_H
6#define DEBUG_REPORT_GENERATOR_H
7
8
9#include <Looper.h>
10
11#include "Function.h"
12#include "Team.h"
13#include "TeamMemoryBlock.h"
14#include "ValueNodeContainer.h"
15
16
17struct entry_ref;
18class Architecture;
19class AreaInfo;
20class BFile;
21class DebuggerInterface;
22class SemaphoreInfo;
23class StackFrame;
24class Team;
25class Thread;
26class UserInterfaceListener;
27class Value;
28class ValueNode;
29class ValueNodeChild;
30class ValueNodeManager;
31
32
33class DebugReportGenerator : public BLooper, private Team::Listener,
34	private TeamMemoryBlock::Listener, private ValueNodeContainer::Listener,
35	private Function::Listener {
36public:
37								DebugReportGenerator(::Team* team,
38									UserInterfaceListener* listener,
39									DebuggerInterface* interface);
40								~DebugReportGenerator();
41
42			status_t			Init();
43
44	static	DebugReportGenerator* Create(::Team* team,
45									UserInterfaceListener* listener,
46									DebuggerInterface* interface);
47
48	virtual void				MessageReceived(BMessage* message);
49
50private:
51	// Team::Listener
52	virtual	void				ThreadStackTraceChanged(
53									const Team::ThreadEvent& event);
54
55	// TeamMemoryBlock::Listener
56	virtual	void				MemoryBlockRetrieved(TeamMemoryBlock* block);
57	virtual	void				MemoryBlockRetrievalFailed(
58									TeamMemoryBlock* block, status_t result);
59
60	// ValueNodeContainer::Listener
61	virtual	void				ValueNodeValueChanged(ValueNode* node);
62
63	// Function::Listener
64	virtual	void				FunctionSourceCodeChanged(Function* function);
65
66private:
67			status_t			_GenerateReport(const char* outputPath);
68			status_t			_GenerateReportHeader(BFile& _output);
69			status_t			_DumpLoadedImages(BFile& _output);
70			status_t			_DumpAreas(BFile& _output);
71			status_t			_DumpSemaphores(BFile& _output);
72			status_t			_DumpRunningThreads(BFile& _output);
73			status_t			_DumpDebuggedThreadInfo(BFile& _output,
74									::Thread* thread);
75			status_t			_DumpFunctionDisassembly(BFile& _output,
76									target_addr_t instructionPointer);
77			status_t			_DumpStackFrameMemory(BFile& _output,
78									CpuState* state,
79									target_addr_t framePointer,
80									uint8 stackDirection);
81
82			status_t			_ResolveValueIfNeeded(ValueNode* node,
83									StackFrame* frame, int32 maxDepth);
84
85			void				_HandleMemoryBlockRetrieved(
86									TeamMemoryBlock* block, status_t result);
87
88	static	int					_CompareAreas(const AreaInfo* a,
89									const AreaInfo* b);
90	static	int					_CompareImages(const Image* a, const Image* b);
91	static	int					_CompareSemaphores(const SemaphoreInfo* a,
92									const SemaphoreInfo* b);
93	static	int					_CompareThreads(const ::Thread* a,
94									const ::Thread* b);
95private:
96			::Team*				fTeam;
97			Architecture*		fArchitecture;
98			DebuggerInterface*	fDebuggerInterface;
99			sem_id				fTeamDataSem;
100			ValueNodeManager*	fNodeManager;
101			UserInterfaceListener* fListener;
102			ValueNode*			fWaitingNode;
103			TeamMemoryBlock*	fCurrentBlock;
104			status_t			fBlockRetrievalStatus;
105			::Thread*			fTraceWaitingThread;
106			Function*			fSourceWaitingFunction;
107};
108
109#endif // DEBUG_REPORT_GENERATOR_H
110