LLDB  mainline
NativeProcessProtocol.cpp
Go to the documentation of this file.
1 //===-- NativeProcessProtocol.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 #include "lldb/Host/Host.h"
15 #include "lldb/Utility/LLDBLog.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/State.h"
18 #include "lldb/lldb-enumerations.h"
19 
20 #include "llvm/Support/Process.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 // NativeProcessProtocol Members
26 
27 NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd,
28  NativeDelegate &delegate)
29  : m_pid(pid), m_delegate(delegate), m_terminal_fd(terminal_fd) {
30  delegate.InitializeDelegate(this);
31 }
32 
34  Status error;
35 #if !defined(SIGSTOP)
36  error.SetErrorString("local host does not support signaling");
37  return error;
38 #else
39  return Signal(SIGSTOP);
40 #endif
41 }
42 
43 Status NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) {
44  m_signals_to_ignore.clear();
45  m_signals_to_ignore.insert(signals.begin(), signals.end());
46  return Status();
47 }
48 
51  MemoryRegionInfo &range_info) {
52  // Default: not implemented.
53  return Status("not implemented");
54 }
55 
58  size_t len, std::vector<uint8_t> &tags) {
59  return Status("not implemented");
60 }
61 
64  size_t len,
65  const std::vector<uint8_t> &tags) {
66  return Status("not implemented");
67 }
68 
69 llvm::Optional<WaitStatus> NativeProcessProtocol::GetExitStatus() {
71  return m_exit_status;
72 
73  return llvm::None;
74 }
75 
77  bool bNotifyStateChange) {
78  Log *log = GetLog(LLDBLog::Process);
79  LLDB_LOG(log, "status = {0}, notify = {1}", status, bNotifyStateChange);
80 
81  // Exit status already set
82  if (m_state == lldb::eStateExited) {
83  if (m_exit_status)
84  LLDB_LOG(log, "exit status already set to {0}", *m_exit_status);
85  else
86  LLDB_LOG(log, "state is exited, but status not set");
87  return false;
88  }
89 
91  m_exit_status = status;
92 
93  if (bNotifyStateChange)
95 
96  return true;
97 }
98 
100  std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
101  if (idx < m_threads.size())
102  return m_threads[idx].get();
103  return nullptr;
104 }
105 
108  for (const auto &thread : m_threads) {
109  if (thread->GetID() == tid)
110  return thread.get();
111  }
112  return nullptr;
113 }
114 
116  std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
117  return GetThreadByIDUnlocked(tid);
118 }
119 
121  return m_state != eStateDetached && m_state != eStateExited &&
123 }
124 
128 }
129 
130 llvm::Optional<std::pair<uint32_t, uint32_t>>
132  Log *log = GetLog(LLDBLog::Process);
133 
134  // get any thread
135  NativeThreadProtocol *thread(
136  const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
137  if (!thread) {
138  LLDB_LOG(log, "failed to find a thread to grab a NativeRegisterContext!");
139  return llvm::None;
140  }
141 
142  NativeRegisterContext &reg_ctx = thread->GetRegisterContext();
143  return std::make_pair(reg_ctx.NumSupportedHardwareBreakpoints(),
145 }
146 
148  uint32_t watch_flags,
149  bool hardware) {
150  // This default implementation assumes setting the watchpoint for the process
151  // will require setting the watchpoint for each of the threads. Furthermore,
152  // it will track watchpoints set for the process and will add them to each
153  // thread that is attached to via the (FIXME implement) OnThreadAttached ()
154  // method.
155 
156  Log *log = GetLog(LLDBLog::Process);
157 
158  // Update the thread list
159  UpdateThreads();
160 
161  // Keep track of the threads we successfully set the watchpoint for. If one
162  // of the thread watchpoint setting operations fails, back off and remove the
163  // watchpoint for all the threads that were successfully set so we get back
164  // to a consistent state.
165  std::vector<NativeThreadProtocol *> watchpoint_established_threads;
166 
167  // Tell each thread to set a watchpoint. In the event that hardware
168  // watchpoints are requested but the SetWatchpoint fails, try to set a
169  // software watchpoint as a fallback. It's conceivable that if there are
170  // more threads than hardware watchpoints available, some of the threads will
171  // fail to set hardware watchpoints while software ones may be available.
172  std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
173  for (const auto &thread : m_threads) {
174  assert(thread && "thread list should not have a NULL thread!");
175 
176  Status thread_error =
177  thread->SetWatchpoint(addr, size, watch_flags, hardware);
178  if (thread_error.Fail() && hardware) {
179  // Try software watchpoints since we failed on hardware watchpoint
180  // setting and we may have just run out of hardware watchpoints.
181  thread_error = thread->SetWatchpoint(addr, size, watch_flags, false);
182  if (thread_error.Success())
183  LLDB_LOG(log,
184  "hardware watchpoint requested but software watchpoint set");
185  }
186 
187  if (thread_error.Success()) {
188  // Remember that we set this watchpoint successfully in case we need to
189  // clear it later.
190  watchpoint_established_threads.push_back(thread.get());
191  } else {
192  // Unset the watchpoint for each thread we successfully set so that we
193  // get back to a consistent state of "not set" for the watchpoint.
194  for (auto unwatch_thread_sp : watchpoint_established_threads) {
195  Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
196  if (remove_error.Fail())
197  LLDB_LOG(log, "RemoveWatchpoint failed for pid={0}, tid={1}: {2}",
198  GetID(), unwatch_thread_sp->GetID(), remove_error);
199  }
200 
201  return thread_error;
202  }
203  }
204  return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
205 }
206 
208  // Update the thread list
209  UpdateThreads();
210 
211  Status overall_error;
212 
213  std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
214  for (const auto &thread : m_threads) {
215  assert(thread && "thread list should not have a NULL thread!");
216 
217  const Status thread_error = thread->RemoveWatchpoint(addr);
218  if (thread_error.Fail()) {
219  // Keep track of the first thread error if any threads fail. We want to
220  // try to remove the watchpoint from every thread, though, even if one or
221  // more have errors.
222  if (!overall_error.Fail())
223  overall_error = thread_error;
224  }
225  }
226  const Status error = m_watchpoint_list.Remove(addr);
227  return overall_error.Fail() ? overall_error : error;
228 }
229 
230 const HardwareBreakpointMap &
232  return m_hw_breakpoints_map;
233 }
234 
236  size_t size) {
237  // This default implementation assumes setting a hardware breakpoint for this
238  // process will require setting same hardware breakpoint for each of its
239  // existing threads. New thread will do the same once created.
240  Log *log = GetLog(LLDBLog::Process);
241 
242  // Update the thread list
243  UpdateThreads();
244 
245  // Exit here if target does not have required hardware breakpoint capability.
246  auto hw_debug_cap = GetHardwareDebugSupportInfo();
247 
248  if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 ||
249  hw_debug_cap->first <= m_hw_breakpoints_map.size())
250  return Status("Target does not have required no of hardware breakpoints");
251 
252  // Vector below stores all thread pointer for which we have we successfully
253  // set this hardware breakpoint. If any of the current process threads fails
254  // to set this hardware breakpoint then roll back and remove this breakpoint
255  // for all the threads that had already set it successfully.
256  std::vector<NativeThreadProtocol *> breakpoint_established_threads;
257 
258  // Request to set a hardware breakpoint for each of current process threads.
259  std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
260  for (const auto &thread : m_threads) {
261  assert(thread && "thread list should not have a NULL thread!");
262 
263  Status thread_error = thread->SetHardwareBreakpoint(addr, size);
264  if (thread_error.Success()) {
265  // Remember that we set this breakpoint successfully in case we need to
266  // clear it later.
267  breakpoint_established_threads.push_back(thread.get());
268  } else {
269  // Unset the breakpoint for each thread we successfully set so that we
270  // get back to a consistent state of "not set" for this hardware
271  // breakpoint.
272  for (auto rollback_thread_sp : breakpoint_established_threads) {
273  Status remove_error =
274  rollback_thread_sp->RemoveHardwareBreakpoint(addr);
275  if (remove_error.Fail())
276  LLDB_LOG(log,
277  "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}",
278  GetID(), rollback_thread_sp->GetID(), remove_error);
279  }
280 
281  return thread_error;
282  }
283  }
284 
285  // Register new hardware breakpoint into hardware breakpoints map of current
286  // process.
287  m_hw_breakpoints_map[addr] = {addr, size};
288 
289  return Status();
290 }
291 
293  // Update the thread list
294  UpdateThreads();
295 
296  Status error;
297 
298  std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
299  for (const auto &thread : m_threads) {
300  assert(thread && "thread list should not have a NULL thread!");
301  error = thread->RemoveHardwareBreakpoint(addr);
302  }
303 
304  // Also remove from hardware breakpoint map of current process.
305  m_hw_breakpoints_map.erase(addr);
306 
307  return error;
308 }
309 
311  lldb::StateType state) {
312  Log *log = GetLog(LLDBLog::Process);
313 
314  m_delegate.ProcessStateChanged(this, state);
315 
316  switch (state) {
317  case eStateStopped:
318  case eStateExited:
319  case eStateCrashed:
321  break;
322  default:
323  break;
324  }
325 
326  LLDB_LOG(log, "sent state notification [{0}] from process {1}", state,
327  GetID());
328 }
329 
331  Log *log = GetLog(LLDBLog::Process);
332  LLDB_LOG(log, "process {0} exec()ed", GetID());
333 
334  m_software_breakpoints.clear();
335 
336  m_delegate.DidExec(this);
337 }
338 
340  uint32_t size_hint) {
342  LLDB_LOG(log, "addr = {0:x}, size_hint = {1}", addr, size_hint);
343 
344  auto it = m_software_breakpoints.find(addr);
345  if (it != m_software_breakpoints.end()) {
346  ++it->second.ref_count;
347  return Status();
348  }
349  auto expected_bkpt = EnableSoftwareBreakpoint(addr, size_hint);
350  if (!expected_bkpt)
351  return Status(expected_bkpt.takeError());
352 
353  m_software_breakpoints.emplace(addr, std::move(*expected_bkpt));
354  return Status();
355 }
356 
359  LLDB_LOG(log, "addr = {0:x}", addr);
360  auto it = m_software_breakpoints.find(addr);
361  if (it == m_software_breakpoints.end())
362  return Status("Breakpoint not found.");
363  assert(it->second.ref_count > 0);
364  if (--it->second.ref_count > 0)
365  return Status();
366 
367  // This is the last reference. Let's remove the breakpoint.
368  Status error;
369 
370  // Clear a software breakpoint instruction
371  llvm::SmallVector<uint8_t, 4> curr_break_op(
372  it->second.breakpoint_opcodes.size(), 0);
373 
374  // Read the breakpoint opcode
375  size_t bytes_read = 0;
376  error =
377  ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read);
378  if (error.Fail() || bytes_read < curr_break_op.size()) {
379  return Status("addr=0x%" PRIx64
380  ": tried to read %zu bytes but only read %zu",
381  addr, curr_break_op.size(), bytes_read);
382  }
383  const auto &saved = it->second.saved_opcodes;
384  // Make sure the breakpoint opcode exists at this address
385  if (makeArrayRef(curr_break_op) != it->second.breakpoint_opcodes) {
386  if (curr_break_op != it->second.saved_opcodes)
387  return Status("Original breakpoint trap is no longer in memory.");
388  LLDB_LOG(log,
389  "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.",
390  llvm::make_range(saved.begin(), saved.end()), addr);
391  } else {
392  // We found a valid breakpoint opcode at this address, now restore the
393  // saved opcode.
394  size_t bytes_written = 0;
395  error = WriteMemory(addr, saved.data(), saved.size(), bytes_written);
396  if (error.Fail() || bytes_written < saved.size()) {
397  return Status("addr=0x%" PRIx64
398  ": tried to write %zu bytes but only wrote %zu",
399  addr, saved.size(), bytes_written);
400  }
401 
402  // Verify that our original opcode made it back to the inferior
403  llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0);
404  size_t verify_bytes_read = 0;
405  error = ReadMemory(addr, verify_opcode.data(), verify_opcode.size(),
406  verify_bytes_read);
407  if (error.Fail() || verify_bytes_read < verify_opcode.size()) {
408  return Status("addr=0x%" PRIx64
409  ": tried to read %zu verification bytes but only read %zu",
410  addr, verify_opcode.size(), verify_bytes_read);
411  }
412  if (verify_opcode != saved)
413  LLDB_LOG(log, "Restoring bytes at {0:x}: {1:@[x]}", addr,
414  llvm::make_range(saved.begin(), saved.end()));
415  }
416 
417  m_software_breakpoints.erase(it);
418  return Status();
419 }
420 
421 llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint>
423  uint32_t size_hint) {
425 
426  auto expected_trap = GetSoftwareBreakpointTrapOpcode(size_hint);
427  if (!expected_trap)
428  return expected_trap.takeError();
429 
430  llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0);
431  // Save the original opcodes by reading them so we can restore later.
432  size_t bytes_read = 0;
433  Status error = ReadMemory(addr, saved_opcode_bytes.data(),
434  saved_opcode_bytes.size(), bytes_read);
435  if (error.Fail())
436  return error.ToError();
437 
438  // Ensure we read as many bytes as we expected.
439  if (bytes_read != saved_opcode_bytes.size()) {
440  return llvm::createStringError(
441  llvm::inconvertibleErrorCode(),
442  "Failed to read memory while attempting to set breakpoint: attempted "
443  "to read {0} bytes but only read {1}.",
444  saved_opcode_bytes.size(), bytes_read);
445  }
446 
447  LLDB_LOG(
448  log, "Overwriting bytes at {0:x}: {1:@[x]}", addr,
449  llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end()));
450 
451  // Write a software breakpoint in place of the original opcode.
452  size_t bytes_written = 0;
453  error = WriteMemory(addr, expected_trap->data(), expected_trap->size(),
454  bytes_written);
455  if (error.Fail())
456  return error.ToError();
457 
458  // Ensure we wrote as many bytes as we expected.
459  if (bytes_written != expected_trap->size()) {
460  return llvm::createStringError(
461  llvm::inconvertibleErrorCode(),
462  "Failed write memory while attempting to set "
463  "breakpoint: attempted to write {0} bytes but only wrote {1}",
464  expected_trap->size(), bytes_written);
465  }
466 
467  llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(),
468  0);
469  size_t verify_bytes_read = 0;
470  error = ReadMemory(addr, verify_bp_opcode_bytes.data(),
471  verify_bp_opcode_bytes.size(), verify_bytes_read);
472  if (error.Fail())
473  return error.ToError();
474 
475  // Ensure we read as many verification bytes as we expected.
476  if (verify_bytes_read != verify_bp_opcode_bytes.size()) {
477  return llvm::createStringError(
478  llvm::inconvertibleErrorCode(),
479  "Failed to read memory while "
480  "attempting to verify breakpoint: attempted to read {0} bytes "
481  "but only read {1}",
482  verify_bp_opcode_bytes.size(), verify_bytes_read);
483  }
484 
485  if (llvm::makeArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) !=
486  *expected_trap) {
487  return llvm::createStringError(
488  llvm::inconvertibleErrorCode(),
489  "Verification of software breakpoint "
490  "writing failed - trap opcodes not successfully read back "
491  "after writing when setting breakpoint at {0:x}",
492  addr);
493  }
494 
495  LLDB_LOG(log, "addr = {0:x}: SUCCESS", addr);
496  return SoftwareBreakpoint{1, saved_opcode_bytes, *expected_trap};
497 }
498 
499 llvm::Expected<llvm::ArrayRef<uint8_t>>
501  static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
502  static const uint8_t g_i386_opcode[] = {0xCC};
503  static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d};
504  static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00};
505  static const uint8_t g_s390x_opcode[] = {0x00, 0x01};
506  static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08}; // trap
507  static const uint8_t g_ppcle_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
508  static const uint8_t g_riscv_opcode[] = {0x73, 0x00, 0x10, 0x00}; // ebreak
509 
510  switch (GetArchitecture().GetMachine()) {
511  case llvm::Triple::aarch64:
512  case llvm::Triple::aarch64_32:
513  return llvm::makeArrayRef(g_aarch64_opcode);
514 
515  case llvm::Triple::x86:
516  case llvm::Triple::x86_64:
517  return llvm::makeArrayRef(g_i386_opcode);
518 
519  case llvm::Triple::mips:
520  case llvm::Triple::mips64:
521  return llvm::makeArrayRef(g_mips64_opcode);
522 
523  case llvm::Triple::mipsel:
524  case llvm::Triple::mips64el:
525  return llvm::makeArrayRef(g_mips64el_opcode);
526 
527  case llvm::Triple::systemz:
528  return llvm::makeArrayRef(g_s390x_opcode);
529 
530  case llvm::Triple::ppc:
531  case llvm::Triple::ppc64:
532  return llvm::makeArrayRef(g_ppc_opcode);
533 
534  case llvm::Triple::ppc64le:
535  return llvm::makeArrayRef(g_ppcle_opcode);
536 
537  case llvm::Triple::riscv32:
538  case llvm::Triple::riscv64:
539  return llvm::makeArrayRef(g_riscv_opcode);
540 
541  default:
542  return llvm::createStringError(llvm::inconvertibleErrorCode(),
543  "CPU type not supported!");
544  }
545 }
546 
548  switch (GetArchitecture().GetMachine()) {
549  case llvm::Triple::x86:
550  case llvm::Triple::x86_64:
551  case llvm::Triple::systemz:
552  // These architectures report increment the PC after breakpoint is hit.
553  return cantFail(GetSoftwareBreakpointTrapOpcode(0)).size();
554 
555  case llvm::Triple::arm:
556  case llvm::Triple::aarch64:
557  case llvm::Triple::aarch64_32:
558  case llvm::Triple::mips64:
559  case llvm::Triple::mips64el:
560  case llvm::Triple::mips:
561  case llvm::Triple::mipsel:
562  case llvm::Triple::ppc:
563  case llvm::Triple::ppc64:
564  case llvm::Triple::ppc64le:
565  case llvm::Triple::riscv32:
566  case llvm::Triple::riscv64:
567  // On these architectures the PC doesn't get updated for breakpoint hits.
568  return 0;
569 
570  default:
571  llvm_unreachable("CPU type not supported!");
572  }
573 }
574 
576  NativeThreadProtocol &thread) {
578 
579  Status error;
580 
581  // Find out the size of a breakpoint (might depend on where we are in the
582  // code).
583  NativeRegisterContext &context = thread.GetRegisterContext();
584 
585  uint32_t breakpoint_size = GetSoftwareBreakpointPCOffset();
586  LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size);
587  if (breakpoint_size == 0)
588  return;
589 
590  // First try probing for a breakpoint at a software breakpoint location: PC -
591  // breakpoint size.
592  const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation();
593  lldb::addr_t breakpoint_addr = initial_pc_addr;
594  // Do not allow breakpoint probe to wrap around.
595  if (breakpoint_addr >= breakpoint_size)
596  breakpoint_addr -= breakpoint_size;
597 
598  if (m_software_breakpoints.count(breakpoint_addr) == 0) {
599  // We didn't find one at a software probe location. Nothing to do.
600  LLDB_LOG(log,
601  "pid {0} no lldb software breakpoint found at current pc with "
602  "adjustment: {1}",
603  GetID(), breakpoint_addr);
604  return;
605  }
606 
607  //
608  // We have a software breakpoint and need to adjust the PC.
609  //
610 
611  // Change the program counter.
612  LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(),
613  thread.GetID(), initial_pc_addr, breakpoint_addr);
614 
615  error = context.SetPC(breakpoint_addr);
616  if (error.Fail()) {
617  // This can happen in case the process was killed between the time we read
618  // the PC and when we are updating it. There's nothing better to do than to
619  // swallow the error.
620  LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(),
621  thread.GetID(), error);
622  }
623 }
624 
626  bool hardware) {
627  if (hardware)
628  return RemoveHardwareBreakpoint(addr);
629  else
630  return RemoveSoftwareBreakpoint(addr);
631 }
632 
634  void *buf, size_t size,
635  size_t &bytes_read) {
636  Status error = ReadMemory(addr, buf, size, bytes_read);
637  if (error.Fail())
638  return error;
639 
640  auto data =
641  llvm::makeMutableArrayRef(static_cast<uint8_t *>(buf), bytes_read);
642  for (const auto &pair : m_software_breakpoints) {
643  lldb::addr_t bp_addr = pair.first;
644  auto saved_opcodes = makeArrayRef(pair.second.saved_opcodes);
645 
646  if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr)
647  continue; // Breakpoint not in range, ignore
648 
649  if (bp_addr < addr) {
650  saved_opcodes = saved_opcodes.drop_front(addr - bp_addr);
651  bp_addr = addr;
652  }
653  auto bp_data = data.drop_front(bp_addr - addr);
654  std::copy_n(saved_opcodes.begin(),
655  std::min(saved_opcodes.size(), bp_data.size()),
656  bp_data.begin());
657  }
658  return Status();
659 }
660 
661 llvm::Expected<llvm::StringRef>
663  size_t max_size,
664  size_t &total_bytes_read) {
665  static const size_t cache_line_size =
666  llvm::sys::Process::getPageSizeEstimate();
667  size_t bytes_read = 0;
668  size_t bytes_left = max_size;
669  addr_t curr_addr = addr;
670  size_t string_size;
671  char *curr_buffer = buffer;
672  total_bytes_read = 0;
673  Status status;
674 
675  while (bytes_left > 0 && status.Success()) {
676  addr_t cache_line_bytes_left =
677  cache_line_size - (curr_addr % cache_line_size);
678  addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
679  status = ReadMemory(curr_addr, static_cast<void *>(curr_buffer),
680  bytes_to_read, bytes_read);
681 
682  if (bytes_read == 0)
683  break;
684 
685  void *str_end = std::memchr(curr_buffer, '\0', bytes_read);
686  if (str_end != nullptr) {
687  total_bytes_read =
688  static_cast<size_t>((static_cast<char *>(str_end) - buffer + 1));
689  status.Clear();
690  break;
691  }
692 
693  total_bytes_read += bytes_read;
694  curr_buffer += bytes_read;
695  curr_addr += bytes_read;
696  bytes_left -= bytes_read;
697  }
698 
699  string_size = total_bytes_read - 1;
700 
701  // Make sure we return a null terminated string.
702  if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] != '\0') {
703  buffer[max_size - 1] = '\0';
704  total_bytes_read--;
705  }
706 
707  if (!status.Success())
708  return status.ToError();
709 
710  return llvm::StringRef(buffer, string_size);
711 }
712 
714  std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
715  return m_state;
716 }
717 
719  bool notify_delegates) {
720  std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
721 
722  if (state == m_state)
723  return;
724 
725  m_state = state;
726 
727  if (StateIsStoppedState(state, false)) {
728  ++m_stop_id;
729 
730  // Give process a chance to do any stop id bump processing, such as
731  // clearing cached data that is invalidated each time the process runs.
732  // Note if/when we support some threads running, we'll end up needing to
733  // manage this per thread and per process.
735  }
736 
737  // Optionally notify delegates of the state change.
738  if (notify_delegates)
740 }
741 
743  std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
744  return m_stop_id;
745 }
746 
748  // Default implementation does nothing.
749 }
750 
lldb_private::NativeProcessProtocol::SetSoftwareBreakpoint
Status SetSoftwareBreakpoint(lldb::addr_t addr, uint32_t size_hint)
Definition: NativeProcessProtocol.cpp:339
lldb_private::NativeProcessProtocol::NativeDelegate
Definition: NativeProcessProtocol.h:236
lldb_private::NativeProcessProtocol::ReadMemoryWithoutTrap
Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read)
Definition: NativeProcessProtocol.cpp:633
lldb_private::NativeProcessProtocol::SetHardwareBreakpoint
virtual Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size)
Definition: NativeProcessProtocol.cpp:235
lldb_private::NativeProcessProtocol::GetThreadAtIndex
NativeThreadProtocol * GetThreadAtIndex(uint32_t idx)
Definition: NativeProcessProtocol.cpp:99
lldb_private::NativeThreadProtocol
Definition: NativeThreadProtocol.h:24
lldb_private::NativeProcessProtocol::ReadCStringFromMemory
llvm::Expected< llvm::StringRef > ReadCStringFromMemory(lldb::addr_t addr, char *buffer, size_t max_size, size_t &total_bytes_read)
Reads a null terminated string from memory.
Definition: NativeProcessProtocol.cpp:662
Host.h
lldb_private::NativeProcessProtocol::SetState
void SetState(lldb::StateType state, bool notify_delegates=true)
Definition: NativeProcessProtocol.cpp:718
lldb_private::NativeProcessProtocol::IgnoreSignals
virtual Status IgnoreSignals(llvm::ArrayRef< int > signals)
Definition: NativeProcessProtocol.cpp:43
lldb_private::NativeProcessProtocol::SetWatchpoint
virtual Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
Definition: NativeProcessProtocol.cpp:147
lldb_private::NativeProcessProtocol::m_watchpoint_list
NativeWatchpointList m_watchpoint_list
Definition: NativeProcessProtocol.h:428
lldb_private::NativeProcessProtocol::GetThreadByID
NativeThreadProtocol * GetThreadByID(lldb::tid_t tid)
Definition: NativeProcessProtocol.cpp:115
lldb_private::NativeProcessProtocol::EnableSoftwareBreakpoint
llvm::Expected< SoftwareBreakpoint > EnableSoftwareBreakpoint(lldb::addr_t addr, uint32_t size_hint)
Definition: NativeProcessProtocol.cpp:422
lldb_private::NativeProcessProtocol::m_software_breakpoints
std::unordered_map< lldb::addr_t, SoftwareBreakpoint > m_software_breakpoints
Definition: NativeProcessProtocol.h:415
lldb_private::NativeProcessProtocol::SetExitStatus
virtual bool SetExitStatus(WaitStatus status, bool bNotifyStateChange)
Definition: NativeProcessProtocol.cpp:76
lldb_private::NativeProcessProtocol::GetHardwareDebugSupportInfo
virtual llvm::Optional< std::pair< uint32_t, uint32_t > > GetHardwareDebugSupportInfo() const
Definition: NativeProcessProtocol.cpp:131
lldb_private::NativeProcessProtocol::GetMemoryRegionInfo
virtual Status GetMemoryRegionInfo(lldb::addr_t load_addr, MemoryRegionInfo &range_info)
Definition: NativeProcessProtocol.cpp:50
lldb_private::MemoryRegionInfo
Definition: MemoryRegionInfo.h:21
lldb_private::NativeProcessProtocol::Signal
virtual Status Signal(int signo)=0
Sends a process a UNIX signal signal.
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
SIGSTOP
#define SIGSTOP
Definition: windows/PosixApi.h:32
lldb_private::NativeProcessProtocol::WriteMemoryTags
virtual Status WriteMemoryTags(int32_t type, lldb::addr_t addr, size_t len, const std::vector< uint8_t > &tags)
Definition: NativeProcessProtocol.cpp:63
lldb_private::NativeProcessProtocol::RemoveWatchpoint
virtual Status RemoveWatchpoint(lldb::addr_t addr)
Definition: NativeProcessProtocol.cpp:207
lldb::eStateExited
@ eStateExited
Process has exited and can't be examined.
Definition: lldb-enumerations.h:90
lldb_private::NativeProcessProtocol::m_delegate
NativeDelegate & m_delegate
Definition: NativeProcessProtocol.h:427
lldb_private::NativeRegisterContext::SetPC
Status SetPC(lldb::addr_t pc)
Definition: NativeRegisterContext.cpp:146
lldb_private::WaitStatus
Definition: Host.h:35
lldb::eStateStopped
@ eStateStopped
Process or thread is stopped and can be examined.
Definition: lldb-enumerations.h:84
lldb_private::NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode
virtual llvm::Expected< llvm::ArrayRef< uint8_t > > GetSoftwareBreakpointTrapOpcode(size_t size_hint)
Definition: NativeProcessProtocol.cpp:500
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::NativeWatchpointList::WatchpointMap
std::map< lldb::addr_t, NativeWatchpoint > WatchpointMap
Definition: NativeWatchpointList.h:32
lldb_private::Status::Success
bool Success() const
Test for success condition.
Definition: Status.cpp:287
lldb_private::NativeProcessProtocol::NotifyDidExec
virtual void NotifyDidExec()
Notify the delegate that an exec occurred.
Definition: NativeProcessProtocol.cpp:330
lldb::eStateCrashed
@ eStateCrashed
Process or thread has crashed and can be examined.
Definition: lldb-enumerations.h:88
LLDBAssert.h
lldb_private::StateIsStoppedState
bool StateIsStoppedState(lldb::StateType state, bool must_exist)
Check if a state represents a state where the process or thread is stopped.
Definition: State.cpp:89
lldb_private::NativeProcessProtocol::RemoveSoftwareBreakpoint
Status RemoveSoftwareBreakpoint(lldb::addr_t addr)
Definition: NativeProcessProtocol.cpp:357
lldb_private::NativeProcessProtocol::ReadMemoryTags
virtual Status ReadMemoryTags(int32_t type, lldb::addr_t addr, size_t len, std::vector< uint8_t > &tags)
Definition: NativeProcessProtocol.cpp:57
Log.h
lldb_private::NativeProcessProtocol::WriteMemory
virtual Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written)=0
lldb_private::NativeThreadProtocol::GetRegisterContext
virtual NativeRegisterContext & GetRegisterContext()=0
lldb_private::NativeProcessProtocol::m_hw_breakpoints_map
HardwareBreakpointMap m_hw_breakpoints_map
Definition: NativeProcessProtocol.h:429
lldb_private::Status::Fail
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
lldb_private::LLDBLog::Process
@ Process
lldb_private::NativeProcessProtocol::m_exit_status
llvm::Optional< WaitStatus > m_exit_status
Definition: NativeProcessProtocol.h:425
lldb_private::NativeProcessProtocol::GetID
lldb::pid_t GetID() const
Definition: NativeProcessProtocol.h:183
lldb_private::NativeProcessProtocol::GetThreadByIDUnlocked
NativeThreadProtocol * GetThreadByIDUnlocked(lldb::tid_t tid)
Definition: NativeProcessProtocol.cpp:107
lldb_private::NativeProcessProtocol::m_signals_to_ignore
llvm::DenseSet< int > m_signals_to_ignore
Definition: NativeProcessProtocol.h:435
lldb-enumerations.h
lldb_private::NativeProcessProtocol::DoStopIDBumped
virtual void DoStopIDBumped(uint32_t newBumpId)
Definition: NativeProcessProtocol.cpp:747
lldb_private::NativeProcessProtocol::m_state
lldb::StateType m_state
Definition: NativeProcessProtocol.h:422
lldb_private::NativeRegisterContext::NumSupportedHardwareBreakpoints
virtual uint32_t NumSupportedHardwareBreakpoints()
Definition: NativeRegisterContext.cpp:247
lldb::eStateDetached
@ eStateDetached
Process has been detached and can't be examined.
Definition: lldb-enumerations.h:89
lldb_private::LLDBLog::Breakpoints
@ Breakpoints
lldb_private::NativeWatchpointList::GetWatchpointMap
const WatchpointMap & GetWatchpointMap() const
Definition: NativeWatchpointList.cpp:28
lldb::eStateUnloaded
@ eStateUnloaded
Process is object is valid, but not currently loaded.
Definition: lldb-enumerations.h:75
lldb_private::NativeProcessProtocol::m_threads
std::vector< std::unique_ptr< NativeThreadProtocol > > m_threads
Definition: NativeProcessProtocol.h:418
lldb_private::NativeProcessProtocol::RemoveBreakpoint
virtual Status RemoveBreakpoint(lldb::addr_t addr, bool hardware=false)
Definition: NativeProcessProtocol.cpp:625
NativeProcessProtocol.h
lldb_private::NativeProcessProtocol::Factory::~Factory
virtual ~Factory()
lldb_private::NativeProcessProtocol::GetSoftwareBreakpointPCOffset
virtual size_t GetSoftwareBreakpointPCOffset()
Return the offset of the PC relative to the software breakpoint that was hit.
Definition: NativeProcessProtocol.cpp:547
lldb_private::NativeProcessProtocol::FixupBreakpointPCAsNeeded
void FixupBreakpointPCAsNeeded(NativeThreadProtocol &thread)
Definition: NativeProcessProtocol.cpp:575
lldb_private::NativeProcessProtocol::GetStopID
uint32_t GetStopID() const
Definition: NativeProcessProtocol.cpp:742
lldb_private::NativeProcessProtocol::SoftwareBreakpoint
Definition: NativeProcessProtocol.h:409
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::HardwareBreakpointMap
std::map< lldb::addr_t, HardwareBreakpoint > HardwareBreakpointMap
Definition: NativeBreakpointList.h:23
NativeThreadProtocol.h
lldb_private::NativeProcessProtocol::RemoveHardwareBreakpoint
virtual Status RemoveHardwareBreakpoint(lldb::addr_t addr)
Definition: NativeProcessProtocol.cpp:292
lldb::pid_t
uint64_t pid_t
Definition: lldb-types.h:85
lldb_private::NativeProcessProtocol
Definition: NativeProcessProtocol.h:48
lldb_private::NativeThreadProtocol::GetID
lldb::tid_t GetID() const
Definition: NativeThreadProtocol.h:39
lldb_private::NativeProcessProtocol::NativeDelegate::ProcessStateChanged
virtual void ProcessStateChanged(NativeProcessProtocol *process, lldb::StateType state)=0
lldb_private::NativeProcessProtocol::GetState
lldb::StateType GetState() const
Definition: NativeProcessProtocol.cpp:713
lldb_private::NativeProcessProtocol::IsAlive
virtual bool IsAlive() const
Definition: NativeProcessProtocol.cpp:120
lldb_private::NativeProcessProtocol::GetHardwareBreakpointMap
virtual const HardwareBreakpointMap & GetHardwareBreakpointMap() const
Definition: NativeProcessProtocol.cpp:231
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:337
lldb_private::NativeProcessProtocol::m_stop_id
uint32_t m_stop_id
Definition: NativeProcessProtocol.h:431
lldb_private::NativeProcessProtocol::GetExitStatus
virtual llvm::Optional< WaitStatus > GetExitStatus()
Definition: NativeProcessProtocol.cpp:69
lldb_private::NativeProcessProtocol::m_state_mutex
std::recursive_mutex m_state_mutex
Definition: NativeProcessProtocol.h:423
lldb_private::NativeRegisterContext::NumSupportedHardwareWatchpoints
virtual uint32_t NumSupportedHardwareWatchpoints()
Definition: NativeRegisterContext.cpp:268
lldb_private::NativeRegisterContext::GetPCfromBreakpointLocation
virtual lldb::addr_t GetPCfromBreakpointLocation(lldb::addr_t fail_value=LLDB_INVALID_ADDRESS)
Definition: NativeRegisterContext.cpp:142
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged
void SynchronouslyNotifyProcessStateChanged(lldb::StateType state)
Definition: NativeProcessProtocol.cpp:310
lldb_private::NativeProcessProtocol::GetWatchpointMap
virtual const NativeWatchpointList::WatchpointMap & GetWatchpointMap() const
Definition: NativeProcessProtocol.cpp:126
lldb::eStateInvalid
@ eStateInvalid
Definition: lldb-enumerations.h:74
lldb_private::Status::Clear
void Clear()
Clear the object state.
Definition: Status.cpp:167
lldb_private::Status::ToError
llvm::Error ToError() const
Definition: Status.cpp:89
lldb_private::NativeProcessProtocol::Interrupt
virtual Status Interrupt()
Tells a process to interrupt all operations as if by a Ctrl-C.
Definition: NativeProcessProtocol.cpp:33
State.h
lldb_private::NativeProcessProtocol::NativeDelegate::InitializeDelegate
virtual void InitializeDelegate(NativeProcessProtocol *process)=0
lldb_private::NativeProcessProtocol::UpdateThreads
virtual size_t UpdateThreads()=0
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:73
lldb_private::Log
Definition: Log.h:115
NativeBreakpointList.h
lldb_private::NativeProcessProtocol::m_threads_mutex
std::recursive_mutex m_threads_mutex
Definition: NativeProcessProtocol.h:420
lldb_private::NativeWatchpointList::Remove
Status Remove(lldb::addr_t addr)
Definition: NativeWatchpointList.cpp:22
lldb_private::NativeRegisterContext
Definition: NativeRegisterContext.h:21
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
lldb_private::NativeWatchpointList::Add
Status Add(lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
Definition: NativeWatchpointList.cpp:16
lldb
Definition: SBAddress.h:15
NativeRegisterContext.h
LLDBLog.h
lldb_private::NativeProcessProtocol::ReadMemory
virtual Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read)=0
lldb_private::NativeProcessProtocol::NotifyTracersProcessDidStop
virtual void NotifyTracersProcessDidStop()
Notify tracers that the target process just stopped.
Definition: NativeProcessProtocol.h:332
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86
lldb_private::NativeProcessProtocol::NativeDelegate::DidExec
virtual void DidExec(NativeProcessProtocol *process)=0
lldb_private::NativeProcessProtocol::GetArchitecture
virtual const ArchSpec & GetArchitecture() const =0