GDBRemoteRegisterContext.cpp revision 341825
1//===-- GDBRemoteRegisterContext.cpp ----------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "GDBRemoteRegisterContext.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15#include "lldb/Core/RegisterValue.h"
16#include "lldb/Core/Scalar.h"
17#include "lldb/Target/ExecutionContext.h"
18#include "lldb/Target/Target.h"
19#include "lldb/Utility/DataBufferHeap.h"
20#include "lldb/Utility/DataExtractor.h"
21#include "lldb/Utility/StreamString.h"
22// Project includes
23#include "ProcessGDBRemote.h"
24#include "ProcessGDBRemoteLog.h"
25#include "ThreadGDBRemote.h"
26#include "Utility/ARM_DWARF_Registers.h"
27#include "Utility/ARM_ehframe_Registers.h"
28#include "lldb/Utility/StringExtractorGDBRemote.h"
29
30using namespace lldb;
31using namespace lldb_private;
32using namespace lldb_private::process_gdb_remote;
33
34//----------------------------------------------------------------------
35// GDBRemoteRegisterContext constructor
36//----------------------------------------------------------------------
37GDBRemoteRegisterContext::GDBRemoteRegisterContext(
38    ThreadGDBRemote &thread, uint32_t concrete_frame_idx,
39    GDBRemoteDynamicRegisterInfo &reg_info, bool read_all_at_once)
40    : RegisterContext(thread, concrete_frame_idx), m_reg_info(reg_info),
41      m_reg_valid(), m_reg_data(), m_read_all_at_once(read_all_at_once) {
42  // Resize our vector of bools to contain one bool for every register. We will
43  // use these boolean values to know when a register value is valid in
44  // m_reg_data.
45  m_reg_valid.resize(reg_info.GetNumRegisters());
46
47  // Make a heap based buffer that is big enough to store all registers
48  DataBufferSP reg_data_sp(
49      new DataBufferHeap(reg_info.GetRegisterDataByteSize(), 0));
50  m_reg_data.SetData(reg_data_sp);
51  m_reg_data.SetByteOrder(thread.GetProcess()->GetByteOrder());
52}
53
54//----------------------------------------------------------------------
55// Destructor
56//----------------------------------------------------------------------
57GDBRemoteRegisterContext::~GDBRemoteRegisterContext() {}
58
59void GDBRemoteRegisterContext::InvalidateAllRegisters() {
60  SetAllRegisterValid(false);
61}
62
63void GDBRemoteRegisterContext::SetAllRegisterValid(bool b) {
64  std::vector<bool>::iterator pos, end = m_reg_valid.end();
65  for (pos = m_reg_valid.begin(); pos != end; ++pos)
66    *pos = b;
67}
68
69size_t GDBRemoteRegisterContext::GetRegisterCount() {
70  return m_reg_info.GetNumRegisters();
71}
72
73const RegisterInfo *
74GDBRemoteRegisterContext::GetRegisterInfoAtIndex(size_t reg) {
75  RegisterInfo *reg_info = m_reg_info.GetRegisterInfoAtIndex(reg);
76
77  if (reg_info && reg_info->dynamic_size_dwarf_expr_bytes) {
78    const ArchSpec &arch = m_thread.GetProcess()->GetTarget().GetArchitecture();
79    uint8_t reg_size = UpdateDynamicRegisterSize(arch, reg_info);
80    reg_info->byte_size = reg_size;
81  }
82  return reg_info;
83}
84
85size_t GDBRemoteRegisterContext::GetRegisterSetCount() {
86  return m_reg_info.GetNumRegisterSets();
87}
88
89const RegisterSet *GDBRemoteRegisterContext::GetRegisterSet(size_t reg_set) {
90  return m_reg_info.GetRegisterSet(reg_set);
91}
92
93bool GDBRemoteRegisterContext::ReadRegister(const RegisterInfo *reg_info,
94                                            RegisterValue &value) {
95  // Read the register
96  if (ReadRegisterBytes(reg_info, m_reg_data)) {
97    const bool partial_data_ok = false;
98    Status error(value.SetValueFromData(
99        reg_info, m_reg_data, reg_info->byte_offset, partial_data_ok));
100    return error.Success();
101  }
102  return false;
103}
104
105bool GDBRemoteRegisterContext::PrivateSetRegisterValue(
106    uint32_t reg, llvm::ArrayRef<uint8_t> data) {
107  const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
108  if (reg_info == NULL)
109    return false;
110
111  // Invalidate if needed
112  InvalidateIfNeeded(false);
113
114  const size_t reg_byte_size = reg_info->byte_size;
115  memcpy(const_cast<uint8_t *>(
116             m_reg_data.PeekData(reg_info->byte_offset, reg_byte_size)),
117         data.data(), std::min(data.size(), reg_byte_size));
118  bool success = data.size() >= reg_byte_size;
119  if (success) {
120    SetRegisterIsValid(reg, true);
121  } else if (data.size() > 0) {
122    // Only set register is valid to false if we copied some bytes, else leave
123    // it as it was.
124    SetRegisterIsValid(reg, false);
125  }
126  return success;
127}
128
129bool GDBRemoteRegisterContext::PrivateSetRegisterValue(uint32_t reg,
130                                                       uint64_t new_reg_val) {
131  const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
132  if (reg_info == NULL)
133    return false;
134
135  // Early in process startup, we can get a thread that has an invalid byte
136  // order because the process hasn't been completely set up yet (see the ctor
137  // where the byte order is setfrom the process).  If that's the case, we
138  // can't set the value here.
139  if (m_reg_data.GetByteOrder() == eByteOrderInvalid) {
140    return false;
141  }
142
143  // Invalidate if needed
144  InvalidateIfNeeded(false);
145
146  DataBufferSP buffer_sp(new DataBufferHeap(&new_reg_val, sizeof(new_reg_val)));
147  DataExtractor data(buffer_sp, endian::InlHostByteOrder(), sizeof(void *));
148
149  // If our register context and our register info disagree, which should never
150  // happen, don't overwrite past the end of the buffer.
151  if (m_reg_data.GetByteSize() < reg_info->byte_offset + reg_info->byte_size)
152    return false;
153
154  // Grab a pointer to where we are going to put this register
155  uint8_t *dst = const_cast<uint8_t *>(
156      m_reg_data.PeekData(reg_info->byte_offset, reg_info->byte_size));
157
158  if (dst == NULL)
159    return false;
160
161  if (data.CopyByteOrderedData(0,                          // src offset
162                               reg_info->byte_size,        // src length
163                               dst,                        // dst
164                               reg_info->byte_size,        // dst length
165                               m_reg_data.GetByteOrder())) // dst byte order
166  {
167    SetRegisterIsValid(reg, true);
168    return true;
169  }
170  return false;
171}
172
173// Helper function for GDBRemoteRegisterContext::ReadRegisterBytes().
174bool GDBRemoteRegisterContext::GetPrimordialRegister(
175    const RegisterInfo *reg_info, GDBRemoteCommunicationClient &gdb_comm) {
176  const uint32_t lldb_reg = reg_info->kinds[eRegisterKindLLDB];
177  const uint32_t remote_reg = reg_info->kinds[eRegisterKindProcessPlugin];
178
179  if (DataBufferSP buffer_sp =
180          gdb_comm.ReadRegister(m_thread.GetProtocolID(), remote_reg))
181    return PrivateSetRegisterValue(
182        lldb_reg, llvm::ArrayRef<uint8_t>(buffer_sp->GetBytes(),
183                                          buffer_sp->GetByteSize()));
184  return false;
185}
186
187bool GDBRemoteRegisterContext::ReadRegisterBytes(const RegisterInfo *reg_info,
188                                                 DataExtractor &data) {
189  ExecutionContext exe_ctx(CalculateThread());
190
191  Process *process = exe_ctx.GetProcessPtr();
192  Thread *thread = exe_ctx.GetThreadPtr();
193  if (process == NULL || thread == NULL)
194    return false;
195
196  GDBRemoteCommunicationClient &gdb_comm(
197      ((ProcessGDBRemote *)process)->GetGDBRemote());
198
199  InvalidateIfNeeded(false);
200
201  const uint32_t reg = reg_info->kinds[eRegisterKindLLDB];
202
203  if (!GetRegisterIsValid(reg)) {
204    if (m_read_all_at_once) {
205      if (DataBufferSP buffer_sp =
206              gdb_comm.ReadAllRegisters(m_thread.GetProtocolID())) {
207        memcpy(const_cast<uint8_t *>(m_reg_data.GetDataStart()),
208               buffer_sp->GetBytes(),
209               std::min(buffer_sp->GetByteSize(), m_reg_data.GetByteSize()));
210        if (buffer_sp->GetByteSize() >= m_reg_data.GetByteSize()) {
211          SetAllRegisterValid(true);
212          return true;
213        }
214      }
215      return false;
216    }
217    if (reg_info->value_regs) {
218      // Process this composite register request by delegating to the
219      // constituent primordial registers.
220
221      // Index of the primordial register.
222      bool success = true;
223      for (uint32_t idx = 0; success; ++idx) {
224        const uint32_t prim_reg = reg_info->value_regs[idx];
225        if (prim_reg == LLDB_INVALID_REGNUM)
226          break;
227        // We have a valid primordial register as our constituent. Grab the
228        // corresponding register info.
229        const RegisterInfo *prim_reg_info = GetRegisterInfoAtIndex(prim_reg);
230        if (prim_reg_info == NULL)
231          success = false;
232        else {
233          // Read the containing register if it hasn't already been read
234          if (!GetRegisterIsValid(prim_reg))
235            success = GetPrimordialRegister(prim_reg_info, gdb_comm);
236        }
237      }
238
239      if (success) {
240        // If we reach this point, all primordial register requests have
241        // succeeded. Validate this composite register.
242        SetRegisterIsValid(reg_info, true);
243      }
244    } else {
245      // Get each register individually
246      GetPrimordialRegister(reg_info, gdb_comm);
247    }
248
249    // Make sure we got a valid register value after reading it
250    if (!GetRegisterIsValid(reg))
251      return false;
252  }
253
254  if (&data != &m_reg_data) {
255#if defined(LLDB_CONFIGURATION_DEBUG)
256    assert(m_reg_data.GetByteSize() >=
257           reg_info->byte_offset + reg_info->byte_size);
258#endif
259    // If our register context and our register info disagree, which should
260    // never happen, don't read past the end of the buffer.
261    if (m_reg_data.GetByteSize() < reg_info->byte_offset + reg_info->byte_size)
262      return false;
263
264    // If we aren't extracting into our own buffer (which only happens when
265    // this function is called from ReadRegisterValue(uint32_t, Scalar&)) then
266    // we transfer bytes from our buffer into the data buffer that was passed
267    // in
268
269    data.SetByteOrder(m_reg_data.GetByteOrder());
270    data.SetData(m_reg_data, reg_info->byte_offset, reg_info->byte_size);
271  }
272  return true;
273}
274
275bool GDBRemoteRegisterContext::WriteRegister(const RegisterInfo *reg_info,
276                                             const RegisterValue &value) {
277  DataExtractor data;
278  if (value.GetData(data))
279    return WriteRegisterBytes(reg_info, data, 0);
280  return false;
281}
282
283// Helper function for GDBRemoteRegisterContext::WriteRegisterBytes().
284bool GDBRemoteRegisterContext::SetPrimordialRegister(
285    const RegisterInfo *reg_info, GDBRemoteCommunicationClient &gdb_comm) {
286  StreamString packet;
287  StringExtractorGDBRemote response;
288  const uint32_t reg = reg_info->kinds[eRegisterKindLLDB];
289  // Invalidate just this register
290  SetRegisterIsValid(reg, false);
291
292  return gdb_comm.WriteRegister(
293      m_thread.GetProtocolID(), reg_info->kinds[eRegisterKindProcessPlugin],
294      {m_reg_data.PeekData(reg_info->byte_offset, reg_info->byte_size),
295       reg_info->byte_size});
296}
297
298bool GDBRemoteRegisterContext::WriteRegisterBytes(const RegisterInfo *reg_info,
299                                                  DataExtractor &data,
300                                                  uint32_t data_offset) {
301  ExecutionContext exe_ctx(CalculateThread());
302
303  Process *process = exe_ctx.GetProcessPtr();
304  Thread *thread = exe_ctx.GetThreadPtr();
305  if (process == NULL || thread == NULL)
306    return false;
307
308  GDBRemoteCommunicationClient &gdb_comm(
309      ((ProcessGDBRemote *)process)->GetGDBRemote());
310
311#if defined(LLDB_CONFIGURATION_DEBUG)
312  assert(m_reg_data.GetByteSize() >=
313         reg_info->byte_offset + reg_info->byte_size);
314#endif
315
316  // If our register context and our register info disagree, which should never
317  // happen, don't overwrite past the end of the buffer.
318  if (m_reg_data.GetByteSize() < reg_info->byte_offset + reg_info->byte_size)
319    return false;
320
321  // Grab a pointer to where we are going to put this register
322  uint8_t *dst = const_cast<uint8_t *>(
323      m_reg_data.PeekData(reg_info->byte_offset, reg_info->byte_size));
324
325  if (dst == NULL)
326    return false;
327
328  if (data.CopyByteOrderedData(data_offset,                // src offset
329                               reg_info->byte_size,        // src length
330                               dst,                        // dst
331                               reg_info->byte_size,        // dst length
332                               m_reg_data.GetByteOrder())) // dst byte order
333  {
334    GDBRemoteClientBase::Lock lock(gdb_comm, false);
335    if (lock) {
336      if (m_read_all_at_once) {
337        // Invalidate all register values
338        InvalidateIfNeeded(true);
339
340        // Set all registers in one packet
341        if (gdb_comm.WriteAllRegisters(
342                m_thread.GetProtocolID(),
343                {m_reg_data.GetDataStart(), size_t(m_reg_data.GetByteSize())}))
344
345        {
346          SetAllRegisterValid(false);
347          return true;
348        }
349      } else {
350        bool success = true;
351
352        if (reg_info->value_regs) {
353          // This register is part of another register. In this case we read
354          // the actual register data for any "value_regs", and once all that
355          // data is read, we will have enough data in our register context
356          // bytes for the value of this register
357
358          // Invalidate this composite register first.
359
360          for (uint32_t idx = 0; success; ++idx) {
361            const uint32_t reg = reg_info->value_regs[idx];
362            if (reg == LLDB_INVALID_REGNUM)
363              break;
364            // We have a valid primordial register as our constituent. Grab the
365            // corresponding register info.
366            const RegisterInfo *value_reg_info = GetRegisterInfoAtIndex(reg);
367            if (value_reg_info == NULL)
368              success = false;
369            else
370              success = SetPrimordialRegister(value_reg_info, gdb_comm);
371          }
372        } else {
373          // This is an actual register, write it
374          success = SetPrimordialRegister(reg_info, gdb_comm);
375        }
376
377        // Check if writing this register will invalidate any other register
378        // values? If so, invalidate them
379        if (reg_info->invalidate_regs) {
380          for (uint32_t idx = 0, reg = reg_info->invalidate_regs[0];
381               reg != LLDB_INVALID_REGNUM;
382               reg = reg_info->invalidate_regs[++idx]) {
383            SetRegisterIsValid(reg, false);
384          }
385        }
386
387        return success;
388      }
389    } else {
390      Log *log(ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(GDBR_LOG_THREAD |
391                                                             GDBR_LOG_PACKETS));
392      if (log) {
393        if (log->GetVerbose()) {
394          StreamString strm;
395          gdb_comm.DumpHistory(strm);
396          log->Printf("error: failed to get packet sequence mutex, not sending "
397                      "write register for \"%s\":\n%s",
398                      reg_info->name, strm.GetData());
399        } else
400          log->Printf("error: failed to get packet sequence mutex, not sending "
401                      "write register for \"%s\"",
402                      reg_info->name);
403      }
404    }
405  }
406  return false;
407}
408
409bool GDBRemoteRegisterContext::ReadAllRegisterValues(
410    RegisterCheckpoint &reg_checkpoint) {
411  ExecutionContext exe_ctx(CalculateThread());
412
413  Process *process = exe_ctx.GetProcessPtr();
414  Thread *thread = exe_ctx.GetThreadPtr();
415  if (process == NULL || thread == NULL)
416    return false;
417
418  GDBRemoteCommunicationClient &gdb_comm(
419      ((ProcessGDBRemote *)process)->GetGDBRemote());
420
421  uint32_t save_id = 0;
422  if (gdb_comm.SaveRegisterState(thread->GetProtocolID(), save_id)) {
423    reg_checkpoint.SetID(save_id);
424    reg_checkpoint.GetData().reset();
425    return true;
426  } else {
427    reg_checkpoint.SetID(0); // Invalid save ID is zero
428    return ReadAllRegisterValues(reg_checkpoint.GetData());
429  }
430}
431
432bool GDBRemoteRegisterContext::WriteAllRegisterValues(
433    const RegisterCheckpoint &reg_checkpoint) {
434  uint32_t save_id = reg_checkpoint.GetID();
435  if (save_id != 0) {
436    ExecutionContext exe_ctx(CalculateThread());
437
438    Process *process = exe_ctx.GetProcessPtr();
439    Thread *thread = exe_ctx.GetThreadPtr();
440    if (process == NULL || thread == NULL)
441      return false;
442
443    GDBRemoteCommunicationClient &gdb_comm(
444        ((ProcessGDBRemote *)process)->GetGDBRemote());
445
446    return gdb_comm.RestoreRegisterState(m_thread.GetProtocolID(), save_id);
447  } else {
448    return WriteAllRegisterValues(reg_checkpoint.GetData());
449  }
450}
451
452bool GDBRemoteRegisterContext::ReadAllRegisterValues(
453    lldb::DataBufferSP &data_sp) {
454  ExecutionContext exe_ctx(CalculateThread());
455
456  Process *process = exe_ctx.GetProcessPtr();
457  Thread *thread = exe_ctx.GetThreadPtr();
458  if (process == NULL || thread == NULL)
459    return false;
460
461  GDBRemoteCommunicationClient &gdb_comm(
462      ((ProcessGDBRemote *)process)->GetGDBRemote());
463
464  const bool use_g_packet =
465      gdb_comm.AvoidGPackets((ProcessGDBRemote *)process) == false;
466
467  GDBRemoteClientBase::Lock lock(gdb_comm, false);
468  if (lock) {
469    if (gdb_comm.SyncThreadState(m_thread.GetProtocolID()))
470      InvalidateAllRegisters();
471
472    if (use_g_packet &&
473        (data_sp = gdb_comm.ReadAllRegisters(m_thread.GetProtocolID())))
474      return true;
475
476    // We're going to read each register
477    // individually and store them as binary data in a buffer.
478    const RegisterInfo *reg_info;
479
480    for (uint32_t i = 0; (reg_info = GetRegisterInfoAtIndex(i)) != NULL; i++) {
481      if (reg_info
482              ->value_regs) // skip registers that are slices of real registers
483        continue;
484      ReadRegisterBytes(reg_info, m_reg_data);
485      // ReadRegisterBytes saves the contents of the register in to the
486      // m_reg_data buffer
487    }
488    data_sp.reset(new DataBufferHeap(m_reg_data.GetDataStart(),
489                                     m_reg_info.GetRegisterDataByteSize()));
490    return true;
491  } else {
492
493    Log *log(ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(GDBR_LOG_THREAD |
494                                                           GDBR_LOG_PACKETS));
495    if (log) {
496      if (log->GetVerbose()) {
497        StreamString strm;
498        gdb_comm.DumpHistory(strm);
499        log->Printf("error: failed to get packet sequence mutex, not sending "
500                    "read all registers:\n%s",
501                    strm.GetData());
502      } else
503        log->Printf("error: failed to get packet sequence mutex, not sending "
504                    "read all registers");
505    }
506  }
507
508  data_sp.reset();
509  return false;
510}
511
512bool GDBRemoteRegisterContext::WriteAllRegisterValues(
513    const lldb::DataBufferSP &data_sp) {
514  if (!data_sp || data_sp->GetBytes() == NULL || data_sp->GetByteSize() == 0)
515    return false;
516
517  ExecutionContext exe_ctx(CalculateThread());
518
519  Process *process = exe_ctx.GetProcessPtr();
520  Thread *thread = exe_ctx.GetThreadPtr();
521  if (process == NULL || thread == NULL)
522    return false;
523
524  GDBRemoteCommunicationClient &gdb_comm(
525      ((ProcessGDBRemote *)process)->GetGDBRemote());
526
527  const bool use_g_packet =
528      gdb_comm.AvoidGPackets((ProcessGDBRemote *)process) == false;
529
530  GDBRemoteClientBase::Lock lock(gdb_comm, false);
531  if (lock) {
532    // The data_sp contains the G response packet.
533    if (use_g_packet) {
534      if (gdb_comm.WriteAllRegisters(
535              m_thread.GetProtocolID(),
536              {data_sp->GetBytes(), size_t(data_sp->GetByteSize())}))
537        return true;
538
539      uint32_t num_restored = 0;
540      // We need to manually go through all of the registers and restore them
541      // manually
542      DataExtractor restore_data(data_sp, m_reg_data.GetByteOrder(),
543                                 m_reg_data.GetAddressByteSize());
544
545      const RegisterInfo *reg_info;
546
547      // The g packet contents may either include the slice registers
548      // (registers defined in terms of other registers, e.g. eax is a subset
549      // of rax) or not.  The slice registers should NOT be in the g packet,
550      // but some implementations may incorrectly include them.
551      //
552      // If the slice registers are included in the packet, we must step over
553      // the slice registers when parsing the packet -- relying on the
554      // RegisterInfo byte_offset field would be incorrect. If the slice
555      // registers are not included, then using the byte_offset values into the
556      // data buffer is the best way to find individual register values.
557
558      uint64_t size_including_slice_registers = 0;
559      uint64_t size_not_including_slice_registers = 0;
560      uint64_t size_by_highest_offset = 0;
561
562      for (uint32_t reg_idx = 0;
563           (reg_info = GetRegisterInfoAtIndex(reg_idx)) != NULL; ++reg_idx) {
564        size_including_slice_registers += reg_info->byte_size;
565        if (reg_info->value_regs == NULL)
566          size_not_including_slice_registers += reg_info->byte_size;
567        if (reg_info->byte_offset >= size_by_highest_offset)
568          size_by_highest_offset = reg_info->byte_offset + reg_info->byte_size;
569      }
570
571      bool use_byte_offset_into_buffer;
572      if (size_by_highest_offset == restore_data.GetByteSize()) {
573        // The size of the packet agrees with the highest offset: + size in the
574        // register file
575        use_byte_offset_into_buffer = true;
576      } else if (size_not_including_slice_registers ==
577                 restore_data.GetByteSize()) {
578        // The size of the packet is the same as concatenating all of the
579        // registers sequentially, skipping the slice registers
580        use_byte_offset_into_buffer = true;
581      } else if (size_including_slice_registers == restore_data.GetByteSize()) {
582        // The slice registers are present in the packet (when they shouldn't
583        // be). Don't try to use the RegisterInfo byte_offset into the
584        // restore_data, it will point to the wrong place.
585        use_byte_offset_into_buffer = false;
586      } else {
587        // None of our expected sizes match the actual g packet data we're
588        // looking at. The most conservative approach here is to use the
589        // running total byte offset.
590        use_byte_offset_into_buffer = false;
591      }
592
593      // In case our register definitions don't include the correct offsets,
594      // keep track of the size of each reg & compute offset based on that.
595      uint32_t running_byte_offset = 0;
596      for (uint32_t reg_idx = 0;
597           (reg_info = GetRegisterInfoAtIndex(reg_idx)) != NULL;
598           ++reg_idx, running_byte_offset += reg_info->byte_size) {
599        // Skip composite aka slice registers (e.g. eax is a slice of rax).
600        if (reg_info->value_regs)
601          continue;
602
603        const uint32_t reg = reg_info->kinds[eRegisterKindLLDB];
604
605        uint32_t register_offset;
606        if (use_byte_offset_into_buffer) {
607          register_offset = reg_info->byte_offset;
608        } else {
609          register_offset = running_byte_offset;
610        }
611
612        const uint32_t reg_byte_size = reg_info->byte_size;
613
614        const uint8_t *restore_src =
615            restore_data.PeekData(register_offset, reg_byte_size);
616        if (restore_src) {
617          SetRegisterIsValid(reg, false);
618          if (gdb_comm.WriteRegister(
619                  m_thread.GetProtocolID(),
620                  reg_info->kinds[eRegisterKindProcessPlugin],
621                  {restore_src, reg_byte_size}))
622            ++num_restored;
623        }
624      }
625      return num_restored > 0;
626    } else {
627      // For the use_g_packet == false case, we're going to write each register
628      // individually.  The data buffer is binary data in this case, instead of
629      // ascii characters.
630
631      bool arm64_debugserver = false;
632      if (m_thread.GetProcess().get()) {
633        const ArchSpec &arch =
634            m_thread.GetProcess()->GetTarget().GetArchitecture();
635        if (arch.IsValid() && arch.GetMachine() == llvm::Triple::aarch64 &&
636            arch.GetTriple().getVendor() == llvm::Triple::Apple &&
637            arch.GetTriple().getOS() == llvm::Triple::IOS) {
638          arm64_debugserver = true;
639        }
640      }
641      uint32_t num_restored = 0;
642      const RegisterInfo *reg_info;
643      for (uint32_t i = 0; (reg_info = GetRegisterInfoAtIndex(i)) != NULL;
644           i++) {
645        if (reg_info->value_regs) // skip registers that are slices of real
646                                  // registers
647          continue;
648        // Skip the fpsr and fpcr floating point status/control register
649        // writing to work around a bug in an older version of debugserver that
650        // would lead to register context corruption when writing fpsr/fpcr.
651        if (arm64_debugserver && (strcmp(reg_info->name, "fpsr") == 0 ||
652                                  strcmp(reg_info->name, "fpcr") == 0)) {
653          continue;
654        }
655
656        SetRegisterIsValid(reg_info, false);
657        if (gdb_comm.WriteRegister(m_thread.GetProtocolID(),
658                                   reg_info->kinds[eRegisterKindProcessPlugin],
659                                   {data_sp->GetBytes() + reg_info->byte_offset,
660                                    reg_info->byte_size}))
661          ++num_restored;
662      }
663      return num_restored > 0;
664    }
665  } else {
666    Log *log(ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(GDBR_LOG_THREAD |
667                                                           GDBR_LOG_PACKETS));
668    if (log) {
669      if (log->GetVerbose()) {
670        StreamString strm;
671        gdb_comm.DumpHistory(strm);
672        log->Printf("error: failed to get packet sequence mutex, not sending "
673                    "write all registers:\n%s",
674                    strm.GetData());
675      } else
676        log->Printf("error: failed to get packet sequence mutex, not sending "
677                    "write all registers");
678    }
679  }
680  return false;
681}
682
683uint32_t GDBRemoteRegisterContext::ConvertRegisterKindToRegisterNumber(
684    lldb::RegisterKind kind, uint32_t num) {
685  return m_reg_info.ConvertRegisterKindToRegisterNumber(kind, num);
686}
687
688void GDBRemoteDynamicRegisterInfo::HardcodeARMRegisters(bool from_scratch) {
689  // For Advanced SIMD and VFP register mapping.
690  static uint32_t g_d0_regs[] = {26, 27, LLDB_INVALID_REGNUM};  // (s0, s1)
691  static uint32_t g_d1_regs[] = {28, 29, LLDB_INVALID_REGNUM};  // (s2, s3)
692  static uint32_t g_d2_regs[] = {30, 31, LLDB_INVALID_REGNUM};  // (s4, s5)
693  static uint32_t g_d3_regs[] = {32, 33, LLDB_INVALID_REGNUM};  // (s6, s7)
694  static uint32_t g_d4_regs[] = {34, 35, LLDB_INVALID_REGNUM};  // (s8, s9)
695  static uint32_t g_d5_regs[] = {36, 37, LLDB_INVALID_REGNUM};  // (s10, s11)
696  static uint32_t g_d6_regs[] = {38, 39, LLDB_INVALID_REGNUM};  // (s12, s13)
697  static uint32_t g_d7_regs[] = {40, 41, LLDB_INVALID_REGNUM};  // (s14, s15)
698  static uint32_t g_d8_regs[] = {42, 43, LLDB_INVALID_REGNUM};  // (s16, s17)
699  static uint32_t g_d9_regs[] = {44, 45, LLDB_INVALID_REGNUM};  // (s18, s19)
700  static uint32_t g_d10_regs[] = {46, 47, LLDB_INVALID_REGNUM}; // (s20, s21)
701  static uint32_t g_d11_regs[] = {48, 49, LLDB_INVALID_REGNUM}; // (s22, s23)
702  static uint32_t g_d12_regs[] = {50, 51, LLDB_INVALID_REGNUM}; // (s24, s25)
703  static uint32_t g_d13_regs[] = {52, 53, LLDB_INVALID_REGNUM}; // (s26, s27)
704  static uint32_t g_d14_regs[] = {54, 55, LLDB_INVALID_REGNUM}; // (s28, s29)
705  static uint32_t g_d15_regs[] = {56, 57, LLDB_INVALID_REGNUM}; // (s30, s31)
706  static uint32_t g_q0_regs[] = {
707      26, 27, 28, 29, LLDB_INVALID_REGNUM}; // (d0, d1) -> (s0, s1, s2, s3)
708  static uint32_t g_q1_regs[] = {
709      30, 31, 32, 33, LLDB_INVALID_REGNUM}; // (d2, d3) -> (s4, s5, s6, s7)
710  static uint32_t g_q2_regs[] = {
711      34, 35, 36, 37, LLDB_INVALID_REGNUM}; // (d4, d5) -> (s8, s9, s10, s11)
712  static uint32_t g_q3_regs[] = {
713      38, 39, 40, 41, LLDB_INVALID_REGNUM}; // (d6, d7) -> (s12, s13, s14, s15)
714  static uint32_t g_q4_regs[] = {
715      42, 43, 44, 45, LLDB_INVALID_REGNUM}; // (d8, d9) -> (s16, s17, s18, s19)
716  static uint32_t g_q5_regs[] = {
717      46, 47, 48, 49,
718      LLDB_INVALID_REGNUM}; // (d10, d11) -> (s20, s21, s22, s23)
719  static uint32_t g_q6_regs[] = {
720      50, 51, 52, 53,
721      LLDB_INVALID_REGNUM}; // (d12, d13) -> (s24, s25, s26, s27)
722  static uint32_t g_q7_regs[] = {
723      54, 55, 56, 57,
724      LLDB_INVALID_REGNUM}; // (d14, d15) -> (s28, s29, s30, s31)
725  static uint32_t g_q8_regs[] = {59, 60, LLDB_INVALID_REGNUM};  // (d16, d17)
726  static uint32_t g_q9_regs[] = {61, 62, LLDB_INVALID_REGNUM};  // (d18, d19)
727  static uint32_t g_q10_regs[] = {63, 64, LLDB_INVALID_REGNUM}; // (d20, d21)
728  static uint32_t g_q11_regs[] = {65, 66, LLDB_INVALID_REGNUM}; // (d22, d23)
729  static uint32_t g_q12_regs[] = {67, 68, LLDB_INVALID_REGNUM}; // (d24, d25)
730  static uint32_t g_q13_regs[] = {69, 70, LLDB_INVALID_REGNUM}; // (d26, d27)
731  static uint32_t g_q14_regs[] = {71, 72, LLDB_INVALID_REGNUM}; // (d28, d29)
732  static uint32_t g_q15_regs[] = {73, 74, LLDB_INVALID_REGNUM}; // (d30, d31)
733
734  // This is our array of composite registers, with each element coming from
735  // the above register mappings.
736  static uint32_t *g_composites[] = {
737      g_d0_regs,  g_d1_regs,  g_d2_regs,  g_d3_regs,  g_d4_regs,  g_d5_regs,
738      g_d6_regs,  g_d7_regs,  g_d8_regs,  g_d9_regs,  g_d10_regs, g_d11_regs,
739      g_d12_regs, g_d13_regs, g_d14_regs, g_d15_regs, g_q0_regs,  g_q1_regs,
740      g_q2_regs,  g_q3_regs,  g_q4_regs,  g_q5_regs,  g_q6_regs,  g_q7_regs,
741      g_q8_regs,  g_q9_regs,  g_q10_regs, g_q11_regs, g_q12_regs, g_q13_regs,
742      g_q14_regs, g_q15_regs};
743
744  // clang-format off
745    static RegisterInfo g_register_infos[] = {
746//   NAME     ALT     SZ   OFF  ENCODING          FORMAT          EH_FRAME             DWARF                GENERIC                 PROCESS PLUGIN  LLDB    VALUE REGS    INVALIDATE REGS SIZE EXPR SIZE LEN
747//   ======   ======  ===  ===  =============     ==========      ===================  ===================  ======================  =============   ====    ==========    =============== ========= ========
748    { "r0",   "arg1",   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r0,          dwarf_r0,            LLDB_REGNUM_GENERIC_ARG1,0,               0 },     nullptr,           nullptr,  nullptr,       0 },
749    { "r1",   "arg2",   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r1,          dwarf_r1,            LLDB_REGNUM_GENERIC_ARG2,1,               1 },     nullptr,           nullptr,  nullptr,       0 },
750    { "r2",   "arg3",   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r2,          dwarf_r2,            LLDB_REGNUM_GENERIC_ARG3,2,               2 },     nullptr,           nullptr,  nullptr,       0 },
751    { "r3",   "arg4",   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r3,          dwarf_r3,            LLDB_REGNUM_GENERIC_ARG4,3,               3 },     nullptr,           nullptr,  nullptr,       0 },
752    { "r4",  nullptr,   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r4,          dwarf_r4,            LLDB_INVALID_REGNUM,     4,               4 },     nullptr,           nullptr,  nullptr,       0 },
753    { "r5",  nullptr,   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r5,          dwarf_r5,            LLDB_INVALID_REGNUM,     5,               5 },     nullptr,           nullptr,  nullptr,       0 },
754    { "r6",  nullptr,   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r6,          dwarf_r6,            LLDB_INVALID_REGNUM,     6,               6 },     nullptr,           nullptr,  nullptr,       0 },
755    { "r7",     "fp",   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r7,          dwarf_r7,            LLDB_REGNUM_GENERIC_FP,  7,               7 },     nullptr,           nullptr,  nullptr,       0 },
756    { "r8",  nullptr,   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r8,          dwarf_r8,            LLDB_INVALID_REGNUM,     8,               8 },     nullptr,           nullptr,  nullptr,       0 },
757    { "r9",  nullptr,   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r9,          dwarf_r9,            LLDB_INVALID_REGNUM,     9,               9 },     nullptr,           nullptr,  nullptr,       0 },
758    { "r10", nullptr,   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r10,         dwarf_r10,           LLDB_INVALID_REGNUM,    10,              10 },     nullptr,           nullptr,  nullptr,       0 },
759    { "r11", nullptr,   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r11,         dwarf_r11,           LLDB_INVALID_REGNUM,    11,              11 },     nullptr,           nullptr,  nullptr,       0 },
760    { "r12", nullptr,   4,   0, eEncodingUint,    eFormatHex,   { ehframe_r12,         dwarf_r12,           LLDB_INVALID_REGNUM,    12,              12 },     nullptr,           nullptr,  nullptr,       0 },
761    { "sp",     "r13",  4,   0, eEncodingUint,    eFormatHex,   { ehframe_sp,          dwarf_sp,            LLDB_REGNUM_GENERIC_SP, 13,              13 },     nullptr,           nullptr,  nullptr,       0 },
762    { "lr",     "r14",  4,   0, eEncodingUint,    eFormatHex,   { ehframe_lr,          dwarf_lr,            LLDB_REGNUM_GENERIC_RA, 14,              14 },     nullptr,           nullptr,  nullptr,       0 },
763    { "pc",     "r15",  4,   0, eEncodingUint,    eFormatHex,   { ehframe_pc,          dwarf_pc,            LLDB_REGNUM_GENERIC_PC, 15,              15 },     nullptr,           nullptr,  nullptr,       0 },
764    { "f0",  nullptr,  12,   0, eEncodingUint,    eFormatHex,   { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,    16,              16 },     nullptr,           nullptr,  nullptr,       0 },
765    { "f1",  nullptr,  12,   0, eEncodingUint,    eFormatHex,   { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,    17,              17 },     nullptr,           nullptr,  nullptr,       0 },
766    { "f2",  nullptr,  12,   0, eEncodingUint,    eFormatHex,   { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,    18,              18 },     nullptr,           nullptr,  nullptr,       0 },
767    { "f3",  nullptr,  12,   0, eEncodingUint,    eFormatHex,   { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,    19,              19 },     nullptr,           nullptr,  nullptr,       0 },
768    { "f4",  nullptr,  12,   0, eEncodingUint,    eFormatHex,   { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,    20,              20 },     nullptr,           nullptr,  nullptr,       0 },
769    { "f5",  nullptr,  12,   0, eEncodingUint,    eFormatHex,   { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,    21,              21 },     nullptr,           nullptr,  nullptr,       0 },
770    { "f6",  nullptr,  12,   0, eEncodingUint,    eFormatHex,   { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,    22,              22 },     nullptr,           nullptr,  nullptr,       0 },
771    { "f7",  nullptr,  12,   0, eEncodingUint,    eFormatHex,   { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,    23,              23 },     nullptr,           nullptr,  nullptr,       0 },
772    { "fps", nullptr,   4,   0, eEncodingUint,    eFormatHex,   { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,    24,              24 },     nullptr,           nullptr,  nullptr,       0 },
773    { "cpsr","flags",   4,   0, eEncodingUint,    eFormatHex,   { ehframe_cpsr,        dwarf_cpsr,          LLDB_INVALID_REGNUM,    25,              25 },     nullptr,           nullptr,  nullptr,       0 },
774    { "s0",  nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s0,            LLDB_INVALID_REGNUM,    26,              26 },     nullptr,           nullptr,  nullptr,       0 },
775    { "s1",  nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s1,            LLDB_INVALID_REGNUM,    27,              27 },     nullptr,           nullptr,  nullptr,       0 },
776    { "s2",  nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s2,            LLDB_INVALID_REGNUM,    28,              28 },     nullptr,           nullptr,  nullptr,       0 },
777    { "s3",  nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s3,            LLDB_INVALID_REGNUM,    29,              29 },     nullptr,           nullptr,  nullptr,       0 },
778    { "s4",  nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s4,            LLDB_INVALID_REGNUM,    30,              30 },     nullptr,           nullptr,  nullptr,       0 },
779    { "s5",  nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s5,            LLDB_INVALID_REGNUM,    31,              31 },     nullptr,           nullptr,  nullptr,       0 },
780    { "s6",  nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s6,            LLDB_INVALID_REGNUM,    32,              32 },     nullptr,           nullptr,  nullptr,       0 },
781    { "s7",  nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s7,            LLDB_INVALID_REGNUM,    33,              33 },     nullptr,           nullptr,  nullptr,       0 },
782    { "s8",  nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s8,            LLDB_INVALID_REGNUM,    34,              34 },     nullptr,           nullptr,  nullptr,       0 },
783    { "s9",  nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s9,            LLDB_INVALID_REGNUM,    35,              35 },     nullptr,           nullptr,  nullptr,       0 },
784    { "s10", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s10,           LLDB_INVALID_REGNUM,    36,              36 },     nullptr,           nullptr,  nullptr,       0 },
785    { "s11", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s11,           LLDB_INVALID_REGNUM,    37,              37 },     nullptr,           nullptr,  nullptr,       0 },
786    { "s12", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s12,           LLDB_INVALID_REGNUM,    38,              38 },     nullptr,           nullptr,  nullptr,       0 },
787    { "s13", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s13,           LLDB_INVALID_REGNUM,    39,              39 },     nullptr,           nullptr,  nullptr,       0 },
788    { "s14", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s14,           LLDB_INVALID_REGNUM,    40,              40 },     nullptr,           nullptr,  nullptr,       0 },
789    { "s15", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s15,           LLDB_INVALID_REGNUM,    41,              41 },     nullptr,           nullptr,  nullptr,       0 },
790    { "s16", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s16,           LLDB_INVALID_REGNUM,    42,              42 },     nullptr,           nullptr,  nullptr,       0 },
791    { "s17", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s17,           LLDB_INVALID_REGNUM,    43,              43 },     nullptr,           nullptr,  nullptr,       0 },
792    { "s18", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s18,           LLDB_INVALID_REGNUM,    44,              44 },     nullptr,           nullptr,  nullptr,       0 },
793    { "s19", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s19,           LLDB_INVALID_REGNUM,    45,              45 },     nullptr,           nullptr,  nullptr,       0 },
794    { "s20", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s20,           LLDB_INVALID_REGNUM,    46,              46 },     nullptr,           nullptr,  nullptr,       0 },
795    { "s21", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s21,           LLDB_INVALID_REGNUM,    47,              47 },     nullptr,           nullptr,  nullptr,       0 },
796    { "s22", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s22,           LLDB_INVALID_REGNUM,    48,              48 },     nullptr,           nullptr,  nullptr,       0 },
797    { "s23", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s23,           LLDB_INVALID_REGNUM,    49,              49 },     nullptr,           nullptr,  nullptr,       0 },
798    { "s24", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s24,           LLDB_INVALID_REGNUM,    50,              50 },     nullptr,           nullptr,  nullptr,       0 },
799    { "s25", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s25,           LLDB_INVALID_REGNUM,    51,              51 },     nullptr,           nullptr,  nullptr,       0 },
800    { "s26", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s26,           LLDB_INVALID_REGNUM,    52,              52 },     nullptr,           nullptr,  nullptr,       0 },
801    { "s27", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s27,           LLDB_INVALID_REGNUM,    53,              53 },     nullptr,           nullptr,  nullptr,       0 },
802    { "s28", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s28,           LLDB_INVALID_REGNUM,    54,              54 },     nullptr,           nullptr,  nullptr,       0 },
803    { "s29", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s29,           LLDB_INVALID_REGNUM,    55,              55 },     nullptr,           nullptr,  nullptr,       0 },
804    { "s30", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s30,           LLDB_INVALID_REGNUM,    56,              56 },     nullptr,           nullptr,  nullptr,       0 },
805    { "s31", nullptr,   4,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_s31,           LLDB_INVALID_REGNUM,    57,              57 },     nullptr,           nullptr,  nullptr,       0 },
806    { "fpscr",nullptr,  4,   0, eEncodingUint,    eFormatHex,   { LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,    58,              58 },     nullptr,           nullptr,  nullptr,       0 },
807    { "d16", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d16,           LLDB_INVALID_REGNUM,    59,              59 },     nullptr,           nullptr,  nullptr,       0 },
808    { "d17", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d17,           LLDB_INVALID_REGNUM,    60,              60 },     nullptr,           nullptr,  nullptr,       0 },
809    { "d18", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d18,           LLDB_INVALID_REGNUM,    61,              61 },     nullptr,           nullptr,  nullptr,       0 },
810    { "d19", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d19,           LLDB_INVALID_REGNUM,    62,              62 },     nullptr,           nullptr,  nullptr,       0 },
811    { "d20", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d20,           LLDB_INVALID_REGNUM,    63,              63 },     nullptr,           nullptr,  nullptr,       0 },
812    { "d21", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d21,           LLDB_INVALID_REGNUM,    64,              64 },     nullptr,           nullptr,  nullptr,       0 },
813    { "d22", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d22,           LLDB_INVALID_REGNUM,    65,              65 },     nullptr,           nullptr,  nullptr,       0 },
814    { "d23", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d23,           LLDB_INVALID_REGNUM,    66,              66 },     nullptr,           nullptr,  nullptr,       0 },
815    { "d24", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d24,           LLDB_INVALID_REGNUM,    67,              67 },     nullptr,           nullptr,  nullptr,       0 },
816    { "d25", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d25,           LLDB_INVALID_REGNUM,    68,              68 },     nullptr,           nullptr,  nullptr,       0 },
817    { "d26", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d26,           LLDB_INVALID_REGNUM,    69,              69 },     nullptr,           nullptr,  nullptr,       0 },
818    { "d27", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d27,           LLDB_INVALID_REGNUM,    70,              70 },     nullptr,           nullptr,  nullptr,       0 },
819    { "d28", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d28,           LLDB_INVALID_REGNUM,    71,              71 },     nullptr,           nullptr,  nullptr,       0 },
820    { "d29", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d29,           LLDB_INVALID_REGNUM,    72,              72 },     nullptr,           nullptr,  nullptr,       0 },
821    { "d30", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d30,           LLDB_INVALID_REGNUM,    73,              73 },     nullptr,           nullptr,  nullptr,       0 },
822    { "d31", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d31,           LLDB_INVALID_REGNUM,    74,              74 },     nullptr,           nullptr,  nullptr,       0 },
823    { "d0",  nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d0,            LLDB_INVALID_REGNUM,    75,              75 },   g_d0_regs,           nullptr,  nullptr,       0 },
824    { "d1",  nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d1,            LLDB_INVALID_REGNUM,    76,              76 },   g_d1_regs,           nullptr,  nullptr,       0 },
825    { "d2",  nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d2,            LLDB_INVALID_REGNUM,    77,              77 },   g_d2_regs,           nullptr,  nullptr,       0 },
826    { "d3",  nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d3,            LLDB_INVALID_REGNUM,    78,              78 },   g_d3_regs,           nullptr,  nullptr,       0 },
827    { "d4",  nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d4,            LLDB_INVALID_REGNUM,    79,              79 },   g_d4_regs,           nullptr,  nullptr,       0 },
828    { "d5",  nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d5,            LLDB_INVALID_REGNUM,    80,              80 },   g_d5_regs,           nullptr,  nullptr,       0 },
829    { "d6",  nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d6,            LLDB_INVALID_REGNUM,    81,              81 },   g_d6_regs,           nullptr,  nullptr,       0 },
830    { "d7",  nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d7,            LLDB_INVALID_REGNUM,    82,              82 },   g_d7_regs,           nullptr,  nullptr,       0 },
831    { "d8",  nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d8,            LLDB_INVALID_REGNUM,    83,              83 },   g_d8_regs,           nullptr,  nullptr,       0 },
832    { "d9",  nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d9,            LLDB_INVALID_REGNUM,    84,              84 },   g_d9_regs,           nullptr,  nullptr,       0 },
833    { "d10", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d10,           LLDB_INVALID_REGNUM,    85,              85 },  g_d10_regs,           nullptr,  nullptr,       0 },
834    { "d11", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d11,           LLDB_INVALID_REGNUM,    86,              86 },  g_d11_regs,           nullptr,  nullptr,       0 },
835    { "d12", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d12,           LLDB_INVALID_REGNUM,    87,              87 },  g_d12_regs,           nullptr,  nullptr,       0 },
836    { "d13", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d13,           LLDB_INVALID_REGNUM,    88,              88 },  g_d13_regs,           nullptr,  nullptr,       0 },
837    { "d14", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d14,           LLDB_INVALID_REGNUM,    89,              89 },  g_d14_regs,           nullptr,  nullptr,       0 },
838    { "d15", nullptr,   8,   0, eEncodingIEEE754, eFormatFloat, { LLDB_INVALID_REGNUM, dwarf_d15,           LLDB_INVALID_REGNUM,    90,              90 },  g_d15_regs,           nullptr,  nullptr,       0 },
839    { "q0",  nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q0,    LLDB_INVALID_REGNUM,    91,              91 },   g_q0_regs,           nullptr,  nullptr,       0 },
840    { "q1",  nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q1,    LLDB_INVALID_REGNUM,    92,              92 },   g_q1_regs,           nullptr,  nullptr,       0 },
841    { "q2",  nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q2,    LLDB_INVALID_REGNUM,    93,              93 },   g_q2_regs,           nullptr,  nullptr,       0 },
842    { "q3",  nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q3,    LLDB_INVALID_REGNUM,    94,              94 },   g_q3_regs,           nullptr,  nullptr,       0 },
843    { "q4",  nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q4,    LLDB_INVALID_REGNUM,    95,              95 },   g_q4_regs,           nullptr,  nullptr,       0 },
844    { "q5",  nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q5,    LLDB_INVALID_REGNUM,    96,              96 },   g_q5_regs,           nullptr,  nullptr,       0 },
845    { "q6",  nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q6,    LLDB_INVALID_REGNUM,    97,              97 },   g_q6_regs,           nullptr,  nullptr,       0 },
846    { "q7",  nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q7,    LLDB_INVALID_REGNUM,    98,              98 },   g_q7_regs,           nullptr,  nullptr,       0 },
847    { "q8",  nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q8,    LLDB_INVALID_REGNUM,    99,              99 },   g_q8_regs,           nullptr,  nullptr,       0 },
848    { "q9",  nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q9,    LLDB_INVALID_REGNUM,   100,             100 },   g_q9_regs,           nullptr,  nullptr,       0 },
849    { "q10", nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q10,   LLDB_INVALID_REGNUM,   101,             101 },  g_q10_regs,           nullptr,  nullptr,       0 },
850    { "q11", nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q11,   LLDB_INVALID_REGNUM,   102,             102 },  g_q11_regs,           nullptr,  nullptr,       0 },
851    { "q12", nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q12,   LLDB_INVALID_REGNUM,   103,             103 },  g_q12_regs,           nullptr,  nullptr,       0 },
852    { "q13", nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q13,   LLDB_INVALID_REGNUM,   104,             104 },  g_q13_regs,           nullptr,  nullptr,       0 },
853    { "q14", nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q14,   LLDB_INVALID_REGNUM,   105,             105 },  g_q14_regs,           nullptr,  nullptr,       0 },
854    { "q15", nullptr,   16,  0, eEncodingVector,  eFormatVectorOfUInt8, { LLDB_INVALID_REGNUM, dwarf_q15,   LLDB_INVALID_REGNUM,   106,             106 },  g_q15_regs,           nullptr,  nullptr,       0 }
855    };
856  // clang-format on
857
858  static const uint32_t num_registers = llvm::array_lengthof(g_register_infos);
859  static ConstString gpr_reg_set("General Purpose Registers");
860  static ConstString sfp_reg_set("Software Floating Point Registers");
861  static ConstString vfp_reg_set("Floating Point Registers");
862  size_t i;
863  if (from_scratch) {
864    // Calculate the offsets of the registers
865    // Note that the layout of the "composite" registers (d0-d15 and q0-q15)
866    // which comes after the "primordial" registers is important.  This enables
867    // us to calculate the offset of the composite register by using the offset
868    // of its first primordial register.  For example, to calculate the offset
869    // of q0, use s0's offset.
870    if (g_register_infos[2].byte_offset == 0) {
871      uint32_t byte_offset = 0;
872      for (i = 0; i < num_registers; ++i) {
873        // For primordial registers, increment the byte_offset by the byte_size
874        // to arrive at the byte_offset for the next register.  Otherwise, we
875        // have a composite register whose offset can be calculated by
876        // consulting the offset of its first primordial register.
877        if (!g_register_infos[i].value_regs) {
878          g_register_infos[i].byte_offset = byte_offset;
879          byte_offset += g_register_infos[i].byte_size;
880        } else {
881          const uint32_t first_primordial_reg =
882              g_register_infos[i].value_regs[0];
883          g_register_infos[i].byte_offset =
884              g_register_infos[first_primordial_reg].byte_offset;
885        }
886      }
887    }
888    for (i = 0; i < num_registers; ++i) {
889      ConstString name;
890      ConstString alt_name;
891      if (g_register_infos[i].name && g_register_infos[i].name[0])
892        name.SetCString(g_register_infos[i].name);
893      if (g_register_infos[i].alt_name && g_register_infos[i].alt_name[0])
894        alt_name.SetCString(g_register_infos[i].alt_name);
895
896      if (i <= 15 || i == 25)
897        AddRegister(g_register_infos[i], name, alt_name, gpr_reg_set);
898      else if (i <= 24)
899        AddRegister(g_register_infos[i], name, alt_name, sfp_reg_set);
900      else
901        AddRegister(g_register_infos[i], name, alt_name, vfp_reg_set);
902    }
903  } else {
904    // Add composite registers to our primordial registers, then.
905    const size_t num_composites = llvm::array_lengthof(g_composites);
906    const size_t num_dynamic_regs = GetNumRegisters();
907    const size_t num_common_regs = num_registers - num_composites;
908    RegisterInfo *g_comp_register_infos = g_register_infos + num_common_regs;
909
910    // First we need to validate that all registers that we already have match
911    // the non composite regs. If so, then we can add the registers, else we
912    // need to bail
913    bool match = true;
914    if (num_dynamic_regs == num_common_regs) {
915      for (i = 0; match && i < num_dynamic_regs; ++i) {
916        // Make sure all register names match
917        if (m_regs[i].name && g_register_infos[i].name) {
918          if (strcmp(m_regs[i].name, g_register_infos[i].name)) {
919            match = false;
920            break;
921          }
922        }
923
924        // Make sure all register byte sizes match
925        if (m_regs[i].byte_size != g_register_infos[i].byte_size) {
926          match = false;
927          break;
928        }
929      }
930    } else {
931      // Wrong number of registers.
932      match = false;
933    }
934    // If "match" is true, then we can add extra registers.
935    if (match) {
936      for (i = 0; i < num_composites; ++i) {
937        ConstString name;
938        ConstString alt_name;
939        const uint32_t first_primordial_reg =
940            g_comp_register_infos[i].value_regs[0];
941        const char *reg_name = g_register_infos[first_primordial_reg].name;
942        if (reg_name && reg_name[0]) {
943          for (uint32_t j = 0; j < num_dynamic_regs; ++j) {
944            const RegisterInfo *reg_info = GetRegisterInfoAtIndex(j);
945            // Find a matching primordial register info entry.
946            if (reg_info && reg_info->name &&
947                ::strcasecmp(reg_info->name, reg_name) == 0) {
948              // The name matches the existing primordial entry. Find and
949              // assign the offset, and then add this composite register entry.
950              g_comp_register_infos[i].byte_offset = reg_info->byte_offset;
951              name.SetCString(g_comp_register_infos[i].name);
952              AddRegister(g_comp_register_infos[i], name, alt_name,
953                          vfp_reg_set);
954            }
955          }
956        }
957      }
958    }
959  }
960}
961