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