OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 #ifndef DEBUGGER_CORE_DEBUG_EXECUTION_ENGINE_H_ | |
5 #define DEBUGGER_CORE_DEBUG_EXECUTION_ENGINE_H_ | |
6 | |
7 #include <windows.h> | |
8 #include <deque> | |
9 #include <map> | |
10 #include <string> | |
11 #include "debugger/core/debug_event.h" | |
12 | |
13 namespace debug { | |
14 class IDebuggeeProcess; | |
15 class DebugAPI; | |
16 | |
17 /// ExecutionEngine provides a central control point for the set of | |
18 /// debuggee processes. | |
19 /// | |
20 /// It has methods for process creation, for attaching to the running | |
21 /// processes, to detach from debuggee. Tracks debugee processes as they come | |
22 /// and go. There could be any number of unrelated or related processes all | |
23 /// tracked by single ExecutionEngine instance. | |
24 /// Example: | |
25 /// | |
26 /// debug::DebugApi debug_api; | |
27 /// debug::ExecutionEngine engine(&debug_api); | |
28 /// if (engine.StartProcess("chrome.exe")) { | |
29 /// // debugger event loop | |
30 /// while (true) { | |
31 /// int halted_pid = 0; | |
32 /// engine.WaitForDebugEventAndDispatchIt(20, &halted_pid); | |
33 /// if (0 != halted_pid) { | |
34 /// // Can set breakpoints here, read registers etc. | |
35 /// debug::DebuggeeProcess* halted_proc = engine.GetProcess(halted_pid); | |
36 /// halted_proc->Continue(); //or halted_proc->SingleStep(); | |
37 /// } else { | |
38 /// // Process can be halted (similar to VisualStudio 'BreakAll' command) | |
39 /// std::deque<int> pids; | |
40 /// engine.GetProcessIds(&pids); | |
41 /// engine.GetProcess(pids[0])->Break(); | |
42 /// // Debuggee process will receive breakpoint exception. | |
43 /// } | |
44 /// } | |
45 /// } | |
46 /// | |
47 /// Note: all methods shall be called from one thread, this is a limitation | |
48 /// of Windows debug API, here's links to Microsoft documentation: | |
49 /// http://msdn.microsoft.com/en-us/library/ms681423%28v=VS.85%29.aspx | |
50 /// http://msdn.microsoft.com/en-us/library/ms681675%28v=vs.85%29.aspx | |
51 class ExecutionEngine { | |
52 public: | |
53 explicit ExecutionEngine(DebugAPI* debug_api); | |
54 virtual ~ExecutionEngine(); | |
55 | |
56 /// Starts debugee process, it will be attached to debugger. | |
57 /// @param[in] cmd the command line to be executed. | |
58 /// @param[in] work_dir the full path to the working directory for | |
59 /// the process, or NULL. | |
60 /// @return false if starting process fails | |
61 virtual bool StartProcess(const char* cmd, const char* work_dir); | |
62 | |
63 /// Enables a debugger to attach to an active process and debug it. | |
64 /// @param[in] pid process id for the process to be debugged. | |
65 /// @return false if attaching to the process fails | |
66 virtual bool AttachToProcess(int pid); | |
67 | |
68 /// Stops the debugger from debugging all process that it's tracking now. | |
69 /// Debuggee processes are not killed. | |
70 /// Individual processes can be detached by calling | |
71 /// IDebeggeeProcess::Detach(). | |
72 virtual void DetachAll(); | |
73 | |
74 /// @param[in] wait_ms number of milliseconds to wait for a debugging event. | |
75 /// @param[out] halted_pid pointer to integer that receives a halted | |
76 /// process id or 0 if no process got halted as a result of this method call. | |
77 /// @return true if debug event is received (and dispatched). | |
78 /// Current implementation halts processes on all debug events, | |
79 /// with single exception - when processing SINGLE_STEP in the process of | |
80 /// continuing from breakpoinnt (see DebuggeeThread for more information). | |
81 virtual bool WaitForDebugEventAndDispatchIt(int wait_ms, int* halted_pid); | |
82 | |
83 /// Initiates termination of all traced processes and waits | |
84 /// until all debugee processes terminates. | |
85 /// @param wait_ms number of milliseconds to wait for processes to stop. | |
86 virtual void Stop(int wait_ms); | |
87 | |
88 /// @param pid process id | |
89 /// @return pointer to traced process object, or NULL if there's no process | |
90 /// with requested pid. | |
91 /// ExecutionEngine owns returned process, caller shall not delete it. | |
92 virtual IDebuggeeProcess* GetProcess(int pid); | |
93 | |
94 /// @param[out] pids list of all tracked processes | |
95 virtual void GetProcessIds(std::deque<int>* pids) const; | |
96 | |
97 /// @return reference to latest received debug event | |
98 DebugEvent& debug_event() { return debug_event_; } | |
99 | |
100 protected: | |
101 /// Handler of debug events. | |
102 /// @param[in] debug_event debug event received from debuggee process | |
103 /// @return a halted process id or 0 if no process got halted as a result | |
104 /// of this method call. | |
105 virtual int OnDebugEvent(const DEBUG_EVENT& debug_event); | |
106 | |
107 DebugAPI& debug_api() { return debug_api_; } | |
108 | |
109 /// Factory method. | |
110 /// Used in unit tests, to provide a way to create mock debugee process. | |
111 /// param[in] pid process id | |
112 /// param[in] handle a handle to the process | |
113 /// param[in] a handle to the process's image file. | |
114 /// @return newly created debugee process object | |
115 virtual IDebuggeeProcess* CreateDebuggeeProcess(int pid, | |
116 HANDLE handle, | |
117 HANDLE file_handle); | |
118 /// Delets dead processe objects. | |
119 virtual void RemoveDeadProcesses(); | |
120 | |
121 typedef std::deque<IDebuggeeProcess*>::iterator ProcessIter; | |
122 typedef std::deque<IDebuggeeProcess*>::const_iterator ProcessConstIter; | |
123 | |
124 private: | |
125 DebugEvent debug_event_; | |
126 std::deque<IDebuggeeProcess*> processes_; | |
127 DebugAPI& debug_api_; | |
128 | |
129 ExecutionEngine(const ExecutionEngine&); // DISALLOW_COPY_AND_ASSIGN | |
130 void operator=(const ExecutionEngine&); | |
131 }; | |
132 } // namespace debug | |
133 | |
134 #endif // DEBUGGER_CORE_DEBUG_EXECUTION_ENGINE_H_ | |
135 | |
OLD | NEW |