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