1 #if !defined(INT_PROCESS_H_)
4 #include "proccontrol/h/Process.h"
5 #include "proccontrol/h/PCErrors.h"
6 #include "proccontrol/h/Event.h"
8 #include "dynutil/h/dyn_regs.h"
9 #include "common/h/dthread.h"
18 using namespace Dyninst;
19 using namespace ProcControlAPI;
26 typedef dyn_detail::boost::shared_ptr<int_iRPC> int_iRPC_ptr;
27 typedef std::map<Dyninst::MachRegister, std::pair<unsigned int, unsigned int> > dynreg_to_user_t;
29 typedef std::list<int_iRPC_ptr> rpc_list_t;
31 class installed_breakpoint;
38 typedef mem_state* ptr;
39 mem_state(int_process *proc);
40 mem_state(mem_state &m, int_process *proc);
43 void addProc(int_process *p);
44 void rmProc(int_process *p, bool &should_clean);
46 std::set<int_process *> procs;
47 std::set<int_library *> libs;
48 std::map<Dyninst::Address, installed_breakpoint *> breakpoints;
49 std::map<Dyninst::Address, unsigned long> inf_malloced_memory;
54 friend class Dyninst::ProcControlAPI::Process;
56 int_process(Dyninst::PID p, std::string e, std::vector<std::string> a);
57 int_process(Dyninst::PID pid_, int_process *p);
59 static int_process *createProcess(Dyninst::PID p, std::string e);
60 static int_process *createProcess(std::string e, std::vector<std::string> a);
61 static int_process *createProcess(Dyninst::PID pid_, int_process *p);
62 virtual ~int_process();
65 virtual bool plat_create() = 0;
66 virtual bool post_create();
69 virtual bool plat_attach() = 0;
71 virtual bool post_attach();
76 virtual bool plat_forked() = 0;
77 virtual bool post_forked();
82 virtual bool plat_execed() = 0;
83 virtual bool plat_detach() = 0;
84 virtual bool plat_terminate(bool &needs_sync) = 0;
86 virtual bool needIndividualThreadAttach() = 0;
87 virtual bool getThreadLWPs(std::vector<Dyninst::LWP> &lwps);
89 static bool multi_attach(std::vector<int_process *> &pids);
90 bool waitfor_startup();
92 void setPid(Dyninst::PID pid);
93 int_thread *findStoppedThread();
98 neonatal_intermediate,
103 State getState() const;
104 void setState(State s);
106 Dyninst::PID getPid() const;
107 int_threadPool *threadPool() const;
109 Process::ptr proc() const;
110 mem_state::ptr memory() const;
112 bool detach(bool &should_clean);
113 bool terminate(bool &needs_sync);
114 void updateSyncState(Event::ptr ev, bool gen);
115 virtual Dyninst::Architecture getTargetArch() = 0;
116 virtual unsigned getTargetPageSize() = 0;
117 virtual Dyninst::Address mallocExecMemory(unsigned size);
118 virtual Dyninst::Address plat_mallocExecMemory(Dyninst::Address min, unsigned size) = 0;
119 virtual void freeExecMemory(Dyninst::Address addr);
121 static bool waitAndHandleEvents(bool block);
122 static bool waitAndHandleForProc(bool block, int_process *proc, bool &proc_exited);
123 static const char *stateName(State s);
124 void initializeProcess(Process::ptr p);
126 void setExitCode(int c);
127 void setCrashSignal(int s);
128 bool getExitCode(int &c);
129 bool getCrashSignal(int &s);
131 virtual bool plat_individualRegAccess() = 0;
133 void addProcStopper(Event::ptr ev);
134 Event::ptr removeProcStopper();
135 bool hasQueuedProcStoppers() const;
137 int getAddressWidth();
138 HandlerPool *handlerPool() const;
140 bool addBreakpoint(Dyninst::Address addr, int_breakpoint *bp);
141 bool rmBreakpoint(Dyninst::Address addr, int_breakpoint *bp);
142 installed_breakpoint *getBreakpoint(Dyninst::Address addr);
144 Dyninst::Address infMalloc(unsigned long size, bool use_addr = false, Dyninst::Address addr = 0x0);
145 bool infFree(Dyninst::Address addr);
147 bool readMem(void *local, Dyninst::Address remote, size_t size);
148 bool writeMem(void *local, Dyninst::Address remote, size_t size);
149 virtual bool plat_readMem(int_thread *thr, void *local,
150 Dyninst::Address remote, size_t size) = 0;
151 virtual bool plat_writeMem(int_thread *thr, void *local,
152 Dyninst::Address remote, size_t size) = 0;
154 virtual bool independentLWPControl() = 0;
155 static bool isInCB();
156 static void setInCB(bool b);
158 int_library *getLibraryByName(std::string s) const;
159 size_t numLibs() const;
160 virtual bool refresh_libraries(std::set<int_library *> &added_libs,
161 std::set<int_library *> &rmd_libs) = 0;
162 virtual bool initLibraryMechanism() = 0;
164 bool forceGeneratorBlock() const;
165 void setForceGeneratorBlock(bool b);
167 std::string getExecutable() const;
168 static bool isInCallback();
170 static int_process *in_waitHandleProc;
174 std::string executable;
175 std::vector<std::string> argv;
176 Dyninst::Architecture arch;
177 int_threadPool *threadpool;
178 Process::ptr up_proc;
179 HandlerPool *handlerpool;
186 static bool in_callback;
188 std::map<Dyninst::Address, unsigned> exec_mem_cache;
189 std::queue<Event::ptr> proc_stoppers;
192 class int_registerPool
196 int_registerPool(const int_registerPool &c);
199 typedef std::map<Dyninst::MachRegister, Dyninst::MachRegisterVal> reg_map_t;
204 typedef reg_map_t::iterator iterator;
205 typedef reg_map_t::const_iterator const_iterator;
208 class thread_exitstate
212 Dyninst::THR_ID thr_id;
213 Process::ptr proc_ptr;
227 * ON THREADING STATES:
229 * Each thread has four different states, which mostly monitor running/stopped
231 * GeneratorState - Thread state as seen by the generator object
232 * HandlerState - Thread state as seen by the handler object
233 * InternalState - Target thread state desired by int_* layer
234 * UserState - Target Thread state as desired by the user
236 * The GeneratorState and HandlerState represent an event as it moves through the
237 * system. For example, an event that stops the thread may first appear
238 * in the Generator object, and move the GeneratorState to 'stopped'. It is then
239 * seen by the handler, which moves the HandlerState to 'stopped'. If the thread is
240 * then continued, the HandlerState and GeneratorStates will go back to 'running'.
241 * These are primarily seperated to prevent race conditions with the handler and
242 * generators modifying the same variable, since they run in seperate threads.
244 * The InternalState and UserState are used to make policy decisions about whether
245 * a thread should be running or stopped. For example, after handling an event
246 * we may check the UserState to see if the user wants us to run/stop a thread.
247 * The InternalState usually matches the UserState, but may override it for
248 * stop/run decisions in a few scenarios. For example, if the user posts a iRPC
249 * to a running process (UserState = running) we may have to temporarily stop the
250 * process to install the iRPC. We don't want to change the UserState, since the user
251 * still wants the process running, so we set the InternalState to stopped while we
252 * set up the iRPC, and then return it to the UserState value when the iRPC is ready.
254 * There are a couple of important assertions about the relationship between these thread
256 * (GeneratorState == running) implies (HandlerState == running)
257 * (HandlerState == running) implies (InternalState == running)
258 * (InternalState == stopped) implies (HandlerState == stopped)
259 * (HandlerState == stopped) implies (GeneratorState == stopped)
263 friend class int_threadPool;
265 int_thread(int_process *p, Dyninst::THR_ID t, Dyninst::LWP l);
266 static int_thread *createThreadPlat(int_process *proc,
267 Dyninst::THR_ID thr_id,
271 static int_thread *createThread(int_process *proc,
272 Dyninst::THR_ID thr_id,
275 Process::ptr proc() const;
276 int_process *llproc() const;
278 Dyninst::THR_ID getTid() const;
279 Dyninst::LWP getLWP() const;
283 neonatal_intermediate,
290 //State management, see above comment on states
291 State getHandlerState() const;
292 State getUserState() const;
293 State getGeneratorState() const;
294 State getInternalState() const;
295 bool setHandlerState(State s);
296 bool setUserState(State s);
297 bool setGeneratorState(State s);
298 bool setInternalState(State s);
299 void restoreInternalState(bool sync = true);
300 void desyncInternalState();
305 bool intStop(bool sync = true);
308 void setContSignal(int sig);
309 bool contWithSignal(int sigOverride = -1);
310 virtual bool plat_cont() = 0;
311 virtual bool plat_stop() = 0;
312 void setPendingUserStop(bool b);
313 bool hasPendingUserStop() const;
314 void setPendingStop(bool b);
315 bool hasPendingStop() const;
318 bool singleStepMode() const;
319 void setSingleStepMode(bool s);
320 bool singleStepUserMode() const;
321 void setSingleStepUserMode(bool s);
322 bool singleStep() const;
323 void markClearingBreakpoint(installed_breakpoint *bp);
324 installed_breakpoint *isClearingBreakpoint();
327 void addPostedRPC(int_iRPC_ptr rpc_);
328 rpc_list_t *getPostedRPCs();
329 bool hasPostedRPCs();
330 void setRunningRPC(int_iRPC_ptr rpc_);
331 void clearRunningRPC();
332 int_iRPC_ptr runningRPC() const;
333 bool saveRegsForRPC();
334 bool restoreRegsForRPC(bool clear);
335 bool hasSavedRPCRegs();
336 bool runningInternalRPC() const;
337 void incSyncRPCCount();
338 void decSyncRPCCount();
340 int_iRPC_ptr nextPostedIRPC() const;
341 bool handleNextPostedIRPC(bool block);
342 int_iRPC_ptr hasRunningProcStopperRPC() const;
344 //Register Management
345 bool getAllRegisters(int_registerPool &pool);
346 bool getRegister(Dyninst::MachRegister reg, Dyninst::MachRegisterVal &val);
347 bool setAllRegisters(int_registerPool &pool);
348 bool setRegister(Dyninst::MachRegister reg, Dyninst::MachRegisterVal val);
349 virtual bool plat_getAllRegisters(int_registerPool &pool) = 0;
350 virtual bool plat_getRegister(Dyninst::MachRegister reg,
351 Dyninst::MachRegisterVal &val) = 0;
352 virtual bool plat_setAllRegisters(int_registerPool &pool) = 0;
353 virtual bool plat_setRegister(Dyninst::MachRegister reg,
354 Dyninst::MachRegisterVal val) = 0;
357 virtual bool attach() = 0;
358 Thread::ptr thread();
360 virtual ~int_thread();
361 static const char *stateStr(int_thread::State s);
366 Thread::ptr up_thread;
370 State generator_state;
371 State internal_state;
373 int_registerPool cached_regpool;
375 int_iRPC_ptr running_rpc;
376 rpc_list_t posted_rpcs;
377 int_registerPool rpc_regs;
378 unsigned sync_rpc_count;
379 bool pending_user_stop;
381 int num_locked_stops;
382 bool user_single_step;
384 installed_breakpoint *clearing_breakpoint;
386 bool setAnyState(int_thread::State *from, int_thread::State to);
395 bool stop(bool user_stop, bool sync);
396 bool cont(bool user_cont);
397 stopcont_ret_t stop(bool user_stop);
398 stopcont_ret_t cont(bool user_cont, bool has_proc_lock);
401 class int_threadPool {
402 friend class Dyninst::ProcControlAPI::ThreadPool;
403 friend class Dyninst::ProcControlAPI::ThreadPool::iterator;
405 std::vector<int_thread *> threads;
406 std::vector<Thread::ptr> hl_threads;
407 std::map<Dyninst::LWP, int_thread *> thrds_by_lwp;
409 int_thread *initial_thread;
413 int_threadPool(int_process *p);
416 void setInitialThread(int_thread *thrd);
417 void addThread(int_thread *thrd);
418 void rmThread(int_thread *thrd);
419 void restoreInternalState(bool sync);
420 void desyncInternalState();
423 typedef std::vector<int_thread *>::iterator iterator;
424 iterator begin() { return threads.begin(); }
425 iterator end() { return threads.end(); }
427 unsigned size() const;
429 int_process *proc() const;
430 ThreadPool *pool() const;
432 int_thread *findThreadByLWP(Dyninst::LWP lwp);
433 int_thread *initialThread() const;
438 bool intStop(bool sync = true);
441 bool cont(bool user_cont);
442 bool stop(bool user_stop, bool sync);
447 friend class Dyninst::ProcControlAPI::LibraryPool;
448 friend class Dyninst::ProcControlAPI::LibraryPool::iterator;
449 friend class Dyninst::ProcControlAPI::LibraryPool::const_iterator;
452 Dyninst::Address load_address;
453 Dyninst::Address data_load_address;
458 int_library(std::string n, Dyninst::Address load_addr);
459 int_library(std::string n, Dyninst::Address load_addr,
460 Dyninst::Address data_load_addr);
461 int_library(int_library *l);
463 std::string getName();
464 Dyninst::Address getAddr();
465 Dyninst::Address getDataAddr();
468 void setMark(bool b);
469 bool isMarked() const;
471 Library::ptr getUpPtr() const;
476 friend class installed_breakpoint;
478 Breakpoint::weak_ptr up_bp;
480 bool isCtrlTransfer_;
483 int_breakpoint(Breakpoint::ptr up);
484 int_breakpoint(Dyninst::Address to, Breakpoint::ptr up);
487 bool isCtrlTransfer() const;
488 Dyninst::Address toAddr() const;
489 Dyninst::Address getAddress(int_process *p) const;
490 void *getData() const;
491 void setData(void *v);
492 Breakpoint::weak_ptr upBreakpoint() const;
495 class installed_breakpoint
497 friend class Dyninst::ProcControlAPI::EventBreakpoint;
499 mem_state::ptr memory;
500 std::set<int_breakpoint *> bps;
501 std::set<Breakpoint::ptr> hl_bps;
503 unsigned char buffer[4]; //At least as large as any arch's trap instruction
507 Dyninst::Address addr;
509 installed_breakpoint(mem_state::ptr memory_, Dyninst::Address addr_);
510 installed_breakpoint(mem_state::ptr memory_, const installed_breakpoint *ip);
511 ~installed_breakpoint();
513 bool addBreakpoint(int_process *proc, int_breakpoint *bp);
514 bool rmBreakpoint(int_process *proc, int_breakpoint *bp, bool &empty);
515 bool install(int_process *proc);
516 bool uninstall(int_process *proc);
517 bool plat_install(int_process *proc, bool should_save);
519 bool suspend(int_process *proc);
520 bool resume(int_process *proc);
522 bool isInstalled() const;
523 Dyninst::Address getAddr() const;
527 friend int_notify *notify();
528 friend EventNotify *Dyninst::ProcControlAPI::evNotify();
530 static int_notify *the_notify;
531 EventNotify *up_notify;
532 std::set<EventNotify::notify_cb_t> cbs;
545 void registerCB(EventNotify::notify_cb_t cb);
546 void removeCB(EventNotify::notify_cb_t cb);
550 int_notify *notify();
552 extern void setGeneratorThread(long t);
553 void setHandlerThread(long t);
554 bool isGeneratorThread();
555 bool isHandlerThread();
556 HandlerPool *createDefaultHandlerPool();
557 HandlerPool *plat_createDefaultHandlerPool(HandlerPool *hpool);
560 friend MTManager *mt();
563 static MTManager *mt_;
564 DThread evhandler_thread;
565 CondVar pending_event_lock;
567 bool have_queued_events;
570 Process::thread_mode_t threadMode;
572 void evhandler_main();
573 static void evhandler_main_wrapper(void *);
574 void eventqueue_cb();
575 static void eventqueue_cb_wrapper();
578 static const Process::thread_mode_t default_thread_mode = Process::HandlerThreading;
588 bool handlerThreading();
589 Process::thread_mode_t getThreadMode();
590 bool setThreadMode(Process::thread_mode_t tm, bool init = false);
593 inline MTManager *mt() {
594 return MTManager::mt_;
602 typedef enum {allow_init} initialize;
603 typedef enum {allow_generator} generator;
604 typedef enum {nocb, deliver_callbacks} callbacks;
605 MTLock(initialize, callbacks c = nocb)
607 should_unlock = true;
608 if (!MTManager::mt_) {
609 MTManager::mt_ = new MTManager();
610 if (MTManager::default_thread_mode == Process::HandlerThreading ||
611 MTManager::default_thread_mode == Process::CallbackThreading) {
614 mt()->setThreadMode(MTManager::default_thread_mode, true);
616 else if (mt()->handlerThreading()) {
618 if (c == deliver_callbacks &&
619 MTManager::default_thread_mode == Process::HandlerThreading &&
620 notify()->hasEvents())
622 pthrd_printf("MTLock triggered event handling\n");
623 int_process::waitAndHandleEvents(false);
624 pthrd_printf("MTLock triggered event handling finished\n");
631 if (isGeneratorThread()) {
632 should_unlock = false;
635 should_unlock = true;
636 if (mt()->handlerThreading()) {
643 assert(!isGeneratorThread());
644 should_unlock = true;
645 if (mt()->handlerThreading()) {
647 if (notify()->hasEvents() &&
648 MTManager::default_thread_mode == Process::HandlerThreading)
650 pthrd_printf("MTLock triggered event handling\n");
651 int_process::waitAndHandleEvents(false);
652 pthrd_printf("MTLock triggered event handling finished\n");
659 assert(!isGeneratorThread());
660 should_unlock = true;
661 if (mt()->handlerThreading())
666 if (should_unlock && mt()->handlerThreading())