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