Alexander Shaposhnikov | 696bd63 | 2016-11-26 05:23:44 +0000 | [diff] [blame] | 1 | //===-- EmulateInstruction.cpp ----------------------------------*- C++ -*-===// |
Greg Clayton | 6da4ca8 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 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 | |
Greg Clayton | 95e3142 | 2011-02-05 02:56:16 +0000 | [diff] [blame] | 10 | #include "lldb/Core/EmulateInstruction.h" |
Greg Clayton | 6da4ca8 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 11 | |
Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Address.h" |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 13 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 14 | #include "lldb/Core/RegisterValue.h" |
| 15 | #include "lldb/Core/StreamFile.h" |
Greg Clayton | 79ea878 | 2011-04-26 23:48:45 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/UnwindPlan.h" |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 17 | #include "lldb/Target/Process.h" |
| 18 | #include "lldb/Target/RegisterContext.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame^] | 19 | #include "lldb/Target/StackFrame.h" // for StackFrame |
| 20 | #include "lldb/Utility/ConstString.h" // for ConstString |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 21 | #include "lldb/Utility/DataExtractor.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 22 | #include "lldb/Utility/Error.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame^] | 23 | #include "lldb/Utility/Stream.h" // for Stream, Stream::::eBinary |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 24 | #include "lldb/Utility/StreamString.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame^] | 25 | #include "lldb/lldb-forward.h" // for ProcessSP |
| 26 | #include "lldb/lldb-private-interfaces.h" // for EmulateInstructionCreateIn... |
| 27 | |
| 28 | #include "llvm/ADT/StringRef.h" // for StringRef |
| 29 | |
| 30 | #include <cstring> |
| 31 | #include <memory> // for shared_ptr |
| 32 | |
| 33 | #include <inttypes.h> // for PRIx64, PRId64, PRIu64 |
| 34 | #include <stdio.h> // for stdout |
| 35 | |
| 36 | namespace lldb_private { |
| 37 | class Target; |
| 38 | } |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 39 | |
Greg Clayton | 6da4ca8 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 40 | using namespace lldb; |
| 41 | using namespace lldb_private; |
| 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | EmulateInstruction * |
| 44 | EmulateInstruction::FindPlugin(const ArchSpec &arch, |
| 45 | InstructionType supported_inst_type, |
| 46 | const char *plugin_name) { |
| 47 | EmulateInstructionCreateInstance create_callback = nullptr; |
| 48 | if (plugin_name) { |
| 49 | ConstString const_plugin_name(plugin_name); |
| 50 | create_callback = |
| 51 | PluginManager::GetEmulateInstructionCreateCallbackForPluginName( |
| 52 | const_plugin_name); |
| 53 | if (create_callback) { |
| 54 | EmulateInstruction *emulate_insn_ptr = |
| 55 | create_callback(arch, supported_inst_type); |
| 56 | if (emulate_insn_ptr) |
| 57 | return emulate_insn_ptr; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 58 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | } else { |
| 60 | for (uint32_t idx = 0; |
| 61 | (create_callback = |
| 62 | PluginManager::GetEmulateInstructionCreateCallbackAtIndex(idx)) != |
| 63 | nullptr; |
| 64 | ++idx) { |
| 65 | EmulateInstruction *emulate_insn_ptr = |
| 66 | create_callback(arch, supported_inst_type); |
| 67 | if (emulate_insn_ptr) |
| 68 | return emulate_insn_ptr; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 69 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | } |
| 71 | return nullptr; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 72 | } |
Greg Clayton | 6da4ca8 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | EmulateInstruction::EmulateInstruction(const ArchSpec &arch) |
| 75 | : m_arch(arch), m_baton(nullptr), m_read_mem_callback(&ReadMemoryDefault), |
| 76 | m_write_mem_callback(&WriteMemoryDefault), |
| 77 | m_read_reg_callback(&ReadRegisterDefault), |
| 78 | m_write_reg_callback(&WriteRegisterDefault), |
| 79 | m_addr(LLDB_INVALID_ADDRESS) { |
| 80 | ::memset(&m_opcode, 0, sizeof(m_opcode)); |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | bool EmulateInstruction::ReadRegister(const RegisterInfo *reg_info, |
| 84 | RegisterValue ®_value) { |
| 85 | if (m_read_reg_callback != nullptr) |
| 86 | return m_read_reg_callback(this, m_baton, reg_info, reg_value); |
| 87 | return false; |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | bool EmulateInstruction::ReadRegister(lldb::RegisterKind reg_kind, |
| 91 | uint32_t reg_num, |
| 92 | RegisterValue ®_value) { |
| 93 | RegisterInfo reg_info; |
| 94 | if (GetRegisterInfo(reg_kind, reg_num, reg_info)) |
| 95 | return ReadRegister(®_info, reg_value); |
| 96 | return false; |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | uint64_t EmulateInstruction::ReadRegisterUnsigned(lldb::RegisterKind reg_kind, |
| 100 | uint32_t reg_num, |
| 101 | uint64_t fail_value, |
| 102 | bool *success_ptr) { |
| 103 | RegisterValue reg_value; |
| 104 | if (ReadRegister(reg_kind, reg_num, reg_value)) |
| 105 | return reg_value.GetAsUInt64(fail_value, success_ptr); |
| 106 | if (success_ptr) |
| 107 | *success_ptr = false; |
| 108 | return fail_value; |
| 109 | } |
| 110 | |
| 111 | uint64_t EmulateInstruction::ReadRegisterUnsigned(const RegisterInfo *reg_info, |
| 112 | uint64_t fail_value, |
| 113 | bool *success_ptr) { |
| 114 | RegisterValue reg_value; |
| 115 | if (ReadRegister(reg_info, reg_value)) |
| 116 | return reg_value.GetAsUInt64(fail_value, success_ptr); |
| 117 | if (success_ptr) |
| 118 | *success_ptr = false; |
| 119 | return fail_value; |
| 120 | } |
| 121 | |
| 122 | bool EmulateInstruction::WriteRegister(const Context &context, |
| 123 | const RegisterInfo *reg_info, |
| 124 | const RegisterValue ®_value) { |
| 125 | if (m_write_reg_callback != nullptr) |
| 126 | return m_write_reg_callback(this, m_baton, context, reg_info, reg_value); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | bool EmulateInstruction::WriteRegister(const Context &context, |
| 131 | lldb::RegisterKind reg_kind, |
| 132 | uint32_t reg_num, |
| 133 | const RegisterValue ®_value) { |
| 134 | RegisterInfo reg_info; |
| 135 | if (GetRegisterInfo(reg_kind, reg_num, reg_info)) |
| 136 | return WriteRegister(context, ®_info, reg_value); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | bool EmulateInstruction::WriteRegisterUnsigned(const Context &context, |
| 141 | lldb::RegisterKind reg_kind, |
| 142 | uint32_t reg_num, |
| 143 | uint64_t uint_value) { |
| 144 | RegisterInfo reg_info; |
| 145 | if (GetRegisterInfo(reg_kind, reg_num, reg_info)) { |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 146 | RegisterValue reg_value; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | if (reg_value.SetUInt(uint_value, reg_info.byte_size)) |
| 148 | return WriteRegister(context, ®_info, reg_value); |
| 149 | } |
| 150 | return false; |
Greg Clayton | 79ea878 | 2011-04-26 23:48:45 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | bool EmulateInstruction::WriteRegisterUnsigned(const Context &context, |
| 154 | const RegisterInfo *reg_info, |
| 155 | uint64_t uint_value) { |
| 156 | if (reg_info != nullptr) { |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 157 | RegisterValue reg_value; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | if (reg_value.SetUInt(uint_value, reg_info->byte_size)) |
| 159 | return WriteRegister(context, reg_info, reg_value); |
| 160 | } |
| 161 | return false; |
Greg Clayton | 6da4ca8 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | size_t EmulateInstruction::ReadMemory(const Context &context, lldb::addr_t addr, |
| 165 | void *dst, size_t dst_len) { |
| 166 | if (m_read_mem_callback != nullptr) |
| 167 | return m_read_mem_callback(this, m_baton, context, addr, dst, dst_len) == |
| 168 | dst_len; |
| 169 | return false; |
Greg Clayton | 79ea878 | 2011-04-26 23:48:45 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | uint64_t EmulateInstruction::ReadMemoryUnsigned(const Context &context, |
| 173 | lldb::addr_t addr, |
| 174 | size_t byte_size, |
| 175 | uint64_t fail_value, |
| 176 | bool *success_ptr) { |
| 177 | uint64_t uval64 = 0; |
| 178 | bool success = false; |
| 179 | if (byte_size <= 8) { |
| 180 | uint8_t buf[sizeof(uint64_t)]; |
| 181 | size_t bytes_read = |
| 182 | m_read_mem_callback(this, m_baton, context, addr, buf, byte_size); |
| 183 | if (bytes_read == byte_size) { |
| 184 | lldb::offset_t offset = 0; |
| 185 | DataExtractor data(buf, byte_size, GetByteOrder(), GetAddressByteSize()); |
| 186 | uval64 = data.GetMaxU64(&offset, byte_size); |
| 187 | success = true; |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 188 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if (success_ptr) |
| 192 | *success_ptr = success; |
| 193 | |
| 194 | if (!success) |
| 195 | uval64 = fail_value; |
| 196 | return uval64; |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | bool EmulateInstruction::WriteMemoryUnsigned(const Context &context, |
| 200 | lldb::addr_t addr, uint64_t uval, |
| 201 | size_t uval_byte_size) { |
| 202 | StreamString strm(Stream::eBinary, GetAddressByteSize(), GetByteOrder()); |
| 203 | strm.PutMaxHex64(uval, uval_byte_size); |
| 204 | |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 205 | size_t bytes_written = m_write_mem_callback( |
| 206 | this, m_baton, context, addr, strm.GetString().data(), uval_byte_size); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | return (bytes_written == uval_byte_size); |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 210 | bool EmulateInstruction::WriteMemory(const Context &context, lldb::addr_t addr, |
| 211 | const void *src, size_t src_len) { |
| 212 | if (m_write_mem_callback != nullptr) |
| 213 | return m_write_mem_callback(this, m_baton, context, addr, src, src_len) == |
| 214 | src_len; |
| 215 | return false; |
Greg Clayton | 6da4ca8 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 218 | void EmulateInstruction::SetBaton(void *baton) { m_baton = baton; } |
Greg Clayton | 6da4ca8 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 219 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | void EmulateInstruction::SetCallbacks( |
| 221 | ReadMemoryCallback read_mem_callback, |
| 222 | WriteMemoryCallback write_mem_callback, |
| 223 | ReadRegisterCallback read_reg_callback, |
| 224 | WriteRegisterCallback write_reg_callback) { |
| 225 | m_read_mem_callback = read_mem_callback; |
| 226 | m_write_mem_callback = write_mem_callback; |
| 227 | m_read_reg_callback = read_reg_callback; |
| 228 | m_write_reg_callback = write_reg_callback; |
Greg Clayton | 6da4ca8 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 231 | void EmulateInstruction::SetReadMemCallback( |
| 232 | ReadMemoryCallback read_mem_callback) { |
| 233 | m_read_mem_callback = read_mem_callback; |
Greg Clayton | 6da4ca8 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 234 | } |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 235 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 236 | void EmulateInstruction::SetWriteMemCallback( |
| 237 | WriteMemoryCallback write_mem_callback) { |
| 238 | m_write_mem_callback = write_mem_callback; |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | void EmulateInstruction::SetReadRegCallback( |
| 242 | ReadRegisterCallback read_reg_callback) { |
| 243 | m_read_reg_callback = read_reg_callback; |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 246 | void EmulateInstruction::SetWriteRegCallback( |
| 247 | WriteRegisterCallback write_reg_callback) { |
| 248 | m_write_reg_callback = write_reg_callback; |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 251 | // |
| 252 | // Read & Write Memory and Registers callback functions. |
| 253 | // |
| 254 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 255 | size_t EmulateInstruction::ReadMemoryFrame(EmulateInstruction *instruction, |
| 256 | void *baton, const Context &context, |
| 257 | lldb::addr_t addr, void *dst, |
| 258 | size_t dst_len) { |
| 259 | if (baton == nullptr || dst == nullptr || dst_len == 0) |
Greg Clayton | d9e416c | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 260 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 261 | |
| 262 | StackFrame *frame = (StackFrame *)baton; |
| 263 | |
| 264 | ProcessSP process_sp(frame->CalculateProcess()); |
| 265 | if (process_sp) { |
| 266 | Error error; |
| 267 | return process_sp->ReadMemory(addr, dst, dst_len, error); |
| 268 | } |
| 269 | return 0; |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 272 | size_t EmulateInstruction::WriteMemoryFrame(EmulateInstruction *instruction, |
| 273 | void *baton, const Context &context, |
| 274 | lldb::addr_t addr, const void *src, |
| 275 | size_t src_len) { |
| 276 | if (baton == nullptr || src == nullptr || src_len == 0) |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 277 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | |
| 279 | StackFrame *frame = (StackFrame *)baton; |
| 280 | |
| 281 | ProcessSP process_sp(frame->CalculateProcess()); |
| 282 | if (process_sp) { |
| 283 | Error error; |
| 284 | return process_sp->WriteMemory(addr, src, src_len, error); |
| 285 | } |
| 286 | |
| 287 | return 0; |
Caroline Tice | ad379efc | 2011-04-05 18:46:00 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 290 | bool EmulateInstruction::ReadRegisterFrame(EmulateInstruction *instruction, |
| 291 | void *baton, |
| 292 | const RegisterInfo *reg_info, |
| 293 | RegisterValue ®_value) { |
| 294 | if (baton == nullptr) |
Greg Clayton | 79ea878 | 2011-04-26 23:48:45 +0000 | [diff] [blame] | 295 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 296 | |
| 297 | StackFrame *frame = (StackFrame *)baton; |
| 298 | return frame->GetRegisterContext()->ReadRegister(reg_info, reg_value); |
| 299 | } |
| 300 | |
| 301 | bool EmulateInstruction::WriteRegisterFrame(EmulateInstruction *instruction, |
| 302 | void *baton, const Context &context, |
| 303 | const RegisterInfo *reg_info, |
| 304 | const RegisterValue ®_value) { |
| 305 | if (baton == nullptr) |
| 306 | return false; |
| 307 | |
| 308 | StackFrame *frame = (StackFrame *)baton; |
| 309 | return frame->GetRegisterContext()->WriteRegister(reg_info, reg_value); |
| 310 | } |
| 311 | |
| 312 | size_t EmulateInstruction::ReadMemoryDefault(EmulateInstruction *instruction, |
| 313 | void *baton, |
| 314 | const Context &context, |
| 315 | lldb::addr_t addr, void *dst, |
| 316 | size_t length) { |
| 317 | StreamFile strm(stdout, false); |
| 318 | strm.Printf(" Read from Memory (address = 0x%" PRIx64 ", length = %" PRIu64 |
| 319 | ", context = ", |
| 320 | addr, (uint64_t)length); |
| 321 | context.Dump(strm, instruction); |
| 322 | strm.EOL(); |
| 323 | *((uint64_t *)dst) = 0xdeadbeef; |
| 324 | return length; |
| 325 | } |
| 326 | |
| 327 | size_t EmulateInstruction::WriteMemoryDefault(EmulateInstruction *instruction, |
| 328 | void *baton, |
| 329 | const Context &context, |
| 330 | lldb::addr_t addr, |
| 331 | const void *dst, size_t length) { |
| 332 | StreamFile strm(stdout, false); |
| 333 | strm.Printf(" Write to Memory (address = 0x%" PRIx64 ", length = %" PRIu64 |
| 334 | ", context = ", |
| 335 | addr, (uint64_t)length); |
| 336 | context.Dump(strm, instruction); |
| 337 | strm.EOL(); |
| 338 | return length; |
| 339 | } |
| 340 | |
| 341 | bool EmulateInstruction::ReadRegisterDefault(EmulateInstruction *instruction, |
| 342 | void *baton, |
| 343 | const RegisterInfo *reg_info, |
| 344 | RegisterValue ®_value) { |
| 345 | StreamFile strm(stdout, false); |
| 346 | strm.Printf(" Read Register (%s)\n", reg_info->name); |
| 347 | lldb::RegisterKind reg_kind; |
| 348 | uint32_t reg_num; |
| 349 | if (GetBestRegisterKindAndNumber(reg_info, reg_kind, reg_num)) |
| 350 | reg_value.SetUInt64((uint64_t)reg_kind << 24 | reg_num); |
| 351 | else |
| 352 | reg_value.SetUInt64(0); |
| 353 | |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | bool EmulateInstruction::WriteRegisterDefault(EmulateInstruction *instruction, |
| 358 | void *baton, |
| 359 | const Context &context, |
| 360 | const RegisterInfo *reg_info, |
| 361 | const RegisterValue ®_value) { |
| 362 | StreamFile strm(stdout, false); |
| 363 | strm.Printf(" Write to Register (name = %s, value = ", reg_info->name); |
| 364 | reg_value.Dump(&strm, reg_info, false, false, eFormatDefault); |
| 365 | strm.PutCString(", context = "); |
| 366 | context.Dump(strm, instruction); |
| 367 | strm.EOL(); |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | void EmulateInstruction::Context::Dump(Stream &strm, |
| 372 | EmulateInstruction *instruction) const { |
| 373 | switch (type) { |
| 374 | case eContextReadOpcode: |
| 375 | strm.PutCString("reading opcode"); |
| 376 | break; |
| 377 | |
| 378 | case eContextImmediate: |
| 379 | strm.PutCString("immediate"); |
| 380 | break; |
| 381 | |
| 382 | case eContextPushRegisterOnStack: |
| 383 | strm.PutCString("push register"); |
| 384 | break; |
| 385 | |
| 386 | case eContextPopRegisterOffStack: |
| 387 | strm.PutCString("pop register"); |
| 388 | break; |
| 389 | |
| 390 | case eContextAdjustStackPointer: |
| 391 | strm.PutCString("adjust sp"); |
| 392 | break; |
| 393 | |
| 394 | case eContextSetFramePointer: |
| 395 | strm.PutCString("set frame pointer"); |
| 396 | break; |
| 397 | |
| 398 | case eContextAdjustBaseRegister: |
| 399 | strm.PutCString("adjusting (writing value back to) a base register"); |
| 400 | break; |
| 401 | |
| 402 | case eContextRegisterPlusOffset: |
| 403 | strm.PutCString("register + offset"); |
| 404 | break; |
| 405 | |
| 406 | case eContextRegisterStore: |
| 407 | strm.PutCString("store register"); |
| 408 | break; |
| 409 | |
| 410 | case eContextRegisterLoad: |
| 411 | strm.PutCString("load register"); |
| 412 | break; |
| 413 | |
| 414 | case eContextRelativeBranchImmediate: |
| 415 | strm.PutCString("relative branch immediate"); |
| 416 | break; |
| 417 | |
| 418 | case eContextAbsoluteBranchRegister: |
| 419 | strm.PutCString("absolute branch register"); |
| 420 | break; |
| 421 | |
| 422 | case eContextSupervisorCall: |
| 423 | strm.PutCString("supervisor call"); |
| 424 | break; |
| 425 | |
| 426 | case eContextTableBranchReadMemory: |
| 427 | strm.PutCString("table branch read memory"); |
| 428 | break; |
| 429 | |
| 430 | case eContextWriteRegisterRandomBits: |
| 431 | strm.PutCString("write random bits to a register"); |
| 432 | break; |
| 433 | |
| 434 | case eContextWriteMemoryRandomBits: |
| 435 | strm.PutCString("write random bits to a memory address"); |
| 436 | break; |
| 437 | |
| 438 | case eContextArithmetic: |
| 439 | strm.PutCString("arithmetic"); |
| 440 | break; |
| 441 | |
| 442 | case eContextReturnFromException: |
| 443 | strm.PutCString("return from exception"); |
| 444 | break; |
| 445 | |
| 446 | default: |
| 447 | strm.PutCString("unrecognized context."); |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | switch (info_type) { |
| 452 | case eInfoTypeRegisterPlusOffset: |
| 453 | strm.Printf(" (reg_plus_offset = %s%+" PRId64 ")", |
| 454 | info.RegisterPlusOffset.reg.name, |
| 455 | info.RegisterPlusOffset.signed_offset); |
| 456 | break; |
| 457 | |
| 458 | case eInfoTypeRegisterPlusIndirectOffset: |
| 459 | strm.Printf(" (reg_plus_reg = %s + %s)", |
| 460 | info.RegisterPlusIndirectOffset.base_reg.name, |
| 461 | info.RegisterPlusIndirectOffset.offset_reg.name); |
| 462 | break; |
| 463 | |
| 464 | case eInfoTypeRegisterToRegisterPlusOffset: |
| 465 | strm.Printf(" (base_and_imm_offset = %s%+" PRId64 ", data_reg = %s)", |
| 466 | info.RegisterToRegisterPlusOffset.base_reg.name, |
| 467 | info.RegisterToRegisterPlusOffset.offset, |
| 468 | info.RegisterToRegisterPlusOffset.data_reg.name); |
| 469 | break; |
| 470 | |
| 471 | case eInfoTypeRegisterToRegisterPlusIndirectOffset: |
| 472 | strm.Printf(" (base_and_reg_offset = %s + %s, data_reg = %s)", |
| 473 | info.RegisterToRegisterPlusIndirectOffset.base_reg.name, |
| 474 | info.RegisterToRegisterPlusIndirectOffset.offset_reg.name, |
| 475 | info.RegisterToRegisterPlusIndirectOffset.data_reg.name); |
| 476 | break; |
| 477 | |
| 478 | case eInfoTypeRegisterRegisterOperands: |
| 479 | strm.Printf(" (register to register binary op: %s and %s)", |
| 480 | info.RegisterRegisterOperands.operand1.name, |
| 481 | info.RegisterRegisterOperands.operand2.name); |
| 482 | break; |
| 483 | |
| 484 | case eInfoTypeOffset: |
| 485 | strm.Printf(" (signed_offset = %+" PRId64 ")", info.signed_offset); |
| 486 | break; |
| 487 | |
| 488 | case eInfoTypeRegister: |
| 489 | strm.Printf(" (reg = %s)", info.reg.name); |
| 490 | break; |
| 491 | |
| 492 | case eInfoTypeImmediate: |
| 493 | strm.Printf(" (unsigned_immediate = %" PRIu64 " (0x%16.16" PRIx64 "))", |
| 494 | info.unsigned_immediate, info.unsigned_immediate); |
| 495 | break; |
| 496 | |
| 497 | case eInfoTypeImmediateSigned: |
| 498 | strm.Printf(" (signed_immediate = %+" PRId64 " (0x%16.16" PRIx64 "))", |
| 499 | info.signed_immediate, info.signed_immediate); |
| 500 | break; |
| 501 | |
| 502 | case eInfoTypeAddress: |
| 503 | strm.Printf(" (address = 0x%" PRIx64 ")", info.address); |
| 504 | break; |
| 505 | |
| 506 | case eInfoTypeISAAndImmediate: |
| 507 | strm.Printf(" (isa = %u, unsigned_immediate = %u (0x%8.8x))", |
| 508 | info.ISAAndImmediate.isa, info.ISAAndImmediate.unsigned_data32, |
| 509 | info.ISAAndImmediate.unsigned_data32); |
| 510 | break; |
| 511 | |
| 512 | case eInfoTypeISAAndImmediateSigned: |
| 513 | strm.Printf(" (isa = %u, signed_immediate = %i (0x%8.8x))", |
| 514 | info.ISAAndImmediateSigned.isa, |
| 515 | info.ISAAndImmediateSigned.signed_data32, |
| 516 | info.ISAAndImmediateSigned.signed_data32); |
| 517 | break; |
| 518 | |
| 519 | case eInfoTypeISA: |
| 520 | strm.Printf(" (isa = %u)", info.isa); |
| 521 | break; |
| 522 | |
| 523 | case eInfoTypeNoArgs: |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | bool EmulateInstruction::SetInstruction(const Opcode &opcode, |
| 529 | const Address &inst_addr, |
| 530 | Target *target) { |
| 531 | m_opcode = opcode; |
| 532 | m_addr = LLDB_INVALID_ADDRESS; |
| 533 | if (inst_addr.IsValid()) { |
| 534 | if (target != nullptr) |
| 535 | m_addr = inst_addr.GetLoadAddress(target); |
| 536 | if (m_addr == LLDB_INVALID_ADDRESS) |
| 537 | m_addr = inst_addr.GetFileAddress(); |
| 538 | } |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | bool EmulateInstruction::GetBestRegisterKindAndNumber( |
| 543 | const RegisterInfo *reg_info, lldb::RegisterKind ®_kind, |
| 544 | uint32_t ®_num) { |
| 545 | // Generic and DWARF should be the two most popular register kinds when |
| 546 | // emulating instructions since they are the most platform agnostic... |
| 547 | reg_num = reg_info->kinds[eRegisterKindGeneric]; |
| 548 | if (reg_num != LLDB_INVALID_REGNUM) { |
| 549 | reg_kind = eRegisterKindGeneric; |
| 550 | return true; |
| 551 | } |
| 552 | |
| 553 | reg_num = reg_info->kinds[eRegisterKindDWARF]; |
| 554 | if (reg_num != LLDB_INVALID_REGNUM) { |
| 555 | reg_kind = eRegisterKindDWARF; |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | reg_num = reg_info->kinds[eRegisterKindLLDB]; |
| 560 | if (reg_num != LLDB_INVALID_REGNUM) { |
| 561 | reg_kind = eRegisterKindLLDB; |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | reg_num = reg_info->kinds[eRegisterKindEHFrame]; |
| 566 | if (reg_num != LLDB_INVALID_REGNUM) { |
| 567 | reg_kind = eRegisterKindEHFrame; |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | reg_num = reg_info->kinds[eRegisterKindProcessPlugin]; |
| 572 | if (reg_num != LLDB_INVALID_REGNUM) { |
| 573 | reg_kind = eRegisterKindProcessPlugin; |
| 574 | return true; |
| 575 | } |
| 576 | return false; |
Greg Clayton | 79ea878 | 2011-04-26 23:48:45 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 580 | EmulateInstruction::GetInternalRegisterNumber(RegisterContext *reg_ctx, |
| 581 | const RegisterInfo ®_info) { |
| 582 | lldb::RegisterKind reg_kind; |
| 583 | uint32_t reg_num; |
| 584 | if (reg_ctx && GetBestRegisterKindAndNumber(®_info, reg_kind, reg_num)) |
| 585 | return reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num); |
| 586 | return LLDB_INVALID_REGNUM; |
Greg Clayton | 79ea878 | 2011-04-26 23:48:45 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 589 | bool EmulateInstruction::CreateFunctionEntryUnwind(UnwindPlan &unwind_plan) { |
| 590 | unwind_plan.Clear(); |
| 591 | return false; |
Greg Clayton | 2ed751b | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 592 | } |