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