1 //===-- RegisterContext.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 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "lldb/Target/RegisterContext.h"
15 #include "lldb/Core/DataExtractor.h"
16 #include "lldb/Core/RegisterValue.h"
17 #include "lldb/Core/Scalar.h"
18 #include "lldb/Host/Endian.h"
19 #include "lldb/Target/ExecutionContext.h"
20 #include "lldb/Target/StackFrame.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/Thread.h"
23
24 using namespace lldb;
25 using namespace lldb_private;
26
RegisterContext(Thread & thread,uint32_t concrete_frame_idx)27 RegisterContext::RegisterContext (Thread &thread, uint32_t concrete_frame_idx) :
28 m_thread (thread),
29 m_concrete_frame_idx (concrete_frame_idx),
30 m_stop_id (thread.GetProcess()->GetStopID())
31 {
32 }
33
34 //----------------------------------------------------------------------
35 // Destructor
36 //----------------------------------------------------------------------
~RegisterContext()37 RegisterContext::~RegisterContext()
38 {
39 }
40
41 void
InvalidateIfNeeded(bool force)42 RegisterContext::InvalidateIfNeeded (bool force)
43 {
44 ProcessSP process_sp (m_thread.GetProcess());
45 bool invalidate = force;
46 uint32_t process_stop_id = UINT32_MAX;
47
48 if (process_sp)
49 process_stop_id = process_sp->GetStopID();
50 else
51 invalidate = true;
52
53 if (!invalidate)
54 invalidate = process_stop_id != GetStopID();
55
56 if (invalidate)
57 {
58 InvalidateAllRegisters ();
59 SetStopID (process_stop_id);
60 }
61 }
62
63
64 const RegisterInfo *
GetRegisterInfoByName(const char * reg_name,uint32_t start_idx)65 RegisterContext::GetRegisterInfoByName (const char *reg_name, uint32_t start_idx)
66 {
67 if (reg_name && reg_name[0])
68 {
69 const uint32_t num_registers = GetRegisterCount();
70 for (uint32_t reg = start_idx; reg < num_registers; ++reg)
71 {
72 const RegisterInfo * reg_info = GetRegisterInfoAtIndex(reg);
73
74 if ((reg_info->name != NULL && ::strcasecmp (reg_info->name, reg_name) == 0) ||
75 (reg_info->alt_name != NULL && ::strcasecmp (reg_info->alt_name, reg_name) == 0))
76 {
77 return reg_info;
78 }
79 }
80 }
81 return NULL;
82 }
83
84 const char *
GetRegisterName(uint32_t reg)85 RegisterContext::GetRegisterName (uint32_t reg)
86 {
87 const RegisterInfo * reg_info = GetRegisterInfoAtIndex(reg);
88 if (reg_info)
89 return reg_info->name;
90 return NULL;
91 }
92
93 uint64_t
GetPC(uint64_t fail_value)94 RegisterContext::GetPC(uint64_t fail_value)
95 {
96 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
97 return ReadRegisterAsUnsigned (reg, fail_value);
98 }
99
100 bool
SetPC(uint64_t pc)101 RegisterContext::SetPC(uint64_t pc)
102 {
103 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
104 bool success = WriteRegisterFromUnsigned (reg, pc);
105 if (success)
106 {
107 StackFrameSP frame_sp(m_thread.GetFrameWithConcreteFrameIndex (m_concrete_frame_idx));
108 if (frame_sp)
109 frame_sp->ChangePC(pc);
110 else
111 m_thread.ClearStackFrames ();
112 }
113 return success;
114 }
115
116 uint64_t
GetSP(uint64_t fail_value)117 RegisterContext::GetSP(uint64_t fail_value)
118 {
119 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
120 return ReadRegisterAsUnsigned (reg, fail_value);
121 }
122
123 bool
SetSP(uint64_t sp)124 RegisterContext::SetSP(uint64_t sp)
125 {
126 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
127 return WriteRegisterFromUnsigned (reg, sp);
128 }
129
130 uint64_t
GetFP(uint64_t fail_value)131 RegisterContext::GetFP(uint64_t fail_value)
132 {
133 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP);
134 return ReadRegisterAsUnsigned (reg, fail_value);
135 }
136
137 bool
SetFP(uint64_t fp)138 RegisterContext::SetFP(uint64_t fp)
139 {
140 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP);
141 return WriteRegisterFromUnsigned (reg, fp);
142 }
143
144 uint64_t
GetReturnAddress(uint64_t fail_value)145 RegisterContext::GetReturnAddress (uint64_t fail_value)
146 {
147 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);
148 return ReadRegisterAsUnsigned (reg, fail_value);
149 }
150
151 uint64_t
GetFlags(uint64_t fail_value)152 RegisterContext::GetFlags (uint64_t fail_value)
153 {
154 uint32_t reg = ConvertRegisterKindToRegisterNumber (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS);
155 return ReadRegisterAsUnsigned (reg, fail_value);
156 }
157
158
159 uint64_t
ReadRegisterAsUnsigned(uint32_t reg,uint64_t fail_value)160 RegisterContext::ReadRegisterAsUnsigned (uint32_t reg, uint64_t fail_value)
161 {
162 if (reg != LLDB_INVALID_REGNUM)
163 return ReadRegisterAsUnsigned (GetRegisterInfoAtIndex (reg), fail_value);
164 return fail_value;
165 }
166
167 uint64_t
ReadRegisterAsUnsigned(const RegisterInfo * reg_info,uint64_t fail_value)168 RegisterContext::ReadRegisterAsUnsigned (const RegisterInfo *reg_info, uint64_t fail_value)
169 {
170 if (reg_info)
171 {
172 RegisterValue value;
173 if (ReadRegister (reg_info, value))
174 return value.GetAsUInt64();
175 }
176 return fail_value;
177 }
178
179 bool
WriteRegisterFromUnsigned(uint32_t reg,uint64_t uval)180 RegisterContext::WriteRegisterFromUnsigned (uint32_t reg, uint64_t uval)
181 {
182 if (reg == LLDB_INVALID_REGNUM)
183 return false;
184 return WriteRegisterFromUnsigned (GetRegisterInfoAtIndex (reg), uval);
185 }
186
187 bool
WriteRegisterFromUnsigned(const RegisterInfo * reg_info,uint64_t uval)188 RegisterContext::WriteRegisterFromUnsigned (const RegisterInfo *reg_info, uint64_t uval)
189 {
190 if (reg_info)
191 {
192 RegisterValue value;
193 if (value.SetUInt(uval, reg_info->byte_size))
194 return WriteRegister (reg_info, value);
195 }
196 return false;
197 }
198
199 bool
CopyFromRegisterContext(lldb::RegisterContextSP context)200 RegisterContext::CopyFromRegisterContext (lldb::RegisterContextSP context)
201 {
202 uint32_t num_register_sets = context->GetRegisterSetCount();
203 // We don't know that two threads have the same register context, so require the threads to be the same.
204 if (context->GetThreadID() != GetThreadID())
205 return false;
206
207 if (num_register_sets != GetRegisterSetCount())
208 return false;
209
210 RegisterContextSP frame_zero_context = m_thread.GetRegisterContext();
211
212 for (uint32_t set_idx = 0; set_idx < num_register_sets; ++set_idx)
213 {
214 const RegisterSet * const reg_set = GetRegisterSet(set_idx);
215
216 const uint32_t num_registers = reg_set->num_registers;
217 for (uint32_t reg_idx = 0; reg_idx < num_registers; ++reg_idx)
218 {
219 const uint32_t reg = reg_set->registers[reg_idx];
220 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
221 if (!reg_info || reg_info->value_regs)
222 continue;
223 RegisterValue reg_value;
224
225 // If we can reconstruct the register from the frame we are copying from, then do so, otherwise
226 // use the value from frame 0.
227 if (context->ReadRegister(reg_info, reg_value))
228 {
229 WriteRegister(reg_info, reg_value);
230 }
231 else if (frame_zero_context->ReadRegister(reg_info, reg_value))
232 {
233 WriteRegister(reg_info, reg_value);
234 }
235 }
236 }
237 return true;
238 }
239
240 lldb::tid_t
GetThreadID() const241 RegisterContext::GetThreadID() const
242 {
243 return m_thread.GetID();
244 }
245
246 uint32_t
NumSupportedHardwareBreakpoints()247 RegisterContext::NumSupportedHardwareBreakpoints ()
248 {
249 return 0;
250 }
251
252 uint32_t
SetHardwareBreakpoint(lldb::addr_t addr,size_t size)253 RegisterContext::SetHardwareBreakpoint (lldb::addr_t addr, size_t size)
254 {
255 return LLDB_INVALID_INDEX32;
256 }
257
258 bool
ClearHardwareBreakpoint(uint32_t hw_idx)259 RegisterContext::ClearHardwareBreakpoint (uint32_t hw_idx)
260 {
261 return false;
262 }
263
264
265 uint32_t
NumSupportedHardwareWatchpoints()266 RegisterContext::NumSupportedHardwareWatchpoints ()
267 {
268 return 0;
269 }
270
271 uint32_t
SetHardwareWatchpoint(lldb::addr_t addr,size_t size,bool read,bool write)272 RegisterContext::SetHardwareWatchpoint (lldb::addr_t addr, size_t size, bool read, bool write)
273 {
274 return LLDB_INVALID_INDEX32;
275 }
276
277 bool
ClearHardwareWatchpoint(uint32_t hw_index)278 RegisterContext::ClearHardwareWatchpoint (uint32_t hw_index)
279 {
280 return false;
281 }
282
283 bool
HardwareSingleStep(bool enable)284 RegisterContext::HardwareSingleStep (bool enable)
285 {
286 return false;
287 }
288
289 Error
ReadRegisterValueFromMemory(const RegisterInfo * reg_info,lldb::addr_t src_addr,uint32_t src_len,RegisterValue & reg_value)290 RegisterContext::ReadRegisterValueFromMemory (const RegisterInfo *reg_info,
291 lldb::addr_t src_addr,
292 uint32_t src_len,
293 RegisterValue ®_value)
294 {
295 Error error;
296 if (reg_info == NULL)
297 {
298 error.SetErrorString ("invalid register info argument.");
299 return error;
300 }
301
302
303 // Moving from addr into a register
304 //
305 // Case 1: src_len == dst_len
306 //
307 // |AABBCCDD| Address contents
308 // |AABBCCDD| Register contents
309 //
310 // Case 2: src_len > dst_len
311 //
312 // Error! (The register should always be big enough to hold the data)
313 //
314 // Case 3: src_len < dst_len
315 //
316 // |AABB| Address contents
317 // |AABB0000| Register contents [on little-endian hardware]
318 // |0000AABB| Register contents [on big-endian hardware]
319 if (src_len > RegisterValue::kMaxRegisterByteSize)
320 {
321 error.SetErrorString ("register too small to receive memory data");
322 return error;
323 }
324
325 const uint32_t dst_len = reg_info->byte_size;
326
327 if (src_len > dst_len)
328 {
329 error.SetErrorStringWithFormat("%u bytes is too big to store in register %s (%u bytes)", src_len, reg_info->name, dst_len);
330 return error;
331 }
332
333 ProcessSP process_sp (m_thread.GetProcess());
334 if (process_sp)
335 {
336 uint8_t src[RegisterValue::kMaxRegisterByteSize];
337
338 // Read the memory
339 const uint32_t bytes_read = process_sp->ReadMemory (src_addr, src, src_len, error);
340
341 // Make sure the memory read succeeded...
342 if (bytes_read != src_len)
343 {
344 if (error.Success())
345 {
346 // This might happen if we read _some_ bytes but not all
347 error.SetErrorStringWithFormat("read %u of %u bytes", bytes_read, src_len);
348 }
349 return error;
350 }
351
352 // We now have a memory buffer that contains the part or all of the register
353 // value. Set the register value using this memory data.
354 // TODO: we might need to add a parameter to this function in case the byte
355 // order of the memory data doesn't match the process. For now we are assuming
356 // they are the same.
357 reg_value.SetFromMemoryData (reg_info,
358 src,
359 src_len,
360 process_sp->GetByteOrder(),
361 error);
362 }
363 else
364 error.SetErrorString("invalid process");
365
366 return error;
367 }
368
369 Error
WriteRegisterValueToMemory(const RegisterInfo * reg_info,lldb::addr_t dst_addr,uint32_t dst_len,const RegisterValue & reg_value)370 RegisterContext::WriteRegisterValueToMemory (const RegisterInfo *reg_info,
371 lldb::addr_t dst_addr,
372 uint32_t dst_len,
373 const RegisterValue ®_value)
374 {
375
376 uint8_t dst[RegisterValue::kMaxRegisterByteSize];
377
378 Error error;
379
380 ProcessSP process_sp (m_thread.GetProcess());
381 if (process_sp)
382 {
383
384 // TODO: we might need to add a parameter to this function in case the byte
385 // order of the memory data doesn't match the process. For now we are assuming
386 // they are the same.
387
388 const uint32_t bytes_copied = reg_value.GetAsMemoryData (reg_info,
389 dst,
390 dst_len,
391 process_sp->GetByteOrder(),
392 error);
393
394 if (error.Success())
395 {
396 if (bytes_copied == 0)
397 {
398 error.SetErrorString("byte copy failed.");
399 }
400 else
401 {
402 const uint32_t bytes_written = process_sp->WriteMemory (dst_addr, dst, bytes_copied, error);
403 if (bytes_written != bytes_copied)
404 {
405 if (error.Success())
406 {
407 // This might happen if we read _some_ bytes but not all
408 error.SetErrorStringWithFormat("only wrote %u of %u bytes", bytes_written, bytes_copied);
409 }
410 }
411 }
412 }
413 }
414 else
415 error.SetErrorString("invalid process");
416
417 return error;
418
419 }
420
421 TargetSP
CalculateTarget()422 RegisterContext::CalculateTarget ()
423 {
424 return m_thread.CalculateTarget();
425 }
426
427
428 ProcessSP
CalculateProcess()429 RegisterContext::CalculateProcess ()
430 {
431 return m_thread.CalculateProcess ();
432 }
433
434 ThreadSP
CalculateThread()435 RegisterContext::CalculateThread ()
436 {
437 return m_thread.shared_from_this();
438 }
439
440 StackFrameSP
CalculateStackFrame()441 RegisterContext::CalculateStackFrame ()
442 {
443 // Register contexts might belong to many frames if we have inlined
444 // functions inside a frame since all inlined functions share the
445 // same registers, so we can't definitively say which frame we come from...
446 return StackFrameSP();
447 }
448
449 void
CalculateExecutionContext(ExecutionContext & exe_ctx)450 RegisterContext::CalculateExecutionContext (ExecutionContext &exe_ctx)
451 {
452 m_thread.CalculateExecutionContext (exe_ctx);
453 }
454
455
456 bool
ConvertBetweenRegisterKinds(int source_rk,uint32_t source_regnum,int target_rk,uint32_t & target_regnum)457 RegisterContext::ConvertBetweenRegisterKinds (int source_rk, uint32_t source_regnum, int target_rk, uint32_t& target_regnum)
458 {
459 const uint32_t num_registers = GetRegisterCount();
460 for (uint32_t reg = 0; reg < num_registers; ++reg)
461 {
462 const RegisterInfo * reg_info = GetRegisterInfoAtIndex (reg);
463
464 if (reg_info->kinds[source_rk] == source_regnum)
465 {
466 target_regnum = reg_info->kinds[target_rk];
467 if (target_regnum == LLDB_INVALID_REGNUM)
468 {
469 return false;
470 }
471 else
472 {
473 return true;
474 }
475 }
476 }
477 return false;
478 }
479
480 //bool
481 //RegisterContext::ReadRegisterValue (uint32_t reg, Scalar &value)
482 //{
483 // DataExtractor data;
484 // if (!ReadRegisterBytes (reg, data))
485 // return false;
486 //
487 // const RegisterInfo *reg_info = GetRegisterInfoAtIndex (reg);
488 // uint32_t offset = 0;
489 // switch (reg_info->encoding)
490 // {
491 // case eEncodingInvalid:
492 // case eEncodingVector:
493 // break;
494 //
495 // case eEncodingUint:
496 // switch (reg_info->byte_size)
497 // {
498 // case 1:
499 // {
500 // value = data.GetU8 (&offset);
501 // return true;
502 // }
503 // case 2:
504 // {
505 // value = data.GetU16 (&offset);
506 // return true;
507 // }
508 // case 4:
509 // {
510 // value = data.GetU32 (&offset);
511 // return true;
512 // }
513 // case 8:
514 // {
515 // value = data.GetU64 (&offset);
516 // return true;
517 // }
518 // }
519 // break;
520 // case eEncodingSint:
521 // switch (reg_info->byte_size)
522 // {
523 // case 1:
524 // {
525 // int8_t v;
526 // if (data.ExtractBytes (0, sizeof (int8_t), lldb::endian::InlHostByteOrder(), &v) != sizeof (int8_t))
527 // return false;
528 // value = v;
529 // return true;
530 // }
531 // case 2:
532 // {
533 // int16_t v;
534 // if (data.ExtractBytes (0, sizeof (int16_t), lldb::endian::InlHostByteOrder(), &v) != sizeof (int16_t))
535 // return false;
536 // value = v;
537 // return true;
538 // }
539 // case 4:
540 // {
541 // int32_t v;
542 // if (data.ExtractBytes (0, sizeof (int32_t), lldb::endian::InlHostByteOrder(), &v) != sizeof (int32_t))
543 // return false;
544 // value = v;
545 // return true;
546 // }
547 // case 8:
548 // {
549 // int64_t v;
550 // if (data.ExtractBytes (0, sizeof (int64_t), lldb::endian::InlHostByteOrder(), &v) != sizeof (int64_t))
551 // return false;
552 // value = v;
553 // return true;
554 // }
555 // }
556 // break;
557 // case eEncodingIEEE754:
558 // switch (reg_info->byte_size)
559 // {
560 // case sizeof (float):
561 // {
562 // float v;
563 // if (data.ExtractBytes (0, sizeof (float), lldb::endian::InlHostByteOrder(), &v) != sizeof (float))
564 // return false;
565 // value = v;
566 // return true;
567 // }
568 // case sizeof (double):
569 // {
570 // double v;
571 // if (data.ExtractBytes (0, sizeof (double), lldb::endian::InlHostByteOrder(), &v) != sizeof (double))
572 // return false;
573 // value = v;
574 // return true;
575 // }
576 // case sizeof (long double):
577 // {
578 // double v;
579 // if (data.ExtractBytes (0, sizeof (long double), lldb::endian::InlHostByteOrder(), &v) != sizeof (long double))
580 // return false;
581 // value = v;
582 // return true;
583 // }
584 // }
585 // break;
586 // }
587 // return false;
588 //}
589 //
590 //bool
591 //RegisterContext::WriteRegisterValue (uint32_t reg, const Scalar &value)
592 //{
593 // DataExtractor data;
594 // if (!value.IsValid())
595 // return false;
596 // if (!value.GetData (data))
597 // return false;
598 //
599 // return WriteRegisterBytes (reg, data);
600 //}
601