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