1 //===-- NativeRegisterContext.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 "lldb/Host/common/NativeRegisterContext.h"
11 
12 #include "lldb/Core/Log.h"
13 #include "lldb/Core/RegisterValue.h"
14 
15 #include "lldb/Host/common/NativeProcessProtocol.h"
16 #include "lldb/Host/common/NativeThreadProtocol.h"
17 #include "lldb/Host/PosixApi.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 NativeRegisterContext::NativeRegisterContext (NativeThreadProtocol &thread, uint32_t concrete_frame_idx) :
23     m_thread (thread),
24     m_concrete_frame_idx (concrete_frame_idx)
25 {
26 }
27 
28 //----------------------------------------------------------------------
29 // Destructor
30 //----------------------------------------------------------------------
31 NativeRegisterContext::~NativeRegisterContext()
32 {
33 }
34 
35 // FIXME revisit invalidation, process stop ids, etc.  Right now we don't
36 // support caching in NativeRegisterContext.  We can do this later by
37 // utilizing NativeProcessProtocol::GetStopID () and adding a stop id to
38 // NativeRegisterContext.
39 
40 // void
41 // NativeRegisterContext::InvalidateIfNeeded (bool force)
42 // {
43 //     ProcessSP process_sp (m_thread.GetProcess());
44 //     bool invalidate = force;
45 //     uint32_t process_stop_id = UINT32_MAX;
46 
47 //     if (process_sp)
48 //         process_stop_id = process_sp->GetStopID();
49 //     else
50 //         invalidate = true;
51 
52 //     if (!invalidate)
53 //         invalidate = process_stop_id != GetStopID();
54 
55 //     if (invalidate)
56 //     {
57 //         InvalidateAllRegisters ();
58 //         SetStopID (process_stop_id);
59 //     }
60 // }
61 
62 
63 const RegisterInfo *
64 NativeRegisterContext::GetRegisterInfoByName (const char *reg_name, uint32_t start_idx)
65 {
66     if (reg_name && reg_name[0])
67     {
68         const uint32_t num_registers = GetRegisterCount();
69         for (uint32_t reg = start_idx; reg < num_registers; ++reg)
70         {
71             const RegisterInfo * reg_info = GetRegisterInfoAtIndex(reg);
72 
73             if ((reg_info->name != nullptr && ::strcasecmp (reg_info->name, reg_name) == 0) ||
74                 (reg_info->alt_name != nullptr && ::strcasecmp (reg_info->alt_name, reg_name) == 0))
75             {
76                 return reg_info;
77             }
78         }
79     }
80     return nullptr;
81 }
82 
83 const RegisterInfo *
84 NativeRegisterContext::GetRegisterInfo (uint32_t kind, uint32_t num)
85 {
86     const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
87     if (reg_num == LLDB_INVALID_REGNUM)
88         return nullptr;
89     return GetRegisterInfoAtIndex (reg_num);
90 }
91 
92 const char *
93 NativeRegisterContext::GetRegisterName (uint32_t reg)
94 {
95     const RegisterInfo * reg_info = GetRegisterInfoAtIndex(reg);
96     if (reg_info)
97         return reg_info->name;
98     return nullptr;
99 }
100 
101 const char*
102 NativeRegisterContext::GetRegisterSetNameForRegisterAtIndex (uint32_t reg_index) const
103 {
104     const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
105     if (!reg_info)
106         return nullptr;
107 
108     for (uint32_t set_index = 0; set_index < GetRegisterSetCount (); ++set_index)
109     {
110         const RegisterSet *const reg_set = GetRegisterSet (set_index);
111         if (!reg_set)
112             continue;
113 
114         for (uint32_t reg_num_index = 0; reg_num_index < reg_set->num_registers; ++reg_num_index)
115         {
116             const uint32_t reg_num = reg_set->registers[reg_num_index];
117             // FIXME double check we're checking the right register kind here.
118             if (reg_info->kinds[RegisterKind::eRegisterKindLLDB] == reg_num)
119             {
120                 // The given register is a member of this register set.  Return the register set name.
121                 return reg_set->name;
122             }
123         }
124     }
125 
126     // Didn't find it.
127     return nullptr;
128 }
129 
130 lldb::addr_t
131 NativeRegisterContext::GetPC (lldb::addr_t fail_value)
132 {
133     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
134 
135     uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
136     if (log)
137         log->Printf ("NativeRegisterContext::%s using reg index %" PRIu32 " (default %" PRIu64 ")", __FUNCTION__, reg, fail_value);
138 
139     const uint64_t retval = ReadRegisterAsUnsigned (reg, fail_value);
140 
141     if (log)
142         log->Printf ("NativeRegisterContext::%s " PRIu32 " retval %" PRIu64, __FUNCTION__, retval);
143 
144     return retval;
145 }
146 
147 lldb::addr_t
148 NativeRegisterContext::GetPCfromBreakpointLocation (lldb::addr_t fail_value)
149 {
150     return GetPC (fail_value);
151 }
152 
153 Error
154 NativeRegisterContext::SetPC (lldb::addr_t pc)
155 {
156     uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
157     return WriteRegisterFromUnsigned (reg, pc);
158 }
159 
160 lldb::addr_t
161 NativeRegisterContext::GetSP (lldb::addr_t fail_value)
162 {
163     uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
164     return ReadRegisterAsUnsigned (reg, fail_value);
165 }
166 
167 Error
168 NativeRegisterContext::SetSP (lldb::addr_t sp)
169 {
170     uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
171     return WriteRegisterFromUnsigned (reg, sp);
172 }
173 
174 lldb::addr_t
175 NativeRegisterContext::GetFP (lldb::addr_t fail_value)
176 {
177     uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP);
178     return ReadRegisterAsUnsigned (reg, fail_value);
179 }
180 
181 Error
182 NativeRegisterContext::SetFP (lldb::addr_t fp)
183 {
184     uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP);
185     return WriteRegisterFromUnsigned (reg, fp);
186 }
187 
188 lldb::addr_t
189 NativeRegisterContext::GetReturnAddress (lldb::addr_t fail_value)
190 {
191     uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);
192     return ReadRegisterAsUnsigned (reg, fail_value);
193 }
194 
195 lldb::addr_t
196 NativeRegisterContext::GetFlags (lldb::addr_t fail_value)
197 {
198     uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS);
199     return ReadRegisterAsUnsigned (reg, fail_value);
200 }
201 
202 
203 lldb::addr_t
204 NativeRegisterContext::ReadRegisterAsUnsigned (uint32_t reg, lldb::addr_t fail_value)
205 {
206     if (reg != LLDB_INVALID_REGNUM)
207         return ReadRegisterAsUnsigned (GetRegisterInfoAtIndex (reg), fail_value);
208     return fail_value;
209 }
210 
211 uint64_t
212 NativeRegisterContext::ReadRegisterAsUnsigned (const RegisterInfo *reg_info, lldb::addr_t fail_value)
213 {
214     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
215 
216     if (reg_info)
217     {
218         RegisterValue value;
219         Error error = ReadRegister (reg_info, value);
220         if (error.Success ())
221         {
222             if (log)
223                 log->Printf ("NativeRegisterContext::%s ReadRegister() succeeded, value %" PRIu64, __FUNCTION__, value.GetAsUInt64());
224             return value.GetAsUInt64();
225         }
226         else
227         {
228             if (log)
229                 log->Printf ("NativeRegisterContext::%s ReadRegister() failed, error %s", __FUNCTION__, error.AsCString ());
230         }
231     }
232     else
233     {
234         if (log)
235             log->Printf ("NativeRegisterContext::%s ReadRegister() null reg_info", __FUNCTION__);
236     }
237     return fail_value;
238 }
239 
240 Error
241 NativeRegisterContext::WriteRegisterFromUnsigned (uint32_t reg, uint64_t uval)
242 {
243     if (reg == LLDB_INVALID_REGNUM)
244         return Error ("NativeRegisterContext::%s (): reg is invalid", __FUNCTION__);
245     return WriteRegisterFromUnsigned (GetRegisterInfoAtIndex (reg), uval);
246 }
247 
248 Error
249 NativeRegisterContext::WriteRegisterFromUnsigned (const RegisterInfo *reg_info, uint64_t uval)
250 {
251     assert (reg_info);
252     if (!reg_info)
253         return Error ("reg_info is nullptr");
254 
255     RegisterValue value;
256     if (!value.SetUInt(uval, reg_info->byte_size))
257         return Error ("RegisterValue::SetUInt () failed");
258 
259     return WriteRegister (reg_info, value);
260 }
261 
262 lldb::tid_t
263 NativeRegisterContext::GetThreadID() const
264 {
265     return m_thread.GetID();
266 }
267 
268 uint32_t
269 NativeRegisterContext::NumSupportedHardwareBreakpoints ()
270 {
271     return 0;
272 }
273 
274 uint32_t
275 NativeRegisterContext::SetHardwareBreakpoint (lldb::addr_t addr, size_t size)
276 {
277     return LLDB_INVALID_INDEX32;
278 }
279 
280 bool
281 NativeRegisterContext::ClearHardwareBreakpoint (uint32_t hw_idx)
282 {
283     return false;
284 }
285 
286 
287 uint32_t
288 NativeRegisterContext::NumSupportedHardwareWatchpoints ()
289 {
290     return 0;
291 }
292 
293 uint32_t
294 NativeRegisterContext::SetHardwareWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags)
295 {
296     return LLDB_INVALID_INDEX32;
297 }
298 
299 bool
300 NativeRegisterContext::ClearHardwareWatchpoint (uint32_t hw_index)
301 {
302     return false;
303 }
304 
305 Error
306 NativeRegisterContext::ClearAllHardwareWatchpoints ()
307 {
308     return Error ("not implemented");
309 }
310 
311 Error
312 NativeRegisterContext::IsWatchpointHit(uint32_t wp_index, bool &is_hit)
313 {
314     is_hit = false;
315     return Error ("not implemented");
316 }
317 
318 Error
319 NativeRegisterContext::GetWatchpointHitIndex(uint32_t &wp_index, lldb::addr_t trap_addr)
320 {
321     wp_index = LLDB_INVALID_INDEX32;
322     return Error ("not implemented");
323 }
324 
325 Error
326 NativeRegisterContext::IsWatchpointVacant (uint32_t wp_index, bool &is_vacant)
327 {
328     is_vacant = false;
329     return Error ("not implemented");
330 }
331 
332 lldb::addr_t
333 NativeRegisterContext::GetWatchpointAddress (uint32_t wp_index)
334 {
335     return LLDB_INVALID_ADDRESS;
336 }
337 
338 lldb::addr_t
339 NativeRegisterContext::GetWatchpointHitAddress (uint32_t wp_index)
340 {
341     return LLDB_INVALID_ADDRESS;
342 }
343 
344 bool
345 NativeRegisterContext::HardwareSingleStep (bool enable)
346 {
347     return false;
348 }
349 
350 Error
351 NativeRegisterContext::ReadRegisterValueFromMemory (
352     const RegisterInfo *reg_info,
353     lldb::addr_t src_addr,
354     size_t src_len,
355     RegisterValue &reg_value)
356 {
357     Error error;
358     if (reg_info == nullptr)
359     {
360         error.SetErrorString ("invalid register info argument.");
361         return error;
362     }
363 
364 
365     // Moving from addr into a register
366     //
367     // Case 1: src_len == dst_len
368     //
369     //   |AABBCCDD| Address contents
370     //   |AABBCCDD| Register contents
371     //
372     // Case 2: src_len > dst_len
373     //
374     //   Error!  (The register should always be big enough to hold the data)
375     //
376     // Case 3: src_len < dst_len
377     //
378     //   |AABB| Address contents
379     //   |AABB0000| Register contents [on little-endian hardware]
380     //   |0000AABB| Register contents [on big-endian hardware]
381     if (src_len > RegisterValue::kMaxRegisterByteSize)
382     {
383         error.SetErrorString ("register too small to receive memory data");
384         return error;
385     }
386 
387     const size_t dst_len = reg_info->byte_size;
388 
389     if (src_len > dst_len)
390     {
391         error.SetErrorStringWithFormat("%" PRIu64 " bytes is too big to store in register %s (%" PRIu64 " bytes)",
392                 static_cast<uint64_t>(src_len), reg_info->name, static_cast<uint64_t>(dst_len));
393         return error;
394     }
395 
396     NativeProcessProtocolSP process_sp (m_thread.GetProcess ());
397     if (!process_sp)
398     {
399         error.SetErrorString("invalid process");
400         return error;
401     }
402 
403     uint8_t src[RegisterValue::kMaxRegisterByteSize];
404 
405     // Read the memory
406     size_t bytes_read;
407     error = process_sp->ReadMemory (src_addr, src, src_len, bytes_read);
408     if (error.Fail ())
409         return error;
410 
411     // Make sure the memory read succeeded...
412     if (bytes_read != src_len)
413     {
414         // This might happen if we read _some_ bytes but not all
415         error.SetErrorStringWithFormat("read %" PRIu64 " of %" PRIu64 " bytes",
416                 static_cast<uint64_t>(bytes_read), static_cast<uint64_t>(src_len));
417         return error;
418     }
419 
420     // We now have a memory buffer that contains the part or all of the register
421     // value. Set the register value using this memory data.
422     // TODO: we might need to add a parameter to this function in case the byte
423     // order of the memory data doesn't match the process. For now we are assuming
424     // they are the same.
425     lldb::ByteOrder byte_order;
426     if (!process_sp->GetByteOrder (byte_order))
427     {
428         error.SetErrorString ( "NativeProcessProtocol::GetByteOrder () failed");
429         return error;
430     }
431 
432     reg_value.SetFromMemoryData (
433         reg_info,
434         src,
435         src_len,
436         byte_order,
437         error);
438 
439     return error;
440 }
441 
442 Error
443 NativeRegisterContext::WriteRegisterValueToMemory (
444     const RegisterInfo *reg_info,
445     lldb::addr_t dst_addr,
446     size_t dst_len,
447     const RegisterValue &reg_value)
448 {
449 
450     uint8_t dst[RegisterValue::kMaxRegisterByteSize];
451 
452     Error error;
453 
454     NativeProcessProtocolSP process_sp (m_thread.GetProcess ());
455     if (process_sp)
456     {
457 
458         // TODO: we might need to add a parameter to this function in case the byte
459         // order of the memory data doesn't match the process. For now we are assuming
460         // they are the same.
461         lldb::ByteOrder byte_order;
462         if (!process_sp->GetByteOrder (byte_order))
463             return Error ("NativeProcessProtocol::GetByteOrder () failed");
464 
465         const size_t bytes_copied = reg_value.GetAsMemoryData (
466             reg_info,
467             dst,
468             dst_len,
469             byte_order,
470             error);
471 
472         if (error.Success())
473         {
474             if (bytes_copied == 0)
475             {
476                 error.SetErrorString("byte copy failed.");
477             }
478             else
479             {
480                 size_t bytes_written;
481                 error = process_sp->WriteMemory(dst_addr, dst, bytes_copied, bytes_written);
482                 if (error.Fail ())
483                     return error;
484 
485                 if (bytes_written != bytes_copied)
486                 {
487                     // This might happen if we read _some_ bytes but not all
488                     error.SetErrorStringWithFormat("only wrote %" PRIu64 " of %" PRIu64 " bytes",
489                             static_cast<uint64_t>(bytes_written), static_cast<uint64_t>(bytes_copied));
490                 }
491             }
492         }
493     }
494     else
495         error.SetErrorString("invalid process");
496 
497     return error;
498 }
499 
500 uint32_t
501 NativeRegisterContext::ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num) const
502 {
503     const uint32_t num_regs = GetRegisterCount();
504 
505     assert (kind < kNumRegisterKinds);
506     for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx)
507     {
508         const RegisterInfo *reg_info = GetRegisterInfoAtIndex (reg_idx);
509 
510         if (reg_info->kinds[kind] == num)
511             return reg_idx;
512     }
513 
514     return LLDB_INVALID_REGNUM;
515 }
516 
517 
518