LLDB mainline
NativeProcessNetBSD.cpp
Go to the documentation of this file.
1//===-- NativeProcessNetBSD.cpp -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
16#include "lldb/Target/Process.h"
17#include "lldb/Utility/State.h"
18#include "llvm/Support/Errno.h"
19
20// System includes - They have to be included after framework includes because
21// they define some macros which collide with variable names in other modules
22// clang-format off
23#include <sys/types.h>
24#include <sys/ptrace.h>
25#include <sys/sysctl.h>
26#include <sys/wait.h>
27#include <uvm/uvm_prot.h>
28#include <elf.h>
29#include <util.h>
30// clang-format on
31
32using namespace lldb;
33using namespace lldb_private;
34using namespace lldb_private::process_netbsd;
35using namespace llvm;
36
37// Simple helper function to ensure flags are enabled on the given file
38// descriptor.
39static Status EnsureFDFlags(int fd, int flags) {
41
42 int status = fcntl(fd, F_GETFL);
43 if (status == -1) {
44 error.SetErrorToErrno();
45 return error;
46 }
47
48 if (fcntl(fd, F_SETFL, status | flags) == -1) {
49 error.SetErrorToErrno();
50 return error;
51 }
52
53 return error;
54}
55
56// Public Static Methods
57
58llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
60 NativeDelegate &native_delegate) {
62
63 Status status;
65 .LaunchProcess(launch_info, status)
66 .GetProcessId();
67 LLDB_LOG(log, "pid = {0:x}", pid);
68 if (status.Fail()) {
69 LLDB_LOG(log, "failed to launch process: {0}", status);
70 return status.ToError();
71 }
72
73 // Wait for the child process to trap on its call to execve.
74 int wstatus;
75 ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0);
76 assert(wpid == pid);
77 (void)wpid;
78 if (!WIFSTOPPED(wstatus)) {
79 LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}",
80 WaitStatus::Decode(wstatus));
81 return llvm::make_error<StringError>("Could not sync with inferior process",
82 llvm::inconvertibleErrorCode());
83 }
84 LLDB_LOG(log, "inferior started, now in stopped state");
85
87 if (!Host::GetProcessInfo(pid, Info)) {
88 return llvm::make_error<StringError>("Cannot get process architecture",
89 llvm::inconvertibleErrorCode());
90 }
91
92 // Set the architecture to the exe architecture.
93 LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid,
94 Info.GetArchitecture().GetArchitectureName());
95
96 std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
97 pid, launch_info.GetPTY().ReleasePrimaryFileDescriptor(), native_delegate,
98 Info.GetArchitecture(), m_mainloop));
99
100 status = process_up->SetupTrace();
101 if (status.Fail())
102 return status.ToError();
103
104 for (const auto &thread : process_up->m_threads)
105 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
106 process_up->SetState(StateType::eStateStopped, false);
107
108 return std::move(process_up);
109}
110
111llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
115 LLDB_LOG(log, "pid = {0:x}", pid);
116
117 // Retrieve the architecture for the running process.
119 if (!Host::GetProcessInfo(pid, Info)) {
120 return llvm::make_error<StringError>("Cannot get process architecture",
121 llvm::inconvertibleErrorCode());
122 }
123
124 std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD(
125 pid, -1, native_delegate, Info.GetArchitecture(), m_mainloop));
126
127 Status status = process_up->Attach();
128 if (!status.Success())
129 return status.ToError();
130
131 return std::move(process_up);
132}
133
139}
140
141// Public Instance Methods
142
144 NativeDelegate &delegate,
145 const ArchSpec &arch,
146 MainLoop &mainloop)
147 : NativeProcessELF(pid, terminal_fd, delegate), m_arch(arch),
148 m_main_loop(mainloop) {
149 if (m_terminal_fd != -1) {
151 assert(status.Success());
152 }
153
154 Status status;
156 SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status);
157 assert(m_sigchld_handle && status.Success());
158}
159
160// Handles all waitpid events from the inferior process.
162 switch (signal) {
163 case SIGTRAP:
164 return MonitorSIGTRAP(pid);
165 case SIGSTOP:
166 return MonitorSIGSTOP(pid);
167 default:
168 return MonitorSignal(pid, signal);
169 }
170}
171
174
175 LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid);
176
177 /* Stop Tracking All Threads attached to Process */
178 m_threads.clear();
179
180 SetExitStatus(status, true);
181
182 // Notify delegate that our process has exited.
183 SetState(StateType::eStateExited, true);
184}
185
187 ptrace_siginfo_t info;
188
189 const auto siginfo_err =
190 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
191
192 // Get details on the signal raised.
193 if (siginfo_err.Success()) {
194 // Handle SIGSTOP from LLGS (LLDB GDB Server)
195 if (info.psi_siginfo.si_code == SI_USER &&
196 info.psi_siginfo.si_pid == ::getpid()) {
197 /* Stop Tracking all Threads attached to Process */
198 for (const auto &thread : m_threads) {
199 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(
200 SIGSTOP, &info.psi_siginfo);
201 }
202 }
203 SetState(StateType::eStateStopped, true);
204 }
205}
206
209 ptrace_siginfo_t info;
210
211 const auto siginfo_err =
212 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
213
214 // Get details on the signal raised.
215 if (siginfo_err.Fail()) {
216 LLDB_LOG(log, "PT_GET_SIGINFO failed {0}", siginfo_err);
217 return;
218 }
219
220 LLDB_LOG(log, "got SIGTRAP, pid = {0}, lwpid = {1}, si_code = {2}", pid,
221 info.psi_lwpid, info.psi_siginfo.si_code);
222 NativeThreadNetBSD *thread = nullptr;
223
224 if (info.psi_lwpid > 0) {
225 for (const auto &t : m_threads) {
226 if (t->GetID() == static_cast<lldb::tid_t>(info.psi_lwpid)) {
227 thread = static_cast<NativeThreadNetBSD *>(t.get());
228 break;
229 }
230 static_cast<NativeThreadNetBSD *>(t.get())->SetStoppedWithNoReason();
231 }
232 if (!thread)
233 LLDB_LOG(log, "thread not found in m_threads, pid = {0}, LWP = {1}", pid,
234 info.psi_lwpid);
235 }
236
237 switch (info.psi_siginfo.si_code) {
238 case TRAP_BRKPT:
239 if (thread) {
240 thread->SetStoppedByBreakpoint();
242 }
243 SetState(StateType::eStateStopped, true);
244 return;
245 case TRAP_TRACE:
246 if (thread)
247 thread->SetStoppedByTrace();
248 SetState(StateType::eStateStopped, true);
249 return;
250 case TRAP_EXEC: {
252 if (error.Fail()) {
253 SetState(StateType::eStateInvalid);
254 return;
255 }
256
257 // Let our delegate know we have just exec'd.
259
260 for (const auto &thread : m_threads)
261 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec();
262 SetState(StateType::eStateStopped, true);
263 return;
264 }
265 case TRAP_CHLD: {
266 ptrace_state_t pst;
267 Status error = PtraceWrapper(PT_GET_PROCESS_STATE, pid, &pst, sizeof(pst));
268 if (error.Fail()) {
269 SetState(StateType::eStateInvalid);
270 return;
271 }
272
273 assert(thread);
274 if (pst.pe_report_event == PTRACE_VFORK_DONE) {
276 thread->SetStoppedByVForkDone();
277 SetState(StateType::eStateStopped, true);
278 } else {
279 Status error =
280 PtraceWrapper(PT_CONTINUE, pid, reinterpret_cast<void *>(1), 0);
281 if (error.Fail())
282 SetState(StateType::eStateInvalid);
283 }
284 } else {
285 assert(pst.pe_report_event == PTRACE_FORK ||
286 pst.pe_report_event == PTRACE_VFORK);
287 MonitorClone(pst.pe_other_pid, pst.pe_report_event == PTRACE_VFORK,
288 *thread);
289 }
290 return;
291 }
292 case TRAP_LWP: {
293 ptrace_state_t pst;
294 Status error = PtraceWrapper(PT_GET_PROCESS_STATE, pid, &pst, sizeof(pst));
295 if (error.Fail()) {
296 SetState(StateType::eStateInvalid);
297 return;
298 }
299
300 switch (pst.pe_report_event) {
301 case PTRACE_LWP_CREATE: {
302 LLDB_LOG(log, "monitoring new thread, pid = {0}, LWP = {1}", pid,
303 pst.pe_lwp);
304 NativeThreadNetBSD &t = AddThread(pst.pe_lwp);
306 static_cast<NativeThreadNetBSD &>(*GetCurrentThread()));
307 if (error.Fail()) {
308 LLDB_LOG(log, "failed to copy watchpoints to new thread {0}: {1}",
309 pst.pe_lwp, error);
310 SetState(StateType::eStateInvalid);
311 return;
312 }
313 } break;
314 case PTRACE_LWP_EXIT:
315 LLDB_LOG(log, "removing exited thread, pid = {0}, LWP = {1}", pid,
316 pst.pe_lwp);
317 RemoveThread(pst.pe_lwp);
318 break;
319 }
320
321 error = PtraceWrapper(PT_CONTINUE, pid, reinterpret_cast<void *>(1), 0);
322 if (error.Fail())
323 SetState(StateType::eStateInvalid);
324 return;
325 }
326 case TRAP_DBREG: {
327 if (!thread)
328 break;
329
330 auto &regctx = static_cast<NativeRegisterContextNetBSD &>(
331 thread->GetRegisterContext());
332 uint32_t wp_index = LLDB_INVALID_INDEX32;
333 Status error = regctx.GetWatchpointHitIndex(
334 wp_index, (uintptr_t)info.psi_siginfo.si_addr);
335 if (error.Fail())
336 LLDB_LOG(log,
337 "received error while checking for watchpoint hits, pid = "
338 "{0}, LWP = {1}, error = {2}",
339 pid, info.psi_lwpid, error);
340 if (wp_index != LLDB_INVALID_INDEX32) {
341 thread->SetStoppedByWatchpoint(wp_index);
342 regctx.ClearWatchpointHit(wp_index);
343 SetState(StateType::eStateStopped, true);
344 return;
345 }
346
347 thread->SetStoppedByTrace();
348 SetState(StateType::eStateStopped, true);
349 return;
350 }
351 }
352
353 // Either user-generated SIGTRAP or an unknown event that would
354 // otherwise leave the debugger hanging.
355 LLDB_LOG(log, "unknown SIGTRAP, passing to generic handler");
357}
358
361 ptrace_siginfo_t info;
362
363 const auto siginfo_err =
364 PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info));
365 if (siginfo_err.Fail()) {
366 LLDB_LOG(log, "PT_LWPINFO failed {0}", siginfo_err);
367 return;
368 }
369
370 for (const auto &abs_thread : m_threads) {
371 NativeThreadNetBSD &thread = static_cast<NativeThreadNetBSD &>(*abs_thread);
372 assert(info.psi_lwpid >= 0);
373 if (info.psi_lwpid == 0 ||
374 static_cast<lldb::tid_t>(info.psi_lwpid) == thread.GetID())
375 thread.SetStoppedBySignal(info.psi_siginfo.si_signo, &info.psi_siginfo);
376 else
377 thread.SetStoppedWithNoReason();
378 }
379 SetState(StateType::eStateStopped, true);
380}
381
383#ifdef PT_STOP
384 return PtraceWrapper(PT_STOP, pid);
385#else
388 int ret;
389
390 errno = 0;
391 ret = kill(pid, SIGSTOP);
392
393 if (ret == -1)
394 error.SetErrorToErrno();
395
396 LLDB_LOG(log, "kill({0}, SIGSTOP)", pid);
397
398 if (error.Fail())
399 LLDB_LOG(log, "kill() failed: {0}", error);
400
401 return error;
402#endif
403}
404
406 int data, int *result) {
409 int ret;
410
411 errno = 0;
412 ret = ptrace(req, static_cast<::pid_t>(pid), addr, data);
413
414 if (ret == -1)
415 error.SetErrorToErrno();
416
417 if (result)
418 *result = ret;
419
420 LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret);
421
422 if (error.Fail())
423 LLDB_LOG(log, "ptrace() failed: {0}", error);
424
425 return error;
426}
427
428static llvm::Expected<ptrace_siginfo_t> ComputeSignalInfo(
429 const std::vector<std::unique_ptr<NativeThreadProtocol>> &threads,
430 const ResumeActionList &resume_actions) {
431 // We need to account for three possible scenarios:
432 // 1. no signal being sent.
433 // 2. a signal being sent to one thread.
434 // 3. a signal being sent to the whole process.
435
436 // Count signaled threads. While at it, determine which signal is being sent
437 // and ensure there's only one.
438 size_t signaled_threads = 0;
439 int signal = LLDB_INVALID_SIGNAL_NUMBER;
440 lldb::tid_t signaled_lwp;
441 for (const auto &thread : threads) {
442 assert(thread && "thread list should not contain NULL threads");
443 const ResumeAction *action =
444 resume_actions.GetActionForThread(thread->GetID(), true);
445 if (action) {
446 if (action->signal != LLDB_INVALID_SIGNAL_NUMBER) {
447 signaled_threads++;
448 if (action->signal != signal) {
449 if (signal != LLDB_INVALID_SIGNAL_NUMBER)
450 return Status("NetBSD does not support passing multiple signals "
451 "simultaneously")
452 .ToError();
453 signal = action->signal;
454 signaled_lwp = thread->GetID();
455 }
456 }
457 }
458 }
459
460 if (signaled_threads == 0) {
461 ptrace_siginfo_t siginfo;
462 siginfo.psi_siginfo.si_signo = LLDB_INVALID_SIGNAL_NUMBER;
463 return siginfo;
464 }
465
466 if (signaled_threads > 1 && signaled_threads < threads.size())
467 return Status("NetBSD does not support passing signal to 1<i<all threads")
468 .ToError();
469
470 ptrace_siginfo_t siginfo;
471 siginfo.psi_siginfo.si_signo = signal;
472 siginfo.psi_siginfo.si_code = SI_USER;
473 siginfo.psi_siginfo.si_pid = getpid();
474 siginfo.psi_siginfo.si_uid = getuid();
475 if (signaled_threads == 1)
476 siginfo.psi_lwpid = signaled_lwp;
477 else // signal for the whole process
478 siginfo.psi_lwpid = 0;
479 return siginfo;
480}
481
484 LLDB_LOG(log, "pid {0}", GetID());
485
486 Status ret;
487
488 Expected<ptrace_siginfo_t> siginfo =
489 ComputeSignalInfo(m_threads, resume_actions);
490 if (!siginfo)
491 return Status(siginfo.takeError());
492
493 for (const auto &abs_thread : m_threads) {
494 assert(abs_thread && "thread list should not contain NULL threads");
495 NativeThreadNetBSD &thread = static_cast<NativeThreadNetBSD &>(*abs_thread);
496
497 const ResumeAction *action =
498 resume_actions.GetActionForThread(thread.GetID(), true);
499 // we need to explicit issue suspend requests, so it is simpler to map it
500 // into proper action
501 ResumeAction suspend_action{thread.GetID(), eStateSuspended,
503
504 if (action == nullptr) {
505 LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(),
506 thread.GetID());
507 action = &suspend_action;
508 }
509
510 LLDB_LOG(
511 log,
512 "processing resume action state {0} signal {1} for pid {2} tid {3}",
513 action->state, action->signal, GetID(), thread.GetID());
514
515 switch (action->state) {
516 case eStateRunning:
517 ret = thread.Resume();
518 break;
519 case eStateStepping:
520 ret = thread.SingleStep();
521 break;
522 case eStateSuspended:
523 case eStateStopped:
524 if (action->signal != LLDB_INVALID_SIGNAL_NUMBER)
525 return Status("Passing signal to suspended thread unsupported");
526
527 ret = thread.Suspend();
528 break;
529
530 default:
531 return Status("NativeProcessNetBSD::%s (): unexpected state %s specified "
532 "for pid %" PRIu64 ", tid %" PRIu64,
533 __FUNCTION__, StateAsCString(action->state), GetID(),
534 thread.GetID());
535 }
536
537 if (!ret.Success())
538 return ret;
539 }
540
541 int signal = 0;
542 if (siginfo->psi_siginfo.si_signo != LLDB_INVALID_SIGNAL_NUMBER) {
543 ret = PtraceWrapper(PT_SET_SIGINFO, GetID(), &siginfo.get(),
544 sizeof(*siginfo));
545 if (!ret.Success())
546 return ret;
547 signal = siginfo->psi_siginfo.si_signo;
548 }
549
550 ret =
551 PtraceWrapper(PT_CONTINUE, GetID(), reinterpret_cast<void *>(1), signal);
552 if (ret.Success())
553 SetState(eStateRunning, true);
554 return ret;
555}
556
558
561
562 // Stop monitoring the inferior.
563 m_sigchld_handle.reset();
564
565 // Tell ptrace to detach from the process.
567 return error;
568
569 return PtraceWrapper(PT_DETACH, GetID(), reinterpret_cast<void *>(1));
570}
571
574
575 if (kill(GetID(), signo))
576 error.SetErrorToErrno();
577
578 return error;
579}
580
582
585 LLDB_LOG(log, "pid {0}", GetID());
586
588
589 switch (m_state) {
590 case StateType::eStateInvalid:
591 case StateType::eStateExited:
592 case StateType::eStateCrashed:
593 case StateType::eStateDetached:
594 case StateType::eStateUnloaded:
595 // Nothing to do - the process is already dead.
596 LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(),
598 return error;
599
600 case StateType::eStateConnected:
601 case StateType::eStateAttaching:
602 case StateType::eStateLaunching:
603 case StateType::eStateStopped:
604 case StateType::eStateRunning:
605 case StateType::eStateStepping:
606 case StateType::eStateSuspended:
607 // We can try to kill a process in these states.
608 break;
609 }
610
611 if (kill(GetID(), SIGKILL) != 0) {
612 error.SetErrorToErrno();
613 return error;
614 }
615
616 return error;
617}
618
620 MemoryRegionInfo &range_info) {
621
623 // We're done.
624 return Status("unsupported");
625 }
626
628 if (error.Fail()) {
629 return error;
630 }
631
632 lldb::addr_t prev_base_address = 0;
633 // FIXME start by finding the last region that is <= target address using
634 // binary search. Data is sorted.
635 // There can be a ton of regions on pthreads apps with lots of threads.
636 for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end();
637 ++it) {
638 MemoryRegionInfo &proc_entry_info = it->first;
639 // Sanity check assumption that memory map entries are ascending.
640 assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) &&
641 "descending memory map entries detected, unexpected");
642 prev_base_address = proc_entry_info.GetRange().GetRangeBase();
643 UNUSED_IF_ASSERT_DISABLED(prev_base_address);
644 // If the target address comes before this entry, indicate distance to next
645 // region.
646 if (load_addr < proc_entry_info.GetRange().GetRangeBase()) {
647 range_info.GetRange().SetRangeBase(load_addr);
648 range_info.GetRange().SetByteSize(
649 proc_entry_info.GetRange().GetRangeBase() - load_addr);
654 return error;
655 } else if (proc_entry_info.GetRange().Contains(load_addr)) {
656 // The target address is within the memory region we're processing here.
657 range_info = proc_entry_info;
658 return error;
659 }
660 // The target memory address comes somewhere after the region we just
661 // parsed.
662 }
663 // If we made it here, we didn't find an entry that contained the given
664 // address. Return the load_addr as start and the amount of bytes betwwen
665 // load address and the end of the memory as size.
666 range_info.GetRange().SetRangeBase(load_addr);
672 return error;
673}
674
677 // If our cache is empty, pull the latest. There should always be at least
678 // one memory region if memory region handling is supported.
679 if (!m_mem_region_cache.empty()) {
680 LLDB_LOG(log, "reusing {0} cached memory region entries",
681 m_mem_region_cache.size());
682 return Status();
683 }
684
685 struct kinfo_vmentry *vm;
686 size_t count, i;
687 vm = kinfo_getvmmap(GetID(), &count);
688 if (vm == NULL) {
691 error.SetErrorString("not supported");
692 return error;
693 }
694 for (i = 0; i < count; i++) {
695 MemoryRegionInfo info;
696 info.Clear();
697 info.GetRange().SetRangeBase(vm[i].kve_start);
698 info.GetRange().SetRangeEnd(vm[i].kve_end);
700
701 if (vm[i].kve_protection & VM_PROT_READ)
703 else
705
706 if (vm[i].kve_protection & VM_PROT_WRITE)
708 else
710
711 if (vm[i].kve_protection & VM_PROT_EXECUTE)
713 else
715
716 if (vm[i].kve_path[0])
717 info.SetName(vm[i].kve_path);
718
719 m_mem_region_cache.emplace_back(info,
720 FileSpec(info.GetName().GetCString()));
721 }
722 free(vm);
723
724 if (m_mem_region_cache.empty()) {
725 // No entries after attempting to read them. This shouldn't happen. Assume
726 // we don't support map entries.
727 LLDB_LOG(log, "failed to find any vmmap entries, assuming no support "
728 "for memory region metadata retrieval");
731 error.SetErrorString("not supported");
732 return error;
733 }
734 LLDB_LOG(log, "read {0} memory region entries from process {1}",
735 m_mem_region_cache.size(), GetID());
736 // We support memory retrieval, remember that.
738 return Status();
739}
740
742 // punt on this for now
744}
745
747
749 bool hardware) {
750 if (hardware)
751 return Status("NativeProcessNetBSD does not support hardware breakpoints");
752 else
753 return SetSoftwareBreakpoint(addr, size);
754}
755
757 FileSpec &file_spec) {
759 if (error.Fail())
760 return error;
761
762 FileSpec module_file_spec(module_path);
763 FileSystem::Instance().Resolve(module_file_spec);
764
765 file_spec.Clear();
766 for (const auto &it : m_mem_region_cache) {
767 if (it.second.GetFilename() == module_file_spec.GetFilename()) {
768 file_spec = it.second;
769 return Status();
770 }
771 }
772 return Status("Module file (%s) not found in process' memory map!",
773 module_file_spec.GetFilename().AsCString());
774}
775
776Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name,
777 lldb::addr_t &load_addr) {
778 load_addr = LLDB_INVALID_ADDRESS;
780 if (error.Fail())
781 return error;
782
783 FileSpec file(file_name);
784 for (const auto &it : m_mem_region_cache) {
785 if (it.second == file) {
786 load_addr = it.first.GetRange().GetRangeBase();
787 return Status();
788 }
789 }
790 return Status("No load address found for file %s.", file_name.str().c_str());
791}
792
795 int status;
796 ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status,
797 WALLSIG | WNOHANG);
798
799 if (wait_pid == 0)
800 return;
801
802 if (wait_pid == -1) {
804 LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error);
805 return;
806 }
807
808 WaitStatus wait_status = WaitStatus::Decode(status);
809 bool exited = wait_status.type == WaitStatus::Exit ||
810 (wait_status.type == WaitStatus::Signal &&
811 wait_pid == static_cast<::pid_t>(GetID()));
812
813 LLDB_LOG(log,
814 "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}",
815 GetID(), wait_pid, status, exited);
816
817 if (exited)
818 MonitorExited(wait_pid, wait_status);
819 else {
820 assert(wait_status.type == WaitStatus::Stop);
821 MonitorCallback(wait_pid, wait_status.status);
822 }
823}
824
826 for (const auto &thread : m_threads) {
827 assert(thread && "thread list should not contain NULL threads");
828 if (thread->GetID() == thread_id) {
829 // We have this thread.
830 return true;
831 }
832 }
833
834 // We don't have this thread.
835 return false;
836}
837
840 LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id);
841
842 assert(thread_id > 0);
843 assert(!HasThreadNoLock(thread_id) &&
844 "attempted to add a thread by id that already exists");
845
846 // If this is the first thread, save it as the current thread
847 if (m_threads.empty())
848 SetCurrentThreadID(thread_id);
849
850 m_threads.push_back(std::make_unique<NativeThreadNetBSD>(*this, thread_id));
851 return static_cast<NativeThreadNetBSD &>(*m_threads.back());
852}
853
856 LLDB_LOG(log, "pid {0} removing thread with tid {1}", GetID(), thread_id);
857
858 assert(thread_id > 0);
859 assert(HasThreadNoLock(thread_id) &&
860 "attempted to remove a thread that does not exist");
861
862 for (auto it = m_threads.begin(); it != m_threads.end(); ++it) {
863 if ((*it)->GetID() == thread_id) {
864 m_threads.erase(it);
865 break;
866 }
867 }
868}
869
871 // Attach to the requested process.
872 // An attach will cause the thread to stop with a SIGSTOP.
873 Status status = PtraceWrapper(PT_ATTACH, m_pid);
874 if (status.Fail())
875 return status;
876
877 int wstatus;
878 // Need to use WALLSIG otherwise we receive an error with errno=ECHLD At this
879 // point we should have a thread stopped if waitpid succeeds.
880 if ((wstatus = llvm::sys::RetryAfterSignal(-1, waitpid, m_pid, nullptr,
881 WALLSIG)) < 0)
882 return Status(errno, eErrorTypePOSIX);
883
884 // Initialize threads and tracing status
885 // NB: this needs to be called before we set thread state
886 status = SetupTrace();
887 if (status.Fail())
888 return status;
889
890 for (const auto &thread : m_threads)
891 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
892
893 // Let our process instance know the thread has stopped.
894 SetCurrentThreadID(m_threads.front()->GetID());
895 SetState(StateType::eStateStopped, false);
896 return Status();
897}
898
900 size_t size, size_t &bytes_read) {
901 unsigned char *dst = static_cast<unsigned char *>(buf);
902 struct ptrace_io_desc io;
903
905 LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
906
907 bytes_read = 0;
908 io.piod_op = PIOD_READ_D;
909 io.piod_len = size;
910
911 do {
912 io.piod_offs = (void *)(addr + bytes_read);
913 io.piod_addr = dst + bytes_read;
914
916 if (error.Fail() || io.piod_len == 0)
917 return error;
918
919 bytes_read += io.piod_len;
920 io.piod_len = size - bytes_read;
921 } while (bytes_read < size);
922
923 return Status();
924}
925
927 size_t size, size_t &bytes_written) {
928 const unsigned char *src = static_cast<const unsigned char *>(buf);
930 struct ptrace_io_desc io;
931
933 LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size);
934
935 bytes_written = 0;
936 io.piod_op = PIOD_WRITE_D;
937 io.piod_len = size;
938
939 do {
940 io.piod_addr =
941 const_cast<void *>(static_cast<const void *>(src + bytes_written));
942 io.piod_offs = (void *)(addr + bytes_written);
943
945 if (error.Fail() || io.piod_len == 0)
946 return error;
947
948 bytes_written += io.piod_len;
949 io.piod_len = size - bytes_written;
950 } while (bytes_written < size);
951
952 return error;
953}
954
955llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
957 /*
958 * ELF_AUX_ENTRIES is currently restricted to kernel
959 * (<sys/exec_elf.h> r. 1.155 specifies 15)
960 *
961 * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this
962 * information isn't needed.
963 */
964 size_t auxv_size = 100 * sizeof(AuxInfo);
965
966 ErrorOr<std::unique_ptr<WritableMemoryBuffer>> buf =
967 llvm::WritableMemoryBuffer::getNewMemBuffer(auxv_size);
968
969 struct ptrace_io_desc io;
970 io.piod_op = PIOD_READ_AUXV;
971 io.piod_offs = 0;
972 io.piod_addr = static_cast<void *>(buf.get()->getBufferStart());
973 io.piod_len = auxv_size;
974
976
977 if (error.Fail())
978 return std::error_code(error.GetError(), std::generic_category());
979
980 if (io.piod_len < 1)
981 return std::error_code(ECANCELED, std::generic_category());
982
983 return std::move(buf);
984}
985
987 // Enable event reporting
988 ptrace_event_t events;
989 Status status =
990 PtraceWrapper(PT_GET_EVENT_MASK, GetID(), &events, sizeof(events));
991 if (status.Fail())
992 return status;
993 // TODO: PTRACE_POSIX_SPAWN?
994 events.pe_set_event |= PTRACE_LWP_CREATE | PTRACE_LWP_EXIT | PTRACE_FORK |
995 PTRACE_VFORK | PTRACE_VFORK_DONE;
996 status = PtraceWrapper(PT_SET_EVENT_MASK, GetID(), &events, sizeof(events));
997 if (status.Fail())
998 return status;
999
1000 return ReinitializeThreads();
1001}
1002
1004 // Clear old threads
1005 m_threads.clear();
1006
1007 // Initialize new thread
1008#ifdef PT_LWPSTATUS
1009 struct ptrace_lwpstatus info = {};
1010 int op = PT_LWPNEXT;
1011#else
1012 struct ptrace_lwpinfo info = {};
1013 int op = PT_LWPINFO;
1014#endif
1015
1016 Status error = PtraceWrapper(op, GetID(), &info, sizeof(info));
1017
1018 if (error.Fail()) {
1019 return error;
1020 }
1021 // Reinitialize from scratch threads and register them in process
1022 while (info.pl_lwpid != 0) {
1023 AddThread(info.pl_lwpid);
1024 error = PtraceWrapper(op, GetID(), &info, sizeof(info));
1025 if (error.Fail()) {
1026 return error;
1027 }
1028 }
1029
1030 return error;
1031}
1032
1033void NativeProcessNetBSD::MonitorClone(::pid_t child_pid, bool is_vfork,
1034 NativeThreadNetBSD &parent_thread) {
1036 LLDB_LOG(log, "clone, child_pid={0}", child_pid);
1037
1038 int status;
1039 ::pid_t wait_pid =
1040 llvm::sys::RetryAfterSignal(-1, ::waitpid, child_pid, &status, 0);
1041 if (wait_pid != child_pid) {
1042 LLDB_LOG(log,
1043 "waiting for pid {0} failed. Assuming the pid has "
1044 "disappeared in the meantime",
1045 child_pid);
1046 return;
1047 }
1048 if (WIFEXITED(status)) {
1049 LLDB_LOG(log,
1050 "waiting for pid {0} returned an 'exited' event. Not "
1051 "tracking it.",
1052 child_pid);
1053 return;
1054 }
1055
1056 ptrace_siginfo_t info;
1057 const auto siginfo_err =
1058 PtraceWrapper(PT_GET_SIGINFO, child_pid, &info, sizeof(info));
1059 if (siginfo_err.Fail()) {
1060 LLDB_LOG(log, "PT_GET_SIGINFO failed {0}", siginfo_err);
1061 return;
1062 }
1063 assert(info.psi_lwpid >= 0);
1064 lldb::tid_t child_tid = info.psi_lwpid;
1065
1066 std::unique_ptr<NativeProcessNetBSD> child_process{
1067 new NativeProcessNetBSD(static_cast<::pid_t>(child_pid), m_terminal_fd,
1069 if (!is_vfork)
1071
1072 Extension expected_ext = is_vfork ? Extension::vfork : Extension::fork;
1073 if ((m_enabled_extensions & expected_ext) == expected_ext) {
1074 child_process->SetupTrace();
1075 for (const auto &thread : child_process->m_threads)
1076 static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP);
1077 child_process->SetState(StateType::eStateStopped, false);
1078
1079 m_delegate.NewSubprocess(this, std::move(child_process));
1080 if (is_vfork)
1081 parent_thread.SetStoppedByVFork(child_pid, child_tid);
1082 else
1083 parent_thread.SetStoppedByFork(child_pid, child_tid);
1084 SetState(StateType::eStateStopped, true);
1085 } else {
1086 child_process->Detach();
1087 Status pt_error =
1088 PtraceWrapper(PT_CONTINUE, GetID(), reinterpret_cast<void *>(1), 0);
1089 if (pt_error.Fail()) {
1090 LLDB_LOG_ERROR(log, std::move(pt_error.ToError()),
1091 "unable to resume parent process {1}: {0}", GetID());
1092 SetState(StateType::eStateInvalid);
1093 }
1094 }
1095}
1096
1097llvm::Expected<std::string>
1098NativeProcessNetBSD::SaveCore(llvm::StringRef path_hint) {
1099 llvm::SmallString<128> path{path_hint};
1100 Status error;
1101
1102 // Try with the suggested path first.
1103 if (!path.empty()) {
1104 error = PtraceWrapper(PT_DUMPCORE, GetID(), path.data(), path.size());
1105 if (!error.Fail())
1106 return path.str().str();
1107
1108 // If the request errored, fall back to a generic temporary file.
1109 }
1110
1111 if (std::error_code errc =
1112 llvm::sys::fs::createTemporaryFile("lldb", "core", path))
1113 return llvm::createStringError(errc, "Unable to create a temporary file");
1114
1115 error = PtraceWrapper(PT_DUMPCORE, GetID(), path.data(), path.size());
1116 if (error.Fail())
1117 return error.ToError();
1118 return path.str().str();
1119}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:359
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:382
static Status EnsureFDFlags(int fd, int flags)
static Status EnsureFDFlags(int fd, int flags)
static llvm::Expected< ptrace_siginfo_t > ComputeSignalInfo(const std::vector< std::unique_ptr< NativeThreadProtocol > > &threads, const ResumeActionList &resume_actions)
An architecture specification class.
Definition: ArchSpec.h:31
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
A file utility class.
Definition: FileSpec.h:56
const ConstString & GetFilename() const
Filename string const get accessor.
Definition: FileSpec.h:240
void Clear()
Clears the object state.
Definition: FileSpec.cpp:259
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
static FileSystem & Instance()
lldb::pid_t GetProcessId() const
Definition: HostProcess.cpp:25
static bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info)
SignalHandleUP RegisterSignal(int signo, const Callback &callback, Status &error)
void SetMapped(OptionalBool val)
void SetReadable(OptionalBool val)
void SetExecutable(OptionalBool val)
void SetName(const char *name)
void SetWritable(OptionalBool val)
Abstract class that extends NativeProcessProtocol with ELF specific logic.
void NotifyDidExec() override
Notify the delegate that an exec occurred.
virtual void NewSubprocess(NativeProcessProtocol *parent_process, std::unique_ptr< NativeProcessProtocol > child_process)=0
Status SetSoftwareBreakpoint(lldb::addr_t addr, uint32_t size_hint)
void SetState(lldb::StateType state, bool notify_delegates=true)
std::vector< std::unique_ptr< NativeThreadProtocol > > m_threads
virtual bool SetExitStatus(WaitStatus status, bool bNotifyStateChange)
void FixupBreakpointPCAsNeeded(NativeThreadProtocol &thread)
Extension
Extension flag constants, returned by Manager::GetSupportedExtensions() and passed to SetEnabledExten...
std::unordered_map< lldb::addr_t, SoftwareBreakpoint > m_software_breakpoints
HostProcess LaunchProcess(const ProcessLaunchInfo &launch_info, Status &error) override
int ReleasePrimaryFileDescriptor()
Release the primary file descriptor.
const ResumeAction * GetActionForThread(lldb::tid_t tid, bool default_ok) const
Definition: Debug.h:74
An error handling class.
Definition: Status.h:44
llvm::Error ToError() const
Definition: Status.cpp:89
bool Fail() const
Test for error condition.
Definition: Status.cpp:180
bool Success() const
Test for success condition.
Definition: Status.cpp:278
llvm::Expected< std::unique_ptr< NativeProcessProtocol > > Attach(lldb::pid_t pid, NativeDelegate &native_delegate) override
Attach to an existing process.
Extension GetSupportedExtensions() const override
Get the bitmask of extensions supported by this process plugin.
llvm::Expected< std::unique_ptr< NativeProcessProtocol > > Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate) override
Launch a process for debugging.
Manages communication with the inferior (debugee) process.
llvm::Expected< std::string > SaveCore(llvm::StringRef path_hint) override
Write a core dump (without crashing the program).
void MonitorClone(::pid_t child_pid, bool is_vfork, NativeThreadNetBSD &parent_thread)
Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override
Status Interrupt() override
Tells a process to interrupt all operations as if by a Ctrl-C.
Status GetMemoryRegionInfo(lldb::addr_t load_addr, MemoryRegionInfo &range_info) override
Status GetLoadedModuleFileSpec(const char *module_path, FileSpec &file_spec) override
Status GetFileLoadAddress(const llvm::StringRef &file_name, lldb::addr_t &load_addr) override
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > GetAuxvData() const override
Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override
std::vector< std::pair< MemoryRegionInfo, FileSpec > > m_mem_region_cache
Status Resume(const ResumeActionList &resume_actions) override
NativeProcessNetBSD(::pid_t pid, int terminal_fd, NativeDelegate &delegate, const ArchSpec &arch, MainLoop &mainloop)
void MonitorExited(lldb::pid_t pid, WaitStatus status)
NativeThreadNetBSD & AddThread(lldb::tid_t thread_id)
Status SetBreakpoint(lldb::addr_t addr, uint32_t size, bool hardware) override
static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr=nullptr, int data=0, int *result=nullptr)
Status Signal(int signo) override
Sends a process a UNIX signal signal.
void SetStoppedByFork(lldb::pid_t child_pid, lldb::tid_t child_tid)
void SetStoppedBySignal(uint32_t signo, const siginfo_t *info=nullptr)
NativeRegisterContextNetBSD & GetRegisterContext() override
void SetStoppedByVFork(lldb::pid_t child_pid, lldb::tid_t child_tid)
llvm::Error CopyWatchpointsFrom(NativeThreadNetBSD &source)
#define LLDB_INVALID_SIGNAL_NUMBER
Definition: lldb-defines.h:92
#define LLDB_INVALID_INDEX32
Definition: lldb-defines.h:83
#define UNUSED_IF_ASSERT_DISABLED(x)
Definition: lldb-defines.h:140
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:89
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:331
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
Definition: SBAddress.h:15
@ eStateStopped
Process or thread is stopped and can be examined.
@ eStateSuspended
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
@ eStateRunning
Process or thread is running and can't be examined.
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
@ eErrorTypePOSIX
POSIX error codes.
uint64_t pid_t
Definition: lldb-types.h:83
uint64_t addr_t
Definition: lldb-types.h:80
uint64_t tid_t
Definition: lldb-types.h:84
Definition: Debugger.h:54
bool Contains(BaseType r) const
Definition: RangeMap.h:93
BaseType GetRangeBase() const
Definition: RangeMap.h:45
void SetRangeEnd(BaseType end)
Definition: RangeMap.h:80
void SetRangeBase(BaseType b)
Set the start value for the range, and keep the same size.
Definition: RangeMap.h:48
void SetByteSize(SizeType s)
Definition: RangeMap.h:89
lldb::StateType state
Definition: Debug.h:23
static WaitStatus Decode(int wstatus)
#define SIGSTOP
#define O_NONBLOCK
#define SIGTRAP
#define SIGKILL