Bug Summary

File:tools/lldb/include/lldb/Utility/TraceOptions.h
Warning:line 27, column 37
Undefined or garbage value returned to caller

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name NativeProcessLinux.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D HAVE_ROUND -D LLDB_CONFIGURATION_RELEASE -D LLDB_USE_BUILTIN_DEMANGLER -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn337204/build-llvm/tools/lldb/source/Plugins/Process/Linux -I /build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux -I /build/llvm-toolchain-snapshot-7~svn337204/build-llvm/tools/lldb/include -I /build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/include -I /build/llvm-toolchain-snapshot-7~svn337204/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn337204/include -I /usr/include/python2.7 -I /build/llvm-toolchain-snapshot-7~svn337204/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn337204/build-llvm/tools/lldb/../clang/include -I /build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/. -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-deprecated-register -Wno-vla-extension -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn337204/build-llvm/tools/lldb/source/Plugins/Process/Linux -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-07-17-043059-5239-1 -x c++ /build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp

/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp

1//===-- NativeProcessLinux.cpp -------------------------------- -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "NativeProcessLinux.h"
11
12// C Includes
13#include <errno(*__errno_location ()).h>
14#include <stdint.h>
15#include <string.h>
16#include <unistd.h>
17
18// C++ Includes
19#include <fstream>
20#include <mutex>
21#include <sstream>
22#include <string>
23#include <unordered_map>
24
25// Other libraries and framework includes
26#include "lldb/Core/EmulateInstruction.h"
27#include "lldb/Core/ModuleSpec.h"
28#include "lldb/Core/RegisterValue.h"
29#include "lldb/Core/State.h"
30#include "lldb/Host/Host.h"
31#include "lldb/Host/HostProcess.h"
32#include "lldb/Host/PseudoTerminal.h"
33#include "lldb/Host/ThreadLauncher.h"
34#include "lldb/Host/common/NativeBreakpoint.h"
35#include "lldb/Host/common/NativeRegisterContext.h"
36#include "lldb/Host/linux/Ptrace.h"
37#include "lldb/Host/linux/Uio.h"
38#include "lldb/Host/posix/ProcessLauncherPosixFork.h"
39#include "lldb/Symbol/ObjectFile.h"
40#include "lldb/Target/Process.h"
41#include "lldb/Target/ProcessLaunchInfo.h"
42#include "lldb/Target/Target.h"
43#include "lldb/Utility/LLDBAssert.h"
44#include "lldb/Utility/Status.h"
45#include "lldb/Utility/StringExtractor.h"
46#include "llvm/Support/Errno.h"
47#include "llvm/Support/FileSystem.h"
48#include "llvm/Support/Threading.h"
49
50#include "NativeThreadLinux.h"
51#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
52#include "Procfs.h"
53
54#include <linux/unistd.h>
55#include <sys/socket.h>
56#include <sys/syscall.h>
57#include <sys/types.h>
58#include <sys/user.h>
59#include <sys/wait.h>
60
61// Support hardware breakpoints in case it has not been defined
62#ifndef TRAP_HWBKPT4
63#define TRAP_HWBKPT4 4
64#endif
65
66using namespace lldb;
67using namespace lldb_private;
68using namespace lldb_private::process_linux;
69using namespace llvm;
70
71// Private bits we only need internally.
72
73static bool ProcessVmReadvSupported() {
74 static bool is_supported;
75 static llvm::once_flag flag;
76
77 llvm::call_once(flag, [] {
78 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
79
80 uint32_t source = 0x47424742;
81 uint32_t dest = 0;
82
83 struct iovec local, remote;
84 remote.iov_base = &source;
85 local.iov_base = &dest;
86 remote.iov_len = local.iov_len = sizeof source;
87
88 // We shall try if cross-process-memory reads work by attempting to read a
89 // value from our own process.
90 ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0);
91 is_supported = (res == sizeof(source) && source == dest);
92 if (is_supported)
93 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Detected kernel support for process_vm_readv syscall. "
"Fast memory reads enabled."); } while (0)
94 "Detected kernel support for process_vm_readv syscall. "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Detected kernel support for process_vm_readv syscall. "
"Fast memory reads enabled."); } while (0)
95 "Fast memory reads enabled.")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Detected kernel support for process_vm_readv syscall. "
"Fast memory reads enabled."); } while (0)
;
96 else
97 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "syscall process_vm_readv failed (error: {0}). Fast memory "
"reads disabled.", llvm::sys::StrError()); } while (0)
98 "syscall process_vm_readv failed (error: {0}). Fast memory "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "syscall process_vm_readv failed (error: {0}). Fast memory "
"reads disabled.", llvm::sys::StrError()); } while (0)
99 "reads disabled.",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "syscall process_vm_readv failed (error: {0}). Fast memory "
"reads disabled.", llvm::sys::StrError()); } while (0)
100 llvm::sys::StrError())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "syscall process_vm_readv failed (error: {0}). Fast memory "
"reads disabled.", llvm::sys::StrError()); } while (0)
;
101 });
102
103 return is_supported;
104}
105
106namespace {
107void MaybeLogLaunchInfo(const ProcessLaunchInfo &info) {
108 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
109 if (!log)
110 return;
111
112 if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO0))
113 LLDB_LOG(log, "setting STDIN to '{0}'", action->GetFileSpec())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "setting STDIN to '{0}'", action->GetFileSpec(
)); } while (0)
;
114 else
115 LLDB_LOG(log, "leaving STDIN as is")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "leaving STDIN as is"); } while (0)
;
116
117 if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO1))
118 LLDB_LOG(log, "setting STDOUT to '{0}'", action->GetFileSpec())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "setting STDOUT to '{0}'", action->GetFileSpec
()); } while (0)
;
119 else
120 LLDB_LOG(log, "leaving STDOUT as is")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "leaving STDOUT as is"); } while (0)
;
121
122 if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO2))
123 LLDB_LOG(log, "setting STDERR to '{0}'", action->GetFileSpec())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "setting STDERR to '{0}'", action->GetFileSpec
()); } while (0)
;
124 else
125 LLDB_LOG(log, "leaving STDERR as is")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "leaving STDERR as is"); } while (0)
;
126
127 int i = 0;
128 for (const char **args = info.GetArguments().GetConstArgumentVector(); *args;
129 ++args, ++i)
130 LLDB_LOG(log, "arg {0}: '{1}'", i, *args)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "arg {0}: '{1}'", i, *args); } while (0)
;
131}
132
133void DisplayBytes(StreamString &s, void *bytes, uint32_t count) {
134 uint8_t *ptr = (uint8_t *)bytes;
135 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES20, count);
136 for (uint32_t i = 0; i < loop_count; i++) {
137 s.Printf("[%x]", *ptr);
138 ptr++;
139 }
140}
141
142void PtraceDisplayBytes(int &req, void *data, size_t data_size) {
143 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE(1u << 5)));
144 if (!log)
145 return;
146 StreamString buf;
147
148 switch (req) {
149 case PTRACE_POKETEXT: {
150 DisplayBytes(buf, &data, 8);
151 LLDB_LOGV(log, "PTRACE_POKETEXT {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private
&& log_private->GetVerbose()) log_private->Format
("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "PTRACE_POKETEXT {0}", buf.GetData()); } while (0
)
;
152 break;
153 }
154 case PTRACE_POKEDATA: {
155 DisplayBytes(buf, &data, 8);
156 LLDB_LOGV(log, "PTRACE_POKEDATA {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private
&& log_private->GetVerbose()) log_private->Format
("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "PTRACE_POKEDATA {0}", buf.GetData()); } while (0
)
;
157 break;
158 }
159 case PTRACE_POKEUSER: {
160 DisplayBytes(buf, &data, 8);
161 LLDB_LOGV(log, "PTRACE_POKEUSER {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private
&& log_private->GetVerbose()) log_private->Format
("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "PTRACE_POKEUSER {0}", buf.GetData()); } while (0
)
;
162 break;
163 }
164 case PTRACE_SETREGS13: {
165 DisplayBytes(buf, data, data_size);
166 LLDB_LOGV(log, "PTRACE_SETREGS {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private
&& log_private->GetVerbose()) log_private->Format
("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "PTRACE_SETREGS {0}", buf.GetData()); } while (0)
;
167 break;
168 }
169 case PTRACE_SETFPREGS15: {
170 DisplayBytes(buf, data, data_size);
171 LLDB_LOGV(log, "PTRACE_SETFPREGS {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private
&& log_private->GetVerbose()) log_private->Format
("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "PTRACE_SETFPREGS {0}", buf.GetData()); } while (
0)
;
172 break;
173 }
174 case PTRACE_SETSIGINFO: {
175 DisplayBytes(buf, data, sizeof(siginfo_t));
176 LLDB_LOGV(log, "PTRACE_SETSIGINFO {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private
&& log_private->GetVerbose()) log_private->Format
("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "PTRACE_SETSIGINFO {0}", buf.GetData()); } while (
0)
;
177 break;
178 }
179 case PTRACE_SETREGSETPTRACE_SETREGSET: {
180 // Extract iov_base from data, which is a pointer to the struct iovec
181 DisplayBytes(buf, *(void **)data, data_size);
182 LLDB_LOGV(log, "PTRACE_SETREGSET {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private
&& log_private->GetVerbose()) log_private->Format
("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "PTRACE_SETREGSET {0}", buf.GetData()); } while (
0)
;
183 break;
184 }
185 default: {}
186 }
187}
188
189static constexpr unsigned k_ptrace_word_size = sizeof(void *);
190static_assert(sizeof(long) >= k_ptrace_word_size,
191 "Size of long must be larger than ptrace word size");
192} // end of anonymous namespace
193
194// Simple helper function to ensure flags are enabled on the given file
195// descriptor.
196static Status EnsureFDFlags(int fd, int flags) {
197 Status error;
198
199 int status = fcntl(fd, F_GETFL3);
200 if (status == -1) {
201 error.SetErrorToErrno();
202 return error;
203 }
204
205 if (fcntl(fd, F_SETFL4, status | flags) == -1) {
206 error.SetErrorToErrno();
207 return error;
208 }
209
210 return error;
211}
212
213// -----------------------------------------------------------------------------
214// Public Static Methods
215// -----------------------------------------------------------------------------
216
217llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
218NativeProcessLinux::Factory::Launch(ProcessLaunchInfo &launch_info,
219 NativeDelegate &native_delegate,
220 MainLoop &mainloop) const {
221 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
222
223 MaybeLogLaunchInfo(launch_info);
224
225 Status status;
226 ::pid_t pid = ProcessLauncherPosixFork()
227 .LaunchProcess(launch_info, status)
228 .GetProcessId();
229 LLDB_LOG(log, "pid = {0:x}", pid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid = {0:x}", pid); } while (0)
;
230 if (status.Fail()) {
231 LLDB_LOG(log, "failed to launch process: {0}", status)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "failed to launch process: {0}", status); } while
(0)
;
232 return status.ToError();
233 }
234
235 // Wait for the child process to trap on its call to execve.
236 int wstatus;
237 ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0);
238 assert(wpid == pid)(static_cast <bool> (wpid == pid) ? void (0) : __assert_fail
("wpid == pid", "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 238, __extension__ __PRETTY_FUNCTION__))
;
239 (void)wpid;
240 if (!WIFSTOPPED(wstatus)(((wstatus) & 0xff) == 0x7f)) {
241 LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Could not sync with inferior process: wstatus={1}"
, WaitStatus::Decode(wstatus)); } while (0)
242 WaitStatus::Decode(wstatus))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Could not sync with inferior process: wstatus={1}"
, WaitStatus::Decode(wstatus)); } while (0)
;
243 return llvm::make_error<StringError>("Could not sync with inferior process",
244 llvm::inconvertibleErrorCode());
245 }
246 LLDB_LOG(log, "inferior started, now in stopped state")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "inferior started, now in stopped state"); } while
(0)
;
247
248 ProcessInstanceInfo Info;
249 if (!Host::GetProcessInfo(pid, Info)) {
250 return llvm::make_error<StringError>("Cannot get process architecture",
251 llvm::inconvertibleErrorCode());
252 }
253
254 // Set the architecture to the exe architecture.
255 LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid = {0:x}, detected architecture {1}", pid, Info
.GetArchitecture().GetArchitectureName()); } while (0)
256 Info.GetArchitecture().GetArchitectureName())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid = {0:x}, detected architecture {1}", pid, Info
.GetArchitecture().GetArchitectureName()); } while (0)
;
257
258 status = SetDefaultPtraceOpts(pid);
259 if (status.Fail()) {
260 LLDB_LOG(log, "failed to set default ptrace options: {0}", status)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "failed to set default ptrace options: {0}", status
); } while (0)
;
261 return status.ToError();
262 }
263
264 return std::unique_ptr<NativeProcessLinux>(new NativeProcessLinux(
265 pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate,
266 Info.GetArchitecture(), mainloop, {pid}));
267}
268
269llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
270NativeProcessLinux::Factory::Attach(
271 lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
272 MainLoop &mainloop) const {
273 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
274 LLDB_LOG(log, "pid = {0:x}", pid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid = {0:x}", pid); } while (0)
;
275
276 // Retrieve the architecture for the running process.
277 ProcessInstanceInfo Info;
278 if (!Host::GetProcessInfo(pid, Info)) {
279 return llvm::make_error<StringError>("Cannot get process architecture",
280 llvm::inconvertibleErrorCode());
281 }
282
283 auto tids_or = NativeProcessLinux::Attach(pid);
284 if (!tids_or)
285 return tids_or.takeError();
286
287 return std::unique_ptr<NativeProcessLinux>(new NativeProcessLinux(
288 pid, -1, native_delegate, Info.GetArchitecture(), mainloop, *tids_or));
289}
290
291// -----------------------------------------------------------------------------
292// Public Instance Methods
293// -----------------------------------------------------------------------------
294
295NativeProcessLinux::NativeProcessLinux(::pid_t pid, int terminal_fd,
296 NativeDelegate &delegate,
297 const ArchSpec &arch, MainLoop &mainloop,
298 llvm::ArrayRef<::pid_t> tids)
299 : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) {
300 if (m_terminal_fd != -1) {
301 Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK04000);
302 assert(status.Success())(static_cast <bool> (status.Success()) ? void (0) : __assert_fail
("status.Success()", "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 302, __extension__ __PRETTY_FUNCTION__))
;
303 }
304
305 Status status;
306 m_sigchld_handle = mainloop.RegisterSignal(
307 SIGCHLD17, [this](MainLoopBase &) { SigchldHandler(); }, status);
308 assert(m_sigchld_handle && status.Success())(static_cast <bool> (m_sigchld_handle && status
.Success()) ? void (0) : __assert_fail ("m_sigchld_handle && status.Success()"
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 308, __extension__ __PRETTY_FUNCTION__))
;
309
310 for (const auto &tid : tids) {
311 NativeThreadLinux &thread = AddThread(tid);
312 thread.SetStoppedBySignal(SIGSTOP19);
313 ThreadWasCreated(thread);
314 }
315
316 // Let our process instance know the thread has stopped.
317 SetCurrentThreadID(tids[0]);
318 SetState(StateType::eStateStopped, false);
319
320 // Proccess any signals we received before installing our handler
321 SigchldHandler();
322}
323
324llvm::Expected<std::vector<::pid_t>> NativeProcessLinux::Attach(::pid_t pid) {
325 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
326
327 Status status;
328 // Use a map to keep track of the threads which we have attached/need to
329 // attach.
330 Host::TidMap tids_to_attach;
331 while (Host::FindProcessThreads(pid, tids_to_attach)) {
332 for (Host::TidMap::iterator it = tids_to_attach.begin();
333 it != tids_to_attach.end();) {
334 if (it->second == false) {
335 lldb::tid_t tid = it->first;
336
337 // Attach to the requested process.
338 // An attach will cause the thread to stop with a SIGSTOP.
339 if ((status = PtraceWrapper(PTRACE_ATTACH, tid)).Fail()) {
340 // No such thread. The thread may have exited. More error handling
341 // may be needed.
342 if (status.GetError() == ESRCH3) {
343 it = tids_to_attach.erase(it);
344 continue;
345 }
346 return status.ToError();
347 }
348
349 int wpid =
350 llvm::sys::RetryAfterSignal(-1, ::waitpid, tid, nullptr, __WALL0x40000000);
351 // Need to use __WALL otherwise we receive an error with errno=ECHLD At
352 // this point we should have a thread stopped if waitpid succeeds.
353 if (wpid < 0) {
354 // No such thread. The thread may have exited. More error handling
355 // may be needed.
356 if (errno(*__errno_location ()) == ESRCH3) {
357 it = tids_to_attach.erase(it);
358 continue;
359 }
360 return llvm::errorCodeToError(
361 std::error_code(errno(*__errno_location ()), std::generic_category()));
362 }
363
364 if ((status = SetDefaultPtraceOpts(tid)).Fail())
365 return status.ToError();
366
367 LLDB_LOG(log, "adding tid = {0}", tid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "adding tid = {0}", tid); } while (0)
;
368 it->second = true;
369 }
370
371 // move the loop forward
372 ++it;
373 }
374 }
375
376 size_t tid_count = tids_to_attach.size();
377 if (tid_count == 0)
378 return llvm::make_error<StringError>("No such process",
379 llvm::inconvertibleErrorCode());
380
381 std::vector<::pid_t> tids;
382 tids.reserve(tid_count);
383 for (const auto &p : tids_to_attach)
384 tids.push_back(p.first);
385 return std::move(tids);
386}
387
388Status NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) {
389 long ptrace_opts = 0;
390
391 // Have the child raise an event on exit. This is used to keep the child in
392 // limbo until it is destroyed.
393 ptrace_opts |= PTRACE_O_TRACEEXIT;
394
395 // Have the tracer trace threads which spawn in the inferior process.
396 // TODO: if we want to support tracing the inferiors' child, add the
397 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
398 ptrace_opts |= PTRACE_O_TRACECLONE;
399
400 // Have the tracer notify us before execve returns (needed to disable legacy
401 // SIGTRAP generation)
402 ptrace_opts |= PTRACE_O_TRACEEXEC;
403
404 return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts);
405}
406
407// Handles all waitpid events from the inferior process.
408void NativeProcessLinux::MonitorCallback(lldb::pid_t pid, bool exited,
409 WaitStatus status) {
410 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS(1u << 1)));
411
412 // Certain activities differ based on whether the pid is the tid of the main
413 // thread.
414 const bool is_main_thread = (pid == GetID());
415
416 // Handle when the thread exits.
417 if (exited) {
418 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "got exit signal({0}) , tid = {1} ({2} main thread), process "
"state = {3}", signal, pid, is_main_thread ? "is" : "is not"
, GetState()); } while (0)
419 "got exit signal({0}) , tid = {1} ({2} main thread), process "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "got exit signal({0}) , tid = {1} ({2} main thread), process "
"state = {3}", signal, pid, is_main_thread ? "is" : "is not"
, GetState()); } while (0)
420 "state = {3}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "got exit signal({0}) , tid = {1} ({2} main thread), process "
"state = {3}", signal, pid, is_main_thread ? "is" : "is not"
, GetState()); } while (0)
421 signal, pid, is_main_thread ? "is" : "is not", GetState())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "got exit signal({0}) , tid = {1} ({2} main thread), process "
"state = {3}", signal, pid, is_main_thread ? "is" : "is not"
, GetState()); } while (0)
;
422
423 // This is a thread that exited. Ensure we're not tracking it anymore.
424 StopTrackingThread(pid);
425
426 if (is_main_thread) {
427 // The main thread exited. We're done monitoring. Report to delegate.
428 SetExitStatus(status, true);
429
430 // Notify delegate that our process has exited.
431 SetState(StateType::eStateExited, true);
432 }
433 return;
434 }
435
436 siginfo_t info;
437 const auto info_err = GetSignalInfo(pid, &info);
438 auto thread_sp = GetThreadByID(pid);
439
440 if (!thread_sp) {
441 // Normally, the only situation when we cannot find the thread is if we
442 // have just received a new thread notification. This is indicated by
443 // GetSignalInfo() returning si_code == SI_USER and si_pid == 0
444 LLDB_LOG(log, "received notification about an unknown tid {0}.", pid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received notification about an unknown tid {0}."
, pid); } while (0)
;
445
446 if (info_err.Fail()) {
447 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "(tid {0}) GetSignalInfo failed ({1}). " "Ingoring this notification."
, pid, info_err); } while (0)
448 "(tid {0}) GetSignalInfo failed ({1}). "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "(tid {0}) GetSignalInfo failed ({1}). " "Ingoring this notification."
, pid, info_err); } while (0)
449 "Ingoring this notification.",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "(tid {0}) GetSignalInfo failed ({1}). " "Ingoring this notification."
, pid, info_err); } while (0)
450 pid, info_err)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "(tid {0}) GetSignalInfo failed ({1}). " "Ingoring this notification."
, pid, info_err); } while (0)
;
451 return;
452 }
453
454 LLDB_LOG(log, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code
, info._sifields._kill.si_pid); } while (0)
455 info.si_pid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code
, info._sifields._kill.si_pid); } while (0)
;
456
457 NativeThreadLinux &thread = AddThread(pid);
458
459 // Resume the newly created thread.
460 ResumeThread(thread, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER(2147483647));
461 ThreadWasCreated(thread);
462 return;
463 }
464
465 // Get details on the signal raised.
466 if (info_err.Success()) {
467 // We have retrieved the signal info. Dispatch appropriately.
468 if (info.si_signo == SIGTRAP5)
469 MonitorSIGTRAP(info, *thread_sp);
470 else
471 MonitorSignal(info, *thread_sp, exited);
472 } else {
473 if (info_err.GetError() == EINVAL22) {
474 // This is a group stop reception for this tid. We can reach here if we
475 // reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU into the tracee,
476 // triggering the group-stop mechanism. Normally receiving these would
477 // stop the process, pending a SIGCONT. Simulating this state in a
478 // debugger is hard and is generally not needed (one use case is
479 // debugging background task being managed by a shell). For general use,
480 // it is sufficient to stop the process in a signal-delivery stop which
481 // happens before the group stop. This done by MonitorSignal and works
482 // correctly for all signals.
483 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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(), pid); } while (0)
484 "received a group stop for pid {0} tid {1}. Transparent "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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(), pid); } while (0)
485 "handling of group stops not supported, resuming the "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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(), pid); } while (0)
486 "thread.",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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(), pid); } while (0)
487 GetID(), pid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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(), pid); } while (0)
;
488 ResumeThread(*thread_sp, thread_sp->GetState(),
489 LLDB_INVALID_SIGNAL_NUMBER(2147483647));
490 } else {
491 // ptrace(GETSIGINFO) failed (but not due to group-stop).
492
493 // A return value of ESRCH means the thread/process is no longer on the
494 // system, so it was killed somehow outside of our control. Either way,
495 // we can't do anything with it anymore.
496
497 // Stop tracking the metadata for the thread since it's entirely off the
498 // system now.
499 const bool thread_found = StopTrackingThread(pid);
500
501 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, "
"status = {3}, main_thread = {4}, thread_found: {5}", info_err
, pid, signal, status, is_main_thread, thread_found); } while
(0)
502 "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, "
"status = {3}, main_thread = {4}, thread_found: {5}", info_err
, pid, signal, status, is_main_thread, thread_found); } while
(0)
503 "status = {3}, main_thread = {4}, thread_found: {5}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, "
"status = {3}, main_thread = {4}, thread_found: {5}", info_err
, pid, signal, status, is_main_thread, thread_found); } while
(0)
504 info_err, pid, signal, status, is_main_thread, thread_found)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, "
"status = {3}, main_thread = {4}, thread_found: {5}", info_err
, pid, signal, status, is_main_thread, thread_found); } while
(0)
;
505
506 if (is_main_thread) {
507 // Notify the delegate - our process is not available but appears to
508 // have been killed outside our control. Is eStateExited the right
509 // exit state in this case?
510 SetExitStatus(status, true);
511 SetState(StateType::eStateExited, true);
512 } else {
513 // This thread was pulled out from underneath us. Anything to do here?
514 // Do we want to do an all stop?
515 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} tid {1} non-main thread exit occurred, didn't "
"tell delegate anything since thread disappeared out " "from underneath us"
, GetID(), pid); } while (0)
516 "pid {0} tid {1} non-main thread exit occurred, didn't "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} tid {1} non-main thread exit occurred, didn't "
"tell delegate anything since thread disappeared out " "from underneath us"
, GetID(), pid); } while (0)
517 "tell delegate anything since thread disappeared out "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} tid {1} non-main thread exit occurred, didn't "
"tell delegate anything since thread disappeared out " "from underneath us"
, GetID(), pid); } while (0)
518 "from underneath us",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} tid {1} non-main thread exit occurred, didn't "
"tell delegate anything since thread disappeared out " "from underneath us"
, GetID(), pid); } while (0)
519 GetID(), pid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} tid {1} non-main thread exit occurred, didn't "
"tell delegate anything since thread disappeared out " "from underneath us"
, GetID(), pid); } while (0)
;
520 }
521 }
522 }
523}
524
525void NativeProcessLinux::WaitForNewThread(::pid_t tid) {
526 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
527
528 if (GetThreadByID(tid)) {
529 // We are already tracking the thread - we got the event on the new thread
530 // (see MonitorSignal) before this one. We are done.
531 return;
532 }
533
534 // The thread is not tracked yet, let's wait for it to appear.
535 int status = -1;
536 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received thread creation event for tid {0}. tid not tracked "
"yet, waiting for thread to appear...", tid); } while (0)
537 "received thread creation event for tid {0}. tid not tracked "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received thread creation event for tid {0}. tid not tracked "
"yet, waiting for thread to appear...", tid); } while (0)
538 "yet, waiting for thread to appear...",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received thread creation event for tid {0}. tid not tracked "
"yet, waiting for thread to appear...", tid); } while (0)
539 tid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received thread creation event for tid {0}. tid not tracked "
"yet, waiting for thread to appear...", tid); } while (0)
;
540 ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, ::waitpid, tid, &status, __WALL0x40000000);
541 // Since we are waiting on a specific tid, this must be the creation event.
542 // But let's do some checks just in case.
543 if (wait_pid != tid) {
544 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waiting for tid {0} failed. Assuming the thread has "
"disappeared in the meantime", tid); } while (0)
545 "waiting for tid {0} failed. Assuming the thread has "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waiting for tid {0} failed. Assuming the thread has "
"disappeared in the meantime", tid); } while (0)
546 "disappeared in the meantime",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waiting for tid {0} failed. Assuming the thread has "
"disappeared in the meantime", tid); } while (0)
547 tid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waiting for tid {0} failed. Assuming the thread has "
"disappeared in the meantime", tid); } while (0)
;
548 // The only way I know of this could happen is if the whole process was
549 // SIGKILLed in the mean time. In any case, we can't do anything about that
550 // now.
551 return;
552 }
553 if (WIFEXITED(status)(((status) & 0x7f) == 0)) {
554 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waiting for tid {0} returned an 'exited' event. Not "
"tracking the thread.", tid); } while (0)
555 "waiting for tid {0} returned an 'exited' event. Not "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waiting for tid {0} returned an 'exited' event. Not "
"tracking the thread.", tid); } while (0)
556 "tracking the thread.",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waiting for tid {0} returned an 'exited' event. Not "
"tracking the thread.", tid); } while (0)
557 tid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waiting for tid {0} returned an 'exited' event. Not "
"tracking the thread.", tid); } while (0)
;
558 // Also a very improbable event.
559 return;
560 }
561
562 LLDB_LOG(log, "pid = {0}: tracking new thread tid {1}", GetID(), tid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid = {0}: tracking new thread tid {1}", GetID()
, tid); } while (0)
;
563 NativeThreadLinux &new_thread = AddThread(tid);
564
565 ResumeThread(new_thread, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER(2147483647));
566 ThreadWasCreated(new_thread);
567}
568
569void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info,
570 NativeThreadLinux &thread) {
571 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
572 const bool is_main_thread = (thread.GetID() == GetID());
573
574 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!\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 574, __extension__ __PRETTY_FUNCTION__))
;
575
576 switch (info.si_code) {
577 // TODO: these two cases are required if we want to support tracing of the
578 // inferiors' children. We'd need this to debug a monitor. case (SIGTRAP |
579 // (PTRACE_EVENT_FORK << 8)): case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
580
581 case (SIGTRAP5 | (PTRACE_EVENT_CLONE << 8)): {
582 // This is the notification on the parent thread which informs us of new
583 // thread creation. We don't want to do anything with the parent thread so
584 // we just resume it. In case we want to implement "break on thread
585 // creation" functionality, we would need to stop here.
586
587 unsigned long event_message = 0;
588 if (GetEventMessage(thread.GetID(), &event_message).Fail()) {
589 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} received thread creation event but " "GetEventMessage failed so we don't know the new tid"
, thread.GetID()); } while (0)
590 "pid {0} received thread creation event but "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} received thread creation event but " "GetEventMessage failed so we don't know the new tid"
, thread.GetID()); } while (0)
591 "GetEventMessage failed so we don't know the new tid",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} received thread creation event but " "GetEventMessage failed so we don't know the new tid"
, thread.GetID()); } while (0)
592 thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} received thread creation event but " "GetEventMessage failed so we don't know the new tid"
, thread.GetID()); } while (0)
;
593 } else
594 WaitForNewThread(event_message);
595
596 ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647));
597 break;
598 }
599
600 case (SIGTRAP5 | (PTRACE_EVENT_EXEC << 8)): {
601 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received exec event, code = {0}", info.si_code ^
5); } while (0)
;
602
603 // Exec clears any pending notifications.
604 m_pending_notification_tid = LLDB_INVALID_THREAD_ID0;
605
606 // Remove all but the main thread here. Linux fork creates a new process
607 // which only copies the main thread.
608 LLDB_LOG(log, "exec received, stop tracking all but main thread")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "exec received, stop tracking all but main thread"
); } while (0)
;
609
610 for (auto i = m_threads.begin(); i != m_threads.end();) {
611 if ((*i)->GetID() == GetID())
612 i = m_threads.erase(i);
613 else
614 ++i;
615 }
616 assert(m_threads.size() == 1)(static_cast <bool> (m_threads.size() == 1) ? void (0) :
__assert_fail ("m_threads.size() == 1", "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 616, __extension__ __PRETTY_FUNCTION__))
;
617 auto *main_thread = static_cast<NativeThreadLinux *>(m_threads[0].get());
618
619 SetCurrentThreadID(main_thread->GetID());
620 main_thread->SetStoppedByExec();
621
622 // Tell coordinator about about the "new" (since exec) stopped main thread.
623 ThreadWasCreated(*main_thread);
624
625 // Let our delegate know we have just exec'd.
626 NotifyDidExec();
627
628 // Let the process know we're stopped.
629 StopRunningThreads(main_thread->GetID());
630
631 break;
632 }
633
634 case (SIGTRAP5 | (PTRACE_EVENT_EXIT << 8)): {
635 // The inferior process or one of its threads is about to exit. We don't
636 // want to do anything with the thread so we just resume it. In case we
637 // want to implement "break on thread exit" functionality, we would need to
638 // stop here.
639
640 unsigned long data = 0;
641 if (GetEventMessage(thread.GetID(), &data).Fail())
642 data = -1;
643
644 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
645 "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
646 "WIFSIGNALED={2}, pid = {3}, main_thread = {4}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
647 data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
648 is_main_thread)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
649
650
651 StateType state = thread.GetState();
652 if (!StateIsRunningState(state)) {
653 // Due to a kernel bug, we may sometimes get this stop after the inferior
654 // gets a SIGKILL. This confuses our state tracking logic in
655 // ResumeThread(), since normally, we should not be receiving any ptrace
656 // events while the inferior is stopped. This makes sure that the
657 // inferior is resumed and exits normally.
658 state = eStateRunning;
659 }
660 ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER(2147483647));
661
662 break;
663 }
664
665 case 0:
666 case TRAP_TRACETRAP_TRACE: // We receive this on single stepping.
667 case TRAP_HWBKPT4: // We receive this on watchpoint hit
668 {
669 // If a watchpoint was hit, report it
670 uint32_t wp_index;
671 Status error = thread.GetRegisterContext().GetWatchpointHitIndex(
672 wp_index, (uintptr_t)info.si_addr_sifields._sigfault.si_addr);
673 if (error.Fail())
674 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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("/build/llvm-toolchain-snapshot-7~svn337204/tools/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("/build/llvm-toolchain-snapshot-7~svn337204/tools/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("/build/llvm-toolchain-snapshot-7~svn337204/tools/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 // If a breakpoint was hit, report it
684 uint32_t bp_index;
685 error = thread.GetRegisterContext().GetHardwareBreakHitIndex(
686 bp_index, (uintptr_t)info.si_addr_sifields._sigfault.si_addr);
687 if (error.Fail())
688 LLDB_LOG(log, "received error while checking for hardware "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
689 "breakpoint hits, pid = {0}, error = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
690 thread.GetID(), error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
691 if (bp_index != LLDB_INVALID_INDEX32(4294967295U)) {
692 MonitorBreakpoint(thread);
693 break;
694 }
695
696 // Otherwise, report step over
697 MonitorTrace(thread);
698 break;
699 }
700
701 case SI_KERNELSI_KERNEL:
702#if defined __mips__
703 // For mips there is no special signal for watchpoint So we check for
704 // watchpoint in kernel trap
705 {
706 // If a watchpoint was hit, report it
707 uint32_t wp_index;
708 Status error = thread.GetRegisterContext().GetWatchpointHitIndex(
709 wp_index, LLDB_INVALID_ADDRESS(18446744073709551615UL));
710 if (error.Fail())
711 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received error while checking for watchpoint hits, pid = "
"{0}, error = {1}", thread.GetID(), error); } while (0)
712 "received error while checking for watchpoint hits, pid = "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received error while checking for watchpoint hits, pid = "
"{0}, error = {1}", thread.GetID(), error); } while (0)
713 "{0}, error = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received error while checking for watchpoint hits, pid = "
"{0}, error = {1}", thread.GetID(), error); } while (0)
714 thread.GetID(), error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received error while checking for watchpoint hits, pid = "
"{0}, error = {1}", thread.GetID(), error); } while (0)
;
715 if (wp_index != LLDB_INVALID_INDEX32(4294967295U)) {
716 MonitorWatchpoint(thread, wp_index);
717 break;
718 }
719 }
720// NO BREAK
721#endif
722 case TRAP_BRKPTTRAP_BRKPT:
723 MonitorBreakpoint(thread);
724 break;
725
726 case SIGTRAP5:
727 case (SIGTRAP5 | 0x80):
728 LLDB_LOG(do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
729 log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
730 "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
731 info.si_code, GetID(), thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
732
733 // Ignore these signals until we know more about them.
734 ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647));
735 break;
736
737 default:
738 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
739 info.si_code, GetID(), thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
740 MonitorSignal(info, thread, false);
741 break;
742 }
743}
744
745void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) {
746 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
747 LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received trace event, pid = {0}", thread.GetID()
); } while (0)
;
748
749 // This thread is currently stopped.
750 thread.SetStoppedByTrace();
751
752 StopRunningThreads(thread.GetID());
753}
754
755void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) {
756 Log *log(
757 GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS(1u << 1) | LIBLLDB_LOG_BREAKPOINTS(1u << 5)));
758 LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received breakpoint event, pid = {0}", thread.GetID
()); } while (0)
;
759
760 // Mark the thread as stopped at breakpoint.
761 thread.SetStoppedByBreakpoint();
762 Status error = FixupBreakpointPCAsNeeded(thread);
763 if (error.Fail())
764 LLDB_LOG(log, "pid = {0} fixup: {1}", thread.GetID(), error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid = {0} fixup: {1}", thread.GetID(), error); }
while (0)
;
765
766 if (m_threads_stepping_with_breakpoint.find(thread.GetID()) !=
767 m_threads_stepping_with_breakpoint.end())
768 thread.SetStoppedByTrace();
769
770 StopRunningThreads(thread.GetID());
771}
772
773void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread,
774 uint32_t wp_index) {
775 Log *log(
776 GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS(1u << 1) | LIBLLDB_LOG_WATCHPOINTS(1u << 6)));
777 LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received watchpoint event, pid = {0}, wp_index = {1}"
, thread.GetID(), wp_index); } while (0)
778 thread.GetID(), wp_index)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received watchpoint event, pid = {0}, wp_index = {1}"
, thread.GetID(), wp_index); } while (0)
;
779
780 // Mark the thread as stopped at watchpoint. The address is at
781 // (lldb::addr_t)info->si_addr if we need it.
782 thread.SetStoppedByWatchpoint(wp_index);
783
784 // We need to tell all other running threads before we notify the delegate
785 // about this stop.
786 StopRunningThreads(thread.GetID());
787}
788
789void NativeProcessLinux::MonitorSignal(const siginfo_t &info,
790 NativeThreadLinux &thread, bool exited) {
791 const int signo = info.si_signo;
792 const bool is_from_llgs = info.si_pid_sifields._kill.si_pid == getpid();
793
794 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
795
796 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
797 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a kill(2)
798 // or raise(3). Similarly for tgkill(2) on Linux.
799 //
800 // IOW, user generated signals never generate what we consider to be a
801 // "crash".
802 //
803 // Similarly, ACK signals generated by this monitor.
804
805 // Handle the signal.
806 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
807 "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
808 "waitpid pid = {4})",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
809 Host::GetSignalAsCString(signo), signo, info.si_code,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
810 thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
811
812 // Check for thread stop notification.
813 if (is_from_llgs && (info.si_code == SI_TKILLSI_TKILL) && (signo == SIGSTOP19)) {
814 // This is a tgkill()-based stop.
815 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} tid {1}, thread stopped", GetID(), thread
.GetID()); } while (0)
;
816
817 // Check that we're not already marked with a stop reason. Note this thread
818 // really shouldn't already be marked as stopped - if we were, that would
819 // imply that the kernel signaled us with the thread stopping which we
820 // handled and marked as stopped, and that, without an intervening resume,
821 // we received another stop. It is more likely that we are missing the
822 // marking of a run state somewhere if we find that the thread was marked
823 // as stopped.
824 const StateType thread_state = thread.GetState();
825 if (!StateIsStoppedState(thread_state, false)) {
826 // An inferior thread has stopped because of a SIGSTOP we have sent it.
827 // Generally, these are not important stops and we don't want to report
828 // them as they are just used to stop other threads when one thread (the
829 // one with the *real* stop reason) hits a breakpoint (watchpoint,
830 // etc...). However, in the case of an asynchronous Interrupt(), this
831 // *is* the real stop reason, so we leave the signal intact if this is
832 // the thread that was chosen as the triggering thread.
833 if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID0) {
834 if (m_pending_notification_tid == thread.GetID())
835 thread.SetStoppedBySignal(SIGSTOP19, &info);
836 else
837 thread.SetStoppedWithNoReason();
838
839 SetCurrentThreadID(thread.GetID());
840 SignalIfAllThreadsStopped();
841 } else {
842 // We can end up here if stop was initiated by LLGS but by this time a
843 // thread stop has occurred - maybe initiated by another event.
844 Status error = ResumeThread(thread, thread.GetState(), 0);
845 if (error.Fail())
846 LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "failed to resume thread {0}: {1}", thread.GetID(
), error); } while (0)
847 error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "failed to resume thread {0}: {1}", thread.GetID(
), error); } while (0)
;
848 }
849 } else {
850 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
851 "pid {0} tid {1}, thread was already marked as a stopped "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
852 "state (state={2}), leaving stop signal as is",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
853 GetID(), thread.GetID(), thread_state)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
854 SignalIfAllThreadsStopped();
855 }
856
857 // Done handling.
858 return;
859 }
860
861 // Check if debugger should stop at this signal or just ignore it and resume
862 // the inferior.
863 if (m_signals_to_ignore.find(signo) != m_signals_to_ignore.end()) {
864 ResumeThread(thread, thread.GetState(), signo);
865 return;
866 }
867
868 // This thread is stopped.
869 LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "received signal {0}", Host::GetSignalAsCString(signo
)); } while (0)
;
870 thread.SetStoppedBySignal(signo, &info);
871
872 // Send a stop to the debugger after we get all other threads to stop.
873 StopRunningThreads(thread.GetID());
874}
875
876namespace {
877
878struct EmulatorBaton {
879 NativeProcessLinux &m_process;
880 NativeRegisterContext &m_reg_context;
881
882 // eRegisterKindDWARF -> RegsiterValue
883 std::unordered_map<uint32_t, RegisterValue> m_register_values;
884
885 EmulatorBaton(NativeProcessLinux &process, NativeRegisterContext &reg_context)
886 : m_process(process), m_reg_context(reg_context) {}
887};
888
889} // anonymous namespace
890
891static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton,
892 const EmulateInstruction::Context &context,
893 lldb::addr_t addr, void *dst, size_t length) {
894 EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton);
895
896 size_t bytes_read;
897 emulator_baton->m_process.ReadMemory(addr, dst, length, bytes_read);
898 return bytes_read;
899}
900
901static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton,
902 const RegisterInfo *reg_info,
903 RegisterValue &reg_value) {
904 EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton);
905
906 auto it = emulator_baton->m_register_values.find(
907 reg_info->kinds[eRegisterKindDWARF]);
908 if (it != emulator_baton->m_register_values.end()) {
909 reg_value = it->second;
910 return true;
911 }
912
913 // The emulator only fill in the dwarf regsiter numbers (and in some case the
914 // generic register numbers). Get the full register info from the register
915 // context based on the dwarf register numbers.
916 const RegisterInfo *full_reg_info =
917 emulator_baton->m_reg_context.GetRegisterInfo(
918 eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]);
919
920 Status error =
921 emulator_baton->m_reg_context.ReadRegister(full_reg_info, reg_value);
922 if (error.Success())
923 return true;
924
925 return false;
926}
927
928static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton,
929 const EmulateInstruction::Context &context,
930 const RegisterInfo *reg_info,
931 const RegisterValue &reg_value) {
932 EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton);
933 emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] =
934 reg_value;
935 return true;
936}
937
938static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton,
939 const EmulateInstruction::Context &context,
940 lldb::addr_t addr, const void *dst,
941 size_t length) {
942 return length;
943}
944
945static lldb::addr_t ReadFlags(NativeRegisterContext &regsiter_context) {
946 const RegisterInfo *flags_info = regsiter_context.GetRegisterInfo(
947 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS4);
948 return regsiter_context.ReadRegisterAsUnsigned(flags_info,
949 LLDB_INVALID_ADDRESS(18446744073709551615UL));
950}
951
952Status
953NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadLinux &thread) {
954 Status error;
955 NativeRegisterContext& register_context = thread.GetRegisterContext();
956
957 std::unique_ptr<EmulateInstruction> emulator_ap(
958 EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying,
959 nullptr));
960
961 if (emulator_ap == nullptr)
962 return Status("Instruction emulator not found!");
963
964 EmulatorBaton baton(*this, register_context);
965 emulator_ap->SetBaton(&baton);
966 emulator_ap->SetReadMemCallback(&ReadMemoryCallback);
967 emulator_ap->SetReadRegCallback(&ReadRegisterCallback);
968 emulator_ap->SetWriteMemCallback(&WriteMemoryCallback);
969 emulator_ap->SetWriteRegCallback(&WriteRegisterCallback);
970
971 if (!emulator_ap->ReadInstruction())
972 return Status("Read instruction failed!");
973
974 bool emulation_result =
975 emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC);
976
977 const RegisterInfo *reg_info_pc = register_context.GetRegisterInfo(
978 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC0);
979 const RegisterInfo *reg_info_flags = register_context.GetRegisterInfo(
980 eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS4);
981
982 auto pc_it =
983 baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]);
984 auto flags_it =
985 baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]);
986
987 lldb::addr_t next_pc;
988 lldb::addr_t next_flags;
989 if (emulation_result) {
990 assert(pc_it != baton.m_register_values.end() &&(static_cast <bool> (pc_it != baton.m_register_values.end
() && "Emulation was successfull but PC wasn't updated"
) ? void (0) : __assert_fail ("pc_it != baton.m_register_values.end() && \"Emulation was successfull but PC wasn't updated\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 991, __extension__ __PRETTY_FUNCTION__))
991 "Emulation was successfull but PC wasn't updated")(static_cast <bool> (pc_it != baton.m_register_values.end
() && "Emulation was successfull but PC wasn't updated"
) ? void (0) : __assert_fail ("pc_it != baton.m_register_values.end() && \"Emulation was successfull but PC wasn't updated\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 991, __extension__ __PRETTY_FUNCTION__))
;
992 next_pc = pc_it->second.GetAsUInt64();
993
994 if (flags_it != baton.m_register_values.end())
995 next_flags = flags_it->second.GetAsUInt64();
996 else
997 next_flags = ReadFlags(register_context);
998 } else if (pc_it == baton.m_register_values.end()) {
999 // Emulate instruction failed and it haven't changed PC. Advance PC with
1000 // the size of the current opcode because the emulation of all
1001 // PC modifying instruction should be successful. The failure most
1002 // likely caused by a not supported instruction which don't modify PC.
1003 next_pc = register_context.GetPC() + emulator_ap->GetOpcode().GetByteSize();
1004 next_flags = ReadFlags(register_context);
1005 } else {
1006 // The instruction emulation failed after it modified the PC. It is an
1007 // unknown error where we can't continue because the next instruction is
1008 // modifying the PC but we don't know how.
1009 return Status("Instruction emulation failed unexpectedly.");
1010 }
1011
1012 if (m_arch.GetMachine() == llvm::Triple::arm) {
1013 if (next_flags & 0x20) {
1014 // Thumb mode
1015 error = SetSoftwareBreakpoint(next_pc, 2);
1016 } else {
1017 // Arm mode
1018 error = SetSoftwareBreakpoint(next_pc, 4);
1019 }
1020 } else if (m_arch.GetMachine() == llvm::Triple::mips64 ||
1021 m_arch.GetMachine() == llvm::Triple::mips64el ||
1022 m_arch.GetMachine() == llvm::Triple::mips ||
1023 m_arch.GetMachine() == llvm::Triple::mipsel ||
1024 m_arch.GetMachine() == llvm::Triple::ppc64le)
1025 error = SetSoftwareBreakpoint(next_pc, 4);
1026 else {
1027 // No size hint is given for the next breakpoint
1028 error = SetSoftwareBreakpoint(next_pc, 0);
1029 }
1030
1031 // If setting the breakpoint fails because next_pc is out of the address
1032 // space, ignore it and let the debugee segfault.
1033 if (error.GetError() == EIO5 || error.GetError() == EFAULT14) {
1034 return Status();
1035 } else if (error.Fail())
1036 return error;
1037
1038 m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc});
1039
1040 return Status();
1041}
1042
1043bool NativeProcessLinux::SupportHardwareSingleStepping() const {
1044 if (m_arch.GetMachine() == llvm::Triple::arm ||
1045 m_arch.GetMachine() == llvm::Triple::mips64 ||
1046 m_arch.GetMachine() == llvm::Triple::mips64el ||
1047 m_arch.GetMachine() == llvm::Triple::mips ||
1048 m_arch.GetMachine() == llvm::Triple::mipsel)
1049 return false;
1050 return true;
1051}
1052
1053Status NativeProcessLinux::Resume(const ResumeActionList &resume_actions) {
1054 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
1055 LLDB_LOG(log, "pid {0}", GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0}", GetID()); } while (0)
;
1056
1057 bool software_single_step = !SupportHardwareSingleStepping();
1058
1059 if (software_single_step) {
1060 for (const auto &thread : m_threads) {
1061 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\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1061, __extension__ __PRETTY_FUNCTION__))
;
1062
1063 const ResumeAction *const action =
1064 resume_actions.GetActionForThread(thread->GetID(), true);
1065 if (action == nullptr)
1066 continue;
1067
1068 if (action->state == eStateStepping) {
1069 Status error = SetupSoftwareSingleStepping(
1070 static_cast<NativeThreadLinux &>(*thread));
1071 if (error.Fail())
1072 return error;
1073 }
1074 }
1075 }
1076
1077 for (const auto &thread : m_threads) {
1078 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\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1078, __extension__ __PRETTY_FUNCTION__))
;
1079
1080 const ResumeAction *const action =
1081 resume_actions.GetActionForThread(thread->GetID(), true);
1082
1083 if (action == nullptr) {
1084 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "no action specified for pid {0} tid {1}", GetID(
), thread->GetID()); } while (0)
1085 thread->GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "no action specified for pid {0} tid {1}", GetID(
), thread->GetID()); } while (0)
;
1086 continue;
1087 }
1088
1089 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1090 action->state, GetID(), thread->GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
1091
1092 switch (action->state) {
1093 case eStateRunning:
1094 case eStateStepping: {
1095 // Run the thread, possibly feeding it the signal.
1096 const int signo = action->signal;
1097 ResumeThread(static_cast<NativeThreadLinux &>(*thread), action->state,
1098 signo);
1099 break;
1100 }
1101
1102 case eStateSuspended:
1103 case eStateStopped:
1104 llvm_unreachable("Unexpected state")::llvm::llvm_unreachable_internal("Unexpected state", "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1104)
;
1105
1106 default:
1107 return Status("NativeProcessLinux::%s (): unexpected state %s specified "
1108 "for pid %" PRIu64"l" "u" ", tid %" PRIu64"l" "u",
1109 __FUNCTION__, StateAsCString(action->state), GetID(),
1110 thread->GetID());
1111 }
1112 }
1113
1114 return Status();
1115}
1116
1117Status NativeProcessLinux::Halt() {
1118 Status error;
1119
1120 if (kill(GetID(), SIGSTOP19) != 0)
1121 error.SetErrorToErrno();
1122
1123 return error;
1124}
1125
1126Status NativeProcessLinux::Detach() {
1127 Status error;
1128
1129 // Stop monitoring the inferior.
1130 m_sigchld_handle.reset();
1131
1132 // Tell ptrace to detach from the process.
1133 if (GetID() == LLDB_INVALID_PROCESS_ID0)
1134 return error;
1135
1136 for (const auto &thread : m_threads) {
1137 Status e = Detach(thread->GetID());
1138 if (e.Fail())
1139 error =
1140 e; // Save the error, but still attempt to detach from other threads.
1141 }
1142
1143 m_processor_trace_monitor.clear();
1144 m_pt_proces_trace_id = LLDB_INVALID_UID(18446744073709551615UL);
1145
1146 return error;
1147}
1148
1149Status NativeProcessLinux::Signal(int signo) {
1150 Status error;
1151
1152 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
1153 LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "sending signal {0} ({1}) to pid {1}", signo, Host
::GetSignalAsCString(signo), GetID()); } while (0)
1154 Host::GetSignalAsCString(signo), GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "sending signal {0} ({1}) to pid {1}", signo, Host
::GetSignalAsCString(signo), GetID()); } while (0)
;
1155
1156 if (kill(GetID(), signo))
1157 error.SetErrorToErrno();
1158
1159 return error;
1160}
1161
1162Status NativeProcessLinux::Interrupt() {
1163 // Pick a running thread (or if none, a not-dead stopped thread) as the
1164 // chosen thread that will be the stop-reason thread.
1165 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
1166
1167 NativeThreadProtocol *running_thread = nullptr;
1168 NativeThreadProtocol *stopped_thread = nullptr;
1169
1170 LLDB_LOG(log, "selecting running thread for interrupt target")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "selecting running thread for interrupt target");
} while (0)
;
1171 for (const auto &thread : m_threads) {
1172 // If we have a running or stepping thread, we'll call that the target of
1173 // the interrupt.
1174 const auto thread_state = thread->GetState();
1175 if (thread_state == eStateRunning || thread_state == eStateStepping) {
1176 running_thread = thread.get();
1177 break;
1178 } else if (!stopped_thread && StateIsStoppedState(thread_state, true)) {
1179 // Remember the first non-dead stopped thread. We'll use that as a
1180 // backup if there are no running threads.
1181 stopped_thread = thread.get();
1182 }
1183 }
1184
1185 if (!running_thread && !stopped_thread) {
1186 Status error("found no running/stepping or live stopped threads as target "
1187 "for interrupt");
1188 LLDB_LOG(log, "skipping due to error: {0}", error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "skipping due to error: {0}", error); } while (0)
;
1189
1190 return error;
1191 }
1192
1193 NativeThreadProtocol *deferred_signal_thread =
1194 running_thread ? running_thread : stopped_thread;
1195
1196 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1197 running_thread ? "running" : "stopped",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1198 deferred_signal_thread->GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
1199
1200 StopRunningThreads(deferred_signal_thread->GetID());
1201
1202 return Status();
1203}
1204
1205Status NativeProcessLinux::Kill() {
1206 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
1207 LLDB_LOG(log, "pid {0}", GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0}", GetID()); } while (0)
;
1208
1209 Status error;
1210
1211 switch (m_state) {
1212 case StateType::eStateInvalid:
1213 case StateType::eStateExited:
1214 case StateType::eStateCrashed:
1215 case StateType::eStateDetached:
1216 case StateType::eStateUnloaded:
1217 // Nothing to do - the process is already dead.
1218 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "ignored for PID {0} due to current state: {1}", GetID
(), m_state); } while (0)
1219 m_state)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "ignored for PID {0} due to current state: {1}", GetID
(), m_state); } while (0)
;
1220 return error;
1221
1222 case StateType::eStateConnected:
1223 case StateType::eStateAttaching:
1224 case StateType::eStateLaunching:
1225 case StateType::eStateStopped:
1226 case StateType::eStateRunning:
1227 case StateType::eStateStepping:
1228 case StateType::eStateSuspended:
1229 // We can try to kill a process in these states.
1230 break;
1231 }
1232
1233 if (kill(GetID(), SIGKILL9) != 0) {
1234 error.SetErrorToErrno();
1235 return error;
1236 }
1237
1238 return error;
1239}
1240
1241static Status
1242ParseMemoryRegionInfoFromProcMapsLine(llvm::StringRef &maps_line,
1243 MemoryRegionInfo &memory_region_info) {
1244 memory_region_info.Clear();
1245
1246 StringExtractor line_extractor(maps_line);
1247
1248 // Format: {address_start_hex}-{address_end_hex} perms offset dev inode
1249 // pathname perms: rwxp (letter is present if set, '-' if not, final
1250 // character is p=private, s=shared).
1251
1252 // Parse out the starting address
1253 lldb::addr_t start_address = line_extractor.GetHexMaxU64(false, 0);
1254
1255 // Parse out hyphen separating start and end address from range.
1256 if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != '-'))
1257 return Status(
1258 "malformed /proc/{pid}/maps entry, missing dash between address range");
1259
1260 // Parse out the ending address
1261 lldb::addr_t end_address = line_extractor.GetHexMaxU64(false, start_address);
1262
1263 // Parse out the space after the address.
1264 if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != ' '))
1265 return Status(
1266 "malformed /proc/{pid}/maps entry, missing space after range");
1267
1268 // Save the range.
1269 memory_region_info.GetRange().SetRangeBase(start_address);
1270 memory_region_info.GetRange().SetRangeEnd(end_address);
1271
1272 // Any memory region in /proc/{pid}/maps is by definition mapped into the
1273 // process.
1274 memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes);
1275
1276 // Parse out each permission entry.
1277 if (line_extractor.GetBytesLeft() < 4)
1278 return Status("malformed /proc/{pid}/maps entry, missing some portion of "
1279 "permissions");
1280
1281 // Handle read permission.
1282 const char read_perm_char = line_extractor.GetChar();
1283 if (read_perm_char == 'r')
1284 memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eYes);
1285 else if (read_perm_char == '-')
1286 memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
1287 else
1288 return Status("unexpected /proc/{pid}/maps read permission char");
1289
1290 // Handle write permission.
1291 const char write_perm_char = line_extractor.GetChar();
1292 if (write_perm_char == 'w')
1293 memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eYes);
1294 else if (write_perm_char == '-')
1295 memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
1296 else
1297 return Status("unexpected /proc/{pid}/maps write permission char");
1298
1299 // Handle execute permission.
1300 const char exec_perm_char = line_extractor.GetChar();
1301 if (exec_perm_char == 'x')
1302 memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes);
1303 else if (exec_perm_char == '-')
1304 memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
1305 else
1306 return Status("unexpected /proc/{pid}/maps exec permission char");
1307
1308 line_extractor.GetChar(); // Read the private bit
1309 line_extractor.SkipSpaces(); // Skip the separator
1310 line_extractor.GetHexMaxU64(false, 0); // Read the offset
1311 line_extractor.GetHexMaxU64(false, 0); // Read the major device number
1312 line_extractor.GetChar(); // Read the device id separator
1313 line_extractor.GetHexMaxU64(false, 0); // Read the major device number
1314 line_extractor.SkipSpaces(); // Skip the separator
1315 line_extractor.GetU64(0, 10); // Read the inode number
1316
1317 line_extractor.SkipSpaces();
1318 const char *name = line_extractor.Peek();
1319 if (name)
1320 memory_region_info.SetName(name);
1321
1322 return Status();
1323}
1324
1325Status NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr,
1326 MemoryRegionInfo &range_info) {
1327 // FIXME review that the final memory region returned extends to the end of
1328 // the virtual address space,
1329 // with no perms if it is not mapped.
1330
1331 // Use an approach that reads memory regions from /proc/{pid}/maps. Assume
1332 // proc maps entries are in ascending order.
1333 // FIXME assert if we find differently.
1334
1335 if (m_supports_mem_region == LazyBool::eLazyBoolNo) {
1336 // We're done.
1337 return Status("unsupported");
1338 }
1339
1340 Status error = PopulateMemoryRegionCache();
1341 if (error.Fail()) {
1342 return error;
1343 }
1344
1345 lldb::addr_t prev_base_address = 0;
1346
1347 // FIXME start by finding the last region that is <= target address using
1348 // binary search. Data is sorted.
1349 // There can be a ton of regions on pthreads apps with lots of threads.
1350 for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end();
1351 ++it) {
1352 MemoryRegionInfo &proc_entry_info = it->first;
1353
1354 // Sanity check assumption that /proc/{pid}/maps entries are ascending.
1355 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\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1356, __extension__ __PRETTY_FUNCTION__))
1356 "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\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1356, __extension__ __PRETTY_FUNCTION__))
;
1357 prev_base_address = proc_entry_info.GetRange().GetRangeBase();
1358 UNUSED_IF_ASSERT_DISABLED(prev_base_address)((void)(prev_base_address));
1359
1360 // If the target address comes before this entry, indicate distance to next
1361 // region.
1362 if (load_addr < proc_entry_info.GetRange().GetRangeBase()) {
1363 range_info.GetRange().SetRangeBase(load_addr);
1364 range_info.GetRange().SetByteSize(
1365 proc_entry_info.GetRange().GetRangeBase() - load_addr);
1366 range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
1367 range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
1368 range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
1369 range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
1370
1371 return error;
1372 } else if (proc_entry_info.GetRange().Contains(load_addr)) {
1373 // The target address is within the memory region we're processing here.
1374 range_info = proc_entry_info;
1375 return error;
1376 }
1377
1378 // The target memory address comes somewhere after the region we just
1379 // parsed.
1380 }
1381
1382 // If we made it here, we didn't find an entry that contained the given
1383 // address. Return the load_addr as start and the amount of bytes betwwen
1384 // load address and the end of the memory as size.
1385 range_info.GetRange().SetRangeBase(load_addr);
1386 range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS(18446744073709551615UL));
1387 range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo);
1388 range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo);
1389 range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo);
1390 range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo);
1391 return error;
1392}
1393
1394Status NativeProcessLinux::PopulateMemoryRegionCache() {
1395 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
1396
1397 // If our cache is empty, pull the latest. There should always be at least
1398 // one memory region if memory region handling is supported.
1399 if (!m_mem_region_cache.empty()) {
1400 LLDB_LOG(log, "reusing {0} cached memory region entries",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "reusing {0} cached memory region entries", m_mem_region_cache
.size()); } while (0)
1401 m_mem_region_cache.size())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "reusing {0} cached memory region entries", m_mem_region_cache
.size()); } while (0)
;
1402 return Status();
1403 }
1404
1405 auto BufferOrError = getProcFile(GetID(), "maps");
1406 if (!BufferOrError) {
1407 m_supports_mem_region = LazyBool::eLazyBoolNo;
1408 return BufferOrError.getError();
1409 }
1410 StringRef Rest = BufferOrError.get()->getBuffer();
1411 while (! Rest.empty()) {
1412 StringRef Line;
1413 std::tie(Line, Rest) = Rest.split('\n');
1414 MemoryRegionInfo info;
1415 const Status parse_error =
1416 ParseMemoryRegionInfoFromProcMapsLine(Line, info);
1417 if (parse_error.Fail()) {
1418 LLDB_LOG(log, "failed to parse proc maps line '{0}': {1}", Line,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "failed to parse proc maps line '{0}': {1}", Line
, parse_error); } while (0)
1419 parse_error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "failed to parse proc maps line '{0}': {1}", Line
, parse_error); } while (0)
;
1420 m_supports_mem_region = LazyBool::eLazyBoolNo;
1421 return parse_error;
1422 }
1423 m_mem_region_cache.emplace_back(
1424 info, FileSpec(info.GetName().GetCString(), true));
1425 }
1426
1427 if (m_mem_region_cache.empty()) {
1428 // No entries after attempting to read them. This shouldn't happen if
1429 // /proc/{pid}/maps is supported. Assume we don't support map entries via
1430 // procfs.
1431 m_supports_mem_region = LazyBool::eLazyBoolNo;
1432 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1433 "failed to find any procfs maps entries, assuming no support "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1434 "for memory region metadata retrieval")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
1435 return Status("not supported");
1436 }
1437
1438 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1439 m_mem_region_cache.size(), GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
1440
1441 // We support memory retrieval, remember that.
1442 m_supports_mem_region = LazyBool::eLazyBoolYes;
1443 return Status();
1444}
1445
1446void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) {
1447 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
1448 LLDB_LOG(log, "newBumpId={0}", newBumpId)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "newBumpId={0}", newBumpId); } while (0)
;
1449 LLDB_LOG(log, "clearing {0} entries from memory region cache",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "clearing {0} entries from memory region cache", m_mem_region_cache
.size()); } while (0)
1450 m_mem_region_cache.size())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "clearing {0} entries from memory region cache", m_mem_region_cache
.size()); } while (0)
;
1451 m_mem_region_cache.clear();
1452}
1453
1454Status NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions,
1455 lldb::addr_t &addr) {
1456// FIXME implementing this requires the equivalent of
1457// InferiorCallPOSIX::InferiorCallMmap, which depends on functional ThreadPlans
1458// working with Native*Protocol.
1459#if 1
1460 return Status("not implemented yet");
1461#else
1462 addr = LLDB_INVALID_ADDRESS(18446744073709551615UL);
1463
1464 unsigned prot = 0;
1465 if (permissions & lldb::ePermissionsReadable)
1466 prot |= eMmapProtRead;
1467 if (permissions & lldb::ePermissionsWritable)
1468 prot |= eMmapProtWrite;
1469 if (permissions & lldb::ePermissionsExecutable)
1470 prot |= eMmapProtExec;
1471
1472 // TODO implement this directly in NativeProcessLinux
1473 // (and lift to NativeProcessPOSIX if/when that class is refactored out).
1474 if (InferiorCallMmap(this, addr, 0, size, prot,
1475 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) {
1476 m_addr_to_mmap_size[addr] = size;
1477 return Status();
1478 } else {
1479 addr = LLDB_INVALID_ADDRESS(18446744073709551615UL);
1480 return Status("unable to allocate %" PRIu64"l" "u"
1481 " bytes of memory with permissions %s",
1482 size, GetPermissionsAsCString(permissions));
1483 }
1484#endif
1485}
1486
1487Status NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) {
1488 // FIXME see comments in AllocateMemory - required lower-level
1489 // bits not in place yet (ThreadPlans)
1490 return Status("not implemented");
1491}
1492
1493lldb::addr_t NativeProcessLinux::GetSharedLibraryInfoAddress() {
1494 // punt on this for now
1495 return LLDB_INVALID_ADDRESS(18446744073709551615UL);
1496}
1497
1498size_t NativeProcessLinux::UpdateThreads() {
1499 // The NativeProcessLinux monitoring threads are always up to date with
1500 // respect to thread state and they keep the thread list populated properly.
1501 // All this method needs to do is return the thread count.
1502 return m_threads.size();
1503}
1504
1505Status NativeProcessLinux::GetSoftwareBreakpointPCOffset(
1506 uint32_t &actual_opcode_size) {
1507 // FIXME put this behind a breakpoint protocol class that can be
1508 // set per architecture. Need ARM, MIPS support here.
1509 static const uint8_t g_i386_opcode[] = {0xCC};
1510 static const uint8_t g_s390x_opcode[] = {0x00, 0x01};
1511
1512 switch (m_arch.GetMachine()) {
1513 case llvm::Triple::x86:
1514 case llvm::Triple::x86_64:
1515 actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode));
1516 return Status();
1517
1518 case llvm::Triple::systemz:
1519 actual_opcode_size = static_cast<uint32_t>(sizeof(g_s390x_opcode));
1520 return Status();
1521
1522 case llvm::Triple::arm:
1523 case llvm::Triple::aarch64:
1524 case llvm::Triple::mips64:
1525 case llvm::Triple::mips64el:
1526 case llvm::Triple::mips:
1527 case llvm::Triple::mipsel:
1528 case llvm::Triple::ppc64le:
1529 // On these architectures the PC don't get updated for breakpoint hits
1530 actual_opcode_size = 0;
1531 return Status();
1532
1533 default:
1534 assert(false && "CPU type not supported!")(static_cast <bool> (false && "CPU type not supported!"
) ? void (0) : __assert_fail ("false && \"CPU type not supported!\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1534, __extension__ __PRETTY_FUNCTION__))
;
1535 return Status("CPU type not supported");
1536 }
1537}
1538
1539Status NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size,
1540 bool hardware) {
1541 if (hardware)
1542 return SetHardwareBreakpoint(addr, size);
1543 else
1544 return SetSoftwareBreakpoint(addr, size);
1545}
1546
1547Status NativeProcessLinux::RemoveBreakpoint(lldb::addr_t addr, bool hardware) {
1548 if (hardware)
1549 return RemoveHardwareBreakpoint(addr);
1550 else
1551 return NativeProcessProtocol::RemoveBreakpoint(addr);
1552}
1553
1554Status NativeProcessLinux::GetSoftwareBreakpointTrapOpcode(
1555 size_t trap_opcode_size_hint, size_t &actual_opcode_size,
1556 const uint8_t *&trap_opcode_bytes) {
1557 // FIXME put this behind a breakpoint protocol class that can be set per
1558 // architecture. Need MIPS support here.
1559 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1560 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1561 // linux kernel does otherwise.
1562 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1563 static const uint8_t g_i386_opcode[] = {0xCC};
1564 static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1565 static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1566 static const uint8_t g_s390x_opcode[] = {0x00, 0x01};
1567 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1568 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1569
1570 switch (m_arch.GetMachine()) {
1571 case llvm::Triple::aarch64:
1572 trap_opcode_bytes = g_aarch64_opcode;
1573 actual_opcode_size = sizeof(g_aarch64_opcode);
1574 return Status();
1575
1576 case llvm::Triple::arm:
1577 switch (trap_opcode_size_hint) {
1578 case 2:
1579 trap_opcode_bytes = g_thumb_breakpoint_opcode;
1580 actual_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1581 return Status();
1582 case 4:
1583 trap_opcode_bytes = g_arm_breakpoint_opcode;
1584 actual_opcode_size = sizeof(g_arm_breakpoint_opcode);
1585 return Status();
1586 default:
1587 assert(false && "Unrecognised trap opcode size hint!")(static_cast <bool> (false && "Unrecognised trap opcode size hint!"
) ? void (0) : __assert_fail ("false && \"Unrecognised trap opcode size hint!\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1587, __extension__ __PRETTY_FUNCTION__))
;
1588 return Status("Unrecognised trap opcode size hint!");
1589 }
1590
1591 case llvm::Triple::x86:
1592 case llvm::Triple::x86_64:
1593 trap_opcode_bytes = g_i386_opcode;
1594 actual_opcode_size = sizeof(g_i386_opcode);
1595 return Status();
1596
1597 case llvm::Triple::mips:
1598 case llvm::Triple::mips64:
1599 trap_opcode_bytes = g_mips64_opcode;
1600 actual_opcode_size = sizeof(g_mips64_opcode);
1601 return Status();
1602
1603 case llvm::Triple::mipsel:
1604 case llvm::Triple::mips64el:
1605 trap_opcode_bytes = g_mips64el_opcode;
1606 actual_opcode_size = sizeof(g_mips64el_opcode);
1607 return Status();
1608
1609 case llvm::Triple::systemz:
1610 trap_opcode_bytes = g_s390x_opcode;
1611 actual_opcode_size = sizeof(g_s390x_opcode);
1612 return Status();
1613
1614 case llvm::Triple::ppc64le:
1615 trap_opcode_bytes = g_ppc64le_opcode;
1616 actual_opcode_size = sizeof(g_ppc64le_opcode);
1617 return Status();
1618
1619 default:
1620 assert(false && "CPU type not supported!")(static_cast <bool> (false && "CPU type not supported!"
) ? void (0) : __assert_fail ("false && \"CPU type not supported!\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1620, __extension__ __PRETTY_FUNCTION__))
;
1621 return Status("CPU type not supported");
1622 }
1623}
1624
1625Status NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
1626 size_t &bytes_read) {
1627 if (ProcessVmReadvSupported()) {
1628 // The process_vm_readv path is about 50 times faster than ptrace api. We
1629 // want to use this syscall if it is supported.
1630
1631 const ::pid_t pid = GetID();
1632
1633 struct iovec local_iov, remote_iov;
1634 local_iov.iov_base = buf;
1635 local_iov.iov_len = size;
1636 remote_iov.iov_base = reinterpret_cast<void *>(addr);
1637 remote_iov.iov_len = size;
1638
1639 bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0);
1640 const bool success = bytes_read == size;
1641
1642 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
1643 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1644 "using process_vm_readv to read {0} bytes from inferior "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1645 "address {1:x}: {2}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1646 size, addr, success ? "Success" : llvm::sys::StrError(errno))do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
1647
1648 if (success)
1649 return Status();
1650 // else the call failed for some reason, let's retry the read using ptrace
1651 // api.
1652 }
1653
1654 unsigned char *dst = static_cast<unsigned char *>(buf);
1655 size_t remainder;
1656 long data;
1657
1658 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY(1u << 4)));
1659 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "addr = {0}, buf = {1}, size = {2}", addr, buf, size
); } while (0)
;
1660
1661 for (bytes_read = 0; bytes_read < size; bytes_read += remainder) {
1662 Status error = NativeProcessLinux::PtraceWrapper(
1663 PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data);
1664 if (error.Fail())
1665 return error;
1666
1667 remainder = size - bytes_read;
1668 remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder;
1669
1670 // Copy the data into our buffer
1671 memcpy(dst, &data, remainder);
1672
1673 LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "[{0:x}]:{1:x}", addr, data); } while (0)
;
1674 addr += k_ptrace_word_size;
1675 dst += k_ptrace_word_size;
1676 }
1677 return Status();
1678}
1679
1680Status NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf,
1681 size_t size,
1682 size_t &bytes_read) {
1683 Status error = ReadMemory(addr, buf, size, bytes_read);
1684 if (error.Fail())
1685 return error;
1686 return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size);
1687}
1688
1689Status NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf,
1690 size_t size, size_t &bytes_written) {
1691 const unsigned char *src = static_cast<const unsigned char *>(buf);
1692 size_t remainder;
1693 Status error;
1694
1695 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY(1u << 4)));
1696 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "addr = {0}, buf = {1}, size = {2}", addr, buf, size
); } while (0)
;
1697
1698 for (bytes_written = 0; bytes_written < size; bytes_written += remainder) {
1699 remainder = size - bytes_written;
1700 remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder;
1701
1702 if (remainder == k_ptrace_word_size) {
1703 unsigned long data = 0;
1704 memcpy(&data, src, k_ptrace_word_size);
1705
1706 LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "[{0:x}]:{1:x}", addr, data); } while (0)
;
1707 error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(),
1708 (void *)addr, (void *)data);
1709 if (error.Fail())
1710 return error;
1711 } else {
1712 unsigned char buff[8];
1713 size_t bytes_read;
1714 error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read);
1715 if (error.Fail())
1716 return error;
1717
1718 memcpy(buff, src, remainder);
1719
1720 size_t bytes_written_rec;
1721 error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec);
1722 if (error.Fail())
1723 return error;
1724
1725 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long
*)src, *(unsigned long *)buff); } while (0)
1726 *(unsigned long *)buff)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long
*)src, *(unsigned long *)buff); } while (0)
;
1727 }
1728
1729 addr += k_ptrace_word_size;
1730 src += k_ptrace_word_size;
1731 }
1732 return error;
1733}
1734
1735Status NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) {
1736 return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo);
1737}
1738
1739Status NativeProcessLinux::GetEventMessage(lldb::tid_t tid,
1740 unsigned long *message) {
1741 return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message);
1742}
1743
1744Status NativeProcessLinux::Detach(lldb::tid_t tid) {
1745 if (tid == LLDB_INVALID_THREAD_ID0)
1746 return Status();
1747
1748 return PtraceWrapper(PTRACE_DETACH, tid);
1749}
1750
1751bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) {
1752 for (const auto &thread : m_threads) {
1753 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\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1753, __extension__ __PRETTY_FUNCTION__))
;
1754 if (thread->GetID() == thread_id) {
1755 // We have this thread.
1756 return true;
1757 }
1758 }
1759
1760 // We don't have this thread.
1761 return false;
1762}
1763
1764bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) {
1765 Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD(1u << 2));
1766 LLDB_LOG(log, "tid: {0})", thread_id)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "tid: {0})", thread_id); } while (0)
;
1767
1768 bool found = false;
1769 for (auto it = m_threads.begin(); it != m_threads.end(); ++it) {
1770 if (*it && ((*it)->GetID() == thread_id)) {
1771 m_threads.erase(it);
1772 found = true;
1773 break;
1774 }
1775 }
1776
1777 if (found)
1778 StopTracingForThread(thread_id);
1779 SignalIfAllThreadsStopped();
1780 return found;
1781}
1782
1783NativeThreadLinux &NativeProcessLinux::AddThread(lldb::tid_t thread_id) {
1784 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD(1u << 2)));
1785 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} adding thread with tid {1}", GetID(), thread_id
); } while (0)
;
1786
1787 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\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1788, __extension__ __PRETTY_FUNCTION__))
1788 "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\""
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1788, __extension__ __PRETTY_FUNCTION__))
;
1789
1790 // If this is the first thread, save it as the current thread
1791 if (m_threads.empty())
1792 SetCurrentThreadID(thread_id);
1793
1794 m_threads.push_back(llvm::make_unique<NativeThreadLinux>(*this, thread_id));
1795
1796 if (m_pt_proces_trace_id != LLDB_INVALID_UID(18446744073709551615UL)) {
1797 auto traceMonitor = ProcessorTraceMonitor::Create(
1798 GetID(), thread_id, m_pt_process_trace_config, true);
1799 if (traceMonitor) {
1800 m_pt_traced_thread_group.insert(thread_id);
1801 m_processor_trace_monitor.insert(
1802 std::make_pair(thread_id, std::move(*traceMonitor)));
1803 } else {
1804 LLDB_LOG(log, "failed to start trace on thread {0}", thread_id)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "failed to start trace on thread {0}", thread_id)
; } while (0)
;
1805 Status error(traceMonitor.takeError());
1806 LLDB_LOG(log, "error {0}", error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "error {0}", error); } while (0)
;
1807 }
1808 }
1809
1810 return static_cast<NativeThreadLinux &>(*m_threads.back());
1811}
1812
1813Status
1814NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) {
1815 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS(1u << 7)));
1816
1817 Status error;
1818
1819 // Find out the size of a breakpoint (might depend on where we are in the
1820 // code).
1821 NativeRegisterContext &context = thread.GetRegisterContext();
1822
1823 uint32_t breakpoint_size = 0;
1824 error = GetSoftwareBreakpointPCOffset(breakpoint_size);
1825 if (error.Fail()) {
1826 LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "GetBreakpointSize() failed: {0}", error); } while
(0)
;
1827 return error;
1828 } else
1829 LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "breakpoint size: {0}", breakpoint_size); } while
(0)
;
1830
1831 // First try probing for a breakpoint at a software breakpoint location: PC -
1832 // breakpoint size.
1833 const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation();
1834 lldb::addr_t breakpoint_addr = initial_pc_addr;
1835 if (breakpoint_size > 0) {
1836 // Do not allow breakpoint probe to wrap around.
1837 if (breakpoint_addr >= breakpoint_size)
1838 breakpoint_addr -= breakpoint_size;
1839 }
1840
1841 // Check if we stopped because of a breakpoint.
1842 NativeBreakpointSP breakpoint_sp;
1843 error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp);
1844 if (!error.Success() || !breakpoint_sp) {
1845 // We didn't find one at a software probe location. Nothing to do.
1846 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} no lldb breakpoint found at current pc with "
"adjustment: {1}", GetID(), breakpoint_addr); } while (0)
1847 "pid {0} no lldb breakpoint found at current pc with "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} no lldb breakpoint found at current pc with "
"adjustment: {1}", GetID(), breakpoint_addr); } while (0)
1848 "adjustment: {1}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} no lldb breakpoint found at current pc with "
"adjustment: {1}", GetID(), breakpoint_addr); } while (0)
1849 GetID(), breakpoint_addr)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} no lldb breakpoint found at current pc with "
"adjustment: {1}", GetID(), breakpoint_addr); } while (0)
;
1850 return Status();
1851 }
1852
1853 // If the breakpoint is not a software breakpoint, nothing to do.
1854 if (!breakpoint_sp->IsSoftwareBreakpoint()) {
1855 LLDB_LOG(do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} breakpoint found at {1:x}, not software, nothing to adjust"
, GetID(), breakpoint_addr); } while (0)
1856 log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} breakpoint found at {1:x}, not software, nothing to adjust"
, GetID(), breakpoint_addr); } while (0)
1857 "pid {0} breakpoint found at {1:x}, not software, nothing to adjust",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} breakpoint found at {1:x}, not software, nothing to adjust"
, GetID(), breakpoint_addr); } while (0)
1858 GetID(), breakpoint_addr)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} breakpoint found at {1:x}, not software, nothing to adjust"
, GetID(), breakpoint_addr); } while (0)
;
1859 return Status();
1860 }
1861
1862 //
1863 // We have a software breakpoint and need to adjust the PC.
1864 //
1865
1866 // Sanity check.
1867 if (breakpoint_size == 0) {
1868 // Nothing to do! How did we get here?
1869 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} breakpoint found at {1:x}, it is software, but the "
"size is zero, nothing to do (unexpected)", GetID(), breakpoint_addr
); } while (0)
1870 "pid {0} breakpoint found at {1:x}, it is software, but the "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} breakpoint found at {1:x}, it is software, but the "
"size is zero, nothing to do (unexpected)", GetID(), breakpoint_addr
); } while (0)
1871 "size is zero, nothing to do (unexpected)",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} breakpoint found at {1:x}, it is software, but the "
"size is zero, nothing to do (unexpected)", GetID(), breakpoint_addr
); } while (0)
1872 GetID(), breakpoint_addr)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} breakpoint found at {1:x}, it is software, but the "
"size is zero, nothing to do (unexpected)", GetID(), breakpoint_addr
); } while (0)
;
1873 return Status();
1874 }
1875
1876 // Change the program counter.
1877 LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} tid {1}: changing PC from {2:x} to {3:x}"
, GetID(), thread.GetID(), initial_pc_addr, breakpoint_addr);
} while (0)
1878 thread.GetID(), initial_pc_addr, breakpoint_addr)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} tid {1}: changing PC from {2:x} to {3:x}"
, GetID(), thread.GetID(), initial_pc_addr, breakpoint_addr);
} while (0)
;
1879
1880 error = context.SetPC(breakpoint_addr);
1881 if (error.Fail()) {
1882 LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} tid {1}: failed to set PC: {2}", GetID()
, thread.GetID(), error); } while (0)
1883 thread.GetID(), error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid {0} tid {1}: failed to set PC: {2}", GetID()
, thread.GetID(), error); } while (0)
;
1884 return error;
1885 }
1886
1887 return error;
1888}
1889
1890Status NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path,
1891 FileSpec &file_spec) {
1892 Status error = PopulateMemoryRegionCache();
1893 if (error.Fail())
1894 return error;
1895
1896 FileSpec module_file_spec(module_path, true);
1897
1898 file_spec.Clear();
1899 for (const auto &it : m_mem_region_cache) {
1900 if (it.second.GetFilename() == module_file_spec.GetFilename()) {
1901 file_spec = it.second;
1902 return Status();
1903 }
1904 }
1905 return Status("Module file (%s) not found in /proc/%" PRIu64"l" "u" "/maps file!",
1906 module_file_spec.GetFilename().AsCString(), GetID());
1907}
1908
1909Status NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name,
1910 lldb::addr_t &load_addr) {
1911 load_addr = LLDB_INVALID_ADDRESS(18446744073709551615UL);
1912 Status error = PopulateMemoryRegionCache();
1913 if (error.Fail())
1914 return error;
1915
1916 FileSpec file(file_name, false);
1917 for (const auto &it : m_mem_region_cache) {
1918 if (it.second == file) {
1919 load_addr = it.first.GetRange().GetRangeBase();
1920 return Status();
1921 }
1922 }
1923 return Status("No load address found for specified file.");
1924}
1925
1926NativeThreadLinux *NativeProcessLinux::GetThreadByID(lldb::tid_t tid) {
1927 return static_cast<NativeThreadLinux *>(
1928 NativeProcessProtocol::GetThreadByID(tid));
1929}
1930
1931Status NativeProcessLinux::ResumeThread(NativeThreadLinux &thread,
1932 lldb::StateType state, int signo) {
1933 Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD(1u << 2));
1934 LLDB_LOG(log, "tid: {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "tid: {0}", thread.GetID()); } while (0)
;
1935
1936 // Before we do the resume below, first check if we have a pending stop
1937 // notification that is currently waiting for all threads to stop. This is
1938 // potentially a buggy situation since we're ostensibly waiting for threads
1939 // to stop before we send out the pending notification, and here we are
1940 // resuming one before we send out the pending stop notification.
1941 if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID0) {
1942 LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1943 "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("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1944 "pending stop notification (tid {1}) that is actively "do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1945 "waiting for this thread to stop. Valid sequence of events?",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
1946 thread.GetID(), m_pending_notification_tid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
1947 }
1948
1949 // Request a resume. We expect this to be synchronous and the system to
1950 // reflect it is running after this completes.
1951 switch (state) {
1952 case eStateRunning: {
1953 const auto resume_result = thread.Resume(signo);
1954 if (resume_result.Success())
1955 SetState(eStateRunning, true);
1956 return resume_result;
1957 }
1958 case eStateStepping: {
1959 const auto step_result = thread.SingleStep(signo);
1960 if (step_result.Success())
1961 SetState(eStateRunning, true);
1962 return step_result;
1963 }
1964 default:
1965 LLDB_LOG(log, "Unhandled state {0}.", state)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Unhandled state {0}.", state); } while (0)
;
1966 llvm_unreachable("Unhandled state for resume")::llvm::llvm_unreachable_internal("Unhandled state for resume"
, "/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, 1966)
;
1967 }
1968}
1969
1970//===----------------------------------------------------------------------===//
1971
1972void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) {
1973 Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD(1u << 2));
1974 LLDB_LOG(log, "about to process event: (triggering_tid: {0})",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "about to process event: (triggering_tid: {0})", triggering_tid
); } while (0)
1975 triggering_tid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "about to process event: (triggering_tid: {0})", triggering_tid
); } while (0)
;
1976
1977 m_pending_notification_tid = triggering_tid;
1978
1979 // Request a stop for all the thread stops that need to be stopped and are
1980 // not already known to be stopped.
1981 for (const auto &thread : m_threads) {
1982 if (StateIsRunningState(thread->GetState()))
1983 static_cast<NativeThreadLinux *>(thread.get())->RequestStop();
1984 }
1985
1986 SignalIfAllThreadsStopped();
1987 LLDB_LOG(log, "event processing done")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "event processing done"); } while (0)
;
1988}
1989
1990void NativeProcessLinux::SignalIfAllThreadsStopped() {
1991 if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID0)
1992 return; // No pending notification. Nothing to do.
1993
1994 for (const auto &thread_sp : m_threads) {
1995 if (StateIsRunningState(thread_sp->GetState()))
1996 return; // Some threads are still running. Don't signal yet.
1997 }
1998
1999 // We have a pending notification and all threads have stopped.
2000 Log *log(
2001 GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS(1u << 1) | LIBLLDB_LOG_BREAKPOINTS(1u << 5)));
2002
2003 // Clear any temporary breakpoints we used to implement software single
2004 // stepping.
2005 for (const auto &thread_info : m_threads_stepping_with_breakpoint) {
2006 Status error = RemoveBreakpoint(thread_info.second);
2007 if (error.Fail())
2008 LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid = {0} remove stepping breakpoint: {1}", thread_info
.first, error); } while (0)
2009 thread_info.first, error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "pid = {0} remove stepping breakpoint: {1}", thread_info
.first, error); } while (0)
;
2010 }
2011 m_threads_stepping_with_breakpoint.clear();
2012
2013 // Notify the delegate about the stop
2014 SetCurrentThreadID(m_pending_notification_tid);
2015 SetState(StateType::eStateStopped, true);
2016 m_pending_notification_tid = LLDB_INVALID_THREAD_ID0;
2017}
2018
2019void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) {
2020 Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD(1u << 2));
2021 LLDB_LOG(log, "tid: {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "tid: {0}", thread.GetID()); } while (0)
;
2022
2023 if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID0 &&
2024 StateIsRunningState(thread.GetState())) {
2025 // We will need to wait for this new thread to stop as well before firing
2026 // the notification.
2027 thread.RequestStop();
2028 }
2029}
2030
2031void NativeProcessLinux::SigchldHandler() {
2032 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS(1u << 1)));
2033 // Process all pending waitpid notifications.
2034 while (true) {
2035 int status = -1;
2036 ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, ::waitpid, -1, &status,
2037 __WALL0x40000000 | __WNOTHREAD0x20000000 | WNOHANG1);
2038
2039 if (wait_pid == 0)
2040 break; // We are done.
2041
2042 if (wait_pid == -1) {
2043 Status error(errno(*__errno_location ()), eErrorTypePOSIX);
2044 LLDB_LOG(log, "waitpid (-1, &status, _) failed: {0}", error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waitpid (-1, &status, _) failed: {0}", error
); } while (0)
;
2045 break;
2046 }
2047
2048 WaitStatus wait_status = WaitStatus::Decode(status);
2049 bool exited = wait_status.type == WaitStatus::Exit ||
2050 (wait_status.type == WaitStatus::Signal &&
2051 wait_pid == static_cast<::pid_t>(GetID()));
2052
2053 LLDB_LOG(do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waitpid (-1, &status, _) => pid = {0}, status = {1}, exited = {2}"
, wait_pid, wait_status, exited); } while (0)
2054 log,do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waitpid (-1, &status, _) => pid = {0}, status = {1}, exited = {2}"
, wait_pid, wait_status, exited); } while (0)
2055 "waitpid (-1, &status, _) => pid = {0}, status = {1}, exited = {2}",do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waitpid (-1, &status, _) => pid = {0}, status = {1}, exited = {2}"
, wait_pid, wait_status, exited); } while (0)
2056 wait_pid, wait_status, exited)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "waitpid (-1, &status, _) => pid = {0}, status = {1}, exited = {2}"
, wait_pid, wait_status, exited); } while (0)
;
2057
2058 MonitorCallback(wait_pid, exited, wait_status);
2059 }
2060}
2061
2062// Wrapper for ptrace to catch errors and log calls. Note that ptrace sets
2063// errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*)
2064Status NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr,
2065 void *data, size_t data_size,
2066 long *result) {
2067 Status error;
2068 long int ret;
2069
2070 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE(1u << 5)));
2071
2072 PtraceDisplayBytes(req, data, data_size);
2073
2074 errno(*__errno_location ()) = 0;
2075 if (req == PTRACE_GETREGSETPTRACE_GETREGSET || req == PTRACE_SETREGSETPTRACE_SETREGSET)
2076 ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid),
2077 *(unsigned int *)addr, data);
2078 else
2079 ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid),
2080 addr, data);
2081
2082 if (ret == -1)
2083 error.SetErrorToErrno();
2084
2085 if (result)
2086 *result = ret;
2087
2088 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("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
2089 data_size, ret)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/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)
;
2090
2091 PtraceDisplayBytes(req, data, data_size);
2092
2093 if (error.Fail())
2094 LLDB_LOG(log, "ptrace() failed: {0}", error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "ptrace() failed: {0}", error); } while (0)
;
2095
2096 return error;
2097}
2098
2099llvm::Expected<ProcessorTraceMonitor &>
2100NativeProcessLinux::LookupProcessorTraceInstance(lldb::user_id_t traceid,
2101 lldb::tid_t thread) {
2102 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE(1u << 5)));
2103 if (thread == LLDB_INVALID_THREAD_ID0 && traceid == m_pt_proces_trace_id) {
2104 LLDB_LOG(log, "thread not specified: {0}", traceid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "thread not specified: {0}", traceid); } while (0
)
;
2105 return Status("tracing not active thread not specified").ToError();
2106 }
2107
2108 for (auto& iter : m_processor_trace_monitor) {
2109 if (traceid == iter.second->GetTraceID() &&
2110 (thread == iter.first || thread == LLDB_INVALID_THREAD_ID0))
2111 return *(iter.second);
2112 }
2113
2114 LLDB_LOG(log, "traceid not being traced: {0}", traceid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "traceid not being traced: {0}", traceid); } while
(0)
;
2115 return Status("tracing not active for this thread").ToError();
2116}
2117
2118Status NativeProcessLinux::GetMetaData(lldb::user_id_t traceid,
2119 lldb::tid_t thread,
2120 llvm::MutableArrayRef<uint8_t> &buffer,
2121 size_t offset) {
2122 TraceOptions trace_options;
2123 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE(1u << 5)));
2124 Status error;
2125
2126 LLDB_LOG(log, "traceid {0}", traceid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "traceid {0}", traceid); } while (0)
;
2127
2128 auto perf_monitor = LookupProcessorTraceInstance(traceid, thread);
2129 if (!perf_monitor) {
2130 LLDB_LOG(log, "traceid not being traced: {0}", traceid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "traceid not being traced: {0}", traceid); } while
(0)
;
2131 buffer = buffer.slice(buffer.size());
2132 error = perf_monitor.takeError();
2133 return error;
2134 }
2135 return (*perf_monitor).ReadPerfTraceData(buffer, offset);
2136}
2137
2138Status NativeProcessLinux::GetData(lldb::user_id_t traceid, lldb::tid_t thread,
2139 llvm::MutableArrayRef<uint8_t> &buffer,
2140 size_t offset) {
2141 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE(1u << 5)));
2142 Status error;
2143
2144 LLDB_LOG(log, "traceid {0}", traceid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "traceid {0}", traceid); } while (0)
;
2145
2146 auto perf_monitor = LookupProcessorTraceInstance(traceid, thread);
2147 if (!perf_monitor) {
2148 LLDB_LOG(log, "traceid not being traced: {0}", traceid)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "traceid not being traced: {0}", traceid); } while
(0)
;
2149 buffer = buffer.slice(buffer.size());
2150 error = perf_monitor.takeError();
2151 return error;
2152 }
2153 return (*perf_monitor).ReadPerfTraceAux(buffer, offset);
2154}
2155
2156Status NativeProcessLinux::GetTraceConfig(lldb::user_id_t traceid,
2157 TraceOptions &config) {
2158 Status error;
2159 if (config.getThreadID() == LLDB_INVALID_THREAD_ID0 &&
5
Assuming the condition is true
6
Taking true branch
2160 m_pt_proces_trace_id == traceid) {
2161 if (m_pt_proces_trace_id == LLDB_INVALID_UID(18446744073709551615UL)) {
7
Assuming the condition is true
8
Taking true branch
2162 error.SetErrorString("tracing not active for this process");
2163 return error;
2164 }
2165 config = m_pt_process_trace_config;
2166 } else {
2167 auto perf_monitor =
2168 LookupProcessorTraceInstance(traceid, config.getThreadID());
2169 if (!perf_monitor) {
2170 error = perf_monitor.takeError();
2171 return error;
2172 }
2173 error = (*perf_monitor).GetTraceConfig(config);
2174 }
2175 return error;
2176}
9
Returning without writing to 'config.m_type'
2177
2178lldb::user_id_t
2179NativeProcessLinux::StartTraceGroup(const TraceOptions &config,
2180 Status &error) {
2181
2182 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE(1u << 5)));
2183 if (config.getType() != TraceType::eTraceTypeProcessorTrace)
2184 return LLDB_INVALID_UID(18446744073709551615UL);
2185
2186 if (m_pt_proces_trace_id != LLDB_INVALID_UID(18446744073709551615UL)) {
2187 error.SetErrorString("tracing already active on this process");
2188 return m_pt_proces_trace_id;
2189 }
2190
2191 for (const auto &thread_sp : m_threads) {
2192 if (auto traceInstance = ProcessorTraceMonitor::Create(
2193 GetID(), thread_sp->GetID(), config, true)) {
2194 m_pt_traced_thread_group.insert(thread_sp->GetID());
2195 m_processor_trace_monitor.insert(
2196 std::make_pair(thread_sp->GetID(), std::move(*traceInstance)));
2197 }
2198 }
2199
2200 m_pt_process_trace_config = config;
2201 error = ProcessorTraceMonitor::GetCPUType(m_pt_process_trace_config);
2202
2203 // Trace on Complete process will have traceid of 0
2204 m_pt_proces_trace_id = 0;
2205
2206 LLDB_LOG(log, "Process Trace ID {0}", m_pt_proces_trace_id)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Process Trace ID {0}", m_pt_proces_trace_id); } while
(0)
;
2207 return m_pt_proces_trace_id;
2208}
2209
2210lldb::user_id_t NativeProcessLinux::StartTrace(const TraceOptions &config,
2211 Status &error) {
2212 if (config.getType() != TraceType::eTraceTypeProcessorTrace)
2213 return NativeProcessProtocol::StartTrace(config, error);
2214
2215 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE(1u << 5)));
2216
2217 lldb::tid_t threadid = config.getThreadID();
2218
2219 if (threadid == LLDB_INVALID_THREAD_ID0)
2220 return StartTraceGroup(config, error);
2221
2222 auto thread_sp = GetThreadByID(threadid);
2223 if (!thread_sp) {
2224 // Thread not tracked by lldb so don't trace.
2225 error.SetErrorString("invalid thread id");
2226 return LLDB_INVALID_UID(18446744073709551615UL);
2227 }
2228
2229 const auto &iter = m_processor_trace_monitor.find(threadid);
2230 if (iter != m_processor_trace_monitor.end()) {
2231 LLDB_LOG(log, "Thread already being traced")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Thread already being traced"); } while (0)
;
2232 error.SetErrorString("tracing already active on this thread");
2233 return LLDB_INVALID_UID(18446744073709551615UL);
2234 }
2235
2236 auto traceMonitor =
2237 ProcessorTraceMonitor::Create(GetID(), threadid, config, false);
2238 if (!traceMonitor) {
2239 error = traceMonitor.takeError();
2240 LLDB_LOG(log, "error {0}", error)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "error {0}", error); } while (0)
;
2241 return LLDB_INVALID_UID(18446744073709551615UL);
2242 }
2243 lldb::user_id_t ret_trace_id = (*traceMonitor)->GetTraceID();
2244 m_processor_trace_monitor.insert(
2245 std::make_pair(threadid, std::move(*traceMonitor)));
2246 return ret_trace_id;
2247}
2248
2249Status NativeProcessLinux::StopTracingForThread(lldb::tid_t thread) {
2250 Status error;
2251 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE(1u << 5)));
2252 LLDB_LOG(log, "Thread {0}", thread)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Thread {0}", thread); } while (0)
;
2253
2254 const auto& iter = m_processor_trace_monitor.find(thread);
2255 if (iter == m_processor_trace_monitor.end()) {
2256 error.SetErrorString("tracing not active for this thread");
2257 return error;
2258 }
2259
2260 if (iter->second->GetTraceID() == m_pt_proces_trace_id) {
2261 // traceid maps to the whole process so we have to erase it from the thread
2262 // group.
2263 LLDB_LOG(log, "traceid maps to process")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "traceid maps to process"); } while (0)
;
2264 m_pt_traced_thread_group.erase(thread);
2265 }
2266 m_processor_trace_monitor.erase(iter);
2267
2268 return error;
2269}
2270
2271Status NativeProcessLinux::StopTrace(lldb::user_id_t traceid,
2272 lldb::tid_t thread) {
2273 Status error;
2274
2275 TraceOptions trace_options;
1
Calling default constructor for 'TraceOptions'
3
Returning from default constructor for 'TraceOptions'
2276 trace_options.setThreadID(thread);
2277 error = NativeProcessLinux::GetTraceConfig(traceid, trace_options);
4
Calling 'NativeProcessLinux::GetTraceConfig'
10
Returning from 'NativeProcessLinux::GetTraceConfig'
2278
2279 if (error.Fail())
11
Assuming the condition is false
12
Taking false branch
2280 return error;
2281
2282 switch (trace_options.getType()) {
13
Calling 'TraceOptions::getType'
2283 case lldb::TraceType::eTraceTypeProcessorTrace:
2284 if (traceid == m_pt_proces_trace_id &&
2285 thread == LLDB_INVALID_THREAD_ID0)
2286 StopProcessorTracingOnProcess();
2287 else
2288 error = StopProcessorTracingOnThread(traceid, thread);
2289 break;
2290 default:
2291 error.SetErrorString("trace not supported");
2292 break;
2293 }
2294
2295 return error;
2296}
2297
2298void NativeProcessLinux::StopProcessorTracingOnProcess() {
2299 for (auto thread_id_iter : m_pt_traced_thread_group)
2300 m_processor_trace_monitor.erase(thread_id_iter);
2301 m_pt_traced_thread_group.clear();
2302 m_pt_proces_trace_id = LLDB_INVALID_UID(18446744073709551615UL);
2303}
2304
2305Status NativeProcessLinux::StopProcessorTracingOnThread(lldb::user_id_t traceid,
2306 lldb::tid_t thread) {
2307 Status error;
2308 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE(1u << 5)));
2309
2310 if (thread == LLDB_INVALID_THREAD_ID0) {
2311 for (auto& iter : m_processor_trace_monitor) {
2312 if (iter.second->GetTraceID() == traceid) {
2313 // Stopping a trace instance for an individual thread hence there will
2314 // only be one traceid that can match.
2315 m_processor_trace_monitor.erase(iter.first);
2316 return error;
2317 }
2318 LLDB_LOG(log, "Trace ID {0}", iter.second->GetTraceID())do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Trace ID {0}", iter.second->GetTraceID()); } while
(0)
;
2319 }
2320
2321 LLDB_LOG(log, "Invalid TraceID")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Invalid TraceID"); } while (0)
;
2322 error.SetErrorString("invalid trace id");
2323 return error;
2324 }
2325
2326 // thread is specified so we can use find function on the map.
2327 const auto& iter = m_processor_trace_monitor.find(thread);
2328 if (iter == m_processor_trace_monitor.end()) {
2329 // thread not found in our map.
2330 LLDB_LOG(log, "thread not being traced")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "thread not being traced"); } while (0)
;
2331 error.SetErrorString("tracing not active for this thread");
2332 return error;
2333 }
2334 if (iter->second->GetTraceID() != traceid) {
2335 // traceid did not match so it has to be invalid.
2336 LLDB_LOG(log, "Invalid TraceID")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "Invalid TraceID"); } while (0)
;
2337 error.SetErrorString("invalid trace id");
2338 return error;
2339 }
2340
2341 LLDB_LOG(log, "UID - {0} , Thread -{1}", traceid, thread)do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "UID - {0} , Thread -{1}", traceid, thread); } while
(0)
;
2342
2343 if (traceid == m_pt_proces_trace_id) {
2344 // traceid maps to the whole process so we have to erase it from the thread
2345 // group.
2346 LLDB_LOG(log, "traceid maps to process")do { ::lldb_private::Log *log_private = (log); if (log_private
) log_private->Format("/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp"
, __func__, "traceid maps to process"); } while (0)
;
2347 m_pt_traced_thread_group.erase(thread);
2348 }
2349 m_processor_trace_monitor.erase(iter);
2350
2351 return error;
2352}

/build/llvm-toolchain-snapshot-7~svn337204/tools/lldb/include/lldb/Utility/TraceOptions.h

1//===-- TraceOptions.h ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_TraceOptions_h_
11#define liblldb_TraceOptions_h_
12
13#include "lldb/lldb-defines.h"
14#include "lldb/lldb-enumerations.h"
15
16#include "lldb/Utility/StructuredData.h"
17
18namespace lldb_private {
19class TraceOptions {
20public:
21 TraceOptions() : m_trace_params(new StructuredData::Dictionary()) {}
2
Returning without writing to 'this->m_type'
22
23 const StructuredData::DictionarySP &getTraceParams() const {
24 return m_trace_params;
25 }
26
27 lldb::TraceType getType() const { return m_type; }
14
Undefined or garbage value returned to caller
28
29 uint64_t getTraceBufferSize() const { return m_trace_buffer_size; }
30
31 uint64_t getMetaDataBufferSize() const { return m_meta_data_buffer_size; }
32
33 void setTraceParams(const StructuredData::DictionarySP &dict_obj) {
34 m_trace_params = dict_obj;
35 }
36
37 void setType(lldb::TraceType type) { m_type = type; }
38
39 void setTraceBufferSize(uint64_t size) { m_trace_buffer_size = size; }
40
41 void setMetaDataBufferSize(uint64_t size) { m_meta_data_buffer_size = size; }
42
43 void setThreadID(lldb::tid_t thread_id) { m_thread_id = thread_id; }
44
45 lldb::tid_t getThreadID() const { return m_thread_id; }
46
47private:
48 lldb::TraceType m_type;
49 uint64_t m_trace_buffer_size;
50 uint64_t m_meta_data_buffer_size;
51 lldb::tid_t m_thread_id;
52
53 /// m_trace_params is meant to hold any custom parameters
54 /// apart from meta buffer size and trace size.
55 /// The interpretation of such parameters is left to
56 /// the lldb-server.
57 StructuredData::DictionarySP m_trace_params;
58};
59}
60
61#endif // liblldb_TraceOptions_h_