File: | build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp |
Warning: | line 141, column 5 2nd function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- NativeProcessLinux.cpp --------------------------------------------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | ||||
9 | #include "NativeProcessLinux.h" | |||
10 | ||||
11 | #include <cerrno> | |||
12 | #include <cstdint> | |||
13 | #include <cstring> | |||
14 | #include <unistd.h> | |||
15 | ||||
16 | #include <fstream> | |||
17 | #include <mutex> | |||
18 | #include <sstream> | |||
19 | #include <string> | |||
20 | #include <unordered_map> | |||
21 | ||||
22 | #include "NativeThreadLinux.h" | |||
23 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" | |||
24 | #include "Plugins/Process/Utility/LinuxProcMaps.h" | |||
25 | #include "Procfs.h" | |||
26 | #include "lldb/Core/ModuleSpec.h" | |||
27 | #include "lldb/Host/Host.h" | |||
28 | #include "lldb/Host/HostProcess.h" | |||
29 | #include "lldb/Host/ProcessLaunchInfo.h" | |||
30 | #include "lldb/Host/PseudoTerminal.h" | |||
31 | #include "lldb/Host/ThreadLauncher.h" | |||
32 | #include "lldb/Host/common/NativeRegisterContext.h" | |||
33 | #include "lldb/Host/linux/Host.h" | |||
34 | #include "lldb/Host/linux/Ptrace.h" | |||
35 | #include "lldb/Host/linux/Uio.h" | |||
36 | #include "lldb/Host/posix/ProcessLauncherPosixFork.h" | |||
37 | #include "lldb/Symbol/ObjectFile.h" | |||
38 | #include "lldb/Target/Process.h" | |||
39 | #include "lldb/Target/Target.h" | |||
40 | #include "lldb/Utility/LLDBAssert.h" | |||
41 | #include "lldb/Utility/LLDBLog.h" | |||
42 | #include "lldb/Utility/State.h" | |||
43 | #include "lldb/Utility/Status.h" | |||
44 | #include "lldb/Utility/StringExtractor.h" | |||
45 | #include "llvm/ADT/ScopeExit.h" | |||
46 | #include "llvm/Support/Errno.h" | |||
47 | #include "llvm/Support/FileSystem.h" | |||
48 | #include "llvm/Support/Threading.h" | |||
49 | ||||
50 | #include <linux/unistd.h> | |||
51 | #include <sys/socket.h> | |||
52 | #include <sys/syscall.h> | |||
53 | #include <sys/types.h> | |||
54 | #include <sys/user.h> | |||
55 | #include <sys/wait.h> | |||
56 | ||||
57 | #ifdef __aarch64__ | |||
58 | #include <asm/hwcap.h> | |||
59 | #include <sys/auxv.h> | |||
60 | #endif | |||
61 | ||||
62 | // Support hardware breakpoints in case it has not been defined | |||
63 | #ifndef TRAP_HWBKPTTRAP_HWBKPT | |||
64 | #define TRAP_HWBKPTTRAP_HWBKPT 4 | |||
65 | #endif | |||
66 | ||||
67 | #ifndef HWCAP2_MTE(1 << 18) | |||
68 | #define HWCAP2_MTE(1 << 18) (1 << 18) | |||
69 | #endif | |||
70 | ||||
71 | using namespace lldb; | |||
72 | using namespace lldb_private; | |||
73 | using namespace lldb_private::process_linux; | |||
74 | using namespace llvm; | |||
75 | ||||
76 | // Private bits we only need internally. | |||
77 | ||||
78 | static bool ProcessVmReadvSupported() { | |||
79 | static bool is_supported; | |||
80 | static llvm::once_flag flag; | |||
81 | ||||
82 | llvm::call_once(flag, [] { | |||
83 | Log *log = GetLog(POSIXLog::Process); | |||
84 | ||||
85 | uint32_t source = 0x47424742; | |||
86 | uint32_t dest = 0; | |||
87 | ||||
88 | struct iovec local, remote; | |||
89 | remote.iov_base = &source; | |||
90 | local.iov_base = &dest; | |||
91 | remote.iov_len = local.iov_len = sizeof source; | |||
92 | ||||
93 | // We shall try if cross-process-memory reads work by attempting to read a | |||
94 | // value from our own process. | |||
95 | ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); | |||
96 | is_supported = (res == sizeof(source) && source == dest); | |||
97 | if (is_supported) | |||
98 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Detected kernel support for process_vm_readv syscall. " "Fast memory reads enabled."); } while (0) | |||
99 | "Detected kernel support for process_vm_readv syscall. "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Detected kernel support for process_vm_readv syscall. " "Fast memory reads enabled."); } while (0) | |||
100 | "Fast memory reads enabled.")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Detected kernel support for process_vm_readv syscall. " "Fast memory reads enabled."); } while (0); | |||
101 | else | |||
102 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "syscall process_vm_readv failed (error: {0}). Fast memory " "reads disabled.", llvm::sys::StrError()); } while (0) | |||
103 | "syscall process_vm_readv failed (error: {0}). Fast memory "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "syscall process_vm_readv failed (error: {0}). Fast memory " "reads disabled.", llvm::sys::StrError()); } while (0) | |||
104 | "reads disabled.",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "syscall process_vm_readv failed (error: {0}). Fast memory " "reads disabled.", llvm::sys::StrError()); } while (0) | |||
105 | llvm::sys::StrError())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "syscall process_vm_readv failed (error: {0}). Fast memory " "reads disabled.", llvm::sys::StrError()); } while (0); | |||
106 | }); | |||
107 | ||||
108 | return is_supported; | |||
109 | } | |||
110 | ||||
111 | static void MaybeLogLaunchInfo(const ProcessLaunchInfo &info) { | |||
112 | Log *log = GetLog(POSIXLog::Process); | |||
113 | if (!log) | |||
114 | return; | |||
115 | ||||
116 | if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO0)) | |||
117 | LLDB_LOG(log, "setting STDIN to '{0}'", action->GetFileSpec())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "setting STDIN to '{0}'", action->GetFileSpec( )); } while (0); | |||
118 | else | |||
119 | LLDB_LOG(log, "leaving STDIN as is")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "leaving STDIN as is"); } while (0); | |||
120 | ||||
121 | if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO1)) | |||
122 | LLDB_LOG(log, "setting STDOUT to '{0}'", action->GetFileSpec())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "setting STDOUT to '{0}'", action->GetFileSpec ()); } while (0); | |||
123 | else | |||
124 | LLDB_LOG(log, "leaving STDOUT as is")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "leaving STDOUT as is"); } while (0); | |||
125 | ||||
126 | if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO2)) | |||
127 | LLDB_LOG(log, "setting STDERR to '{0}'", action->GetFileSpec())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "setting STDERR to '{0}'", action->GetFileSpec ()); } while (0); | |||
128 | else | |||
129 | LLDB_LOG(log, "leaving STDERR as is")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "leaving STDERR as is"); } while (0); | |||
130 | ||||
131 | int i = 0; | |||
132 | for (const char **args = info.GetArguments().GetConstArgumentVector(); *args; | |||
133 | ++args, ++i) | |||
134 | LLDB_LOG(log, "arg {0}: '{1}'", i, *args)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "arg {0}: '{1}'", i, *args); } while (0); | |||
135 | } | |||
136 | ||||
137 | static void DisplayBytes(StreamString &s, void *bytes, uint32_t count) { | |||
138 | uint8_t *ptr = (uint8_t *)bytes; | |||
139 | const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES20, count); | |||
140 | for (uint32_t i = 0; i < loop_count; i++) { | |||
141 | s.Printf("[%x]", *ptr); | |||
| ||||
142 | ptr++; | |||
143 | } | |||
144 | } | |||
145 | ||||
146 | static void PtraceDisplayBytes(int &req, void *data, size_t data_size) { | |||
147 | Log *log = GetLog(POSIXLog::Ptrace); | |||
148 | if (!log
| |||
149 | return; | |||
150 | StreamString buf; | |||
151 | ||||
152 | switch (req) { | |||
153 | case PTRACE_POKETEXT: { | |||
154 | DisplayBytes(buf, &data, 8); | |||
155 | LLDB_LOGV(log, "PTRACE_POKETEXT {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_POKETEXT {0}", buf.GetData()); } while (0); | |||
156 | break; | |||
157 | } | |||
158 | case PTRACE_POKEDATA: { | |||
159 | DisplayBytes(buf, &data, 8); | |||
160 | LLDB_LOGV(log, "PTRACE_POKEDATA {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_POKEDATA {0}", buf.GetData()); } while (0); | |||
161 | break; | |||
162 | } | |||
163 | case PTRACE_POKEUSER: { | |||
164 | DisplayBytes(buf, &data, 8); | |||
165 | LLDB_LOGV(log, "PTRACE_POKEUSER {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_POKEUSER {0}", buf.GetData()); } while (0); | |||
166 | break; | |||
167 | } | |||
168 | case PTRACE_SETREGS13: { | |||
169 | DisplayBytes(buf, data, data_size); | |||
170 | LLDB_LOGV(log, "PTRACE_SETREGS {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_SETREGS {0}", buf.GetData()); } while (0); | |||
171 | break; | |||
172 | } | |||
173 | case PTRACE_SETFPREGS15: { | |||
174 | DisplayBytes(buf, data, data_size); | |||
175 | LLDB_LOGV(log, "PTRACE_SETFPREGS {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_SETFPREGS {0}", buf.GetData()); } while (0); | |||
176 | break; | |||
177 | } | |||
178 | case PTRACE_SETSIGINFO: { | |||
179 | DisplayBytes(buf, data, sizeof(siginfo_t)); | |||
180 | LLDB_LOGV(log, "PTRACE_SETSIGINFO {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_SETSIGINFO {0}", buf.GetData()); } while (0); | |||
181 | break; | |||
182 | } | |||
183 | case PTRACE_SETREGSETPTRACE_SETREGSET: { | |||
184 | // Extract iov_base from data, which is a pointer to the struct iovec | |||
185 | DisplayBytes(buf, *(void **)data, data_size); | |||
186 | LLDB_LOGV(log, "PTRACE_SETREGSET {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_SETREGSET {0}", buf.GetData()); } while (0); | |||
187 | break; | |||
188 | } | |||
189 | default: {} | |||
190 | } | |||
191 | } | |||
192 | ||||
193 | static constexpr unsigned k_ptrace_word_size = sizeof(void *); | |||
194 | static_assert(sizeof(long) >= k_ptrace_word_size, | |||
195 | "Size of long must be larger than ptrace word size"); | |||
196 | ||||
197 | // Simple helper function to ensure flags are enabled on the given file | |||
198 | // descriptor. | |||
199 | static Status EnsureFDFlags(int fd, int flags) { | |||
200 | Status error; | |||
201 | ||||
202 | int status = fcntl(fd, F_GETFL3); | |||
203 | if (status == -1) { | |||
204 | error.SetErrorToErrno(); | |||
205 | return error; | |||
206 | } | |||
207 | ||||
208 | if (fcntl(fd, F_SETFL4, status | flags) == -1) { | |||
209 | error.SetErrorToErrno(); | |||
210 | return error; | |||
211 | } | |||
212 | ||||
213 | return error; | |||
214 | } | |||
215 | ||||
216 | // Public Static Methods | |||
217 | ||||
218 | llvm::Expected<std::unique_ptr<NativeProcessProtocol>> | |||
219 | NativeProcessLinux::Factory::Launch(ProcessLaunchInfo &launch_info, | |||
220 | NativeDelegate &native_delegate, | |||
221 | MainLoop &mainloop) const { | |||
222 | Log *log = GetLog(POSIXLog::Process); | |||
223 | ||||
224 | MaybeLogLaunchInfo(launch_info); | |||
225 | ||||
226 | Status status; | |||
227 | ::pid_t pid = ProcessLauncherPosixFork() | |||
228 | .LaunchProcess(launch_info, status) | |||
229 | .GetProcessId(); | |||
230 | LLDB_LOG(log, "pid = {0:x}", pid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid = {0:x}", pid); } while (0); | |||
231 | if (status.Fail()) { | |||
232 | LLDB_LOG(log, "failed to launch process: {0}", status)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to launch process: {0}", status); } while (0); | |||
233 | return status.ToError(); | |||
234 | } | |||
235 | ||||
236 | // Wait for the child process to trap on its call to execve. | |||
237 | int wstatus; | |||
238 | ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0); | |||
239 | assert(wpid == pid)(static_cast <bool> (wpid == pid) ? void (0) : __assert_fail ("wpid == pid", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , 239, __extension__ __PRETTY_FUNCTION__)); | |||
240 | (void)wpid; | |||
241 | if (!WIFSTOPPED(wstatus)(((wstatus) & 0xff) == 0x7f)) { | |||
242 | LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Could not sync with inferior process: wstatus={1}" , WaitStatus::Decode(wstatus)); } while (0) | |||
243 | WaitStatus::Decode(wstatus))do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Could not sync with inferior process: wstatus={1}" , WaitStatus::Decode(wstatus)); } while (0); | |||
244 | return llvm::make_error<StringError>("Could not sync with inferior process", | |||
245 | llvm::inconvertibleErrorCode()); | |||
246 | } | |||
247 | LLDB_LOG(log, "inferior started, now in stopped state")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "inferior started, now in stopped state"); } while (0); | |||
248 | ||||
249 | ProcessInstanceInfo Info; | |||
250 | if (!Host::GetProcessInfo(pid, Info)) { | |||
251 | return llvm::make_error<StringError>("Cannot get process architecture", | |||
252 | llvm::inconvertibleErrorCode()); | |||
253 | } | |||
254 | ||||
255 | // Set the architecture to the exe architecture. | |||
256 | LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid = {0:x}, detected architecture {1}", pid, Info .GetArchitecture().GetArchitectureName()); } while (0) | |||
257 | Info.GetArchitecture().GetArchitectureName())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid = {0:x}, detected architecture {1}", pid, Info .GetArchitecture().GetArchitectureName()); } while (0); | |||
258 | ||||
259 | status = SetDefaultPtraceOpts(pid); | |||
260 | if (status.Fail()) { | |||
261 | LLDB_LOG(log, "failed to set default ptrace options: {0}", status)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to set default ptrace options: {0}", status ); } while (0); | |||
262 | return status.ToError(); | |||
263 | } | |||
264 | ||||
265 | return std::unique_ptr<NativeProcessLinux>(new NativeProcessLinux( | |||
266 | pid, launch_info.GetPTY().ReleasePrimaryFileDescriptor(), native_delegate, | |||
267 | Info.GetArchitecture(), mainloop, {pid})); | |||
268 | } | |||
269 | ||||
270 | llvm::Expected<std::unique_ptr<NativeProcessProtocol>> | |||
271 | NativeProcessLinux::Factory::Attach( | |||
272 | lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, | |||
273 | MainLoop &mainloop) const { | |||
274 | Log *log = GetLog(POSIXLog::Process); | |||
275 | LLDB_LOG(log, "pid = {0:x}", pid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid = {0:x}", pid); } while (0); | |||
276 | ||||
277 | // Retrieve the architecture for the running process. | |||
278 | ProcessInstanceInfo Info; | |||
279 | if (!Host::GetProcessInfo(pid, Info)) { | |||
280 | return llvm::make_error<StringError>("Cannot get process architecture", | |||
281 | llvm::inconvertibleErrorCode()); | |||
282 | } | |||
283 | ||||
284 | auto tids_or = NativeProcessLinux::Attach(pid); | |||
285 | if (!tids_or) | |||
286 | return tids_or.takeError(); | |||
287 | ||||
288 | return std::unique_ptr<NativeProcessLinux>(new NativeProcessLinux( | |||
289 | pid, -1, native_delegate, Info.GetArchitecture(), mainloop, *tids_or)); | |||
290 | } | |||
291 | ||||
292 | NativeProcessLinux::Extension | |||
293 | NativeProcessLinux::Factory::GetSupportedExtensions() const { | |||
294 | NativeProcessLinux::Extension supported = | |||
295 | Extension::multiprocess | Extension::fork | Extension::vfork | | |||
296 | Extension::pass_signals | Extension::auxv | Extension::libraries_svr4 | | |||
297 | Extension::siginfo_read; | |||
298 | ||||
299 | #ifdef __aarch64__ | |||
300 | // At this point we do not have a process so read auxv directly. | |||
301 | if ((getauxval(AT_HWCAP2) & HWCAP2_MTE(1 << 18))) | |||
302 | supported |= Extension::memory_tagging; | |||
303 | #endif | |||
304 | ||||
305 | return supported; | |||
306 | } | |||
307 | ||||
308 | // Public Instance Methods | |||
309 | ||||
310 | NativeProcessLinux::NativeProcessLinux(::pid_t pid, int terminal_fd, | |||
311 | NativeDelegate &delegate, | |||
312 | const ArchSpec &arch, MainLoop &mainloop, | |||
313 | llvm::ArrayRef<::pid_t> tids) | |||
314 | : NativeProcessELF(pid, terminal_fd, delegate), m_arch(arch), | |||
315 | m_main_loop(mainloop), m_intel_pt_collector(pid) { | |||
316 | if (m_terminal_fd != -1) { | |||
317 | Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK04000); | |||
318 | assert(status.Success())(static_cast <bool> (status.Success()) ? void (0) : __assert_fail ("status.Success()", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , 318, __extension__ __PRETTY_FUNCTION__)); | |||
319 | } | |||
320 | ||||
321 | Status status; | |||
322 | m_sigchld_handle = mainloop.RegisterSignal( | |||
323 | SIGCHLD17, [this](MainLoopBase &) { SigchldHandler(); }, status); | |||
324 | assert(m_sigchld_handle && status.Success())(static_cast <bool> (m_sigchld_handle && status .Success()) ? void (0) : __assert_fail ("m_sigchld_handle && status.Success()" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 324, __extension__ __PRETTY_FUNCTION__)); | |||
325 | ||||
326 | for (const auto &tid : tids) { | |||
327 | NativeThreadLinux &thread = AddThread(tid, /*resume*/ false); | |||
328 | ThreadWasCreated(thread); | |||
329 | } | |||
330 | ||||
331 | // Let our process instance know the thread has stopped. | |||
332 | SetCurrentThreadID(tids[0]); | |||
333 | SetState(StateType::eStateStopped, false); | |||
334 | ||||
335 | // Proccess any signals we received before installing our handler | |||
336 | SigchldHandler(); | |||
337 | } | |||
338 | ||||
339 | llvm::Expected<std::vector<::pid_t>> NativeProcessLinux::Attach(::pid_t pid) { | |||
340 | Log *log = GetLog(POSIXLog::Process); | |||
341 | ||||
342 | Status status; | |||
343 | // Use a map to keep track of the threads which we have attached/need to | |||
344 | // attach. | |||
345 | Host::TidMap tids_to_attach; | |||
346 | while (Host::FindProcessThreads(pid, tids_to_attach)) { | |||
347 | for (Host::TidMap::iterator it = tids_to_attach.begin(); | |||
348 | it != tids_to_attach.end();) { | |||
349 | if (it->second == false) { | |||
350 | lldb::tid_t tid = it->first; | |||
351 | ||||
352 | // Attach to the requested process. | |||
353 | // An attach will cause the thread to stop with a SIGSTOP. | |||
354 | if ((status = PtraceWrapper(PTRACE_ATTACH, tid)).Fail()) { | |||
355 | // No such thread. The thread may have exited. More error handling | |||
356 | // may be needed. | |||
357 | if (status.GetError() == ESRCH3) { | |||
358 | it = tids_to_attach.erase(it); | |||
359 | continue; | |||
360 | } | |||
361 | return status.ToError(); | |||
362 | } | |||
363 | ||||
364 | int wpid = | |||
365 | llvm::sys::RetryAfterSignal(-1, ::waitpid, tid, nullptr, __WALL0x40000000); | |||
366 | // Need to use __WALL otherwise we receive an error with errno=ECHLD At | |||
367 | // this point we should have a thread stopped if waitpid succeeds. | |||
368 | if (wpid < 0) { | |||
369 | // No such thread. The thread may have exited. More error handling | |||
370 | // may be needed. | |||
371 | if (errno(*__errno_location ()) == ESRCH3) { | |||
372 | it = tids_to_attach.erase(it); | |||
373 | continue; | |||
374 | } | |||
375 | return llvm::errorCodeToError( | |||
376 | std::error_code(errno(*__errno_location ()), std::generic_category())); | |||
377 | } | |||
378 | ||||
379 | if ((status = SetDefaultPtraceOpts(tid)).Fail()) | |||
380 | return status.ToError(); | |||
381 | ||||
382 | LLDB_LOG(log, "adding tid = {0}", tid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "adding tid = {0}", tid); } while (0); | |||
383 | it->second = true; | |||
384 | } | |||
385 | ||||
386 | // move the loop forward | |||
387 | ++it; | |||
388 | } | |||
389 | } | |||
390 | ||||
391 | size_t tid_count = tids_to_attach.size(); | |||
392 | if (tid_count == 0) | |||
393 | return llvm::make_error<StringError>("No such process", | |||
394 | llvm::inconvertibleErrorCode()); | |||
395 | ||||
396 | std::vector<::pid_t> tids; | |||
397 | tids.reserve(tid_count); | |||
398 | for (const auto &p : tids_to_attach) | |||
399 | tids.push_back(p.first); | |||
400 | return std::move(tids); | |||
401 | } | |||
402 | ||||
403 | Status NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) { | |||
404 | long ptrace_opts = 0; | |||
405 | ||||
406 | // Have the child raise an event on exit. This is used to keep the child in | |||
407 | // limbo until it is destroyed. | |||
408 | ptrace_opts |= PTRACE_O_TRACEEXIT; | |||
409 | ||||
410 | // Have the tracer trace threads which spawn in the inferior process. | |||
411 | ptrace_opts |= PTRACE_O_TRACECLONE; | |||
412 | ||||
413 | // Have the tracer notify us before execve returns (needed to disable legacy | |||
414 | // SIGTRAP generation) | |||
415 | ptrace_opts |= PTRACE_O_TRACEEXEC; | |||
416 | ||||
417 | // Have the tracer trace forked children. | |||
418 | ptrace_opts |= PTRACE_O_TRACEFORK; | |||
419 | ||||
420 | // Have the tracer trace vforks. | |||
421 | ptrace_opts |= PTRACE_O_TRACEVFORK; | |||
422 | ||||
423 | // Have the tracer trace vfork-done in order to restore breakpoints after | |||
424 | // the child finishes sharing memory. | |||
425 | ptrace_opts |= PTRACE_O_TRACEVFORKDONE; | |||
426 | ||||
427 | return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts); | |||
428 | } | |||
429 | ||||
430 | // Handles all waitpid events from the inferior process. | |||
431 | void NativeProcessLinux::MonitorCallback(NativeThreadLinux &thread, | |||
432 | WaitStatus status) { | |||
433 | Log *log = GetLog(LLDBLog::Process); | |||
434 | ||||
435 | // Certain activities differ based on whether the pid is the tid of the main | |||
436 | // thread. | |||
437 | const bool is_main_thread = (thread.GetID() == GetID()); | |||
438 | ||||
439 | // Handle when the thread exits. | |||
440 | if (status.type == WaitStatus::Exit || status.type == WaitStatus::Signal) { | |||
441 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "got exit status({0}) , tid = {1} ({2} main thread), process " "state = {3}", status, thread.GetID(), is_main_thread ? "is" : "is not", GetState()); } while (0) | |||
442 | "got exit status({0}) , tid = {1} ({2} main thread), process "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "got exit status({0}) , tid = {1} ({2} main thread), process " "state = {3}", status, thread.GetID(), is_main_thread ? "is" : "is not", GetState()); } while (0) | |||
443 | "state = {3}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "got exit status({0}) , tid = {1} ({2} main thread), process " "state = {3}", status, thread.GetID(), is_main_thread ? "is" : "is not", GetState()); } while (0) | |||
444 | status, thread.GetID(), is_main_thread ? "is" : "is not",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "got exit status({0}) , tid = {1} ({2} main thread), process " "state = {3}", status, thread.GetID(), is_main_thread ? "is" : "is not", GetState()); } while (0) | |||
445 | GetState())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "got exit status({0}) , tid = {1} ({2} main thread), process " "state = {3}", status, thread.GetID(), is_main_thread ? "is" : "is not", GetState()); } while (0); | |||
446 | ||||
447 | // This is a thread that exited. Ensure we're not tracking it anymore. | |||
448 | StopTrackingThread(thread); | |||
449 | ||||
450 | assert(!is_main_thread && "Main thread exits handled elsewhere")(static_cast <bool> (!is_main_thread && "Main thread exits handled elsewhere" ) ? void (0) : __assert_fail ("!is_main_thread && \"Main thread exits handled elsewhere\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 450, __extension__ __PRETTY_FUNCTION__)); | |||
451 | return; | |||
452 | } | |||
453 | ||||
454 | siginfo_t info; | |||
455 | const auto info_err = GetSignalInfo(thread.GetID(), &info); | |||
456 | ||||
457 | // Get details on the signal raised. | |||
458 | if (info_err.Success()) { | |||
459 | // We have retrieved the signal info. Dispatch appropriately. | |||
460 | if (info.si_signo == SIGTRAP5) | |||
461 | MonitorSIGTRAP(info, thread); | |||
462 | else | |||
463 | MonitorSignal(info, thread); | |||
464 | } else { | |||
465 | if (info_err.GetError() == EINVAL22) { | |||
466 | // This is a group stop reception for this tid. We can reach here if we | |||
467 | // reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU into the tracee, | |||
468 | // triggering the group-stop mechanism. Normally receiving these would | |||
469 | // stop the process, pending a SIGCONT. Simulating this state in a | |||
470 | // debugger is hard and is generally not needed (one use case is | |||
471 | // debugging background task being managed by a shell). For general use, | |||
472 | // it is sufficient to stop the process in a signal-delivery stop which | |||
473 | // happens before the group stop. This done by MonitorSignal and works | |||
474 | // correctly for all signals. | |||
475 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received a group stop for pid {0} tid {1}. Transparent " "handling of group stops not supported, resuming the " "thread." , GetID(), thread.GetID()); } while (0) | |||
476 | "received a group stop for pid {0} tid {1}. Transparent "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received a group stop for pid {0} tid {1}. Transparent " "handling of group stops not supported, resuming the " "thread." , GetID(), thread.GetID()); } while (0) | |||
477 | "handling of group stops not supported, resuming the "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received a group stop for pid {0} tid {1}. Transparent " "handling of group stops not supported, resuming the " "thread." , GetID(), thread.GetID()); } while (0) | |||
478 | "thread.",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received a group stop for pid {0} tid {1}. Transparent " "handling of group stops not supported, resuming the " "thread." , GetID(), thread.GetID()); } while (0) | |||
479 | GetID(), thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received a group stop for pid {0} tid {1}. Transparent " "handling of group stops not supported, resuming the " "thread." , GetID(), thread.GetID()); } while (0); | |||
480 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
481 | } else { | |||
482 | // ptrace(GETSIGINFO) failed (but not due to group-stop). | |||
483 | ||||
484 | // A return value of ESRCH means the thread/process has died in the mean | |||
485 | // time. This can (e.g.) happen when another thread does an exit_group(2) | |||
486 | // or the entire process get SIGKILLed. | |||
487 | // We can't do anything with this thread anymore, but we keep it around | |||
488 | // until we get the WIFEXITED event. | |||
489 | ||||
490 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "GetSignalInfo({0}) failed: {1}, status = {2}, main_thread = " "{3}. Expecting WIFEXITED soon.", thread.GetID(), info_err, status , is_main_thread); } while (0) | |||
491 | "GetSignalInfo({0}) failed: {1}, status = {2}, main_thread = "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "GetSignalInfo({0}) failed: {1}, status = {2}, main_thread = " "{3}. Expecting WIFEXITED soon.", thread.GetID(), info_err, status , is_main_thread); } while (0) | |||
492 | "{3}. Expecting WIFEXITED soon.",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "GetSignalInfo({0}) failed: {1}, status = {2}, main_thread = " "{3}. Expecting WIFEXITED soon.", thread.GetID(), info_err, status , is_main_thread); } while (0) | |||
493 | thread.GetID(), info_err, status, is_main_thread)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "GetSignalInfo({0}) failed: {1}, status = {2}, main_thread = " "{3}. Expecting WIFEXITED soon.", thread.GetID(), info_err, status , is_main_thread); } while (0); | |||
494 | } | |||
495 | } | |||
496 | } | |||
497 | ||||
498 | void NativeProcessLinux::WaitForCloneNotification(::pid_t pid) { | |||
499 | Log *log = GetLog(POSIXLog::Process); | |||
500 | ||||
501 | // The PID is not tracked yet, let's wait for it to appear. | |||
502 | int status = -1; | |||
503 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received clone event for pid {0}. pid not tracked yet, " "waiting for it to appear...", pid); } while (0) | |||
504 | "received clone event for pid {0}. pid not tracked yet, "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received clone event for pid {0}. pid not tracked yet, " "waiting for it to appear...", pid); } while (0) | |||
505 | "waiting for it to appear...",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received clone event for pid {0}. pid not tracked yet, " "waiting for it to appear...", pid); } while (0) | |||
506 | pid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received clone event for pid {0}. pid not tracked yet, " "waiting for it to appear...", pid); } while (0); | |||
507 | ::pid_t wait_pid = | |||
508 | llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &status, __WALL0x40000000); | |||
509 | ||||
510 | // It's theoretically possible to get other events if the entire process was | |||
511 | // SIGKILLed before we got a chance to check this. In that case, we'll just | |||
512 | // clean everything up when we get the process exit event. | |||
513 | ||||
514 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid({0}, &status, __WALL) => {1} (errno: {2}, status = {3})" , pid, wait_pid, (*__errno_location ()), WaitStatus::Decode(status )); } while (0) | |||
515 | "waitpid({0}, &status, __WALL) => {1} (errno: {2}, status = {3})",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid({0}, &status, __WALL) => {1} (errno: {2}, status = {3})" , pid, wait_pid, (*__errno_location ()), WaitStatus::Decode(status )); } while (0) | |||
516 | pid, wait_pid, errno, WaitStatus::Decode(status))do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid({0}, &status, __WALL) => {1} (errno: {2}, status = {3})" , pid, wait_pid, (*__errno_location ()), WaitStatus::Decode(status )); } while (0); | |||
517 | } | |||
518 | ||||
519 | void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, | |||
520 | NativeThreadLinux &thread) { | |||
521 | Log *log = GetLog(POSIXLog::Process); | |||
522 | const bool is_main_thread = (thread.GetID() == GetID()); | |||
523 | ||||
524 | assert(info.si_signo == SIGTRAP && "Unexpected child signal!")(static_cast <bool> (info.si_signo == 5 && "Unexpected child signal!" ) ? void (0) : __assert_fail ("info.si_signo == SIGTRAP && \"Unexpected child signal!\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 524, __extension__ __PRETTY_FUNCTION__)); | |||
525 | ||||
526 | switch (info.si_code) { | |||
527 | case (SIGTRAP5 | (PTRACE_EVENT_FORK << 8)): | |||
528 | case (SIGTRAP5 | (PTRACE_EVENT_VFORK << 8)): | |||
529 | case (SIGTRAP5 | (PTRACE_EVENT_CLONE << 8)): { | |||
530 | // This can either mean a new thread or a new process spawned via | |||
531 | // clone(2) without SIGCHLD or CLONE_VFORK flag. Note that clone(2) | |||
532 | // can also cause PTRACE_EVENT_FORK and PTRACE_EVENT_VFORK if one | |||
533 | // of these flags are passed. | |||
534 | ||||
535 | unsigned long event_message = 0; | |||
536 | if (GetEventMessage(thread.GetID(), &event_message).Fail()) { | |||
537 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} received clone() event but GetEventMessage failed " "so we don't know the new pid/tid", thread.GetID()); } while (0) | |||
538 | "pid {0} received clone() event but GetEventMessage failed "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} received clone() event but GetEventMessage failed " "so we don't know the new pid/tid", thread.GetID()); } while (0) | |||
539 | "so we don't know the new pid/tid",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} received clone() event but GetEventMessage failed " "so we don't know the new pid/tid", thread.GetID()); } while (0) | |||
540 | thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} received clone() event but GetEventMessage failed " "so we don't know the new pid/tid", thread.GetID()); } while (0); | |||
541 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
542 | } else { | |||
543 | MonitorClone(thread, event_message, info.si_code >> 8); | |||
544 | } | |||
545 | ||||
546 | break; | |||
547 | } | |||
548 | ||||
549 | case (SIGTRAP5 | (PTRACE_EVENT_EXEC << 8)): { | |||
550 | LLDB_LOG(log, "received exec event, code = {0}", info.si_code ^ SIGTRAP)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received exec event, code = {0}", info.si_code ^ 5); } while (0); | |||
551 | ||||
552 | // Exec clears any pending notifications. | |||
553 | m_pending_notification_tid = LLDB_INVALID_THREAD_ID0; | |||
554 | ||||
555 | // Remove all but the main thread here. Linux fork creates a new process | |||
556 | // which only copies the main thread. | |||
557 | LLDB_LOG(log, "exec received, stop tracking all but main thread")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "exec received, stop tracking all but main thread" ); } while (0); | |||
558 | ||||
559 | llvm::erase_if(m_threads, [&](std::unique_ptr<NativeThreadProtocol> &t) { | |||
560 | return t->GetID() != GetID(); | |||
561 | }); | |||
562 | assert(m_threads.size() == 1)(static_cast <bool> (m_threads.size() == 1) ? void (0) : __assert_fail ("m_threads.size() == 1", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , 562, __extension__ __PRETTY_FUNCTION__)); | |||
563 | auto *main_thread = static_cast<NativeThreadLinux *>(m_threads[0].get()); | |||
564 | ||||
565 | SetCurrentThreadID(main_thread->GetID()); | |||
566 | main_thread->SetStoppedByExec(); | |||
567 | ||||
568 | // Tell coordinator about about the "new" (since exec) stopped main thread. | |||
569 | ThreadWasCreated(*main_thread); | |||
570 | ||||
571 | // Let our delegate know we have just exec'd. | |||
572 | NotifyDidExec(); | |||
573 | ||||
574 | // Let the process know we're stopped. | |||
575 | StopRunningThreads(main_thread->GetID()); | |||
576 | ||||
577 | break; | |||
578 | } | |||
579 | ||||
580 | case (SIGTRAP5 | (PTRACE_EVENT_EXIT << 8)): { | |||
581 | // The inferior process or one of its threads is about to exit. We don't | |||
582 | // want to do anything with the thread so we just resume it. In case we | |||
583 | // want to implement "break on thread exit" functionality, we would need to | |||
584 | // stop here. | |||
585 | ||||
586 | unsigned long data = 0; | |||
587 | if (GetEventMessage(thread.GetID(), &data).Fail()) | |||
588 | data = -1; | |||
589 | ||||
590 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", data, (((data ) & 0x7f) == 0), (((signed char) (((data) & 0x7f) + 1 ) >> 1) > 0), thread.GetID(), is_main_thread); } while (0) | |||
591 | "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", data, (((data ) & 0x7f) == 0), (((signed char) (((data) & 0x7f) + 1 ) >> 1) > 0), thread.GetID(), is_main_thread); } while (0) | |||
592 | "WIFSIGNALED={2}, pid = {3}, main_thread = {4}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", data, (((data ) & 0x7f) == 0), (((signed char) (((data) & 0x7f) + 1 ) >> 1) > 0), thread.GetID(), is_main_thread); } while (0) | |||
593 | data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", data, (((data ) & 0x7f) == 0), (((signed char) (((data) & 0x7f) + 1 ) >> 1) > 0), thread.GetID(), is_main_thread); } while (0) | |||
594 | is_main_thread)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", data, (((data ) & 0x7f) == 0), (((signed char) (((data) & 0x7f) + 1 ) >> 1) > 0), thread.GetID(), is_main_thread); } while (0); | |||
595 | ||||
596 | ||||
597 | StateType state = thread.GetState(); | |||
598 | if (!StateIsRunningState(state)) { | |||
599 | // Due to a kernel bug, we may sometimes get this stop after the inferior | |||
600 | // gets a SIGKILL. This confuses our state tracking logic in | |||
601 | // ResumeThread(), since normally, we should not be receiving any ptrace | |||
602 | // events while the inferior is stopped. This makes sure that the | |||
603 | // inferior is resumed and exits normally. | |||
604 | state = eStateRunning; | |||
605 | } | |||
606 | ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
607 | ||||
608 | if (is_main_thread) { | |||
609 | // Main thread report the read (WIFEXITED) event only after all threads in | |||
610 | // the process exit, so we need to stop tracking it here instead of in | |||
611 | // MonitorCallback | |||
612 | StopTrackingThread(thread); | |||
613 | } | |||
614 | ||||
615 | break; | |||
616 | } | |||
617 | ||||
618 | case (SIGTRAP5 | (PTRACE_EVENT_VFORK_DONE << 8)): { | |||
619 | if (bool(m_enabled_extensions & Extension::vfork)) { | |||
620 | thread.SetStoppedByVForkDone(); | |||
621 | StopRunningThreads(thread.GetID()); | |||
622 | } | |||
623 | else | |||
624 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
625 | break; | |||
626 | } | |||
627 | ||||
628 | case 0: | |||
629 | case TRAP_TRACETRAP_TRACE: // We receive this on single stepping. | |||
630 | case TRAP_HWBKPTTRAP_HWBKPT: // We receive this on watchpoint hit | |||
631 | { | |||
632 | // If a watchpoint was hit, report it | |||
633 | uint32_t wp_index; | |||
634 | Status error = thread.GetRegisterContext().GetWatchpointHitIndex( | |||
635 | wp_index, (uintptr_t)info.si_addr_sifields._sigfault.si_addr); | |||
636 | if (error.Fail()) | |||
637 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
638 | "received error while checking for watchpoint hits, pid = "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
639 | "{0}, error = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
640 | thread.GetID(), error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0); | |||
641 | if (wp_index != LLDB_INVALID_INDEX32(4294967295U)) { | |||
642 | MonitorWatchpoint(thread, wp_index); | |||
643 | break; | |||
644 | } | |||
645 | ||||
646 | // If a breakpoint was hit, report it | |||
647 | uint32_t bp_index; | |||
648 | error = thread.GetRegisterContext().GetHardwareBreakHitIndex( | |||
649 | bp_index, (uintptr_t)info.si_addr_sifields._sigfault.si_addr); | |||
650 | if (error.Fail()) | |||
651 | LLDB_LOG(log, "received error while checking for hardware "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for hardware " "breakpoint hits, pid = {0}, error = {1}" , thread.GetID(), error); } while (0) | |||
652 | "breakpoint hits, pid = {0}, error = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for hardware " "breakpoint hits, pid = {0}, error = {1}" , thread.GetID(), error); } while (0) | |||
653 | thread.GetID(), error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for hardware " "breakpoint hits, pid = {0}, error = {1}" , thread.GetID(), error); } while (0); | |||
654 | if (bp_index != LLDB_INVALID_INDEX32(4294967295U)) { | |||
655 | MonitorBreakpoint(thread); | |||
656 | break; | |||
657 | } | |||
658 | ||||
659 | // Otherwise, report step over | |||
660 | MonitorTrace(thread); | |||
661 | break; | |||
662 | } | |||
663 | ||||
664 | case SI_KERNELSI_KERNEL: | |||
665 | #if defined __mips__ | |||
666 | // For mips there is no special signal for watchpoint So we check for | |||
667 | // watchpoint in kernel trap | |||
668 | { | |||
669 | // If a watchpoint was hit, report it | |||
670 | uint32_t wp_index; | |||
671 | Status error = thread.GetRegisterContext().GetWatchpointHitIndex( | |||
672 | wp_index, LLDB_INVALID_ADDRESS(18446744073709551615UL)); | |||
673 | if (error.Fail()) | |||
674 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
675 | "received error while checking for watchpoint hits, pid = "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
676 | "{0}, error = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
677 | thread.GetID(), error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0); | |||
678 | if (wp_index != LLDB_INVALID_INDEX32(4294967295U)) { | |||
679 | MonitorWatchpoint(thread, wp_index); | |||
680 | break; | |||
681 | } | |||
682 | } | |||
683 | // NO BREAK | |||
684 | #endif | |||
685 | case TRAP_BRKPTTRAP_BRKPT: | |||
686 | MonitorBreakpoint(thread); | |||
687 | break; | |||
688 | ||||
689 | case SIGTRAP5: | |||
690 | case (SIGTRAP5 | 0x80): | |||
691 | LLDB_LOG(do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming" , info.si_code, GetID(), thread.GetID()); } while (0) | |||
692 | log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming" , info.si_code, GetID(), thread.GetID()); } while (0) | |||
693 | "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming" , info.si_code, GetID(), thread.GetID()); } while (0) | |||
694 | info.si_code, GetID(), thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming" , info.si_code, GetID(), thread.GetID()); } while (0); | |||
695 | ||||
696 | // Ignore these signals until we know more about them. | |||
697 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
698 | break; | |||
699 | ||||
700 | default: | |||
701 | LLDB_LOG(log, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}" , info.si_code, GetID(), thread.GetID()); } while (0) | |||
702 | info.si_code, GetID(), thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}" , info.si_code, GetID(), thread.GetID()); } while (0); | |||
703 | MonitorSignal(info, thread); | |||
704 | break; | |||
705 | } | |||
706 | } | |||
707 | ||||
708 | void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { | |||
709 | Log *log = GetLog(POSIXLog::Process); | |||
710 | LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received trace event, pid = {0}", thread.GetID() ); } while (0); | |||
711 | ||||
712 | // This thread is currently stopped. | |||
713 | thread.SetStoppedByTrace(); | |||
714 | ||||
715 | StopRunningThreads(thread.GetID()); | |||
716 | } | |||
717 | ||||
718 | void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { | |||
719 | Log *log = GetLog(LLDBLog::Process | LLDBLog::Breakpoints); | |||
720 | LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received breakpoint event, pid = {0}", thread.GetID ()); } while (0); | |||
721 | ||||
722 | // Mark the thread as stopped at breakpoint. | |||
723 | thread.SetStoppedByBreakpoint(); | |||
724 | FixupBreakpointPCAsNeeded(thread); | |||
725 | ||||
726 | if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != | |||
727 | m_threads_stepping_with_breakpoint.end()) | |||
728 | thread.SetStoppedByTrace(); | |||
729 | ||||
730 | StopRunningThreads(thread.GetID()); | |||
731 | } | |||
732 | ||||
733 | void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, | |||
734 | uint32_t wp_index) { | |||
735 | Log *log = GetLog(LLDBLog::Process | LLDBLog::Watchpoints); | |||
736 | LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received watchpoint event, pid = {0}, wp_index = {1}" , thread.GetID(), wp_index); } while (0) | |||
737 | thread.GetID(), wp_index)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received watchpoint event, pid = {0}, wp_index = {1}" , thread.GetID(), wp_index); } while (0); | |||
738 | ||||
739 | // Mark the thread as stopped at watchpoint. The address is at | |||
740 | // (lldb::addr_t)info->si_addr if we need it. | |||
741 | thread.SetStoppedByWatchpoint(wp_index); | |||
742 | ||||
743 | // We need to tell all other running threads before we notify the delegate | |||
744 | // about this stop. | |||
745 | StopRunningThreads(thread.GetID()); | |||
746 | } | |||
747 | ||||
748 | void NativeProcessLinux::MonitorSignal(const siginfo_t &info, | |||
749 | NativeThreadLinux &thread) { | |||
750 | const int signo = info.si_signo; | |||
751 | const bool is_from_llgs = info.si_pid_sifields._kill.si_pid == getpid(); | |||
752 | ||||
753 | Log *log = GetLog(POSIXLog::Process); | |||
754 | ||||
755 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, | |||
756 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a kill(2) | |||
757 | // or raise(3). Similarly for tgkill(2) on Linux. | |||
758 | // | |||
759 | // IOW, user generated signals never generate what we consider to be a | |||
760 | // "crash". | |||
761 | // | |||
762 | // Similarly, ACK signals generated by this monitor. | |||
763 | ||||
764 | // Handle the signal. | |||
765 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " "waitpid pid = {4})", Host::GetSignalAsCString(signo), signo , info.si_code, thread.GetID()); } while (0) | |||
766 | "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " "waitpid pid = {4})", Host::GetSignalAsCString(signo), signo , info.si_code, thread.GetID()); } while (0) | |||
767 | "waitpid pid = {4})",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " "waitpid pid = {4})", Host::GetSignalAsCString(signo), signo , info.si_code, thread.GetID()); } while (0) | |||
768 | Host::GetSignalAsCString(signo), signo, info.si_code,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " "waitpid pid = {4})", Host::GetSignalAsCString(signo), signo , info.si_code, thread.GetID()); } while (0) | |||
769 | thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " "waitpid pid = {4})", Host::GetSignalAsCString(signo), signo , info.si_code, thread.GetID()); } while (0); | |||
770 | ||||
771 | // Check for thread stop notification. | |||
772 | if (is_from_llgs && (info.si_code == SI_TKILLSI_TKILL) && (signo == SIGSTOP19)) { | |||
773 | // This is a tgkill()-based stop. | |||
774 | LLDB_LOG(log, "pid {0} tid {1}, thread stopped", GetID(), thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} tid {1}, thread stopped", GetID(), thread .GetID()); } while (0); | |||
775 | ||||
776 | // Check that we're not already marked with a stop reason. Note this thread | |||
777 | // really shouldn't already be marked as stopped - if we were, that would | |||
778 | // imply that the kernel signaled us with the thread stopping which we | |||
779 | // handled and marked as stopped, and that, without an intervening resume, | |||
780 | // we received another stop. It is more likely that we are missing the | |||
781 | // marking of a run state somewhere if we find that the thread was marked | |||
782 | // as stopped. | |||
783 | const StateType thread_state = thread.GetState(); | |||
784 | if (!StateIsStoppedState(thread_state, false)) { | |||
785 | // An inferior thread has stopped because of a SIGSTOP we have sent it. | |||
786 | // Generally, these are not important stops and we don't want to report | |||
787 | // them as they are just used to stop other threads when one thread (the | |||
788 | // one with the *real* stop reason) hits a breakpoint (watchpoint, | |||
789 | // etc...). However, in the case of an asynchronous Interrupt(), this | |||
790 | // *is* the real stop reason, so we leave the signal intact if this is | |||
791 | // the thread that was chosen as the triggering thread. | |||
792 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID0) { | |||
793 | if (m_pending_notification_tid == thread.GetID()) | |||
794 | thread.SetStoppedBySignal(SIGSTOP19, &info); | |||
795 | else | |||
796 | thread.SetStoppedWithNoReason(); | |||
797 | ||||
798 | SetCurrentThreadID(thread.GetID()); | |||
799 | SignalIfAllThreadsStopped(); | |||
800 | } else { | |||
801 | // We can end up here if stop was initiated by LLGS but by this time a | |||
802 | // thread stop has occurred - maybe initiated by another event. | |||
803 | Status error = ResumeThread(thread, thread.GetState(), 0); | |||
804 | if (error.Fail()) | |||
805 | LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to resume thread {0}: {1}", thread.GetID( ), error); } while (0) | |||
806 | error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to resume thread {0}: {1}", thread.GetID( ), error); } while (0); | |||
807 | } | |||
808 | } else { | |||
809 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} tid {1}, thread was already marked as a stopped " "state (state={2}), leaving stop signal as is", GetID(), thread .GetID(), thread_state); } while (0) | |||
810 | "pid {0} tid {1}, thread was already marked as a stopped "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} tid {1}, thread was already marked as a stopped " "state (state={2}), leaving stop signal as is", GetID(), thread .GetID(), thread_state); } while (0) | |||
811 | "state (state={2}), leaving stop signal as is",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} tid {1}, thread was already marked as a stopped " "state (state={2}), leaving stop signal as is", GetID(), thread .GetID(), thread_state); } while (0) | |||
812 | GetID(), thread.GetID(), thread_state)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} tid {1}, thread was already marked as a stopped " "state (state={2}), leaving stop signal as is", GetID(), thread .GetID(), thread_state); } while (0); | |||
813 | SignalIfAllThreadsStopped(); | |||
814 | } | |||
815 | ||||
816 | // Done handling. | |||
817 | return; | |||
818 | } | |||
819 | ||||
820 | // Check if debugger should stop at this signal or just ignore it and resume | |||
821 | // the inferior. | |||
822 | if (m_signals_to_ignore.contains(signo)) { | |||
823 | ResumeThread(thread, thread.GetState(), signo); | |||
824 | return; | |||
825 | } | |||
826 | ||||
827 | // This thread is stopped. | |||
828 | LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo))do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0}", Host::GetSignalAsCString(signo )); } while (0); | |||
829 | thread.SetStoppedBySignal(signo, &info); | |||
830 | ||||
831 | // Send a stop to the debugger after we get all other threads to stop. | |||
832 | StopRunningThreads(thread.GetID()); | |||
833 | } | |||
834 | ||||
835 | bool NativeProcessLinux::MonitorClone(NativeThreadLinux &parent, | |||
836 | lldb::pid_t child_pid, int event) { | |||
837 | Log *log = GetLog(POSIXLog::Process); | |||
838 | LLDB_LOG(log, "parent_tid={0}, child_pid={1}, event={2}", parent.GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "parent_tid={0}, child_pid={1}, event={2}", parent .GetID(), child_pid, event); } while (0) | |||
839 | child_pid, event)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "parent_tid={0}, child_pid={1}, event={2}", parent .GetID(), child_pid, event); } while (0); | |||
840 | ||||
841 | WaitForCloneNotification(child_pid); | |||
842 | ||||
843 | switch (event) { | |||
844 | case PTRACE_EVENT_CLONE: { | |||
845 | // PTRACE_EVENT_CLONE can either mean a new thread or a new process. | |||
846 | // Try to grab the new process' PGID to figure out which one it is. | |||
847 | // If PGID is the same as the PID, then it's a new process. Otherwise, | |||
848 | // it's a thread. | |||
849 | auto tgid_ret = getPIDForTID(child_pid); | |||
850 | if (tgid_ret != child_pid) { | |||
851 | // A new thread should have PGID matching our process' PID. | |||
852 | assert(!tgid_ret || tgid_ret.getValue() == GetID())(static_cast <bool> (!tgid_ret || tgid_ret.getValue() == GetID()) ? void (0) : __assert_fail ("!tgid_ret || tgid_ret.getValue() == GetID()" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 852, __extension__ __PRETTY_FUNCTION__)); | |||
853 | ||||
854 | NativeThreadLinux &child_thread = AddThread(child_pid, /*resume*/ true); | |||
855 | ThreadWasCreated(child_thread); | |||
856 | ||||
857 | // Resume the parent. | |||
858 | ResumeThread(parent, parent.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
859 | break; | |||
860 | } | |||
861 | } | |||
862 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
863 | case PTRACE_EVENT_FORK: | |||
864 | case PTRACE_EVENT_VFORK: { | |||
865 | bool is_vfork = event == PTRACE_EVENT_VFORK; | |||
866 | std::unique_ptr<NativeProcessLinux> child_process{new NativeProcessLinux( | |||
867 | static_cast<::pid_t>(child_pid), m_terminal_fd, m_delegate, m_arch, | |||
868 | m_main_loop, {static_cast<::pid_t>(child_pid)})}; | |||
869 | if (!is_vfork) | |||
870 | child_process->m_software_breakpoints = m_software_breakpoints; | |||
871 | ||||
872 | Extension expected_ext = is_vfork ? Extension::vfork : Extension::fork; | |||
873 | if (bool(m_enabled_extensions & expected_ext)) { | |||
874 | m_delegate.NewSubprocess(this, std::move(child_process)); | |||
875 | // NB: non-vfork clone() is reported as fork | |||
876 | parent.SetStoppedByFork(is_vfork, child_pid); | |||
877 | StopRunningThreads(parent.GetID()); | |||
878 | } else { | |||
879 | child_process->Detach(); | |||
880 | ResumeThread(parent, parent.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
881 | } | |||
882 | break; | |||
883 | } | |||
884 | default: | |||
885 | llvm_unreachable("unknown clone_info.event")::llvm::llvm_unreachable_internal("unknown clone_info.event", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 885 ); | |||
886 | } | |||
887 | ||||
888 | return true; | |||
889 | } | |||
890 | ||||
891 | bool NativeProcessLinux::SupportHardwareSingleStepping() const { | |||
892 | if (m_arch.GetMachine() == llvm::Triple::arm || m_arch.IsMIPS()) | |||
893 | return false; | |||
894 | return true; | |||
895 | } | |||
896 | ||||
897 | Status NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { | |||
898 | Log *log = GetLog(POSIXLog::Process); | |||
899 | LLDB_LOG(log, "pid {0}", GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0}", GetID()); } while (0); | |||
900 | ||||
901 | bool software_single_step = !SupportHardwareSingleStepping(); | |||
902 | ||||
903 | if (software_single_step) { | |||
904 | for (const auto &thread : m_threads) { | |||
905 | assert(thread && "thread list should not contain NULL threads")(static_cast <bool> (thread && "thread list should not contain NULL threads" ) ? void (0) : __assert_fail ("thread && \"thread list should not contain NULL threads\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 905, __extension__ __PRETTY_FUNCTION__)); | |||
906 | ||||
907 | const ResumeAction *const action = | |||
908 | resume_actions.GetActionForThread(thread->GetID(), true); | |||
909 | if (action == nullptr) | |||
910 | continue; | |||
911 | ||||
912 | if (action->state == eStateStepping) { | |||
913 | Status error = SetupSoftwareSingleStepping( | |||
914 | static_cast<NativeThreadLinux &>(*thread)); | |||
915 | if (error.Fail()) | |||
916 | return error; | |||
917 | } | |||
918 | } | |||
919 | } | |||
920 | ||||
921 | for (const auto &thread : m_threads) { | |||
922 | assert(thread && "thread list should not contain NULL threads")(static_cast <bool> (thread && "thread list should not contain NULL threads" ) ? void (0) : __assert_fail ("thread && \"thread list should not contain NULL threads\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 922, __extension__ __PRETTY_FUNCTION__)); | |||
923 | ||||
924 | const ResumeAction *const action = | |||
925 | resume_actions.GetActionForThread(thread->GetID(), true); | |||
926 | ||||
927 | if (action == nullptr) { | |||
928 | LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "no action specified for pid {0} tid {1}", GetID( ), thread->GetID()); } while (0) | |||
929 | thread->GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "no action specified for pid {0} tid {1}", GetID( ), thread->GetID()); } while (0); | |||
930 | continue; | |||
931 | } | |||
932 | ||||
933 | LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "processing resume action state {0} for pid {1} tid {2}" , action->state, GetID(), thread->GetID()); } while (0) | |||
934 | action->state, GetID(), thread->GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "processing resume action state {0} for pid {1} tid {2}" , action->state, GetID(), thread->GetID()); } while (0); | |||
935 | ||||
936 | switch (action->state) { | |||
937 | case eStateRunning: | |||
938 | case eStateStepping: { | |||
939 | // Run the thread, possibly feeding it the signal. | |||
940 | const int signo = action->signal; | |||
941 | ResumeThread(static_cast<NativeThreadLinux &>(*thread), action->state, | |||
942 | signo); | |||
943 | break; | |||
944 | } | |||
945 | ||||
946 | case eStateSuspended: | |||
947 | case eStateStopped: | |||
948 | llvm_unreachable("Unexpected state")::llvm::llvm_unreachable_internal("Unexpected state", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , 948); | |||
949 | ||||
950 | default: | |||
951 | return Status("NativeProcessLinux::%s (): unexpected state %s specified " | |||
952 | "for pid %" PRIu64"l" "u" ", tid %" PRIu64"l" "u", | |||
953 | __FUNCTION__, StateAsCString(action->state), GetID(), | |||
954 | thread->GetID()); | |||
955 | } | |||
956 | } | |||
957 | ||||
958 | return Status(); | |||
959 | } | |||
960 | ||||
961 | Status NativeProcessLinux::Halt() { | |||
962 | Status error; | |||
963 | ||||
964 | if (kill(GetID(), SIGSTOP19) != 0) | |||
965 | error.SetErrorToErrno(); | |||
966 | ||||
967 | return error; | |||
968 | } | |||
969 | ||||
970 | Status NativeProcessLinux::Detach() { | |||
971 | Status error; | |||
972 | ||||
973 | // Stop monitoring the inferior. | |||
974 | m_sigchld_handle.reset(); | |||
975 | ||||
976 | // Tell ptrace to detach from the process. | |||
977 | if (GetID() == LLDB_INVALID_PROCESS_ID0) | |||
978 | return error; | |||
979 | ||||
980 | for (const auto &thread : m_threads) { | |||
981 | Status e = Detach(thread->GetID()); | |||
982 | if (e.Fail()) | |||
983 | error = | |||
984 | e; // Save the error, but still attempt to detach from other threads. | |||
985 | } | |||
986 | ||||
987 | m_intel_pt_collector.Clear(); | |||
988 | ||||
989 | return error; | |||
990 | } | |||
991 | ||||
992 | Status NativeProcessLinux::Signal(int signo) { | |||
993 | Status error; | |||
994 | ||||
995 | Log *log = GetLog(POSIXLog::Process); | |||
996 | LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "sending signal {0} ({1}) to pid {1}", signo, Host ::GetSignalAsCString(signo), GetID()); } while (0) | |||
997 | Host::GetSignalAsCString(signo), GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "sending signal {0} ({1}) to pid {1}", signo, Host ::GetSignalAsCString(signo), GetID()); } while (0); | |||
998 | ||||
999 | if (kill(GetID(), signo)) | |||
1000 | error.SetErrorToErrno(); | |||
1001 | ||||
1002 | return error; | |||
1003 | } | |||
1004 | ||||
1005 | Status NativeProcessLinux::Interrupt() { | |||
1006 | // Pick a running thread (or if none, a not-dead stopped thread) as the | |||
1007 | // chosen thread that will be the stop-reason thread. | |||
1008 | Log *log = GetLog(POSIXLog::Process); | |||
1009 | ||||
1010 | NativeThreadProtocol *running_thread = nullptr; | |||
1011 | NativeThreadProtocol *stopped_thread = nullptr; | |||
1012 | ||||
1013 | LLDB_LOG(log, "selecting running thread for interrupt target")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "selecting running thread for interrupt target"); } while (0); | |||
1014 | for (const auto &thread : m_threads) { | |||
1015 | // If we have a running or stepping thread, we'll call that the target of | |||
1016 | // the interrupt. | |||
1017 | const auto thread_state = thread->GetState(); | |||
1018 | if (thread_state == eStateRunning || thread_state == eStateStepping) { | |||
1019 | running_thread = thread.get(); | |||
1020 | break; | |||
1021 | } else if (!stopped_thread && StateIsStoppedState(thread_state, true)) { | |||
1022 | // Remember the first non-dead stopped thread. We'll use that as a | |||
1023 | // backup if there are no running threads. | |||
1024 | stopped_thread = thread.get(); | |||
1025 | } | |||
1026 | } | |||
1027 | ||||
1028 | if (!running_thread && !stopped_thread) { | |||
1029 | Status error("found no running/stepping or live stopped threads as target " | |||
1030 | "for interrupt"); | |||
1031 | LLDB_LOG(log, "skipping due to error: {0}", error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "skipping due to error: {0}", error); } while (0); | |||
1032 | ||||
1033 | return error; | |||
1034 | } | |||
1035 | ||||
1036 | NativeThreadProtocol *deferred_signal_thread = | |||
1037 | running_thread ? running_thread : stopped_thread; | |||
1038 | ||||
1039 | LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} {1} tid {2} chosen for interrupt target" , GetID(), running_thread ? "running" : "stopped", deferred_signal_thread ->GetID()); } while (0) | |||
1040 | running_thread ? "running" : "stopped",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} {1} tid {2} chosen for interrupt target" , GetID(), running_thread ? "running" : "stopped", deferred_signal_thread ->GetID()); } while (0) | |||
1041 | deferred_signal_thread->GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} {1} tid {2} chosen for interrupt target" , GetID(), running_thread ? "running" : "stopped", deferred_signal_thread ->GetID()); } while (0); | |||
1042 | ||||
1043 | StopRunningThreads(deferred_signal_thread->GetID()); | |||
1044 | ||||
1045 | return Status(); | |||
1046 | } | |||
1047 | ||||
1048 | Status NativeProcessLinux::Kill() { | |||
1049 | Log *log = GetLog(POSIXLog::Process); | |||
1050 | LLDB_LOG(log, "pid {0}", GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0}", GetID()); } while (0); | |||
1051 | ||||
1052 | Status error; | |||
1053 | ||||
1054 | switch (m_state) { | |||
1055 | case StateType::eStateInvalid: | |||
1056 | case StateType::eStateExited: | |||
1057 | case StateType::eStateCrashed: | |||
1058 | case StateType::eStateDetached: | |||
1059 | case StateType::eStateUnloaded: | |||
1060 | // Nothing to do - the process is already dead. | |||
1061 | LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "ignored for PID {0} due to current state: {1}", GetID (), m_state); } while (0) | |||
1062 | m_state)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "ignored for PID {0} due to current state: {1}", GetID (), m_state); } while (0); | |||
1063 | return error; | |||
1064 | ||||
1065 | case StateType::eStateConnected: | |||
1066 | case StateType::eStateAttaching: | |||
1067 | case StateType::eStateLaunching: | |||
1068 | case StateType::eStateStopped: | |||
1069 | case StateType::eStateRunning: | |||
1070 | case StateType::eStateStepping: | |||
1071 | case StateType::eStateSuspended: | |||
1072 | // We can try to kill a process in these states. | |||
1073 | break; | |||
1074 | } | |||
1075 | ||||
1076 | if (kill(GetID(), SIGKILL9) != 0) { | |||
1077 | error.SetErrorToErrno(); | |||
1078 | return error; | |||
1079 | } | |||
1080 | ||||
1081 | return error; | |||
1082 | } | |||
1083 | ||||
1084 | Status NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, | |||
1085 | MemoryRegionInfo &range_info) { | |||
1086 | // FIXME review that the final memory region returned extends to the end of | |||
1087 | // the virtual address space, | |||
1088 | // with no perms if it is not mapped. | |||
1089 | ||||
1090 | // Use an approach that reads memory regions from /proc/{pid}/maps. Assume | |||
1091 | // proc maps entries are in ascending order. | |||
1092 | // FIXME assert if we find differently. | |||
1093 | ||||
1094 | if (m_supports_mem_region == LazyBool::eLazyBoolNo) { | |||
1095 | // We're done. | |||
1096 | return Status("unsupported"); | |||
1097 | } | |||
1098 | ||||
1099 | Status error = PopulateMemoryRegionCache(); | |||
1100 | if (error.Fail()) { | |||
1101 | return error; | |||
1102 | } | |||
1103 | ||||
1104 | lldb::addr_t prev_base_address = 0; | |||
1105 | ||||
1106 | // FIXME start by finding the last region that is <= target address using | |||
1107 | // binary search. Data is sorted. | |||
1108 | // There can be a ton of regions on pthreads apps with lots of threads. | |||
1109 | for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); | |||
1110 | ++it) { | |||
1111 | MemoryRegionInfo &proc_entry_info = it->first; | |||
1112 | ||||
1113 | // Sanity check assumption that /proc/{pid}/maps entries are ascending. | |||
1114 | assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) &&(static_cast <bool> ((proc_entry_info.GetRange().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected" ) ? void (0) : __assert_fail ("(proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && \"descending /proc/pid/maps entries detected, unexpected\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1115, __extension__ __PRETTY_FUNCTION__)) | |||
1115 | "descending /proc/pid/maps entries detected, unexpected")(static_cast <bool> ((proc_entry_info.GetRange().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected" ) ? void (0) : __assert_fail ("(proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && \"descending /proc/pid/maps entries detected, unexpected\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1115, __extension__ __PRETTY_FUNCTION__)); | |||
1116 | prev_base_address = proc_entry_info.GetRange().GetRangeBase(); | |||
1117 | UNUSED_IF_ASSERT_DISABLED(prev_base_address)((void)(prev_base_address)); | |||
1118 | ||||
1119 | // If the target address comes before this entry, indicate distance to next | |||
1120 | // region. | |||
1121 | if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { | |||
1122 | range_info.GetRange().SetRangeBase(load_addr); | |||
1123 | range_info.GetRange().SetByteSize( | |||
1124 | proc_entry_info.GetRange().GetRangeBase() - load_addr); | |||
1125 | range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); | |||
1126 | range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); | |||
1127 | range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); | |||
1128 | range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); | |||
1129 | ||||
1130 | return error; | |||
1131 | } else if (proc_entry_info.GetRange().Contains(load_addr)) { | |||
1132 | // The target address is within the memory region we're processing here. | |||
1133 | range_info = proc_entry_info; | |||
1134 | return error; | |||
1135 | } | |||
1136 | ||||
1137 | // The target memory address comes somewhere after the region we just | |||
1138 | // parsed. | |||
1139 | } | |||
1140 | ||||
1141 | // If we made it here, we didn't find an entry that contained the given | |||
1142 | // address. Return the load_addr as start and the amount of bytes betwwen | |||
1143 | // load address and the end of the memory as size. | |||
1144 | range_info.GetRange().SetRangeBase(load_addr); | |||
1145 | range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS(18446744073709551615UL)); | |||
1146 | range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); | |||
1147 | range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); | |||
1148 | range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); | |||
1149 | range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); | |||
1150 | return error; | |||
1151 | } | |||
1152 | ||||
1153 | Status NativeProcessLinux::PopulateMemoryRegionCache() { | |||
1154 | Log *log = GetLog(POSIXLog::Process); | |||
1155 | ||||
1156 | // If our cache is empty, pull the latest. There should always be at least | |||
1157 | // one memory region if memory region handling is supported. | |||
1158 | if (!m_mem_region_cache.empty()) { | |||
1159 | LLDB_LOG(log, "reusing {0} cached memory region entries",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "reusing {0} cached memory region entries", m_mem_region_cache .size()); } while (0) | |||
1160 | m_mem_region_cache.size())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "reusing {0} cached memory region entries", m_mem_region_cache .size()); } while (0); | |||
1161 | return Status(); | |||
1162 | } | |||
1163 | ||||
1164 | Status Result; | |||
1165 | LinuxMapCallback callback = [&](llvm::Expected<MemoryRegionInfo> Info) { | |||
1166 | if (Info) { | |||
1167 | FileSpec file_spec(Info->GetName().GetCString()); | |||
1168 | FileSystem::Instance().Resolve(file_spec); | |||
1169 | m_mem_region_cache.emplace_back(*Info, file_spec); | |||
1170 | return true; | |||
1171 | } | |||
1172 | ||||
1173 | Result = Info.takeError(); | |||
1174 | m_supports_mem_region = LazyBool::eLazyBoolNo; | |||
1175 | LLDB_LOG(log, "failed to parse proc maps: {0}", Result)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to parse proc maps: {0}", Result); } while (0); | |||
1176 | return false; | |||
1177 | }; | |||
1178 | ||||
1179 | // Linux kernel since 2.6.14 has /proc/{pid}/smaps | |||
1180 | // if CONFIG_PROC_PAGE_MONITOR is enabled | |||
1181 | auto BufferOrError = getProcFile(GetID(), GetCurrentThreadID(), "smaps"); | |||
1182 | if (BufferOrError) | |||
1183 | ParseLinuxSMapRegions(BufferOrError.get()->getBuffer(), callback); | |||
1184 | else { | |||
1185 | BufferOrError = getProcFile(GetID(), GetCurrentThreadID(), "maps"); | |||
1186 | if (!BufferOrError) { | |||
1187 | m_supports_mem_region = LazyBool::eLazyBoolNo; | |||
1188 | return BufferOrError.getError(); | |||
1189 | } | |||
1190 | ||||
1191 | ParseLinuxMapRegions(BufferOrError.get()->getBuffer(), callback); | |||
1192 | } | |||
1193 | ||||
1194 | if (Result.Fail()) | |||
1195 | return Result; | |||
1196 | ||||
1197 | if (m_mem_region_cache.empty()) { | |||
1198 | // No entries after attempting to read them. This shouldn't happen if | |||
1199 | // /proc/{pid}/maps is supported. Assume we don't support map entries via | |||
1200 | // procfs. | |||
1201 | m_supports_mem_region = LazyBool::eLazyBoolNo; | |||
1202 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to find any procfs maps entries, assuming no support " "for memory region metadata retrieval"); } while (0) | |||
1203 | "failed to find any procfs maps entries, assuming no support "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to find any procfs maps entries, assuming no support " "for memory region metadata retrieval"); } while (0) | |||
1204 | "for memory region metadata retrieval")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to find any procfs maps entries, assuming no support " "for memory region metadata retrieval"); } while (0); | |||
1205 | return Status("not supported"); | |||
1206 | } | |||
1207 | ||||
1208 | LLDB_LOG(log, "read {0} memory region entries from /proc/{1}/maps",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "read {0} memory region entries from /proc/{1}/maps" , m_mem_region_cache.size(), GetID()); } while (0) | |||
1209 | m_mem_region_cache.size(), GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "read {0} memory region entries from /proc/{1}/maps" , m_mem_region_cache.size(), GetID()); } while (0); | |||
1210 | ||||
1211 | // We support memory retrieval, remember that. | |||
1212 | m_supports_mem_region = LazyBool::eLazyBoolYes; | |||
1213 | return Status(); | |||
1214 | } | |||
1215 | ||||
1216 | void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { | |||
1217 | Log *log = GetLog(POSIXLog::Process); | |||
1218 | LLDB_LOG(log, "newBumpId={0}", newBumpId)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "newBumpId={0}", newBumpId); } while (0); | |||
1219 | LLDB_LOG(log, "clearing {0} entries from memory region cache",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "clearing {0} entries from memory region cache", m_mem_region_cache .size()); } while (0) | |||
1220 | m_mem_region_cache.size())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "clearing {0} entries from memory region cache", m_mem_region_cache .size()); } while (0); | |||
1221 | m_mem_region_cache.clear(); | |||
1222 | } | |||
1223 | ||||
1224 | llvm::Expected<uint64_t> | |||
1225 | NativeProcessLinux::Syscall(llvm::ArrayRef<uint64_t> args) { | |||
1226 | PopulateMemoryRegionCache(); | |||
1227 | auto region_it = llvm::find_if(m_mem_region_cache, [](const auto &pair) { | |||
1228 | return pair.first.GetExecutable() == MemoryRegionInfo::eYes; | |||
1229 | }); | |||
1230 | if (region_it == m_mem_region_cache.end()) | |||
1231 | return llvm::createStringError(llvm::inconvertibleErrorCode(), | |||
1232 | "No executable memory region found!"); | |||
1233 | ||||
1234 | addr_t exe_addr = region_it->first.GetRange().GetRangeBase(); | |||
1235 | ||||
1236 | NativeThreadLinux &thread = *GetCurrentThread(); | |||
1237 | assert(thread.GetState() == eStateStopped)(static_cast <bool> (thread.GetState() == eStateStopped ) ? void (0) : __assert_fail ("thread.GetState() == eStateStopped" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1237, __extension__ __PRETTY_FUNCTION__)); | |||
1238 | NativeRegisterContextLinux ®_ctx = thread.GetRegisterContext(); | |||
1239 | ||||
1240 | NativeRegisterContextLinux::SyscallData syscall_data = | |||
1241 | *reg_ctx.GetSyscallData(); | |||
1242 | ||||
1243 | WritableDataBufferSP registers_sp; | |||
1244 | if (llvm::Error Err = reg_ctx.ReadAllRegisterValues(registers_sp).ToError()) | |||
1245 | return std::move(Err); | |||
1246 | auto restore_regs = llvm::make_scope_exit( | |||
1247 | [&] { reg_ctx.WriteAllRegisterValues(registers_sp); }); | |||
1248 | ||||
1249 | llvm::SmallVector<uint8_t, 8> memory(syscall_data.Insn.size()); | |||
1250 | size_t bytes_read; | |||
1251 | if (llvm::Error Err = | |||
1252 | ReadMemory(exe_addr, memory.data(), memory.size(), bytes_read) | |||
1253 | .ToError()) { | |||
1254 | return std::move(Err); | |||
1255 | } | |||
1256 | ||||
1257 | auto restore_mem = llvm::make_scope_exit( | |||
1258 | [&] { WriteMemory(exe_addr, memory.data(), memory.size(), bytes_read); }); | |||
1259 | ||||
1260 | if (llvm::Error Err = reg_ctx.SetPC(exe_addr).ToError()) | |||
1261 | return std::move(Err); | |||
1262 | ||||
1263 | for (const auto &zip : llvm::zip_first(args, syscall_data.Args)) { | |||
1264 | if (llvm::Error Err = | |||
1265 | reg_ctx | |||
1266 | .WriteRegisterFromUnsigned(std::get<1>(zip), std::get<0>(zip)) | |||
1267 | .ToError()) { | |||
1268 | return std::move(Err); | |||
1269 | } | |||
1270 | } | |||
1271 | if (llvm::Error Err = WriteMemory(exe_addr, syscall_data.Insn.data(), | |||
1272 | syscall_data.Insn.size(), bytes_read) | |||
1273 | .ToError()) | |||
1274 | return std::move(Err); | |||
1275 | ||||
1276 | m_mem_region_cache.clear(); | |||
1277 | ||||
1278 | // With software single stepping the syscall insn buffer must also include a | |||
1279 | // trap instruction to stop the process. | |||
1280 | int req = SupportHardwareSingleStepping() ? PTRACE_SINGLESTEP : PTRACE_CONT; | |||
1281 | if (llvm::Error Err = | |||
1282 | PtraceWrapper(req, thread.GetID(), nullptr, nullptr).ToError()) | |||
1283 | return std::move(Err); | |||
1284 | ||||
1285 | int status; | |||
1286 | ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, ::waitpid, thread.GetID(), | |||
1287 | &status, __WALL0x40000000); | |||
1288 | if (wait_pid == -1) { | |||
1289 | return llvm::errorCodeToError( | |||
1290 | std::error_code(errno(*__errno_location ()), std::generic_category())); | |||
1291 | } | |||
1292 | assert((unsigned)wait_pid == thread.GetID())(static_cast <bool> ((unsigned)wait_pid == thread.GetID ()) ? void (0) : __assert_fail ("(unsigned)wait_pid == thread.GetID()" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1292, __extension__ __PRETTY_FUNCTION__)); | |||
1293 | ||||
1294 | uint64_t result = reg_ctx.ReadRegisterAsUnsigned(syscall_data.Result, -ESRCH3); | |||
1295 | ||||
1296 | // Values larger than this are actually negative errno numbers. | |||
1297 | uint64_t errno_threshold = | |||
1298 | (uint64_t(-1) >> (64 - 8 * m_arch.GetAddressByteSize())) - 0x1000; | |||
1299 | if (result > errno_threshold) { | |||
1300 | return llvm::errorCodeToError( | |||
1301 | std::error_code(-result & 0xfff, std::generic_category())); | |||
1302 | } | |||
1303 | ||||
1304 | return result; | |||
1305 | } | |||
1306 | ||||
1307 | llvm::Expected<addr_t> | |||
1308 | NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions) { | |||
1309 | ||||
1310 | llvm::Optional<NativeRegisterContextLinux::MmapData> mmap_data = | |||
1311 | GetCurrentThread()->GetRegisterContext().GetMmapData(); | |||
1312 | if (!mmap_data) | |||
1313 | return llvm::make_error<UnimplementedError>(); | |||
1314 | ||||
1315 | unsigned prot = PROT_NONE0x0; | |||
1316 | assert((permissions & (ePermissionsReadable | ePermissionsWritable |(static_cast <bool> ((permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && "Unknown permission!") ? void (0) : __assert_fail ("(permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && \"Unknown permission!\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1318, __extension__ __PRETTY_FUNCTION__)) | |||
1317 | ePermissionsExecutable)) == permissions &&(static_cast <bool> ((permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && "Unknown permission!") ? void (0) : __assert_fail ("(permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && \"Unknown permission!\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1318, __extension__ __PRETTY_FUNCTION__)) | |||
1318 | "Unknown permission!")(static_cast <bool> ((permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && "Unknown permission!") ? void (0) : __assert_fail ("(permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && \"Unknown permission!\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1318, __extension__ __PRETTY_FUNCTION__)); | |||
1319 | if (permissions & ePermissionsReadable) | |||
1320 | prot |= PROT_READ0x1; | |||
1321 | if (permissions & ePermissionsWritable) | |||
1322 | prot |= PROT_WRITE0x2; | |||
1323 | if (permissions & ePermissionsExecutable) | |||
1324 | prot |= PROT_EXEC0x4; | |||
1325 | ||||
1326 | llvm::Expected<uint64_t> Result = | |||
1327 | Syscall({mmap_data->SysMmap, 0, size, prot, MAP_ANONYMOUS0x20 | MAP_PRIVATE0x02, | |||
1328 | uint64_t(-1), 0}); | |||
1329 | if (Result) | |||
1330 | m_allocated_memory.try_emplace(*Result, size); | |||
1331 | return Result; | |||
1332 | } | |||
1333 | ||||
1334 | llvm::Error NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { | |||
1335 | llvm::Optional<NativeRegisterContextLinux::MmapData> mmap_data = | |||
1336 | GetCurrentThread()->GetRegisterContext().GetMmapData(); | |||
1337 | if (!mmap_data) | |||
1338 | return llvm::make_error<UnimplementedError>(); | |||
1339 | ||||
1340 | auto it = m_allocated_memory.find(addr); | |||
1341 | if (it == m_allocated_memory.end()) | |||
1342 | return llvm::createStringError(llvm::errc::invalid_argument, | |||
1343 | "Memory not allocated by the debugger."); | |||
1344 | ||||
1345 | llvm::Expected<uint64_t> Result = | |||
1346 | Syscall({mmap_data->SysMunmap, addr, it->second}); | |||
1347 | if (!Result) | |||
1348 | return Result.takeError(); | |||
1349 | ||||
1350 | m_allocated_memory.erase(it); | |||
1351 | return llvm::Error::success(); | |||
1352 | } | |||
1353 | ||||
1354 | Status NativeProcessLinux::ReadMemoryTags(int32_t type, lldb::addr_t addr, | |||
1355 | size_t len, | |||
1356 | std::vector<uint8_t> &tags) { | |||
1357 | llvm::Expected<NativeRegisterContextLinux::MemoryTaggingDetails> details = | |||
1358 | GetCurrentThread()->GetRegisterContext().GetMemoryTaggingDetails(type); | |||
1359 | if (!details) | |||
1360 | return Status(details.takeError()); | |||
1361 | ||||
1362 | // Ignore 0 length read | |||
1363 | if (!len) | |||
1364 | return Status(); | |||
1365 | ||||
1366 | // lldb will align the range it requests but it is not required to by | |||
1367 | // the protocol so we'll do it again just in case. | |||
1368 | // Remove tag bits too. Ptrace calls may work regardless but that | |||
1369 | // is not a guarantee. | |||
1370 | MemoryTagManager::TagRange range(details->manager->RemoveTagBits(addr), len); | |||
1371 | range = details->manager->ExpandToGranule(range); | |||
1372 | ||||
1373 | // Allocate enough space for all tags to be read | |||
1374 | size_t num_tags = range.GetByteSize() / details->manager->GetGranuleSize(); | |||
1375 | tags.resize(num_tags * details->manager->GetTagSizeInBytes()); | |||
1376 | ||||
1377 | struct iovec tags_iovec; | |||
1378 | uint8_t *dest = tags.data(); | |||
1379 | lldb::addr_t read_addr = range.GetRangeBase(); | |||
1380 | ||||
1381 | // This call can return partial data so loop until we error or | |||
1382 | // get all tags back. | |||
1383 | while (num_tags) { | |||
1384 | tags_iovec.iov_base = dest; | |||
1385 | tags_iovec.iov_len = num_tags; | |||
1386 | ||||
1387 | Status error = NativeProcessLinux::PtraceWrapper( | |||
1388 | details->ptrace_read_req, GetCurrentThreadID(), | |||
1389 | reinterpret_cast<void *>(read_addr), static_cast<void *>(&tags_iovec), | |||
1390 | 0, nullptr); | |||
1391 | ||||
1392 | if (error.Fail()) { | |||
1393 | // Discard partial reads | |||
1394 | tags.resize(0); | |||
1395 | return error; | |||
1396 | } | |||
1397 | ||||
1398 | size_t tags_read = tags_iovec.iov_len; | |||
1399 | assert(tags_read && (tags_read <= num_tags))(static_cast <bool> (tags_read && (tags_read <= num_tags)) ? void (0) : __assert_fail ("tags_read && (tags_read <= num_tags)" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1399, __extension__ __PRETTY_FUNCTION__)); | |||
1400 | ||||
1401 | dest += tags_read * details->manager->GetTagSizeInBytes(); | |||
1402 | read_addr += details->manager->GetGranuleSize() * tags_read; | |||
1403 | num_tags -= tags_read; | |||
1404 | } | |||
1405 | ||||
1406 | return Status(); | |||
1407 | } | |||
1408 | ||||
1409 | Status NativeProcessLinux::WriteMemoryTags(int32_t type, lldb::addr_t addr, | |||
1410 | size_t len, | |||
1411 | const std::vector<uint8_t> &tags) { | |||
1412 | llvm::Expected<NativeRegisterContextLinux::MemoryTaggingDetails> details = | |||
1413 | GetCurrentThread()->GetRegisterContext().GetMemoryTaggingDetails(type); | |||
1414 | if (!details) | |||
| ||||
1415 | return Status(details.takeError()); | |||
1416 | ||||
1417 | // Ignore 0 length write | |||
1418 | if (!len) | |||
1419 | return Status(); | |||
1420 | ||||
1421 | // lldb will align the range it requests but it is not required to by | |||
1422 | // the protocol so we'll do it again just in case. | |||
1423 | // Remove tag bits too. Ptrace calls may work regardless but that | |||
1424 | // is not a guarantee. | |||
1425 | MemoryTagManager::TagRange range(details->manager->RemoveTagBits(addr), len); | |||
1426 | range = details->manager->ExpandToGranule(range); | |||
1427 | ||||
1428 | // Not checking number of tags here, we may repeat them below | |||
1429 | llvm::Expected<std::vector<lldb::addr_t>> unpacked_tags_or_err = | |||
1430 | details->manager->UnpackTagsData(tags); | |||
1431 | if (!unpacked_tags_or_err) | |||
1432 | return Status(unpacked_tags_or_err.takeError()); | |||
1433 | ||||
1434 | llvm::Expected<std::vector<lldb::addr_t>> repeated_tags_or_err = | |||
1435 | details->manager->RepeatTagsForRange(*unpacked_tags_or_err, range); | |||
1436 | if (!repeated_tags_or_err) | |||
1437 | return Status(repeated_tags_or_err.takeError()); | |||
1438 | ||||
1439 | // Repack them for ptrace to use | |||
1440 | llvm::Expected<std::vector<uint8_t>> final_tag_data = | |||
1441 | details->manager->PackTags(*repeated_tags_or_err); | |||
1442 | if (!final_tag_data) | |||
1443 | return Status(final_tag_data.takeError()); | |||
1444 | ||||
1445 | struct iovec tags_vec; | |||
1446 | uint8_t *src = final_tag_data->data(); | |||
1447 | lldb::addr_t write_addr = range.GetRangeBase(); | |||
1448 | // unpacked tags size because the number of bytes per tag might not be 1 | |||
1449 | size_t num_tags = repeated_tags_or_err->size(); | |||
1450 | ||||
1451 | // This call can partially write tags, so we loop until we | |||
1452 | // error or all tags have been written. | |||
1453 | while (num_tags > 0) { | |||
1454 | tags_vec.iov_base = src; | |||
1455 | tags_vec.iov_len = num_tags; | |||
1456 | ||||
1457 | Status error = NativeProcessLinux::PtraceWrapper( | |||
1458 | details->ptrace_write_req, GetCurrentThreadID(), | |||
1459 | reinterpret_cast<void *>(write_addr), static_cast<void *>(&tags_vec), 0, | |||
1460 | nullptr); | |||
1461 | ||||
1462 | if (error.Fail()) { | |||
1463 | // Don't attempt to restore the original values in the case of a partial | |||
1464 | // write | |||
1465 | return error; | |||
1466 | } | |||
1467 | ||||
1468 | size_t tags_written = tags_vec.iov_len; | |||
1469 | assert(tags_written && (tags_written <= num_tags))(static_cast <bool> (tags_written && (tags_written <= num_tags)) ? void (0) : __assert_fail ("tags_written && (tags_written <= num_tags)" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1469, __extension__ __PRETTY_FUNCTION__)); | |||
1470 | ||||
1471 | src += tags_written * details->manager->GetTagSizeInBytes(); | |||
1472 | write_addr += details->manager->GetGranuleSize() * tags_written; | |||
1473 | num_tags -= tags_written; | |||
1474 | } | |||
1475 | ||||
1476 | return Status(); | |||
1477 | } | |||
1478 | ||||
1479 | size_t NativeProcessLinux::UpdateThreads() { | |||
1480 | // The NativeProcessLinux monitoring threads are always up to date with | |||
1481 | // respect to thread state and they keep the thread list populated properly. | |||
1482 | // All this method needs to do is return the thread count. | |||
1483 | return m_threads.size(); | |||
1484 | } | |||
1485 | ||||
1486 | Status NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, | |||
1487 | bool hardware) { | |||
1488 | if (hardware) | |||
1489 | return SetHardwareBreakpoint(addr, size); | |||
1490 | else | |||
1491 | return SetSoftwareBreakpoint(addr, size); | |||
1492 | } | |||
1493 | ||||
1494 | Status NativeProcessLinux::RemoveBreakpoint(lldb::addr_t addr, bool hardware) { | |||
1495 | if (hardware) | |||
1496 | return RemoveHardwareBreakpoint(addr); | |||
1497 | else | |||
1498 | return NativeProcessProtocol::RemoveBreakpoint(addr); | |||
1499 | } | |||
1500 | ||||
1501 | llvm::Expected<llvm::ArrayRef<uint8_t>> | |||
1502 | NativeProcessLinux::GetSoftwareBreakpointTrapOpcode(size_t size_hint) { | |||
1503 | // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the | |||
1504 | // linux kernel does otherwise. | |||
1505 | static const uint8_t g_arm_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; | |||
1506 | static const uint8_t g_thumb_opcode[] = {0x01, 0xde}; | |||
1507 | ||||
1508 | switch (GetArchitecture().GetMachine()) { | |||
1509 | case llvm::Triple::arm: | |||
1510 | switch (size_hint) { | |||
1511 | case 2: | |||
1512 | return llvm::makeArrayRef(g_thumb_opcode); | |||
1513 | case 4: | |||
1514 | return llvm::makeArrayRef(g_arm_opcode); | |||
1515 | default: | |||
1516 | return llvm::createStringError(llvm::inconvertibleErrorCode(), | |||
1517 | "Unrecognised trap opcode size hint!"); | |||
1518 | } | |||
1519 | default: | |||
1520 | return NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_hint); | |||
1521 | } | |||
1522 | } | |||
1523 | ||||
1524 | Status NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, | |||
1525 | size_t &bytes_read) { | |||
1526 | if (ProcessVmReadvSupported()) { | |||
1527 | // The process_vm_readv path is about 50 times faster than ptrace api. We | |||
1528 | // want to use this syscall if it is supported. | |||
1529 | ||||
1530 | struct iovec local_iov, remote_iov; | |||
1531 | local_iov.iov_base = buf; | |||
1532 | local_iov.iov_len = size; | |||
1533 | remote_iov.iov_base = reinterpret_cast<void *>(addr); | |||
1534 | remote_iov.iov_len = size; | |||
1535 | ||||
1536 | bytes_read = process_vm_readv(GetCurrentThreadID(), &local_iov, 1, | |||
1537 | &remote_iov, 1, 0); | |||
1538 | const bool success = bytes_read == size; | |||
1539 | ||||
1540 | Log *log = GetLog(POSIXLog::Process); | |||
1541 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "using process_vm_readv to read {0} bytes from inferior " "address {1:x}: {2}", size, addr, success ? "Success" : llvm ::sys::StrError((*__errno_location ()))); } while (0) | |||
1542 | "using process_vm_readv to read {0} bytes from inferior "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "using process_vm_readv to read {0} bytes from inferior " "address {1:x}: {2}", size, addr, success ? "Success" : llvm ::sys::StrError((*__errno_location ()))); } while (0) | |||
1543 | "address {1:x}: {2}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "using process_vm_readv to read {0} bytes from inferior " "address {1:x}: {2}", size, addr, success ? "Success" : llvm ::sys::StrError((*__errno_location ()))); } while (0) | |||
1544 | size, addr, success ? "Success" : llvm::sys::StrError(errno))do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "using process_vm_readv to read {0} bytes from inferior " "address {1:x}: {2}", size, addr, success ? "Success" : llvm ::sys::StrError((*__errno_location ()))); } while (0); | |||
1545 | ||||
1546 | if (success) | |||
1547 | return Status(); | |||
1548 | // else the call failed for some reason, let's retry the read using ptrace | |||
1549 | // api. | |||
1550 | } | |||
1551 | ||||
1552 | unsigned char *dst = static_cast<unsigned char *>(buf); | |||
1553 | size_t remainder; | |||
1554 | long data; | |||
1555 | ||||
1556 | Log *log = GetLog(POSIXLog::Memory); | |||
1557 | LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "addr = {0}, buf = {1}, size = {2}", addr, buf, size ); } while (0); | |||
1558 | ||||
1559 | for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { | |||
1560 | Status error = NativeProcessLinux::PtraceWrapper( | |||
1561 | PTRACE_PEEKDATA, GetCurrentThreadID(), (void *)addr, nullptr, 0, &data); | |||
1562 | if (error.Fail()) | |||
1563 | return error; | |||
1564 | ||||
1565 | remainder = size - bytes_read; | |||
1566 | remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; | |||
1567 | ||||
1568 | // Copy the data into our buffer | |||
1569 | memcpy(dst, &data, remainder); | |||
1570 | ||||
1571 | LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "[{0:x}]:{1:x}", addr, data); } while (0); | |||
1572 | addr += k_ptrace_word_size; | |||
1573 | dst += k_ptrace_word_size; | |||
1574 | } | |||
1575 | return Status(); | |||
1576 | } | |||
1577 | ||||
1578 | Status NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, | |||
1579 | size_t size, size_t &bytes_written) { | |||
1580 | const unsigned char *src = static_cast<const unsigned char *>(buf); | |||
1581 | size_t remainder; | |||
1582 | Status error; | |||
1583 | ||||
1584 | Log *log = GetLog(POSIXLog::Memory); | |||
1585 | LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "addr = {0}, buf = {1}, size = {2}", addr, buf, size ); } while (0); | |||
1586 | ||||
1587 | for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { | |||
1588 | remainder = size - bytes_written; | |||
1589 | remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; | |||
1590 | ||||
1591 | if (remainder == k_ptrace_word_size) { | |||
1592 | unsigned long data = 0; | |||
1593 | memcpy(&data, src, k_ptrace_word_size); | |||
1594 | ||||
1595 | LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "[{0:x}]:{1:x}", addr, data); } while (0); | |||
1596 | error = NativeProcessLinux::PtraceWrapper( | |||
1597 | PTRACE_POKEDATA, GetCurrentThreadID(), (void *)addr, (void *)data); | |||
1598 | if (error.Fail()) | |||
1599 | return error; | |||
1600 | } else { | |||
1601 | unsigned char buff[8]; | |||
1602 | size_t bytes_read; | |||
1603 | error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); | |||
1604 | if (error.Fail()) | |||
1605 | return error; | |||
1606 | ||||
1607 | memcpy(buff, src, remainder); | |||
1608 | ||||
1609 | size_t bytes_written_rec; | |||
1610 | error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); | |||
1611 | if (error.Fail()) | |||
1612 | return error; | |||
1613 | ||||
1614 | LLDB_LOG(log, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, *(unsigned long *)buff); } while (0) | |||
1615 | *(unsigned long *)buff)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, *(unsigned long *)buff); } while (0); | |||
1616 | } | |||
1617 | ||||
1618 | addr += k_ptrace_word_size; | |||
1619 | src += k_ptrace_word_size; | |||
1620 | } | |||
1621 | return error; | |||
1622 | } | |||
1623 | ||||
1624 | Status NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) const { | |||
1625 | return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); | |||
1626 | } | |||
1627 | ||||
1628 | Status NativeProcessLinux::GetEventMessage(lldb::tid_t tid, | |||
1629 | unsigned long *message) { | |||
1630 | return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); | |||
1631 | } | |||
1632 | ||||
1633 | Status NativeProcessLinux::Detach(lldb::tid_t tid) { | |||
1634 | if (tid == LLDB_INVALID_THREAD_ID0) | |||
1635 | return Status(); | |||
1636 | ||||
1637 | return PtraceWrapper(PTRACE_DETACH, tid); | |||
1638 | } | |||
1639 | ||||
1640 | bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { | |||
1641 | for (const auto &thread : m_threads) { | |||
1642 | assert(thread && "thread list should not contain NULL threads")(static_cast <bool> (thread && "thread list should not contain NULL threads" ) ? void (0) : __assert_fail ("thread && \"thread list should not contain NULL threads\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1642, __extension__ __PRETTY_FUNCTION__)); | |||
1643 | if (thread->GetID() == thread_id) { | |||
1644 | // We have this thread. | |||
1645 | return true; | |||
1646 | } | |||
1647 | } | |||
1648 | ||||
1649 | // We don't have this thread. | |||
1650 | return false; | |||
1651 | } | |||
1652 | ||||
1653 | void NativeProcessLinux::StopTrackingThread(NativeThreadLinux &thread) { | |||
1654 | Log *const log = GetLog(POSIXLog::Thread); | |||
1655 | lldb::tid_t thread_id = thread.GetID(); | |||
1656 | LLDB_LOG(log, "tid: {0}", thread_id)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "tid: {0}", thread_id); } while (0); | |||
1657 | ||||
1658 | auto it = llvm::find_if(m_threads, [&](const auto &thread_up) { | |||
1659 | return thread_up.get() == &thread; | |||
1660 | }); | |||
1661 | assert(it != m_threads.end())(static_cast <bool> (it != m_threads.end()) ? void (0) : __assert_fail ("it != m_threads.end()", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , 1661, __extension__ __PRETTY_FUNCTION__)); | |||
1662 | m_threads.erase(it); | |||
1663 | ||||
1664 | NotifyTracersOfThreadDestroyed(thread_id); | |||
1665 | SignalIfAllThreadsStopped(); | |||
1666 | } | |||
1667 | ||||
1668 | Status NativeProcessLinux::NotifyTracersOfNewThread(lldb::tid_t tid) { | |||
1669 | Log *log = GetLog(POSIXLog::Thread); | |||
1670 | Status error(m_intel_pt_collector.OnThreadCreated(tid)); | |||
1671 | if (error.Fail()) | |||
1672 | LLDB_LOG(log, "Failed to trace a new thread with intel-pt, tid = {0}. {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Failed to trace a new thread with intel-pt, tid = {0}. {1}" , tid, error.AsCString()); } while (0) | |||
1673 | tid, error.AsCString())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Failed to trace a new thread with intel-pt, tid = {0}. {1}" , tid, error.AsCString()); } while (0); | |||
1674 | return error; | |||
1675 | } | |||
1676 | ||||
1677 | Status NativeProcessLinux::NotifyTracersOfThreadDestroyed(lldb::tid_t tid) { | |||
1678 | Log *log = GetLog(POSIXLog::Thread); | |||
1679 | Status error(m_intel_pt_collector.OnThreadDestroyed(tid)); | |||
1680 | if (error.Fail()) | |||
1681 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Failed to stop a destroyed thread with intel-pt, tid = {0}. {1}" , tid, error.AsCString()); } while (0) | |||
1682 | "Failed to stop a destroyed thread with intel-pt, tid = {0}. {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Failed to stop a destroyed thread with intel-pt, tid = {0}. {1}" , tid, error.AsCString()); } while (0) | |||
1683 | tid, error.AsCString())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Failed to stop a destroyed thread with intel-pt, tid = {0}. {1}" , tid, error.AsCString()); } while (0); | |||
1684 | return error; | |||
1685 | } | |||
1686 | ||||
1687 | NativeThreadLinux &NativeProcessLinux::AddThread(lldb::tid_t thread_id, | |||
1688 | bool resume) { | |||
1689 | Log *log = GetLog(POSIXLog::Thread); | |||
1690 | LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} adding thread with tid {1}", GetID(), thread_id ); } while (0); | |||
1691 | ||||
1692 | assert(!HasThreadNoLock(thread_id) &&(static_cast <bool> (!HasThreadNoLock(thread_id) && "attempted to add a thread by id that already exists") ? void (0) : __assert_fail ("!HasThreadNoLock(thread_id) && \"attempted to add a thread by id that already exists\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1693, __extension__ __PRETTY_FUNCTION__)) | |||
1693 | "attempted to add a thread by id that already exists")(static_cast <bool> (!HasThreadNoLock(thread_id) && "attempted to add a thread by id that already exists") ? void (0) : __assert_fail ("!HasThreadNoLock(thread_id) && \"attempted to add a thread by id that already exists\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1693, __extension__ __PRETTY_FUNCTION__)); | |||
1694 | ||||
1695 | // If this is the first thread, save it as the current thread | |||
1696 | if (m_threads.empty()) | |||
1697 | SetCurrentThreadID(thread_id); | |||
1698 | ||||
1699 | m_threads.push_back(std::make_unique<NativeThreadLinux>(*this, thread_id)); | |||
1700 | NativeThreadLinux &thread = | |||
1701 | static_cast<NativeThreadLinux &>(*m_threads.back()); | |||
1702 | ||||
1703 | Status tracing_error = NotifyTracersOfNewThread(thread.GetID()); | |||
1704 | if (tracing_error.Fail()) { | |||
1705 | thread.SetStoppedByProcessorTrace(tracing_error.AsCString()); | |||
1706 | StopRunningThreads(thread.GetID()); | |||
1707 | } else if (resume) | |||
1708 | ResumeThread(thread, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
1709 | else | |||
1710 | thread.SetStoppedBySignal(SIGSTOP19); | |||
1711 | ||||
1712 | return thread; | |||
1713 | } | |||
1714 | ||||
1715 | Status NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, | |||
1716 | FileSpec &file_spec) { | |||
1717 | Status error = PopulateMemoryRegionCache(); | |||
1718 | if (error.Fail()) | |||
1719 | return error; | |||
1720 | ||||
1721 | FileSpec module_file_spec(module_path); | |||
1722 | FileSystem::Instance().Resolve(module_file_spec); | |||
1723 | ||||
1724 | file_spec.Clear(); | |||
1725 | for (const auto &it : m_mem_region_cache) { | |||
1726 | if (it.second.GetFilename() == module_file_spec.GetFilename()) { | |||
1727 | file_spec = it.second; | |||
1728 | return Status(); | |||
1729 | } | |||
1730 | } | |||
1731 | return Status("Module file (%s) not found in /proc/%" PRIu64"l" "u" "/maps file!", | |||
1732 | module_file_spec.GetFilename().AsCString(), GetID()); | |||
1733 | } | |||
1734 | ||||
1735 | Status NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, | |||
1736 | lldb::addr_t &load_addr) { | |||
1737 | load_addr = LLDB_INVALID_ADDRESS(18446744073709551615UL); | |||
1738 | Status error = PopulateMemoryRegionCache(); | |||
1739 | if (error.Fail()) | |||
1740 | return error; | |||
1741 | ||||
1742 | FileSpec file(file_name); | |||
1743 | for (const auto &it : m_mem_region_cache) { | |||
1744 | if (it.second == file) { | |||
1745 | load_addr = it.first.GetRange().GetRangeBase(); | |||
1746 | return Status(); | |||
1747 | } | |||
1748 | } | |||
1749 | return Status("No load address found for specified file."); | |||
1750 | } | |||
1751 | ||||
1752 | NativeThreadLinux *NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { | |||
1753 | return static_cast<NativeThreadLinux *>( | |||
1754 | NativeProcessProtocol::GetThreadByID(tid)); | |||
1755 | } | |||
1756 | ||||
1757 | NativeThreadLinux *NativeProcessLinux::GetCurrentThread() { | |||
1758 | return static_cast<NativeThreadLinux *>( | |||
1759 | NativeProcessProtocol::GetCurrentThread()); | |||
1760 | } | |||
1761 | ||||
1762 | Status NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, | |||
1763 | lldb::StateType state, int signo) { | |||
1764 | Log *const log = GetLog(POSIXLog::Thread); | |||
1765 | LLDB_LOG(log, "tid: {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "tid: {0}", thread.GetID()); } while (0); | |||
1766 | ||||
1767 | // Before we do the resume below, first check if we have a pending stop | |||
1768 | // notification that is currently waiting for all threads to stop. This is | |||
1769 | // potentially a buggy situation since we're ostensibly waiting for threads | |||
1770 | // to stop before we send out the pending notification, and here we are | |||
1771 | // resuming one before we send out the pending stop notification. | |||
1772 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID0) { | |||
1773 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to resume tid {0} per explicit request but we have a " "pending stop notification (tid {1}) that is actively " "waiting for this thread to stop. Valid sequence of events?" , thread.GetID(), m_pending_notification_tid); } while (0) | |||
1774 | "about to resume tid {0} per explicit request but we have a "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to resume tid {0} per explicit request but we have a " "pending stop notification (tid {1}) that is actively " "waiting for this thread to stop. Valid sequence of events?" , thread.GetID(), m_pending_notification_tid); } while (0) | |||
1775 | "pending stop notification (tid {1}) that is actively "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to resume tid {0} per explicit request but we have a " "pending stop notification (tid {1}) that is actively " "waiting for this thread to stop. Valid sequence of events?" , thread.GetID(), m_pending_notification_tid); } while (0) | |||
1776 | "waiting for this thread to stop. Valid sequence of events?",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to resume tid {0} per explicit request but we have a " "pending stop notification (tid {1}) that is actively " "waiting for this thread to stop. Valid sequence of events?" , thread.GetID(), m_pending_notification_tid); } while (0) | |||
1777 | thread.GetID(), m_pending_notification_tid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to resume tid {0} per explicit request but we have a " "pending stop notification (tid {1}) that is actively " "waiting for this thread to stop. Valid sequence of events?" , thread.GetID(), m_pending_notification_tid); } while (0); | |||
1778 | } | |||
1779 | ||||
1780 | // Request a resume. We expect this to be synchronous and the system to | |||
1781 | // reflect it is running after this completes. | |||
1782 | switch (state) { | |||
1783 | case eStateRunning: { | |||
1784 | const auto resume_result = thread.Resume(signo); | |||
1785 | if (resume_result.Success()) | |||
1786 | SetState(eStateRunning, true); | |||
1787 | return resume_result; | |||
1788 | } | |||
1789 | case eStateStepping: { | |||
1790 | const auto step_result = thread.SingleStep(signo); | |||
1791 | if (step_result.Success()) | |||
1792 | SetState(eStateRunning, true); | |||
1793 | return step_result; | |||
1794 | } | |||
1795 | default: | |||
1796 | LLDB_LOG(log, "Unhandled state {0}.", state)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Unhandled state {0}.", state); } while (0); | |||
1797 | llvm_unreachable("Unhandled state for resume")::llvm::llvm_unreachable_internal("Unhandled state for resume" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1797); | |||
1798 | } | |||
1799 | } | |||
1800 | ||||
1801 | //===----------------------------------------------------------------------===// | |||
1802 | ||||
1803 | void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { | |||
1804 | Log *const log = GetLog(POSIXLog::Thread); | |||
1805 | LLDB_LOG(log, "about to process event: (triggering_tid: {0})",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to process event: (triggering_tid: {0})", triggering_tid ); } while (0) | |||
1806 | triggering_tid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to process event: (triggering_tid: {0})", triggering_tid ); } while (0); | |||
1807 | ||||
1808 | m_pending_notification_tid = triggering_tid; | |||
1809 | ||||
1810 | // Request a stop for all the thread stops that need to be stopped and are | |||
1811 | // not already known to be stopped. | |||
1812 | for (const auto &thread : m_threads) { | |||
1813 | if (StateIsRunningState(thread->GetState())) | |||
1814 | static_cast<NativeThreadLinux *>(thread.get())->RequestStop(); | |||
1815 | } | |||
1816 | ||||
1817 | SignalIfAllThreadsStopped(); | |||
1818 | LLDB_LOG(log, "event processing done")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "event processing done"); } while (0); | |||
1819 | } | |||
1820 | ||||
1821 | void NativeProcessLinux::SignalIfAllThreadsStopped() { | |||
1822 | if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID0) | |||
1823 | return; // No pending notification. Nothing to do. | |||
1824 | ||||
1825 | for (const auto &thread_sp : m_threads) { | |||
1826 | if (StateIsRunningState(thread_sp->GetState())) | |||
1827 | return; // Some threads are still running. Don't signal yet. | |||
1828 | } | |||
1829 | ||||
1830 | // We have a pending notification and all threads have stopped. | |||
1831 | Log *log = GetLog(LLDBLog::Process | LLDBLog::Breakpoints); | |||
1832 | ||||
1833 | // Clear any temporary breakpoints we used to implement software single | |||
1834 | // stepping. | |||
1835 | for (const auto &thread_info : m_threads_stepping_with_breakpoint) { | |||
1836 | Status error = RemoveBreakpoint(thread_info.second); | |||
1837 | if (error.Fail()) | |||
1838 | LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid = {0} remove stepping breakpoint: {1}", thread_info .first, error); } while (0) | |||
1839 | thread_info.first, error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid = {0} remove stepping breakpoint: {1}", thread_info .first, error); } while (0); | |||
1840 | } | |||
1841 | m_threads_stepping_with_breakpoint.clear(); | |||
1842 | ||||
1843 | // Notify the delegate about the stop | |||
1844 | SetCurrentThreadID(m_pending_notification_tid); | |||
1845 | SetState(StateType::eStateStopped, true); | |||
1846 | m_pending_notification_tid = LLDB_INVALID_THREAD_ID0; | |||
1847 | } | |||
1848 | ||||
1849 | void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { | |||
1850 | Log *const log = GetLog(POSIXLog::Thread); | |||
1851 | LLDB_LOG(log, "tid: {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "tid: {0}", thread.GetID()); } while (0); | |||
1852 | ||||
1853 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID0 && | |||
1854 | StateIsRunningState(thread.GetState())) { | |||
1855 | // We will need to wait for this new thread to stop as well before firing | |||
1856 | // the notification. | |||
1857 | thread.RequestStop(); | |||
1858 | } | |||
1859 | } | |||
1860 | ||||
1861 | static llvm::Optional<WaitStatus> HandlePid(::pid_t pid) { | |||
1862 | Log *log = GetLog(POSIXLog::Process); | |||
1863 | ||||
1864 | int status; | |||
1865 | ::pid_t wait_pid = llvm::sys::RetryAfterSignal( | |||
1866 | -1, ::waitpid, pid, &status, __WALL0x40000000 | __WNOTHREAD0x20000000 | WNOHANG1); | |||
1867 | ||||
1868 | if (wait_pid == 0) | |||
1869 | return llvm::None; | |||
1870 | ||||
1871 | if (wait_pid == -1) { | |||
1872 | Status error(errno(*__errno_location ()), eErrorTypePOSIX); | |||
1873 | LLDB_LOG(log, "waitpid({0}, &status, _) failed: {1}", pid,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid({0}, &status, _) failed: {1}", pid, error ); } while (0) | |||
1874 | error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid({0}, &status, _) failed: {1}", pid, error ); } while (0); | |||
1875 | return llvm::None; | |||
1876 | } | |||
1877 | ||||
1878 | assert(wait_pid == pid)(static_cast <bool> (wait_pid == pid) ? void (0) : __assert_fail ("wait_pid == pid", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , 1878, __extension__ __PRETTY_FUNCTION__)); | |||
1879 | ||||
1880 | WaitStatus wait_status = WaitStatus::Decode(status); | |||
1881 | ||||
1882 | LLDB_LOG(log, "waitpid({0}) got status = {1}", pid, wait_status)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid({0}) got status = {1}", pid, wait_status ); } while (0); | |||
1883 | return wait_status; | |||
1884 | } | |||
1885 | ||||
1886 | void NativeProcessLinux::SigchldHandler() { | |||
1887 | Log *log = GetLog(POSIXLog::Process); | |||
1888 | ||||
1889 | // Threads can appear or disappear as a result of event processing, so gather | |||
1890 | // the events upfront. | |||
1891 | llvm::DenseMap<lldb::tid_t, WaitStatus> tid_events; | |||
1892 | bool checked_main_thread = false; | |||
1893 | for (const auto &thread_up : m_threads) { | |||
1894 | if (thread_up->GetID() == GetID()) | |||
1895 | checked_main_thread = true; | |||
1896 | ||||
1897 | if (llvm::Optional<WaitStatus> status = HandlePid(thread_up->GetID())) | |||
1898 | tid_events.try_emplace(thread_up->GetID(), *status); | |||
1899 | } | |||
1900 | // Check the main thread even when we're not tracking it as process exit | |||
1901 | // events are reported that way. | |||
1902 | if (!checked_main_thread) { | |||
1903 | if (llvm::Optional<WaitStatus> status = HandlePid(GetID())) | |||
1904 | tid_events.try_emplace(GetID(), *status); | |||
1905 | } | |||
1906 | ||||
1907 | for (auto &KV : tid_events) { | |||
1908 | LLDB_LOG(log, "processing {0}({1}) ...", KV.first, KV.second)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "processing {0}({1}) ...", KV.first, KV.second); } while (0); | |||
1909 | if (KV.first == GetID() && (KV.second.type == WaitStatus::Exit || | |||
1910 | KV.second.type == WaitStatus::Signal)) { | |||
1911 | ||||
1912 | // The process exited. We're done monitoring. Report to delegate. | |||
1913 | SetExitStatus(KV.second, true); | |||
1914 | return; | |||
1915 | } | |||
1916 | NativeThreadLinux *thread = GetThreadByID(KV.first); | |||
1917 | assert(thread && "Why did this thread disappear?")(static_cast <bool> (thread && "Why did this thread disappear?" ) ? void (0) : __assert_fail ("thread && \"Why did this thread disappear?\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1917, __extension__ __PRETTY_FUNCTION__)); | |||
1918 | MonitorCallback(*thread, KV.second); | |||
1919 | } | |||
1920 | } | |||
1921 | ||||
1922 | // Wrapper for ptrace to catch errors and log calls. Note that ptrace sets | |||
1923 | // errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) | |||
1924 | Status NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, | |||
1925 | void *data, size_t data_size, | |||
1926 | long *result) { | |||
1927 | Status error; | |||
1928 | long int ret; | |||
1929 | ||||
1930 | Log *log = GetLog(POSIXLog::Ptrace); | |||
1931 | ||||
1932 | PtraceDisplayBytes(req, data, data_size); | |||
1933 | ||||
1934 | errno(*__errno_location ()) = 0; | |||
1935 | if (req == PTRACE_GETREGSETPTRACE_GETREGSET || req == PTRACE_SETREGSETPTRACE_SETREGSET) | |||
1936 | ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), | |||
1937 | *(unsigned int *)addr, data); | |||
1938 | else | |||
1939 | ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), | |||
1940 | addr, data); | |||
1941 | ||||
1942 | if (ret == -1) | |||
1943 | error.SetErrorToErrno(); | |||
1944 | ||||
1945 | if (result) | |||
1946 | *result = ret; | |||
1947 | ||||
1948 | LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid, addr, data,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid , addr, data, data_size, ret); } while (0) | |||
1949 | data_size, ret)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid , addr, data, data_size, ret); } while (0); | |||
1950 | ||||
1951 | PtraceDisplayBytes(req, data, data_size); | |||
1952 | ||||
1953 | if (error.Fail()) | |||
1954 | LLDB_LOG(log, "ptrace() failed: {0}", error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "ptrace() failed: {0}", error); } while (0); | |||
1955 | ||||
1956 | return error; | |||
1957 | } | |||
1958 | ||||
1959 | llvm::Expected<TraceSupportedResponse> NativeProcessLinux::TraceSupported() { | |||
1960 | if (IntelPTCollector::IsSupported()) | |||
1961 | return TraceSupportedResponse{"intel-pt", "Intel Processor Trace"}; | |||
1962 | return NativeProcessProtocol::TraceSupported(); | |||
1963 | } | |||
1964 | ||||
1965 | Error NativeProcessLinux::TraceStart(StringRef json_request, StringRef type) { | |||
1966 | if (type == "intel-pt") { | |||
1967 | if (Expected<TraceIntelPTStartRequest> request = | |||
1968 | json::parse<TraceIntelPTStartRequest>(json_request, | |||
1969 | "TraceIntelPTStartRequest")) { | |||
1970 | std::vector<lldb::tid_t> process_threads; | |||
1971 | for (auto &thread : m_threads) | |||
1972 | process_threads.push_back(thread->GetID()); | |||
1973 | return m_intel_pt_collector.TraceStart(*request, process_threads); | |||
1974 | } else | |||
1975 | return request.takeError(); | |||
1976 | } | |||
1977 | ||||
1978 | return NativeProcessProtocol::TraceStart(json_request, type); | |||
1979 | } | |||
1980 | ||||
1981 | Error NativeProcessLinux::TraceStop(const TraceStopRequest &request) { | |||
1982 | if (request.type == "intel-pt") | |||
1983 | return m_intel_pt_collector.TraceStop(request); | |||
1984 | return NativeProcessProtocol::TraceStop(request); | |||
1985 | } | |||
1986 | ||||
1987 | Expected<json::Value> NativeProcessLinux::TraceGetState(StringRef type) { | |||
1988 | if (type == "intel-pt") | |||
1989 | return m_intel_pt_collector.GetState(); | |||
1990 | return NativeProcessProtocol::TraceGetState(type); | |||
1991 | } | |||
1992 | ||||
1993 | Expected<std::vector<uint8_t>> NativeProcessLinux::TraceGetBinaryData( | |||
1994 | const TraceGetBinaryDataRequest &request) { | |||
1995 | if (request.type == "intel-pt") | |||
1996 | return m_intel_pt_collector.GetBinaryData(request); | |||
1997 | return NativeProcessProtocol::TraceGetBinaryData(request); | |||
1998 | } |