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