MCPcopy Create free account
hub / github.com/ELMERIKH/PyinMemoryPE / detach

Method detach

windows/debug/debugger.py:96–149  ·  view source on GitHub ↗

Detach from all debugged processes or process ``target``

(self, target=None)

Source from the content-addressed store, hash-verified

94 return cls(target)
95
96 def detach(self, target=None):
97 """Detach from all debugged processes or process ``target``"""
98 if target is None:
99 targets = self.processes.values()
100 if not targets:
101 # We are not following any process
102 # maybe a attach/detach with Debugger.loop
103 # Just detach from the initial target
104 if self.target:
105 tpid = self.target.pid
106 self.target = None # Remove ref to process -> GC -> CloseHandle -> process is destroyed
107 windows.winproxy.DebugActiveProcessStop(tpid)
108 return
109 for proc in list(targets):
110 self.detach(proc)
111 del targets
112 return
113 if not isinstance(target, WinProcess):
114 raise ValueError("Detach accept only WinProcess")
115
116 self.disable_all_memory_breakpoints(target)
117 for bp in list(self.breakpoints[target.pid].values()):
118 if not bp.apply_to_target(target):
119 target_threads = [t for t in target.threads if t.tid in self.threads]
120 bp_threads = []
121 # TODO: clean API tu request HXBP on a thread
122 for t in target_threads:
123 t_bps = [pos for pos, hbp in self._hardware_breakpoint[t.tid].items() if hbp == bp]
124 if t_bps:
125 bp_threads.append(t)
126 self.del_bp(bp, bp_threads)
127 else:
128 self.del_bp(bp, [target])
129
130 for thread in [t for t in target.threads if t.tid in self.threads]:
131 del self._explicit_single_step[thread.tid]
132 del self._breakpoint_to_reput[thread.tid]
133 del self.threads[thread.tid]
134 ctx = thread.context
135 if ctx.EEFlags.TF: # Remove TRAPFlag before detaching (or it will lead to a crash)
136 ctx.EEFlags.TF = 0
137 thread.set_context(ctx)
138 del self.processes[target.pid]
139 del self._watched_pages[target.pid]
140 del self._module_by_process[target.pid]
141
142 if target is self.current_process: # Bug if CTRL+C and current_process changed ?
143 self._finish_debug_event(self._current_debug_event, DBG_CONTINUE)
144 self.current_process = None
145 self.current_thread = None
146
147 if self.target and target.pid == self.target.pid:
148 self.target = None
149 windows.winproxy.DebugActiveProcessStop(target.pid)
150
151 def _killed_in_action(self):
152 """Return ``True`` if current process have been detached by user callback"""

Callers

nothing calls this directly

Calls 7

del_bpMethod · 0.95
_finish_debug_eventMethod · 0.95
valuesMethod · 0.45
apply_to_targetMethod · 0.45
itemsMethod · 0.45
set_contextMethod · 0.45

Tested by

no test coverage detected