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