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