LLDB  mainline
GDBRemoteRegisterContext.cpp
Go to the documentation of this file.
1 //===-- GDBRemoteRegisterContext.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 
12 #include "lldb/Target/Target.h"
16 #include "lldb/Utility/Scalar.h"
18 #include "ProcessGDBRemote.h"
19 #include "ProcessGDBRemoteLog.h"
20 #include "ThreadGDBRemote.h"
24 
25 #include <memory>
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 using namespace lldb_private::process_gdb_remote;
30 
31 // GDBRemoteRegisterContext constructor
32 GDBRemoteRegisterContext::GDBRemoteRegisterContext(
33  ThreadGDBRemote &thread, uint32_t concrete_frame_idx,
34  GDBRemoteDynamicRegisterInfoSP reg_info_sp, bool read_all_at_once,
35  bool write_all_at_once)
36  : RegisterContext(thread, concrete_frame_idx),
37  m_reg_info_sp(std::move(reg_info_sp)), m_reg_valid(), m_reg_data(),
38  m_read_all_at_once(read_all_at_once),
39  m_write_all_at_once(write_all_at_once), m_gpacket_cached(false) {
40  // Resize our vector of bools to contain one bool for every register. We will
41  // use these boolean values to know when a register value is valid in
42  // m_reg_data.
43  m_reg_valid.resize(m_reg_info_sp->GetNumRegisters());
44 
45  // Make a heap based buffer that is big enough to store all registers
46  DataBufferSP reg_data_sp(
47  new DataBufferHeap(m_reg_info_sp->GetRegisterDataByteSize(), 0));
48  m_reg_data.SetData(reg_data_sp);
49  m_reg_data.SetByteOrder(thread.GetProcess()->GetByteOrder());
50 }
51 
52 // Destructor
54 
56  SetAllRegisterValid(false);
57 }
58 
60  m_gpacket_cached = b;
61  std::vector<bool>::iterator pos, end = m_reg_valid.end();
62  for (pos = m_reg_valid.begin(); pos != end; ++pos)
63  *pos = b;
64 }
65 
67  return m_reg_info_sp->GetNumRegisters();
68 }
69 
70 const RegisterInfo *
72  return m_reg_info_sp->GetRegisterInfoAtIndex(reg);
73 }
74 
76  return m_reg_info_sp->GetNumRegisterSets();
77 }
78 
79 const RegisterSet *GDBRemoteRegisterContext::GetRegisterSet(size_t reg_set) {
80  return m_reg_info_sp->GetRegisterSet(reg_set);
81 }
82 
83 bool GDBRemoteRegisterContext::ReadRegister(const RegisterInfo *reg_info,
84  RegisterValue &value) {
85  // Read the register
86  if (ReadRegisterBytes(reg_info)) {
87  const uint32_t reg = reg_info->kinds[eRegisterKindLLDB];
88  if (m_reg_valid[reg] == false)
89  return false;
90  if (reg_info->value_regs &&
91  reg_info->value_regs[0] != LLDB_INVALID_REGNUM &&
92  reg_info->value_regs[1] != LLDB_INVALID_REGNUM) {
93  std::vector<char> combined_data;
94  uint32_t offset = 0;
95  for (int i = 0; reg_info->value_regs[i] != LLDB_INVALID_REGNUM; i++) {
96  const RegisterInfo *parent_reg = GetRegisterInfo(
97  eRegisterKindLLDB, reg_info->value_regs[i]);
98  if (!parent_reg)
99  return false;
100  combined_data.resize(offset + parent_reg->byte_size);
101  if (m_reg_data.CopyData(parent_reg->byte_offset, parent_reg->byte_size,
102  combined_data.data() + offset) !=
103  parent_reg->byte_size)
104  return false;
105  offset += parent_reg->byte_size;
106  }
107 
108  Status error;
109  return value.SetFromMemoryData(
110  reg_info, combined_data.data(), combined_data.size(),
111  m_reg_data.GetByteOrder(), error) == combined_data.size();
112  } else {
113  const bool partial_data_ok = false;
115  reg_info, m_reg_data, reg_info->byte_offset, partial_data_ok));
116  return error.Success();
117  }
118  }
119  return false;
120 }
121 
123  uint32_t reg, llvm::ArrayRef<uint8_t> data) {
124  const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
125  if (reg_info == nullptr)
126  return false;
127 
128  // Invalidate if needed
129  InvalidateIfNeeded(false);
130 
131  const size_t reg_byte_size = reg_info->byte_size;
132  memcpy(const_cast<uint8_t *>(
133  m_reg_data.PeekData(reg_info->byte_offset, reg_byte_size)),
134  data.data(), std::min(data.size(), reg_byte_size));
135  bool success = data.size() >= reg_byte_size;
136  if (success) {
137  SetRegisterIsValid(reg, true);
138  } else if (data.size() > 0) {
139  // Only set register is valid to false if we copied some bytes, else leave
140  // it as it was.
141  SetRegisterIsValid(reg, false);
142  }
143  return success;
144 }
145 
147  uint64_t new_reg_val) {
148  const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
149  if (reg_info == nullptr)
150  return false;
151 
152  // Early in process startup, we can get a thread that has an invalid byte
153  // order because the process hasn't been completely set up yet (see the ctor
154  // where the byte order is setfrom the process). If that's the case, we
155  // can't set the value here.
157  return false;
158  }
159 
160  // Invalidate if needed
161  InvalidateIfNeeded(false);
162 
163  DataBufferSP buffer_sp(new DataBufferHeap(&new_reg_val, sizeof(new_reg_val)));
164  DataExtractor data(buffer_sp, endian::InlHostByteOrder(), sizeof(void *));
165 
166  // If our register context and our register info disagree, which should never
167  // happen, don't overwrite past the end of the buffer.
168  if (m_reg_data.GetByteSize() < reg_info->byte_offset + reg_info->byte_size)
169  return false;
170 
171  // Grab a pointer to where we are going to put this register
172  uint8_t *dst = const_cast<uint8_t *>(
173  m_reg_data.PeekData(reg_info->byte_offset, reg_info->byte_size));
174 
175  if (dst == nullptr)
176  return false;
177 
178  if (data.CopyByteOrderedData(0, // src offset
179  reg_info->byte_size, // src length
180  dst, // dst
181  reg_info->byte_size, // dst length
182  m_reg_data.GetByteOrder())) // dst byte order
183  {
184  SetRegisterIsValid(reg, true);
185  return true;
186  }
187  return false;
188 }
189 
190 // Helper function for GDBRemoteRegisterContext::ReadRegisterBytes().
192  const RegisterInfo *reg_info, GDBRemoteCommunicationClient &gdb_comm) {
193  const uint32_t lldb_reg = reg_info->kinds[eRegisterKindLLDB];
194  const uint32_t remote_reg = reg_info->kinds[eRegisterKindProcessPlugin];
195 
196  if (DataBufferSP buffer_sp =
197  gdb_comm.ReadRegister(m_thread.GetProtocolID(), remote_reg))
199  lldb_reg, llvm::ArrayRef<uint8_t>(buffer_sp->GetBytes(),
200  buffer_sp->GetByteSize()));
201  return false;
202 }
203 
204 bool GDBRemoteRegisterContext::ReadRegisterBytes(const RegisterInfo *reg_info) {
206 
207  Process *process = exe_ctx.GetProcessPtr();
208  Thread *thread = exe_ctx.GetThreadPtr();
209  if (process == nullptr || thread == nullptr)
210  return false;
211 
213  ((ProcessGDBRemote *)process)->GetGDBRemote());
214 
215  InvalidateIfNeeded(false);
216 
217  const uint32_t reg = reg_info->kinds[eRegisterKindLLDB];
218 
219  if (!GetRegisterIsValid(reg)) {
221  if (DataBufferSP buffer_sp =
223  memcpy(const_cast<uint8_t *>(m_reg_data.GetDataStart()),
224  buffer_sp->GetBytes(),
225  std::min(buffer_sp->GetByteSize(), m_reg_data.GetByteSize()));
226  if (buffer_sp->GetByteSize() >= m_reg_data.GetByteSize()) {
227  SetAllRegisterValid(true);
228  return true;
229  } else if (buffer_sp->GetByteSize() > 0) {
230  for (auto x : llvm::enumerate(m_reg_info_sp->registers())) {
231  const struct RegisterInfo &reginfo = x.value();
232  m_reg_valid[x.index()] =
233  (reginfo.byte_offset + reginfo.byte_size <=
234  buffer_sp->GetByteSize());
235  }
236 
237  m_gpacket_cached = true;
238  if (GetRegisterIsValid(reg))
239  return true;
240  } else {
243  LLDB_LOGF(
244  log,
245  "error: GDBRemoteRegisterContext::ReadRegisterBytes tried "
246  "to read the "
247  "entire register context at once, expected at least %" PRId64
248  " bytes "
249  "but only got %" PRId64 " bytes.",
250  m_reg_data.GetByteSize(), buffer_sp->GetByteSize());
251  return false;
252  }
253  }
254  }
255  if (reg_info->value_regs) {
256  // Process this composite register request by delegating to the
257  // constituent primordial registers.
258 
259  // Index of the primordial register.
260  bool success = true;
261  for (uint32_t idx = 0; success; ++idx) {
262  const uint32_t prim_reg = reg_info->value_regs[idx];
263  if (prim_reg == LLDB_INVALID_REGNUM)
264  break;
265  // We have a valid primordial register as our constituent. Grab the
266  // corresponding register info.
267  const RegisterInfo *prim_reg_info =
269  if (prim_reg_info == nullptr)
270  success = false;
271  else {
272  // Read the containing register if it hasn't already been read
273  if (!GetRegisterIsValid(prim_reg))
274  success = GetPrimordialRegister(prim_reg_info, gdb_comm);
275  }
276  }
277 
278  if (success) {
279  // If we reach this point, all primordial register requests have
280  // succeeded. Validate this composite register.
281  SetRegisterIsValid(reg_info, true);
282  }
283  } else {
284  // Get each register individually
285  GetPrimordialRegister(reg_info, gdb_comm);
286  }
287 
288  // Make sure we got a valid register value after reading it
289  if (!GetRegisterIsValid(reg))
290  return false;
291  }
292 
293  return true;
294 }
295 
296 bool GDBRemoteRegisterContext::WriteRegister(const RegisterInfo *reg_info,
297  const RegisterValue &value) {
298  DataExtractor data;
299  if (value.GetData(data)) {
300  if (reg_info->value_regs &&
301  reg_info->value_regs[0] != LLDB_INVALID_REGNUM &&
302  reg_info->value_regs[1] != LLDB_INVALID_REGNUM) {
303  uint32_t combined_size = 0;
304  for (int i = 0; reg_info->value_regs[i] != LLDB_INVALID_REGNUM; i++) {
305  const RegisterInfo *parent_reg = GetRegisterInfo(
306  eRegisterKindLLDB, reg_info->value_regs[i]);
307  if (!parent_reg)
308  return false;
309  combined_size += parent_reg->byte_size;
310  }
311 
312  if (data.GetByteSize() < combined_size)
313  return false;
314 
315  uint32_t offset = 0;
316  for (int i = 0; reg_info->value_regs[i] != LLDB_INVALID_REGNUM; i++) {
317  const RegisterInfo *parent_reg = GetRegisterInfo(
318  eRegisterKindLLDB, reg_info->value_regs[i]);
319  assert(parent_reg);
320 
321  DataExtractor parent_data{data, offset, parent_reg->byte_size};
322  if (!WriteRegisterBytes(parent_reg, parent_data, 0))
323  return false;
324  offset += parent_reg->byte_size;
325  }
326  assert(offset == combined_size);
327  return true;
328  } else
329  return WriteRegisterBytes(reg_info, data, 0);
330  }
331  return false;
332 }
333 
334 // Helper function for GDBRemoteRegisterContext::WriteRegisterBytes().
336  const RegisterInfo *reg_info, GDBRemoteCommunicationClient &gdb_comm) {
337  StreamString packet;
338  StringExtractorGDBRemote response;
339  const uint32_t reg = reg_info->kinds[eRegisterKindLLDB];
340  // Invalidate just this register
341  SetRegisterIsValid(reg, false);
342 
343  return gdb_comm.WriteRegister(
345  {m_reg_data.PeekData(reg_info->byte_offset, reg_info->byte_size),
346  reg_info->byte_size});
347 }
348 
349 bool GDBRemoteRegisterContext::WriteRegisterBytes(const RegisterInfo *reg_info,
350  DataExtractor &data,
351  uint32_t data_offset) {
353 
354  Process *process = exe_ctx.GetProcessPtr();
355  Thread *thread = exe_ctx.GetThreadPtr();
356  if (process == nullptr || thread == nullptr)
357  return false;
358 
360  ((ProcessGDBRemote *)process)->GetGDBRemote());
361 
362  assert(m_reg_data.GetByteSize() >=
363  reg_info->byte_offset + reg_info->byte_size);
364 
365  // If our register context and our register info disagree, which should never
366  // happen, don't overwrite past the end of the buffer.
367  if (m_reg_data.GetByteSize() < reg_info->byte_offset + reg_info->byte_size)
368  return false;
369 
370  // Grab a pointer to where we are going to put this register
371  uint8_t *dst = const_cast<uint8_t *>(
372  m_reg_data.PeekData(reg_info->byte_offset, reg_info->byte_size));
373 
374  if (dst == nullptr)
375  return false;
376 
377  // Code below is specific to AArch64 target in SVE state
378  // If vector granule (vg) register is being written then thread's
379  // register context reconfiguration is triggered on success.
380  bool do_reconfigure_arm64_sve = false;
381  const ArchSpec &arch = process->GetTarget().GetArchitecture();
382  if (arch.IsValid() && arch.GetTriple().isAArch64())
383  if (strcmp(reg_info->name, "vg") == 0)
384  do_reconfigure_arm64_sve = true;
385 
386  if (data.CopyByteOrderedData(data_offset, // src offset
387  reg_info->byte_size, // src length
388  dst, // dst
389  reg_info->byte_size, // dst length
390  m_reg_data.GetByteOrder())) // dst byte order
391  {
392  GDBRemoteClientBase::Lock lock(gdb_comm);
393  if (lock) {
394  if (m_write_all_at_once) {
395  // Invalidate all register values
396  InvalidateIfNeeded(true);
397 
398  // Set all registers in one packet
399  if (gdb_comm.WriteAllRegisters(
401  {m_reg_data.GetDataStart(), size_t(m_reg_data.GetByteSize())}))
402 
403  {
404  SetAllRegisterValid(false);
405 
406  if (do_reconfigure_arm64_sve)
408 
409  return true;
410  }
411  } else {
412  bool success = true;
413 
414  if (reg_info->value_regs) {
415  // This register is part of another register. In this case we read
416  // the actual register data for any "value_regs", and once all that
417  // data is read, we will have enough data in our register context
418  // bytes for the value of this register
419 
420  // Invalidate this composite register first.
421 
422  for (uint32_t idx = 0; success; ++idx) {
423  const uint32_t reg = reg_info->value_regs[idx];
424  if (reg == LLDB_INVALID_REGNUM)
425  break;
426  // We have a valid primordial register as our constituent. Grab the
427  // corresponding register info.
428  const RegisterInfo *value_reg_info =
430  if (value_reg_info == nullptr)
431  success = false;
432  else
433  success = SetPrimordialRegister(value_reg_info, gdb_comm);
434  }
435  } else {
436  // This is an actual register, write it
437  success = SetPrimordialRegister(reg_info, gdb_comm);
438 
439  if (success && do_reconfigure_arm64_sve)
441  }
442 
443  // Check if writing this register will invalidate any other register
444  // values? If so, invalidate them
445  if (reg_info->invalidate_regs) {
446  for (uint32_t idx = 0, reg = reg_info->invalidate_regs[0];
447  reg != LLDB_INVALID_REGNUM;
448  reg = reg_info->invalidate_regs[++idx])
450  eRegisterKindLLDB, reg),
451  false);
452  }
453 
454  return success;
455  }
456  } else {
459  if (log) {
460  if (log->GetVerbose()) {
461  StreamString strm;
462  gdb_comm.DumpHistory(strm);
463  LLDB_LOGF(log,
464  "error: failed to get packet sequence mutex, not sending "
465  "write register for \"%s\":\n%s",
466  reg_info->name, strm.GetData());
467  } else
468  LLDB_LOGF(log,
469  "error: failed to get packet sequence mutex, not sending "
470  "write register for \"%s\"",
471  reg_info->name);
472  }
473  }
474  }
475  return false;
476 }
477 
479  RegisterCheckpoint &reg_checkpoint) {
481 
482  Process *process = exe_ctx.GetProcessPtr();
483  Thread *thread = exe_ctx.GetThreadPtr();
484  if (process == nullptr || thread == nullptr)
485  return false;
486 
488  ((ProcessGDBRemote *)process)->GetGDBRemote());
489 
490  uint32_t save_id = 0;
491  if (gdb_comm.SaveRegisterState(thread->GetProtocolID(), save_id)) {
492  reg_checkpoint.SetID(save_id);
493  reg_checkpoint.GetData().reset();
494  return true;
495  } else {
496  reg_checkpoint.SetID(0); // Invalid save ID is zero
497  return ReadAllRegisterValues(reg_checkpoint.GetData());
498  }
499 }
500 
502  const RegisterCheckpoint &reg_checkpoint) {
503  uint32_t save_id = reg_checkpoint.GetID();
504  if (save_id != 0) {
506 
507  Process *process = exe_ctx.GetProcessPtr();
508  Thread *thread = exe_ctx.GetThreadPtr();
509  if (process == nullptr || thread == nullptr)
510  return false;
511 
513  ((ProcessGDBRemote *)process)->GetGDBRemote());
514 
515  return gdb_comm.RestoreRegisterState(m_thread.GetProtocolID(), save_id);
516  } else {
517  return WriteAllRegisterValues(reg_checkpoint.GetData());
518  }
519 }
520 
522  lldb::DataBufferSP &data_sp) {
524 
525  Process *process = exe_ctx.GetProcessPtr();
526  Thread *thread = exe_ctx.GetThreadPtr();
527  if (process == nullptr || thread == nullptr)
528  return false;
529 
531  ((ProcessGDBRemote *)process)->GetGDBRemote());
532 
533  const bool use_g_packet =
534  !gdb_comm.AvoidGPackets((ProcessGDBRemote *)process);
535 
536  GDBRemoteClientBase::Lock lock(gdb_comm);
537  if (lock) {
538  if (gdb_comm.SyncThreadState(m_thread.GetProtocolID()))
540 
541  if (use_g_packet &&
542  (data_sp = gdb_comm.ReadAllRegisters(m_thread.GetProtocolID())))
543  return true;
544 
545  // We're going to read each register
546  // individually and store them as binary data in a buffer.
547  const RegisterInfo *reg_info;
548 
549  for (uint32_t i = 0; (reg_info = GetRegisterInfoAtIndex(i)) != nullptr;
550  i++) {
551  if (reg_info
552  ->value_regs) // skip registers that are slices of real registers
553  continue;
554  ReadRegisterBytes(reg_info);
555  // ReadRegisterBytes saves the contents of the register in to the
556  // m_reg_data buffer
557  }
558  data_sp = std::make_shared<DataBufferHeap>(
559  m_reg_data.GetDataStart(), m_reg_info_sp->GetRegisterDataByteSize());
560  return true;
561  } else {
562 
565  if (log) {
566  if (log->GetVerbose()) {
567  StreamString strm;
568  gdb_comm.DumpHistory(strm);
569  LLDB_LOGF(log,
570  "error: failed to get packet sequence mutex, not sending "
571  "read all registers:\n%s",
572  strm.GetData());
573  } else
574  LLDB_LOGF(log,
575  "error: failed to get packet sequence mutex, not sending "
576  "read all registers");
577  }
578  }
579 
580  data_sp.reset();
581  return false;
582 }
583 
585  const lldb::DataBufferSP &data_sp) {
586  if (!data_sp || data_sp->GetBytes() == nullptr || data_sp->GetByteSize() == 0)
587  return false;
588 
590 
591  Process *process = exe_ctx.GetProcessPtr();
592  Thread *thread = exe_ctx.GetThreadPtr();
593  if (process == nullptr || thread == nullptr)
594  return false;
595 
597  ((ProcessGDBRemote *)process)->GetGDBRemote());
598 
599  const bool use_g_packet =
600  !gdb_comm.AvoidGPackets((ProcessGDBRemote *)process);
601 
602  GDBRemoteClientBase::Lock lock(gdb_comm);
603  if (lock) {
604  // The data_sp contains the G response packet.
605  if (use_g_packet) {
606  if (gdb_comm.WriteAllRegisters(
608  {data_sp->GetBytes(), size_t(data_sp->GetByteSize())}))
609  return true;
610 
611  uint32_t num_restored = 0;
612  // We need to manually go through all of the registers and restore them
613  // manually
614  DataExtractor restore_data(data_sp, m_reg_data.GetByteOrder(),
616 
617  const RegisterInfo *reg_info;
618 
619  // The g packet contents may either include the slice registers
620  // (registers defined in terms of other registers, e.g. eax is a subset
621  // of rax) or not. The slice registers should NOT be in the g packet,
622  // but some implementations may incorrectly include them.
623  //
624  // If the slice registers are included in the packet, we must step over
625  // the slice registers when parsing the packet -- relying on the
626  // RegisterInfo byte_offset field would be incorrect. If the slice
627  // registers are not included, then using the byte_offset values into the
628  // data buffer is the best way to find individual register values.
629 
630  uint64_t size_including_slice_registers = 0;
631  uint64_t size_not_including_slice_registers = 0;
632  uint64_t size_by_highest_offset = 0;
633 
634  for (uint32_t reg_idx = 0;
635  (reg_info = GetRegisterInfoAtIndex(reg_idx)) != nullptr; ++reg_idx) {
636  size_including_slice_registers += reg_info->byte_size;
637  if (reg_info->value_regs == nullptr)
638  size_not_including_slice_registers += reg_info->byte_size;
639  if (reg_info->byte_offset >= size_by_highest_offset)
640  size_by_highest_offset = reg_info->byte_offset + reg_info->byte_size;
641  }
642 
643  bool use_byte_offset_into_buffer;
644  if (size_by_highest_offset == restore_data.GetByteSize()) {
645  // The size of the packet agrees with the highest offset: + size in the
646  // register file
647  use_byte_offset_into_buffer = true;
648  } else if (size_not_including_slice_registers ==
649  restore_data.GetByteSize()) {
650  // The size of the packet is the same as concatenating all of the
651  // registers sequentially, skipping the slice registers
652  use_byte_offset_into_buffer = true;
653  } else if (size_including_slice_registers == restore_data.GetByteSize()) {
654  // The slice registers are present in the packet (when they shouldn't
655  // be). Don't try to use the RegisterInfo byte_offset into the
656  // restore_data, it will point to the wrong place.
657  use_byte_offset_into_buffer = false;
658  } else {
659  // None of our expected sizes match the actual g packet data we're
660  // looking at. The most conservative approach here is to use the
661  // running total byte offset.
662  use_byte_offset_into_buffer = false;
663  }
664 
665  // In case our register definitions don't include the correct offsets,
666  // keep track of the size of each reg & compute offset based on that.
667  uint32_t running_byte_offset = 0;
668  for (uint32_t reg_idx = 0;
669  (reg_info = GetRegisterInfoAtIndex(reg_idx)) != nullptr;
670  ++reg_idx, running_byte_offset += reg_info->byte_size) {
671  // Skip composite aka slice registers (e.g. eax is a slice of rax).
672  if (reg_info->value_regs)
673  continue;
674 
675  const uint32_t reg = reg_info->kinds[eRegisterKindLLDB];
676 
677  uint32_t register_offset;
678  if (use_byte_offset_into_buffer) {
679  register_offset = reg_info->byte_offset;
680  } else {
681  register_offset = running_byte_offset;
682  }
683 
684  const uint32_t reg_byte_size = reg_info->byte_size;
685 
686  const uint8_t *restore_src =
687  restore_data.PeekData(register_offset, reg_byte_size);
688  if (restore_src) {
689  SetRegisterIsValid(reg, false);
690  if (gdb_comm.WriteRegister(
692  reg_info->kinds[eRegisterKindProcessPlugin],
693  {restore_src, reg_byte_size}))
694  ++num_restored;
695  }
696  }
697  return num_restored > 0;
698  } else {
699  // For the use_g_packet == false case, we're going to write each register
700  // individually. The data buffer is binary data in this case, instead of
701  // ascii characters.
702 
703  bool arm64_debugserver = false;
704  if (m_thread.GetProcess().get()) {
705  const ArchSpec &arch =
706  m_thread.GetProcess()->GetTarget().GetArchitecture();
707  if (arch.IsValid() && (arch.GetMachine() == llvm::Triple::aarch64 ||
708  arch.GetMachine() == llvm::Triple::aarch64_32) &&
709  arch.GetTriple().getVendor() == llvm::Triple::Apple &&
710  arch.GetTriple().getOS() == llvm::Triple::IOS) {
711  arm64_debugserver = true;
712  }
713  }
714  uint32_t num_restored = 0;
715  const RegisterInfo *reg_info;
716  for (uint32_t i = 0; (reg_info = GetRegisterInfoAtIndex(i)) != nullptr;
717  i++) {
718  if (reg_info->value_regs) // skip registers that are slices of real
719  // registers
720  continue;
721  // Skip the fpsr and fpcr floating point status/control register
722  // writing to work around a bug in an older version of debugserver that
723  // would lead to register context corruption when writing fpsr/fpcr.
724  if (arm64_debugserver && (strcmp(reg_info->name, "fpsr") == 0 ||
725  strcmp(reg_info->name, "fpcr") == 0)) {
726  continue;
727  }
728 
729  SetRegisterIsValid(reg_info, false);
730  if (gdb_comm.WriteRegister(m_thread.GetProtocolID(),
731  reg_info->kinds[eRegisterKindProcessPlugin],
732  {data_sp->GetBytes() + reg_info->byte_offset,
733  reg_info->byte_size}))
734  ++num_restored;
735  }
736  return num_restored > 0;
737  }
738  } else {
741  if (log) {
742  if (log->GetVerbose()) {
743  StreamString strm;
744  gdb_comm.DumpHistory(strm);
745  LLDB_LOGF(log,
746  "error: failed to get packet sequence mutex, not sending "
747  "write all registers:\n%s",
748  strm.GetData());
749  } else
750  LLDB_LOGF(log,
751  "error: failed to get packet sequence mutex, not sending "
752  "write all registers");
753  }
754  }
755  return false;
756 }
757 
759  lldb::RegisterKind kind, uint32_t num) {
760  return m_reg_info_sp->ConvertRegisterKindToRegisterNumber(kind, num);
761 }
762 
764  if (!m_reg_info_sp)
765  return false;
766 
767  const RegisterInfo *reg_info = m_reg_info_sp->GetRegisterInfo("vg");
768  if (!reg_info)
769  return false;
770 
771  uint64_t fail_value = LLDB_INVALID_ADDRESS;
772  uint32_t vg_reg_num = reg_info->kinds[eRegisterKindLLDB];
773  uint64_t vg_reg_value = ReadRegisterAsUnsigned(vg_reg_num, fail_value);
774 
775  if (vg_reg_value != fail_value && vg_reg_value <= 32) {
776  const RegisterInfo *reg_info = m_reg_info_sp->GetRegisterInfo("p0");
777  if (!reg_info || vg_reg_value == reg_info->byte_size)
778  return false;
779 
780  if (m_reg_info_sp->UpdateARM64SVERegistersInfos(vg_reg_value)) {
781  // Make a heap based buffer that is big enough to store all registers
782  m_reg_data.SetData(std::make_shared<DataBufferHeap>(
783  m_reg_info_sp->GetRegisterDataByteSize(), 0));
785 
787 
788  return true;
789  }
790  }
791 
792  return false;
793 }
794 
796  // SVE Z register size is vg x 8 bytes.
797  uint32_t z_reg_byte_size = vg * 8;
798 
799  // SVE vector length has changed, accordingly set size of Z, P and FFR
800  // registers. Also invalidate register offsets it will be recalculated
801  // after SVE register size update.
802  for (auto &reg : m_regs) {
803  if (reg.value_regs == nullptr) {
804  if (reg.name[0] == 'z' && isdigit(reg.name[1]))
805  reg.byte_size = z_reg_byte_size;
806  else if (reg.name[0] == 'p' && isdigit(reg.name[1]))
807  reg.byte_size = vg;
808  else if (strcmp(reg.name, "ffr") == 0)
809  reg.byte_size = vg;
810  }
811  reg.byte_offset = LLDB_INVALID_INDEX32;
812  }
813 
814  // Re-calculate register offsets
816  return true;
817 }
lldb_private::process_gdb_remote::GDBRemoteCommunicationClient::SyncThreadState
bool SyncThreadState(lldb::tid_t tid)
Definition: GDBRemoteCommunicationClient.cpp:3505
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::PrivateSetRegisterValue
bool PrivateSetRegisterValue(uint32_t reg, llvm::ArrayRef< uint8_t > data)
Definition: GDBRemoteRegisterContext.cpp:122
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
Scalar.h
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::Log::GetVerbose
bool GetVerbose() const
Definition: Log.cpp:276
LLDB_INVALID_REGNUM
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:91
lldb_private::ArchSpec::GetMachine
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:667
lldb_private::RegisterValue
Definition: RegisterValue.h:28
lldb_private::process_gdb_remote::GDBRemoteCommunicationClient
Definition: GDBRemoteCommunicationClient.h:58
lldb_private::ExecutionContext::GetProcessPtr
Process * GetProcessPtr() const
Returns a pointer to the process object.
Definition: ExecutionContext.cpp:208
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::~GDBRemoteRegisterContext
~GDBRemoteRegisterContext() override
ThreadGDBRemote.h
lldb_private::RegisterCheckpoint::GetData
lldb::DataBufferSP & GetData()
Definition: RegisterCheckpoint.h:39
GDBR_LOG_THREAD
#define GDBR_LOG_THREAD
Definition: ProcessGDBRemoteLog.h:15
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:249
lldb_private::Process
Definition: Process.h:341
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1209
lldb_private::RegisterContext::ReadRegisterAsUnsigned
uint64_t ReadRegisterAsUnsigned(uint32_t reg, uint64_t fail_value)
Definition: RegisterContext.cpp:188
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::WriteAllRegisterValues
bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override
Definition: GDBRemoteRegisterContext.cpp:584
lldb_private::DataExtractor::PeekData
const uint8_t * PeekData(lldb::offset_t offset, lldb::offset_t length) const
Peek at a bytes at offset.
Definition: DataExtractor.h:832
lldb_private::process_gdb_remote::GDBRemoteDynamicRegisterInfoSP
std::shared_ptr< GDBRemoteDynamicRegisterInfo > GDBRemoteDynamicRegisterInfoSP
Definition: GDBRemoteRegisterContext.h:30
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::SetRegisterIsValid
void SetRegisterIsValid(const RegisterInfo *reg_info, bool valid)
Definition: GDBRemoteRegisterContext.h:103
RegisterValue.h
StringExtractorGDBRemote.h
lldb_private::UserID::SetID
void SetID(lldb::user_id_t uid)
Set accessor for the user ID.
Definition: UserID.h:53
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:444
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::m_read_all_at_once
bool m_read_all_at_once
Definition: GDBRemoteRegisterContext.h:118
lldb_private::DynamicRegisterInfo::m_regs
reg_collection m_regs
Definition: DynamicRegisterInfo.h:118
lldb_private::ExecutionContext::GetThreadPtr
Thread * GetThreadPtr() const
Returns a pointer to the thread object.
Definition: ExecutionContext.h:399
lldb_private::Thread::GetProtocolID
virtual lldb::user_id_t GetProtocolID() const
Definition: Thread.h:1076
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::SetAllRegisterValid
void SetAllRegisterValid(bool b)
Definition: GDBRemoteRegisterContext.cpp:59
lldb_private::DataExtractor::SetData
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
Definition: DataExtractor.cpp:225
lldb_private::RegisterContext::InvalidateIfNeeded
void InvalidateIfNeeded(bool force)
Definition: RegisterContext.cpp:32
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::InvalidateAllRegisters
void InvalidateAllRegisters() override
Definition: GDBRemoteRegisterContext.cpp:55
lldb_private::process_gdb_remote::ThreadGDBRemote
Definition: ThreadGDBRemote.h:28
lldb_private::process_gdb_remote::GDBRemoteCommunication::DumpHistory
void DumpHistory(Stream &strm)
Definition: GDBRemoteCommunication.cpp:1231
Target.h
lldb_private::DataExtractor::SetByteOrder
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
Definition: DataExtractor.h:931
lldb_private::Thread::GetProcess
lldb::ProcessSP GetProcess() const
Definition: Thread.h:152
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::RegisterContext::m_thread
Thread & m_thread
Definition: RegisterContext.h:234
ARM_ehframe_Registers.h
ProcessGDBRemote.h
lldb::eRegisterKindLLDB
@ eRegisterKindLLDB
lldb's internal register numbers
Definition: lldb-enumerations.h:234
lldb_private::DataExtractor::GetAddressByteSize
uint32_t GetAddressByteSize() const
Get the current address size.
Definition: DataExtractor.h:264
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::process_gdb_remote::GDBRemoteCommunicationClient::WriteRegister
bool WriteRegister(lldb::tid_t tid, uint32_t reg_num, llvm::ArrayRef< uint8_t > data)
Definition: GDBRemoteCommunicationClient.cpp:3430
lldb::eRegisterKindProcessPlugin
@ eRegisterKindProcessPlugin
num used by the process plugin - e.g.
Definition: lldb-enumerations.h:232
lldb_private::Thread
Definition: Thread.h:60
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::GetRegisterSetCount
size_t GetRegisterSetCount() override
Definition: GDBRemoteRegisterContext.cpp:75
lldb_private::StreamString::GetData
const char * GetData() const
Definition: StreamString.h:43
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::GetPrimordialRegister
bool GetPrimordialRegister(const RegisterInfo *reg_info, GDBRemoteCommunicationClient &gdb_comm)
Definition: GDBRemoteRegisterContext.cpp:191
StreamString.h
lldb_private::RegisterValue::GetData
bool GetData(DataExtractor &data) const
Definition: RegisterValue.cpp:34
lldb_private::RegisterContext::GetRegisterInfo
const RegisterInfo * GetRegisterInfo(lldb::RegisterKind reg_kind, uint32_t reg_num)
Definition: RegisterContext.cpp:80
lldb_private::DataExtractor::GetDataStart
const uint8_t * GetDataStart() const
Get the data start pointer.
Definition: DataExtractor.h:422
lldb_private::DataExtractor::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
Definition: DataExtractor.h:594
GDBRemoteRegisterContext.h
lldb_private::process_gdb_remote::GDBRemoteCommunicationClient::ReadAllRegisters
lldb::DataBufferSP ReadAllRegisters(lldb::tid_t tid)
Definition: GDBRemoteCommunicationClient.cpp:3415
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::GetRegisterIsValid
bool GetRegisterIsValid(uint32_t reg) const
Definition: GDBRemoteRegisterContext.h:96
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::ReadRegister
bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &value) override
Definition: GDBRemoteRegisterContext.cpp:83
lldb_private::process_gdb_remote::GDBRemoteDynamicRegisterInfo::UpdateARM64SVERegistersInfos
bool UpdateARM64SVERegistersInfos(uint64_t vg)
Definition: GDBRemoteRegisterContext.cpp:795
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::GetRegisterInfoAtIndex
const RegisterInfo * GetRegisterInfoAtIndex(size_t reg) override
Definition: GDBRemoteRegisterContext.cpp:71
lldb_private::process_gdb_remote::GDBRemoteCommunicationClient::SaveRegisterState
bool SaveRegisterState(lldb::tid_t tid, uint32_t &save_id)
Definition: GDBRemoteCommunicationClient.cpp:3457
arm64_dwarf::vg
@ vg
Definition: ARM64_DWARF_Registers.h:57
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::AArch64SVEReconfigure
bool AArch64SVEReconfigure()
Definition: GDBRemoteRegisterContext.cpp:763
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:342
lldb_private::process_gdb_remote
Definition: GDBRemoteClientBase.h:17
lldb_private::RegisterContext
Definition: RegisterContext.h:17
ARM_DWARF_Registers.h
lldb_private::UserID::GetID
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47
lldb::RegisterKind
RegisterKind
Register numbering types.
Definition: lldb-enumerations.h:227
lldb_private::process_gdb_remote::GDBRemoteCommunicationClient::RestoreRegisterState
bool RestoreRegisterState(lldb::tid_t tid, uint32_t save_id)
Definition: GDBRemoteCommunicationClient.cpp:3482
lldb_private::RegisterContext::GetByteOrder
virtual lldb::ByteOrder GetByteOrder()
Definition: RegisterContext.cpp:406
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::ReadAllRegisterValues
bool ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override
Definition: GDBRemoteRegisterContext.cpp:521
lldb_private::RegisterValue::SetFromMemoryData
uint32_t SetFromMemoryData(const RegisterInfo *reg_info, const void *src, uint32_t src_len, lldb::ByteOrder src_byte_order, Status &error)
Definition: RegisterValue.cpp:84
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::m_write_all_at_once
bool m_write_all_at_once
Definition: GDBRemoteRegisterContext.h:119
lldb_private::RegisterValue::SetValueFromData
Status SetValueFromData(const RegisterInfo *reg_info, DataExtractor &data, lldb::offset_t offset, bool partial_data_ok)
Definition: RegisterValue.cpp:172
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:967
lldb_private::Status
Definition: Status.h:44
lldb_private::process_gdb_remote::GDBRemoteCommunicationClient::WriteAllRegisters
bool WriteAllRegisters(lldb::tid_t tid, llvm::ArrayRef< uint8_t > data)
Definition: GDBRemoteCommunicationClient.cpp:3444
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::WriteRegisterBytes
bool WriteRegisterBytes(const RegisterInfo *reg_info, DataExtractor &data, uint32_t data_offset)
Definition: GDBRemoteRegisterContext.cpp:349
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::m_reg_info_sp
GDBRemoteDynamicRegisterInfoSP m_reg_info_sp
Definition: GDBRemoteRegisterContext.h:115
lldb::eByteOrderInvalid
@ eByteOrderInvalid
Definition: lldb-enumerations.h:139
uint32_t
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::m_gpacket_cached
bool m_gpacket_cached
Definition: GDBRemoteRegisterContext.h:120
lldb_private::endian::InlHostByteOrder
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
lldb_private::RegisterCheckpoint
Definition: RegisterCheckpoint.h:21
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::m_reg_data
DataExtractor m_reg_data
Definition: GDBRemoteRegisterContext.h:117
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::GetRegisterSet
const RegisterSet * GetRegisterSet(size_t reg_set) override
Definition: GDBRemoteRegisterContext.cpp:79
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::ReadRegisterBytes
bool ReadRegisterBytes(const RegisterInfo *reg_info)
Definition: GDBRemoteRegisterContext.cpp:204
ProcessGDBRemoteLog.h
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::WriteRegister
bool WriteRegister(const RegisterInfo *reg_info, const RegisterValue &value) override
Definition: GDBRemoteRegisterContext.cpp:296
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
DataExtractor.h
lldb_private::RegisterContext::CalculateThread
lldb::ThreadSP CalculateThread() override
Definition: RegisterContext.cpp:435
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::GetRegisterCount
size_t GetRegisterCount() override
Definition: GDBRemoteRegisterContext.cpp:66
lldb_private::process_gdb_remote::ProcessGDBRemote
Definition: ProcessGDBRemote.h:50
lldb_private::DataExtractor::CopyData
lldb::offset_t CopyData(lldb::offset_t offset, lldb::offset_t length, void *dst) const
Copy length bytes from *offset, without swapping bytes.
Definition: DataExtractor.cpp:680
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::process_gdb_remote::GDBRemoteClientBase::Lock
Definition: GDBRemoteClientBase.h:62
lldb_private::DynamicRegisterInfo::ConfigureOffsets
void ConfigureOffsets()
Definition: DynamicRegisterInfo.cpp:617
lldb_private::DataExtractor::CopyByteOrderedData
lldb::offset_t CopyByteOrderedData(lldb::offset_t src_offset, lldb::offset_t src_len, void *dst, lldb::offset_t dst_len, lldb::ByteOrder dst_byte_order) const
Copy dst_len bytes from *offset_ptr and ensure the copied data is treated as a value that can be swap...
Definition: DataExtractor.cpp:692
lldb_private::process_gdb_remote::GDBRemoteCommunicationClient::ReadRegister
lldb::DataBufferSP ReadRegister(lldb::tid_t tid, uint32_t reg_num)
Definition: GDBRemoteCommunicationClient.cpp:3399
StringExtractorGDBRemote
Definition: StringExtractorGDBRemote.h:21
LLDB_INVALID_INDEX32
#define LLDB_INVALID_INDEX32
Definition: lldb-defines.h:87
lldb_private::Log
Definition: Log.h:49
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::ConvertRegisterKindToRegisterNumber
uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num) override
Convert from a given register numbering scheme to the lldb register numbering scheme.
Definition: GDBRemoteRegisterContext.cpp:758
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::SetPrimordialRegister
bool SetPrimordialRegister(const RegisterInfo *reg_info, GDBRemoteCommunicationClient &gdb_comm)
Definition: GDBRemoteRegisterContext.cpp:335
lldb_private::process_gdb_remote::ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet
static Log * GetLogIfAnyCategoryIsSet(uint32_t mask)
Definition: ProcessGDBRemoteLog.h:39
lldb_private::process_gdb_remote::GDBRemoteRegisterContext::m_reg_valid
std::vector< bool > m_reg_valid
Definition: GDBRemoteRegisterContext.h:116
lldb_private::DataExtractor::GetByteSize
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
Definition: DataExtractor.h:270
GDBR_LOG_PACKETS
#define GDBR_LOG_PACKETS
Definition: ProcessGDBRemoteLog.h:16
lldb_private::DataBufferHeap
Definition: DataBufferHeap.h:30
lldb
Definition: SBAddress.h:15
ExecutionContext.h
DataBufferHeap.h
lldb_private::process_gdb_remote::GDBRemoteCommunicationClient::AvoidGPackets
bool AvoidGPackets(ProcessGDBRemote *process)
Definition: GDBRemoteCommunicationClient.cpp:3373