1 //===-- DWARFExpression.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/Expression/DWARFExpression.h" 11 12 // C Includes 13 #include <inttypes.h> 14 15 // C++ Includes 16 #include <vector> 17 18 #include "lldb/Core/DataEncoder.h" 19 #include "lldb/Core/dwarf.h" 20 #include "lldb/Core/Log.h" 21 #include "lldb/Core/RegisterValue.h" 22 #include "lldb/Core/StreamString.h" 23 #include "lldb/Core/Scalar.h" 24 #include "lldb/Core/Value.h" 25 #include "lldb/Core/VMRange.h" 26 27 #include "lldb/Expression/ClangExpressionDeclMap.h" 28 #include "lldb/Expression/ClangExpressionVariable.h" 29 30 #include "lldb/Host/Endian.h" 31 #include "lldb/Host/Host.h" 32 33 #include "lldb/lldb-private-log.h" 34 35 #include "lldb/Target/ABI.h" 36 #include "lldb/Target/ExecutionContext.h" 37 #include "lldb/Target/Process.h" 38 #include "lldb/Target/RegisterContext.h" 39 #include "lldb/Target/StackFrame.h" 40 #include "lldb/Target/StackID.h" 41 #include "lldb/Target/Thread.h" 42 43 using namespace lldb; 44 using namespace lldb_private; 45 46 // TODO- why is this also defined (in a better way) in DWARFDefines.cpp? 47 const char * 48 DW_OP_value_to_name (uint32_t val) 49 { 50 static char invalid[100]; 51 switch (val) { 52 case 0x03: return "DW_OP_addr"; 53 case 0x06: return "DW_OP_deref"; 54 case 0x08: return "DW_OP_const1u"; 55 case 0x09: return "DW_OP_const1s"; 56 case 0x0a: return "DW_OP_const2u"; 57 case 0x0b: return "DW_OP_const2s"; 58 case 0x0c: return "DW_OP_const4u"; 59 case 0x0d: return "DW_OP_const4s"; 60 case 0x0e: return "DW_OP_const8u"; 61 case 0x0f: return "DW_OP_const8s"; 62 case 0x10: return "DW_OP_constu"; 63 case 0x11: return "DW_OP_consts"; 64 case 0x12: return "DW_OP_dup"; 65 case 0x13: return "DW_OP_drop"; 66 case 0x14: return "DW_OP_over"; 67 case 0x15: return "DW_OP_pick"; 68 case 0x16: return "DW_OP_swap"; 69 case 0x17: return "DW_OP_rot"; 70 case 0x18: return "DW_OP_xderef"; 71 case 0x19: return "DW_OP_abs"; 72 case 0x1a: return "DW_OP_and"; 73 case 0x1b: return "DW_OP_div"; 74 case 0x1c: return "DW_OP_minus"; 75 case 0x1d: return "DW_OP_mod"; 76 case 0x1e: return "DW_OP_mul"; 77 case 0x1f: return "DW_OP_neg"; 78 case 0x20: return "DW_OP_not"; 79 case 0x21: return "DW_OP_or"; 80 case 0x22: return "DW_OP_plus"; 81 case 0x23: return "DW_OP_plus_uconst"; 82 case 0x24: return "DW_OP_shl"; 83 case 0x25: return "DW_OP_shr"; 84 case 0x26: return "DW_OP_shra"; 85 case 0x27: return "DW_OP_xor"; 86 case 0x2f: return "DW_OP_skip"; 87 case 0x28: return "DW_OP_bra"; 88 case 0x29: return "DW_OP_eq"; 89 case 0x2a: return "DW_OP_ge"; 90 case 0x2b: return "DW_OP_gt"; 91 case 0x2c: return "DW_OP_le"; 92 case 0x2d: return "DW_OP_lt"; 93 case 0x2e: return "DW_OP_ne"; 94 case 0x30: return "DW_OP_lit0"; 95 case 0x31: return "DW_OP_lit1"; 96 case 0x32: return "DW_OP_lit2"; 97 case 0x33: return "DW_OP_lit3"; 98 case 0x34: return "DW_OP_lit4"; 99 case 0x35: return "DW_OP_lit5"; 100 case 0x36: return "DW_OP_lit6"; 101 case 0x37: return "DW_OP_lit7"; 102 case 0x38: return "DW_OP_lit8"; 103 case 0x39: return "DW_OP_lit9"; 104 case 0x3a: return "DW_OP_lit10"; 105 case 0x3b: return "DW_OP_lit11"; 106 case 0x3c: return "DW_OP_lit12"; 107 case 0x3d: return "DW_OP_lit13"; 108 case 0x3e: return "DW_OP_lit14"; 109 case 0x3f: return "DW_OP_lit15"; 110 case 0x40: return "DW_OP_lit16"; 111 case 0x41: return "DW_OP_lit17"; 112 case 0x42: return "DW_OP_lit18"; 113 case 0x43: return "DW_OP_lit19"; 114 case 0x44: return "DW_OP_lit20"; 115 case 0x45: return "DW_OP_lit21"; 116 case 0x46: return "DW_OP_lit22"; 117 case 0x47: return "DW_OP_lit23"; 118 case 0x48: return "DW_OP_lit24"; 119 case 0x49: return "DW_OP_lit25"; 120 case 0x4a: return "DW_OP_lit26"; 121 case 0x4b: return "DW_OP_lit27"; 122 case 0x4c: return "DW_OP_lit28"; 123 case 0x4d: return "DW_OP_lit29"; 124 case 0x4e: return "DW_OP_lit30"; 125 case 0x4f: return "DW_OP_lit31"; 126 case 0x50: return "DW_OP_reg0"; 127 case 0x51: return "DW_OP_reg1"; 128 case 0x52: return "DW_OP_reg2"; 129 case 0x53: return "DW_OP_reg3"; 130 case 0x54: return "DW_OP_reg4"; 131 case 0x55: return "DW_OP_reg5"; 132 case 0x56: return "DW_OP_reg6"; 133 case 0x57: return "DW_OP_reg7"; 134 case 0x58: return "DW_OP_reg8"; 135 case 0x59: return "DW_OP_reg9"; 136 case 0x5a: return "DW_OP_reg10"; 137 case 0x5b: return "DW_OP_reg11"; 138 case 0x5c: return "DW_OP_reg12"; 139 case 0x5d: return "DW_OP_reg13"; 140 case 0x5e: return "DW_OP_reg14"; 141 case 0x5f: return "DW_OP_reg15"; 142 case 0x60: return "DW_OP_reg16"; 143 case 0x61: return "DW_OP_reg17"; 144 case 0x62: return "DW_OP_reg18"; 145 case 0x63: return "DW_OP_reg19"; 146 case 0x64: return "DW_OP_reg20"; 147 case 0x65: return "DW_OP_reg21"; 148 case 0x66: return "DW_OP_reg22"; 149 case 0x67: return "DW_OP_reg23"; 150 case 0x68: return "DW_OP_reg24"; 151 case 0x69: return "DW_OP_reg25"; 152 case 0x6a: return "DW_OP_reg26"; 153 case 0x6b: return "DW_OP_reg27"; 154 case 0x6c: return "DW_OP_reg28"; 155 case 0x6d: return "DW_OP_reg29"; 156 case 0x6e: return "DW_OP_reg30"; 157 case 0x6f: return "DW_OP_reg31"; 158 case 0x70: return "DW_OP_breg0"; 159 case 0x71: return "DW_OP_breg1"; 160 case 0x72: return "DW_OP_breg2"; 161 case 0x73: return "DW_OP_breg3"; 162 case 0x74: return "DW_OP_breg4"; 163 case 0x75: return "DW_OP_breg5"; 164 case 0x76: return "DW_OP_breg6"; 165 case 0x77: return "DW_OP_breg7"; 166 case 0x78: return "DW_OP_breg8"; 167 case 0x79: return "DW_OP_breg9"; 168 case 0x7a: return "DW_OP_breg10"; 169 case 0x7b: return "DW_OP_breg11"; 170 case 0x7c: return "DW_OP_breg12"; 171 case 0x7d: return "DW_OP_breg13"; 172 case 0x7e: return "DW_OP_breg14"; 173 case 0x7f: return "DW_OP_breg15"; 174 case 0x80: return "DW_OP_breg16"; 175 case 0x81: return "DW_OP_breg17"; 176 case 0x82: return "DW_OP_breg18"; 177 case 0x83: return "DW_OP_breg19"; 178 case 0x84: return "DW_OP_breg20"; 179 case 0x85: return "DW_OP_breg21"; 180 case 0x86: return "DW_OP_breg22"; 181 case 0x87: return "DW_OP_breg23"; 182 case 0x88: return "DW_OP_breg24"; 183 case 0x89: return "DW_OP_breg25"; 184 case 0x8a: return "DW_OP_breg26"; 185 case 0x8b: return "DW_OP_breg27"; 186 case 0x8c: return "DW_OP_breg28"; 187 case 0x8d: return "DW_OP_breg29"; 188 case 0x8e: return "DW_OP_breg30"; 189 case 0x8f: return "DW_OP_breg31"; 190 case 0x90: return "DW_OP_regx"; 191 case 0x91: return "DW_OP_fbreg"; 192 case 0x92: return "DW_OP_bregx"; 193 case 0x93: return "DW_OP_piece"; 194 case 0x94: return "DW_OP_deref_size"; 195 case 0x95: return "DW_OP_xderef_size"; 196 case 0x96: return "DW_OP_nop"; 197 case 0x97: return "DW_OP_push_object_address"; 198 case 0x98: return "DW_OP_call2"; 199 case 0x99: return "DW_OP_call4"; 200 case 0x9a: return "DW_OP_call_ref"; 201 // case DW_OP_APPLE_array_ref: return "DW_OP_APPLE_array_ref"; 202 // case DW_OP_APPLE_extern: return "DW_OP_APPLE_extern"; 203 case DW_OP_APPLE_uninit: return "DW_OP_APPLE_uninit"; 204 // case DW_OP_APPLE_assign: return "DW_OP_APPLE_assign"; 205 // case DW_OP_APPLE_address_of: return "DW_OP_APPLE_address_of"; 206 // case DW_OP_APPLE_value_of: return "DW_OP_APPLE_value_of"; 207 // case DW_OP_APPLE_deref_type: return "DW_OP_APPLE_deref_type"; 208 // case DW_OP_APPLE_expr_local: return "DW_OP_APPLE_expr_local"; 209 // case DW_OP_APPLE_constf: return "DW_OP_APPLE_constf"; 210 // case DW_OP_APPLE_scalar_cast: return "DW_OP_APPLE_scalar_cast"; 211 // case DW_OP_APPLE_clang_cast: return "DW_OP_APPLE_clang_cast"; 212 // case DW_OP_APPLE_clear: return "DW_OP_APPLE_clear"; 213 // case DW_OP_APPLE_error: return "DW_OP_APPLE_error"; 214 default: 215 snprintf (invalid, sizeof(invalid), "Unknown DW_OP constant: 0x%x", val); 216 return invalid; 217 } 218 } 219 220 221 //---------------------------------------------------------------------- 222 // DWARFExpression constructor 223 //---------------------------------------------------------------------- 224 DWARFExpression::DWARFExpression() : 225 m_module_wp(), 226 m_data(), 227 m_reg_kind (eRegisterKindDWARF), 228 m_loclist_slide (LLDB_INVALID_ADDRESS) 229 { 230 } 231 232 DWARFExpression::DWARFExpression(const DWARFExpression& rhs) : 233 m_module_wp(rhs.m_module_wp), 234 m_data(rhs.m_data), 235 m_reg_kind (rhs.m_reg_kind), 236 m_loclist_slide(rhs.m_loclist_slide) 237 { 238 } 239 240 241 DWARFExpression::DWARFExpression(lldb::ModuleSP module_sp, const DataExtractor& data, lldb::offset_t data_offset, lldb::offset_t data_length) : 242 m_module_wp(), 243 m_data(data, data_offset, data_length), 244 m_reg_kind (eRegisterKindDWARF), 245 m_loclist_slide(LLDB_INVALID_ADDRESS) 246 { 247 if (module_sp) 248 m_module_wp = module_sp; 249 } 250 251 //---------------------------------------------------------------------- 252 // Destructor 253 //---------------------------------------------------------------------- 254 DWARFExpression::~DWARFExpression() 255 { 256 } 257 258 259 bool 260 DWARFExpression::IsValid() const 261 { 262 return m_data.GetByteSize() > 0; 263 } 264 265 void 266 DWARFExpression::SetOpcodeData (const DataExtractor& data) 267 { 268 m_data = data; 269 } 270 271 void 272 DWARFExpression::CopyOpcodeData (lldb::ModuleSP module_sp, const DataExtractor& data, lldb::offset_t data_offset, lldb::offset_t data_length) 273 { 274 const uint8_t *bytes = data.PeekData(data_offset, data_length); 275 if (bytes) 276 { 277 m_module_wp = module_sp; 278 m_data.SetData(DataBufferSP(new DataBufferHeap(bytes, data_length))); 279 m_data.SetByteOrder(data.GetByteOrder()); 280 m_data.SetAddressByteSize(data.GetAddressByteSize()); 281 } 282 } 283 284 void 285 DWARFExpression::CopyOpcodeData (const void *data, 286 lldb::offset_t data_length, 287 ByteOrder byte_order, 288 uint8_t addr_byte_size) 289 { 290 if (data && data_length) 291 { 292 m_data.SetData(DataBufferSP(new DataBufferHeap(data, data_length))); 293 m_data.SetByteOrder(byte_order); 294 m_data.SetAddressByteSize(addr_byte_size); 295 } 296 } 297 298 void 299 DWARFExpression::CopyOpcodeData (uint64_t const_value, 300 lldb::offset_t const_value_byte_size, 301 uint8_t addr_byte_size) 302 { 303 if (const_value_byte_size) 304 { 305 m_data.SetData(DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size))); 306 m_data.SetByteOrder(endian::InlHostByteOrder()); 307 m_data.SetAddressByteSize(addr_byte_size); 308 } 309 } 310 311 void 312 DWARFExpression::SetOpcodeData (lldb::ModuleSP module_sp, const DataExtractor& data, lldb::offset_t data_offset, lldb::offset_t data_length) 313 { 314 m_module_wp = module_sp; 315 m_data.SetData(data, data_offset, data_length); 316 } 317 318 void 319 DWARFExpression::DumpLocation (Stream *s, lldb::offset_t offset, lldb::offset_t length, lldb::DescriptionLevel level, ABI *abi) const 320 { 321 if (!m_data.ValidOffsetForDataOfSize(offset, length)) 322 return; 323 const lldb::offset_t start_offset = offset; 324 const lldb::offset_t end_offset = offset + length; 325 while (m_data.ValidOffset(offset) && offset < end_offset) 326 { 327 const lldb::offset_t op_offset = offset; 328 const uint8_t op = m_data.GetU8(&offset); 329 330 switch (level) 331 { 332 default: 333 break; 334 335 case lldb::eDescriptionLevelBrief: 336 if (offset > start_offset) 337 s->PutChar(' '); 338 break; 339 340 case lldb::eDescriptionLevelFull: 341 case lldb::eDescriptionLevelVerbose: 342 if (offset > start_offset) 343 s->EOL(); 344 s->Indent(); 345 if (level == lldb::eDescriptionLevelFull) 346 break; 347 // Fall through for verbose and print offset and DW_OP prefix.. 348 s->Printf("0x%8.8" PRIx64 ": %s", op_offset, op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_"); 349 break; 350 } 351 352 switch (op) 353 { 354 case DW_OP_addr: *s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") "; break; // 0x03 1 address 355 case DW_OP_deref: *s << "DW_OP_deref"; break; // 0x06 356 case DW_OP_const1u: s->Printf("DW_OP_const1u(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x08 1 1-byte constant 357 case DW_OP_const1s: s->Printf("DW_OP_const1s(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x09 1 1-byte constant 358 case DW_OP_const2u: s->Printf("DW_OP_const2u(0x%4.4x) ", m_data.GetU16(&offset)); break; // 0x0a 1 2-byte constant 359 case DW_OP_const2s: s->Printf("DW_OP_const2s(0x%4.4x) ", m_data.GetU16(&offset)); break; // 0x0b 1 2-byte constant 360 case DW_OP_const4u: s->Printf("DW_OP_const4u(0x%8.8x) ", m_data.GetU32(&offset)); break; // 0x0c 1 4-byte constant 361 case DW_OP_const4s: s->Printf("DW_OP_const4s(0x%8.8x) ", m_data.GetU32(&offset)); break; // 0x0d 1 4-byte constant 362 case DW_OP_const8u: s->Printf("DW_OP_const8u(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset)); break; // 0x0e 1 8-byte constant 363 case DW_OP_const8s: s->Printf("DW_OP_const8s(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset)); break; // 0x0f 1 8-byte constant 364 case DW_OP_constu: s->Printf("DW_OP_constu(0x%" PRIx64 ") ", m_data.GetULEB128(&offset)); break; // 0x10 1 ULEB128 constant 365 case DW_OP_consts: s->Printf("DW_OP_consts(0x%" PRId64 ") ", m_data.GetSLEB128(&offset)); break; // 0x11 1 SLEB128 constant 366 case DW_OP_dup: s->PutCString("DW_OP_dup"); break; // 0x12 367 case DW_OP_drop: s->PutCString("DW_OP_drop"); break; // 0x13 368 case DW_OP_over: s->PutCString("DW_OP_over"); break; // 0x14 369 case DW_OP_pick: s->Printf("DW_OP_pick(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x15 1 1-byte stack index 370 case DW_OP_swap: s->PutCString("DW_OP_swap"); break; // 0x16 371 case DW_OP_rot: s->PutCString("DW_OP_rot"); break; // 0x17 372 case DW_OP_xderef: s->PutCString("DW_OP_xderef"); break; // 0x18 373 case DW_OP_abs: s->PutCString("DW_OP_abs"); break; // 0x19 374 case DW_OP_and: s->PutCString("DW_OP_and"); break; // 0x1a 375 case DW_OP_div: s->PutCString("DW_OP_div"); break; // 0x1b 376 case DW_OP_minus: s->PutCString("DW_OP_minus"); break; // 0x1c 377 case DW_OP_mod: s->PutCString("DW_OP_mod"); break; // 0x1d 378 case DW_OP_mul: s->PutCString("DW_OP_mul"); break; // 0x1e 379 case DW_OP_neg: s->PutCString("DW_OP_neg"); break; // 0x1f 380 case DW_OP_not: s->PutCString("DW_OP_not"); break; // 0x20 381 case DW_OP_or: s->PutCString("DW_OP_or"); break; // 0x21 382 case DW_OP_plus: s->PutCString("DW_OP_plus"); break; // 0x22 383 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend 384 s->Printf("DW_OP_plus_uconst(0x%" PRIx64 ") ", m_data.GetULEB128(&offset)); 385 break; 386 387 case DW_OP_shl: s->PutCString("DW_OP_shl"); break; // 0x24 388 case DW_OP_shr: s->PutCString("DW_OP_shr"); break; // 0x25 389 case DW_OP_shra: s->PutCString("DW_OP_shra"); break; // 0x26 390 case DW_OP_xor: s->PutCString("DW_OP_xor"); break; // 0x27 391 case DW_OP_skip: s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset)); break; // 0x2f 1 signed 2-byte constant 392 case DW_OP_bra: s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset)); break; // 0x28 1 signed 2-byte constant 393 case DW_OP_eq: s->PutCString("DW_OP_eq"); break; // 0x29 394 case DW_OP_ge: s->PutCString("DW_OP_ge"); break; // 0x2a 395 case DW_OP_gt: s->PutCString("DW_OP_gt"); break; // 0x2b 396 case DW_OP_le: s->PutCString("DW_OP_le"); break; // 0x2c 397 case DW_OP_lt: s->PutCString("DW_OP_lt"); break; // 0x2d 398 case DW_OP_ne: s->PutCString("DW_OP_ne"); break; // 0x2e 399 400 case DW_OP_lit0: // 0x30 401 case DW_OP_lit1: // 0x31 402 case DW_OP_lit2: // 0x32 403 case DW_OP_lit3: // 0x33 404 case DW_OP_lit4: // 0x34 405 case DW_OP_lit5: // 0x35 406 case DW_OP_lit6: // 0x36 407 case DW_OP_lit7: // 0x37 408 case DW_OP_lit8: // 0x38 409 case DW_OP_lit9: // 0x39 410 case DW_OP_lit10: // 0x3A 411 case DW_OP_lit11: // 0x3B 412 case DW_OP_lit12: // 0x3C 413 case DW_OP_lit13: // 0x3D 414 case DW_OP_lit14: // 0x3E 415 case DW_OP_lit15: // 0x3F 416 case DW_OP_lit16: // 0x40 417 case DW_OP_lit17: // 0x41 418 case DW_OP_lit18: // 0x42 419 case DW_OP_lit19: // 0x43 420 case DW_OP_lit20: // 0x44 421 case DW_OP_lit21: // 0x45 422 case DW_OP_lit22: // 0x46 423 case DW_OP_lit23: // 0x47 424 case DW_OP_lit24: // 0x48 425 case DW_OP_lit25: // 0x49 426 case DW_OP_lit26: // 0x4A 427 case DW_OP_lit27: // 0x4B 428 case DW_OP_lit28: // 0x4C 429 case DW_OP_lit29: // 0x4D 430 case DW_OP_lit30: // 0x4E 431 case DW_OP_lit31: s->Printf("DW_OP_lit%i", op - DW_OP_lit0); break; // 0x4f 432 433 case DW_OP_reg0: // 0x50 434 case DW_OP_reg1: // 0x51 435 case DW_OP_reg2: // 0x52 436 case DW_OP_reg3: // 0x53 437 case DW_OP_reg4: // 0x54 438 case DW_OP_reg5: // 0x55 439 case DW_OP_reg6: // 0x56 440 case DW_OP_reg7: // 0x57 441 case DW_OP_reg8: // 0x58 442 case DW_OP_reg9: // 0x59 443 case DW_OP_reg10: // 0x5A 444 case DW_OP_reg11: // 0x5B 445 case DW_OP_reg12: // 0x5C 446 case DW_OP_reg13: // 0x5D 447 case DW_OP_reg14: // 0x5E 448 case DW_OP_reg15: // 0x5F 449 case DW_OP_reg16: // 0x60 450 case DW_OP_reg17: // 0x61 451 case DW_OP_reg18: // 0x62 452 case DW_OP_reg19: // 0x63 453 case DW_OP_reg20: // 0x64 454 case DW_OP_reg21: // 0x65 455 case DW_OP_reg22: // 0x66 456 case DW_OP_reg23: // 0x67 457 case DW_OP_reg24: // 0x68 458 case DW_OP_reg25: // 0x69 459 case DW_OP_reg26: // 0x6A 460 case DW_OP_reg27: // 0x6B 461 case DW_OP_reg28: // 0x6C 462 case DW_OP_reg29: // 0x6D 463 case DW_OP_reg30: // 0x6E 464 case DW_OP_reg31: // 0x6F 465 { 466 uint32_t reg_num = op - DW_OP_reg0; 467 if (abi) 468 { 469 RegisterInfo reg_info; 470 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) 471 { 472 if (reg_info.name) 473 { 474 s->PutCString (reg_info.name); 475 break; 476 } 477 else if (reg_info.alt_name) 478 { 479 s->PutCString (reg_info.alt_name); 480 break; 481 } 482 } 483 } 484 s->Printf("DW_OP_reg%u", reg_num); break; 485 } 486 break; 487 488 case DW_OP_breg0: 489 case DW_OP_breg1: 490 case DW_OP_breg2: 491 case DW_OP_breg3: 492 case DW_OP_breg4: 493 case DW_OP_breg5: 494 case DW_OP_breg6: 495 case DW_OP_breg7: 496 case DW_OP_breg8: 497 case DW_OP_breg9: 498 case DW_OP_breg10: 499 case DW_OP_breg11: 500 case DW_OP_breg12: 501 case DW_OP_breg13: 502 case DW_OP_breg14: 503 case DW_OP_breg15: 504 case DW_OP_breg16: 505 case DW_OP_breg17: 506 case DW_OP_breg18: 507 case DW_OP_breg19: 508 case DW_OP_breg20: 509 case DW_OP_breg21: 510 case DW_OP_breg22: 511 case DW_OP_breg23: 512 case DW_OP_breg24: 513 case DW_OP_breg25: 514 case DW_OP_breg26: 515 case DW_OP_breg27: 516 case DW_OP_breg28: 517 case DW_OP_breg29: 518 case DW_OP_breg30: 519 case DW_OP_breg31: 520 { 521 uint32_t reg_num = op - DW_OP_breg0; 522 int64_t reg_offset = m_data.GetSLEB128(&offset); 523 if (abi) 524 { 525 RegisterInfo reg_info; 526 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) 527 { 528 if (reg_info.name) 529 { 530 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset); 531 break; 532 } 533 else if (reg_info.alt_name) 534 { 535 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset); 536 break; 537 } 538 } 539 } 540 s->Printf("DW_OP_breg%i(0x%" PRIx64 ")", reg_num, reg_offset); 541 } 542 break; 543 544 case DW_OP_regx: // 0x90 1 ULEB128 register 545 { 546 uint32_t reg_num = m_data.GetULEB128(&offset); 547 if (abi) 548 { 549 RegisterInfo reg_info; 550 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) 551 { 552 if (reg_info.name) 553 { 554 s->PutCString (reg_info.name); 555 break; 556 } 557 else if (reg_info.alt_name) 558 { 559 s->PutCString (reg_info.alt_name); 560 break; 561 } 562 } 563 } 564 s->Printf("DW_OP_regx(%" PRIu32 ")", reg_num); break; 565 } 566 break; 567 case DW_OP_fbreg: // 0x91 1 SLEB128 offset 568 s->Printf("DW_OP_fbreg(%" PRIi64 ")",m_data.GetSLEB128(&offset)); 569 break; 570 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset 571 { 572 uint32_t reg_num = m_data.GetULEB128(&offset); 573 int64_t reg_offset = m_data.GetSLEB128(&offset); 574 if (abi) 575 { 576 RegisterInfo reg_info; 577 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) 578 { 579 if (reg_info.name) 580 { 581 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset); 582 break; 583 } 584 else if (reg_info.alt_name) 585 { 586 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset); 587 break; 588 } 589 } 590 } 591 s->Printf("DW_OP_bregx(reg=%" PRIu32 ",offset=%" PRIi64 ")", reg_num, reg_offset); 592 } 593 break; 594 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed 595 s->Printf("DW_OP_piece(0x%" PRIx64 ")", m_data.GetULEB128(&offset)); 596 break; 597 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved 598 s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset)); 599 break; 600 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved 601 s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset)); 602 break; 603 case DW_OP_nop: s->PutCString("DW_OP_nop"); break; // 0x96 604 case DW_OP_push_object_address: s->PutCString("DW_OP_push_object_address"); break; // 0x97 DWARF3 605 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE 606 s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset)); 607 break; 608 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE 609 s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset)); 610 break; 611 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE 612 s->Printf("DW_OP_call_ref(0x%8.8" PRIx64 ")", m_data.GetAddress(&offset)); 613 break; 614 // case DW_OP_form_tls_address: s << "form_tls_address"; break; // 0x9b DWARF3 615 // case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break; // 0x9c DWARF3 616 // case DW_OP_bit_piece: // 0x9d DWARF3 2 617 // s->Printf("DW_OP_bit_piece(0x%x, 0x%x)", m_data.GetULEB128(&offset), m_data.GetULEB128(&offset)); 618 // break; 619 // case DW_OP_lo_user: s->PutCString("DW_OP_lo_user"); break; // 0xe0 620 // case DW_OP_hi_user: s->PutCString("DW_OP_hi_user"); break; // 0xff 621 // case DW_OP_APPLE_extern: 622 // s->Printf("DW_OP_APPLE_extern(%" PRIu64 ")", m_data.GetULEB128(&offset)); 623 // break; 624 // case DW_OP_APPLE_array_ref: 625 // s->PutCString("DW_OP_APPLE_array_ref"); 626 // break; 627 case DW_OP_GNU_push_tls_address: 628 s->PutCString("DW_OP_GNU_push_tls_address"); // 0xe0 629 break; 630 case DW_OP_APPLE_uninit: 631 s->PutCString("DW_OP_APPLE_uninit"); // 0xF0 632 break; 633 // case DW_OP_APPLE_assign: // 0xF1 - pops value off and assigns it to second item on stack (2nd item must have assignable context) 634 // s->PutCString("DW_OP_APPLE_assign"); 635 // break; 636 // case DW_OP_APPLE_address_of: // 0xF2 - gets the address of the top stack item (top item must be a variable, or have value_type that is an address already) 637 // s->PutCString("DW_OP_APPLE_address_of"); 638 // break; 639 // case DW_OP_APPLE_value_of: // 0xF3 - pops the value off the stack and pushes the value of that object (top item must be a variable, or expression local) 640 // s->PutCString("DW_OP_APPLE_value_of"); 641 // break; 642 // case DW_OP_APPLE_deref_type: // 0xF4 - gets the address of the top stack item (top item must be a variable, or a clang type) 643 // s->PutCString("DW_OP_APPLE_deref_type"); 644 // break; 645 // case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression local index 646 // s->Printf("DW_OP_APPLE_expr_local(%" PRIu64 ")", m_data.GetULEB128(&offset)); 647 // break; 648 // case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size, followed by constant float data 649 // { 650 // uint8_t float_length = m_data.GetU8(&offset); 651 // s->Printf("DW_OP_APPLE_constf(<%u> ", float_length); 652 // m_data.Dump(s, offset, eFormatHex, float_length, 1, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0); 653 // s->PutChar(')'); 654 // // Consume the float data 655 // m_data.GetData(&offset, float_length); 656 // } 657 // break; 658 // case DW_OP_APPLE_scalar_cast: 659 // s->Printf("DW_OP_APPLE_scalar_cast(%s)", Scalar::GetValueTypeAsCString ((Scalar::Type)m_data.GetU8(&offset))); 660 // break; 661 // case DW_OP_APPLE_clang_cast: 662 // { 663 // clang::Type *clang_type = (clang::Type *)m_data.GetMaxU64(&offset, sizeof(void*)); 664 // s->Printf("DW_OP_APPLE_clang_cast(%p)", clang_type); 665 // } 666 // break; 667 // case DW_OP_APPLE_clear: 668 // s->PutCString("DW_OP_APPLE_clear"); 669 // break; 670 // case DW_OP_APPLE_error: // 0xFF - Stops expression evaluation and returns an error (no args) 671 // s->PutCString("DW_OP_APPLE_error"); 672 // break; 673 } 674 } 675 } 676 677 void 678 DWARFExpression::SetLocationListSlide (addr_t slide) 679 { 680 m_loclist_slide = slide; 681 } 682 683 int 684 DWARFExpression::GetRegisterKind () 685 { 686 return m_reg_kind; 687 } 688 689 void 690 DWARFExpression::SetRegisterKind (RegisterKind reg_kind) 691 { 692 m_reg_kind = reg_kind; 693 } 694 695 bool 696 DWARFExpression::IsLocationList() const 697 { 698 return m_loclist_slide != LLDB_INVALID_ADDRESS; 699 } 700 701 void 702 DWARFExpression::GetDescription (Stream *s, lldb::DescriptionLevel level, addr_t location_list_base_addr, ABI *abi) const 703 { 704 if (IsLocationList()) 705 { 706 // We have a location list 707 lldb::offset_t offset = 0; 708 uint32_t count = 0; 709 addr_t curr_base_addr = location_list_base_addr; 710 while (m_data.ValidOffset(offset)) 711 { 712 lldb::addr_t begin_addr_offset = m_data.GetAddress(&offset); 713 lldb::addr_t end_addr_offset = m_data.GetAddress(&offset); 714 if (begin_addr_offset < end_addr_offset) 715 { 716 if (count > 0) 717 s->PutCString(", "); 718 VMRange addr_range(curr_base_addr + begin_addr_offset, curr_base_addr + end_addr_offset); 719 addr_range.Dump(s, 0, 8); 720 s->PutChar('{'); 721 lldb::offset_t location_length = m_data.GetU16(&offset); 722 DumpLocation (s, offset, location_length, level, abi); 723 s->PutChar('}'); 724 offset += location_length; 725 } 726 else if (begin_addr_offset == 0 && end_addr_offset == 0) 727 { 728 // The end of the location list is marked by both the start and end offset being zero 729 break; 730 } 731 else 732 { 733 if ((m_data.GetAddressByteSize() == 4 && (begin_addr_offset == UINT32_MAX)) || 734 (m_data.GetAddressByteSize() == 8 && (begin_addr_offset == UINT64_MAX))) 735 { 736 curr_base_addr = end_addr_offset + location_list_base_addr; 737 // We have a new base address 738 if (count > 0) 739 s->PutCString(", "); 740 *s << "base_addr = " << end_addr_offset; 741 } 742 } 743 744 count++; 745 } 746 } 747 else 748 { 749 // We have a normal location that contains DW_OP location opcodes 750 DumpLocation (s, 0, m_data.GetByteSize(), level, abi); 751 } 752 } 753 754 static bool 755 ReadRegisterValueAsScalar 756 ( 757 RegisterContext *reg_ctx, 758 lldb::RegisterKind reg_kind, 759 uint32_t reg_num, 760 Error *error_ptr, 761 Value &value 762 ) 763 { 764 if (reg_ctx == NULL) 765 { 766 if (error_ptr) 767 error_ptr->SetErrorStringWithFormat("No register context in frame.\n"); 768 } 769 else 770 { 771 uint32_t native_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num); 772 if (native_reg == LLDB_INVALID_REGNUM) 773 { 774 if (error_ptr) 775 error_ptr->SetErrorStringWithFormat("Unable to convert register kind=%u reg_num=%u to a native register number.\n", reg_kind, reg_num); 776 } 777 else 778 { 779 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(native_reg); 780 RegisterValue reg_value; 781 if (reg_ctx->ReadRegister (reg_info, reg_value)) 782 { 783 if (reg_value.GetScalarValue(value.GetScalar())) 784 { 785 value.SetValueType (Value::eValueTypeScalar); 786 value.SetContext (Value::eContextTypeRegisterInfo, 787 const_cast<RegisterInfo *>(reg_info)); 788 if (error_ptr) 789 error_ptr->Clear(); 790 return true; 791 } 792 else 793 { 794 // If we get this error, then we need to implement a value 795 // buffer in the dwarf expression evaluation function... 796 if (error_ptr) 797 error_ptr->SetErrorStringWithFormat ("register %s can't be converted to a scalar value", 798 reg_info->name); 799 } 800 } 801 else 802 { 803 if (error_ptr) 804 error_ptr->SetErrorStringWithFormat("register %s is not available", reg_info->name); 805 } 806 } 807 } 808 return false; 809 } 810 811 //bool 812 //DWARFExpression::LocationListContainsLoadAddress (Process* process, const Address &addr) const 813 //{ 814 // return LocationListContainsLoadAddress(process, addr.GetLoadAddress(process)); 815 //} 816 // 817 //bool 818 //DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t load_addr) const 819 //{ 820 // if (load_addr == LLDB_INVALID_ADDRESS) 821 // return false; 822 // 823 // if (IsLocationList()) 824 // { 825 // lldb::offset_t offset = 0; 826 // 827 // addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process); 828 // 829 // if (loc_list_base_addr == LLDB_INVALID_ADDRESS) 830 // return false; 831 // 832 // while (m_data.ValidOffset(offset)) 833 // { 834 // // We need to figure out what the value is for the location. 835 // addr_t lo_pc = m_data.GetAddress(&offset); 836 // addr_t hi_pc = m_data.GetAddress(&offset); 837 // if (lo_pc == 0 && hi_pc == 0) 838 // break; 839 // else 840 // { 841 // lo_pc += loc_list_base_addr; 842 // hi_pc += loc_list_base_addr; 843 // 844 // if (lo_pc <= load_addr && load_addr < hi_pc) 845 // return true; 846 // 847 // offset += m_data.GetU16(&offset); 848 // } 849 // } 850 // } 851 // return false; 852 //} 853 854 static offset_t 855 GetOpcodeDataSize (const DataExtractor &data, const lldb::offset_t data_offset, const uint8_t op) 856 { 857 lldb::offset_t offset = data_offset; 858 switch (op) 859 { 860 case DW_OP_addr: 861 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3) 862 return data.GetAddressByteSize(); 863 864 // Opcodes with no arguments 865 case DW_OP_deref: // 0x06 866 case DW_OP_dup: // 0x12 867 case DW_OP_drop: // 0x13 868 case DW_OP_over: // 0x14 869 case DW_OP_swap: // 0x16 870 case DW_OP_rot: // 0x17 871 case DW_OP_xderef: // 0x18 872 case DW_OP_abs: // 0x19 873 case DW_OP_and: // 0x1a 874 case DW_OP_div: // 0x1b 875 case DW_OP_minus: // 0x1c 876 case DW_OP_mod: // 0x1d 877 case DW_OP_mul: // 0x1e 878 case DW_OP_neg: // 0x1f 879 case DW_OP_not: // 0x20 880 case DW_OP_or: // 0x21 881 case DW_OP_plus: // 0x22 882 case DW_OP_shl: // 0x24 883 case DW_OP_shr: // 0x25 884 case DW_OP_shra: // 0x26 885 case DW_OP_xor: // 0x27 886 case DW_OP_eq: // 0x29 887 case DW_OP_ge: // 0x2a 888 case DW_OP_gt: // 0x2b 889 case DW_OP_le: // 0x2c 890 case DW_OP_lt: // 0x2d 891 case DW_OP_ne: // 0x2e 892 case DW_OP_lit0: // 0x30 893 case DW_OP_lit1: // 0x31 894 case DW_OP_lit2: // 0x32 895 case DW_OP_lit3: // 0x33 896 case DW_OP_lit4: // 0x34 897 case DW_OP_lit5: // 0x35 898 case DW_OP_lit6: // 0x36 899 case DW_OP_lit7: // 0x37 900 case DW_OP_lit8: // 0x38 901 case DW_OP_lit9: // 0x39 902 case DW_OP_lit10: // 0x3A 903 case DW_OP_lit11: // 0x3B 904 case DW_OP_lit12: // 0x3C 905 case DW_OP_lit13: // 0x3D 906 case DW_OP_lit14: // 0x3E 907 case DW_OP_lit15: // 0x3F 908 case DW_OP_lit16: // 0x40 909 case DW_OP_lit17: // 0x41 910 case DW_OP_lit18: // 0x42 911 case DW_OP_lit19: // 0x43 912 case DW_OP_lit20: // 0x44 913 case DW_OP_lit21: // 0x45 914 case DW_OP_lit22: // 0x46 915 case DW_OP_lit23: // 0x47 916 case DW_OP_lit24: // 0x48 917 case DW_OP_lit25: // 0x49 918 case DW_OP_lit26: // 0x4A 919 case DW_OP_lit27: // 0x4B 920 case DW_OP_lit28: // 0x4C 921 case DW_OP_lit29: // 0x4D 922 case DW_OP_lit30: // 0x4E 923 case DW_OP_lit31: // 0x4f 924 case DW_OP_reg0: // 0x50 925 case DW_OP_reg1: // 0x51 926 case DW_OP_reg2: // 0x52 927 case DW_OP_reg3: // 0x53 928 case DW_OP_reg4: // 0x54 929 case DW_OP_reg5: // 0x55 930 case DW_OP_reg6: // 0x56 931 case DW_OP_reg7: // 0x57 932 case DW_OP_reg8: // 0x58 933 case DW_OP_reg9: // 0x59 934 case DW_OP_reg10: // 0x5A 935 case DW_OP_reg11: // 0x5B 936 case DW_OP_reg12: // 0x5C 937 case DW_OP_reg13: // 0x5D 938 case DW_OP_reg14: // 0x5E 939 case DW_OP_reg15: // 0x5F 940 case DW_OP_reg16: // 0x60 941 case DW_OP_reg17: // 0x61 942 case DW_OP_reg18: // 0x62 943 case DW_OP_reg19: // 0x63 944 case DW_OP_reg20: // 0x64 945 case DW_OP_reg21: // 0x65 946 case DW_OP_reg22: // 0x66 947 case DW_OP_reg23: // 0x67 948 case DW_OP_reg24: // 0x68 949 case DW_OP_reg25: // 0x69 950 case DW_OP_reg26: // 0x6A 951 case DW_OP_reg27: // 0x6B 952 case DW_OP_reg28: // 0x6C 953 case DW_OP_reg29: // 0x6D 954 case DW_OP_reg30: // 0x6E 955 case DW_OP_reg31: // 0x6F 956 case DW_OP_nop: // 0x96 957 case DW_OP_push_object_address: // 0x97 DWARF3 958 case DW_OP_form_tls_address: // 0x9b DWARF3 959 case DW_OP_call_frame_cfa: // 0x9c DWARF3 960 case DW_OP_stack_value: // 0x9f DWARF4 961 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension 962 return 0; 963 964 // Opcodes with a single 1 byte arguments 965 case DW_OP_const1u: // 0x08 1 1-byte constant 966 case DW_OP_const1s: // 0x09 1 1-byte constant 967 case DW_OP_pick: // 0x15 1 1-byte stack index 968 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved 969 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved 970 return 1; 971 972 // Opcodes with a single 2 byte arguments 973 case DW_OP_const2u: // 0x0a 1 2-byte constant 974 case DW_OP_const2s: // 0x0b 1 2-byte constant 975 case DW_OP_skip: // 0x2f 1 signed 2-byte constant 976 case DW_OP_bra: // 0x28 1 signed 2-byte constant 977 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3) 978 return 2; 979 980 // Opcodes with a single 4 byte arguments 981 case DW_OP_const4u: // 0x0c 1 4-byte constant 982 case DW_OP_const4s: // 0x0d 1 4-byte constant 983 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3) 984 return 4; 985 986 // Opcodes with a single 8 byte arguments 987 case DW_OP_const8u: // 0x0e 1 8-byte constant 988 case DW_OP_const8s: // 0x0f 1 8-byte constant 989 return 8; 990 991 // All opcodes that have a single ULEB (signed or unsigned) argument 992 case DW_OP_constu: // 0x10 1 ULEB128 constant 993 case DW_OP_consts: // 0x11 1 SLEB128 constant 994 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend 995 case DW_OP_breg0: // 0x70 1 ULEB128 register 996 case DW_OP_breg1: // 0x71 1 ULEB128 register 997 case DW_OP_breg2: // 0x72 1 ULEB128 register 998 case DW_OP_breg3: // 0x73 1 ULEB128 register 999 case DW_OP_breg4: // 0x74 1 ULEB128 register 1000 case DW_OP_breg5: // 0x75 1 ULEB128 register 1001 case DW_OP_breg6: // 0x76 1 ULEB128 register 1002 case DW_OP_breg7: // 0x77 1 ULEB128 register 1003 case DW_OP_breg8: // 0x78 1 ULEB128 register 1004 case DW_OP_breg9: // 0x79 1 ULEB128 register 1005 case DW_OP_breg10: // 0x7a 1 ULEB128 register 1006 case DW_OP_breg11: // 0x7b 1 ULEB128 register 1007 case DW_OP_breg12: // 0x7c 1 ULEB128 register 1008 case DW_OP_breg13: // 0x7d 1 ULEB128 register 1009 case DW_OP_breg14: // 0x7e 1 ULEB128 register 1010 case DW_OP_breg15: // 0x7f 1 ULEB128 register 1011 case DW_OP_breg16: // 0x80 1 ULEB128 register 1012 case DW_OP_breg17: // 0x81 1 ULEB128 register 1013 case DW_OP_breg18: // 0x82 1 ULEB128 register 1014 case DW_OP_breg19: // 0x83 1 ULEB128 register 1015 case DW_OP_breg20: // 0x84 1 ULEB128 register 1016 case DW_OP_breg21: // 0x85 1 ULEB128 register 1017 case DW_OP_breg22: // 0x86 1 ULEB128 register 1018 case DW_OP_breg23: // 0x87 1 ULEB128 register 1019 case DW_OP_breg24: // 0x88 1 ULEB128 register 1020 case DW_OP_breg25: // 0x89 1 ULEB128 register 1021 case DW_OP_breg26: // 0x8a 1 ULEB128 register 1022 case DW_OP_breg27: // 0x8b 1 ULEB128 register 1023 case DW_OP_breg28: // 0x8c 1 ULEB128 register 1024 case DW_OP_breg29: // 0x8d 1 ULEB128 register 1025 case DW_OP_breg30: // 0x8e 1 ULEB128 register 1026 case DW_OP_breg31: // 0x8f 1 ULEB128 register 1027 case DW_OP_regx: // 0x90 1 ULEB128 register 1028 case DW_OP_fbreg: // 0x91 1 SLEB128 offset 1029 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed 1030 data.Skip_LEB128(&offset); 1031 return offset - data_offset; 1032 1033 // All opcodes that have a 2 ULEB (signed or unsigned) arguments 1034 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset 1035 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3); 1036 data.Skip_LEB128(&offset); 1037 data.Skip_LEB128(&offset); 1038 return offset - data_offset; 1039 1040 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size (DWARF4) 1041 { 1042 uint64_t block_len = data.Skip_LEB128(&offset); 1043 offset += block_len; 1044 return offset - data_offset; 1045 } 1046 1047 default: 1048 break; 1049 } 1050 return LLDB_INVALID_OFFSET; 1051 } 1052 1053 lldb::addr_t 1054 DWARFExpression::GetLocation_DW_OP_addr (uint32_t op_addr_idx, bool &error) const 1055 { 1056 error = false; 1057 if (IsLocationList()) 1058 return LLDB_INVALID_ADDRESS; 1059 lldb::offset_t offset = 0; 1060 uint32_t curr_op_addr_idx = 0; 1061 while (m_data.ValidOffset(offset)) 1062 { 1063 const uint8_t op = m_data.GetU8(&offset); 1064 1065 if (op == DW_OP_addr) 1066 { 1067 const lldb::addr_t op_file_addr = m_data.GetAddress(&offset); 1068 if (curr_op_addr_idx == op_addr_idx) 1069 return op_file_addr; 1070 else 1071 ++curr_op_addr_idx; 1072 } 1073 else 1074 { 1075 const offset_t op_arg_size = GetOpcodeDataSize (m_data, offset, op); 1076 if (op_arg_size == LLDB_INVALID_OFFSET) 1077 { 1078 error = true; 1079 break; 1080 } 1081 offset += op_arg_size; 1082 } 1083 } 1084 return LLDB_INVALID_ADDRESS; 1085 } 1086 1087 bool 1088 DWARFExpression::Update_DW_OP_addr (lldb::addr_t file_addr) 1089 { 1090 if (IsLocationList()) 1091 return false; 1092 lldb::offset_t offset = 0; 1093 while (m_data.ValidOffset(offset)) 1094 { 1095 const uint8_t op = m_data.GetU8(&offset); 1096 1097 if (op == DW_OP_addr) 1098 { 1099 const uint32_t addr_byte_size = m_data.GetAddressByteSize(); 1100 // We have to make a copy of the data as we don't know if this 1101 // data is from a read only memory mapped buffer, so we duplicate 1102 // all of the data first, then modify it, and if all goes well, 1103 // we then replace the data for this expression 1104 1105 // So first we copy the data into a heap buffer 1106 std::unique_ptr<DataBufferHeap> head_data_ap (new DataBufferHeap (m_data.GetDataStart(), 1107 m_data.GetByteSize())); 1108 1109 // Make en encoder so we can write the address into the buffer using 1110 // the correct byte order (endianness) 1111 DataEncoder encoder (head_data_ap->GetBytes(), 1112 head_data_ap->GetByteSize(), 1113 m_data.GetByteOrder(), 1114 addr_byte_size); 1115 1116 // Replace the address in the new buffer 1117 if (encoder.PutMaxU64 (offset, addr_byte_size, file_addr) == UINT32_MAX) 1118 return false; 1119 1120 // All went well, so now we can reset the data using a shared 1121 // pointer to the heap data so "m_data" will now correctly 1122 // manage the heap data. 1123 m_data.SetData (DataBufferSP (head_data_ap.release())); 1124 return true; 1125 } 1126 else 1127 { 1128 const offset_t op_arg_size = GetOpcodeDataSize (m_data, offset, op); 1129 if (op_arg_size == LLDB_INVALID_OFFSET) 1130 break; 1131 offset += op_arg_size; 1132 } 1133 } 1134 return false; 1135 } 1136 1137 bool 1138 DWARFExpression::LocationListContainsAddress (lldb::addr_t loclist_base_addr, lldb::addr_t addr) const 1139 { 1140 if (addr == LLDB_INVALID_ADDRESS) 1141 return false; 1142 1143 if (IsLocationList()) 1144 { 1145 lldb::offset_t offset = 0; 1146 1147 if (loclist_base_addr == LLDB_INVALID_ADDRESS) 1148 return false; 1149 1150 while (m_data.ValidOffset(offset)) 1151 { 1152 // We need to figure out what the value is for the location. 1153 addr_t lo_pc = m_data.GetAddress(&offset); 1154 addr_t hi_pc = m_data.GetAddress(&offset); 1155 if (lo_pc == 0 && hi_pc == 0) 1156 break; 1157 else 1158 { 1159 lo_pc += loclist_base_addr - m_loclist_slide; 1160 hi_pc += loclist_base_addr - m_loclist_slide; 1161 1162 if (lo_pc <= addr && addr < hi_pc) 1163 return true; 1164 1165 offset += m_data.GetU16(&offset); 1166 } 1167 } 1168 } 1169 return false; 1170 } 1171 1172 bool 1173 DWARFExpression::GetLocation (addr_t base_addr, addr_t pc, lldb::offset_t &offset, lldb::offset_t &length) 1174 { 1175 offset = 0; 1176 if (!IsLocationList()) 1177 { 1178 length = m_data.GetByteSize(); 1179 return true; 1180 } 1181 1182 if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS) 1183 { 1184 addr_t curr_base_addr = base_addr; 1185 1186 while (m_data.ValidOffset(offset)) 1187 { 1188 // We need to figure out what the value is for the location. 1189 addr_t lo_pc = m_data.GetAddress(&offset); 1190 addr_t hi_pc = m_data.GetAddress(&offset); 1191 if (lo_pc == 0 && hi_pc == 0) 1192 { 1193 break; 1194 } 1195 else 1196 { 1197 lo_pc += curr_base_addr - m_loclist_slide; 1198 hi_pc += curr_base_addr - m_loclist_slide; 1199 1200 length = m_data.GetU16(&offset); 1201 1202 if (length > 0 && lo_pc <= pc && pc < hi_pc) 1203 return true; 1204 1205 offset += length; 1206 } 1207 } 1208 } 1209 offset = LLDB_INVALID_OFFSET; 1210 length = 0; 1211 return false; 1212 } 1213 1214 bool 1215 DWARFExpression::DumpLocationForAddress (Stream *s, 1216 lldb::DescriptionLevel level, 1217 addr_t base_addr, 1218 addr_t address, 1219 ABI *abi) 1220 { 1221 lldb::offset_t offset = 0; 1222 lldb::offset_t length = 0; 1223 1224 if (GetLocation (base_addr, address, offset, length)) 1225 { 1226 if (length > 0) 1227 { 1228 DumpLocation(s, offset, length, level, abi); 1229 return true; 1230 } 1231 } 1232 return false; 1233 } 1234 1235 bool 1236 DWARFExpression::Evaluate 1237 ( 1238 ExecutionContextScope *exe_scope, 1239 ClangExpressionVariableList *expr_locals, 1240 ClangExpressionDeclMap *decl_map, 1241 lldb::addr_t loclist_base_load_addr, 1242 const Value* initial_value_ptr, 1243 Value& result, 1244 Error *error_ptr 1245 ) const 1246 { 1247 ExecutionContext exe_ctx (exe_scope); 1248 return Evaluate(&exe_ctx, expr_locals, decl_map, NULL, loclist_base_load_addr, initial_value_ptr, result, error_ptr); 1249 } 1250 1251 bool 1252 DWARFExpression::Evaluate 1253 ( 1254 ExecutionContext *exe_ctx, 1255 ClangExpressionVariableList *expr_locals, 1256 ClangExpressionDeclMap *decl_map, 1257 RegisterContext *reg_ctx, 1258 lldb::addr_t loclist_base_load_addr, 1259 const Value* initial_value_ptr, 1260 Value& result, 1261 Error *error_ptr 1262 ) const 1263 { 1264 ModuleSP module_sp = m_module_wp.lock(); 1265 1266 if (IsLocationList()) 1267 { 1268 lldb::offset_t offset = 0; 1269 addr_t pc; 1270 StackFrame *frame = NULL; 1271 if (reg_ctx) 1272 pc = reg_ctx->GetPC(); 1273 else 1274 { 1275 frame = exe_ctx->GetFramePtr(); 1276 if (!frame) 1277 return false; 1278 RegisterContextSP reg_ctx_sp = frame->GetRegisterContext(); 1279 if (!reg_ctx_sp) 1280 return false; 1281 pc = reg_ctx_sp->GetPC(); 1282 } 1283 1284 if (loclist_base_load_addr != LLDB_INVALID_ADDRESS) 1285 { 1286 if (pc == LLDB_INVALID_ADDRESS) 1287 { 1288 if (error_ptr) 1289 error_ptr->SetErrorString("Invalid PC in frame."); 1290 return false; 1291 } 1292 1293 addr_t curr_loclist_base_load_addr = loclist_base_load_addr; 1294 1295 while (m_data.ValidOffset(offset)) 1296 { 1297 // We need to figure out what the value is for the location. 1298 addr_t lo_pc = m_data.GetAddress(&offset); 1299 addr_t hi_pc = m_data.GetAddress(&offset); 1300 if (lo_pc == 0 && hi_pc == 0) 1301 { 1302 break; 1303 } 1304 else 1305 { 1306 lo_pc += curr_loclist_base_load_addr - m_loclist_slide; 1307 hi_pc += curr_loclist_base_load_addr - m_loclist_slide; 1308 1309 uint16_t length = m_data.GetU16(&offset); 1310 1311 if (length > 0 && lo_pc <= pc && pc < hi_pc) 1312 { 1313 return DWARFExpression::Evaluate (exe_ctx, expr_locals, decl_map, reg_ctx, module_sp, m_data, offset, length, m_reg_kind, initial_value_ptr, result, error_ptr); 1314 } 1315 offset += length; 1316 } 1317 } 1318 } 1319 if (error_ptr) 1320 error_ptr->SetErrorString ("variable not available"); 1321 return false; 1322 } 1323 1324 // Not a location list, just a single expression. 1325 return DWARFExpression::Evaluate (exe_ctx, expr_locals, decl_map, reg_ctx, module_sp, m_data, 0, m_data.GetByteSize(), m_reg_kind, initial_value_ptr, result, error_ptr); 1326 } 1327 1328 1329 1330 bool 1331 DWARFExpression::Evaluate 1332 ( 1333 ExecutionContext *exe_ctx, 1334 ClangExpressionVariableList *expr_locals, 1335 ClangExpressionDeclMap *decl_map, 1336 RegisterContext *reg_ctx, 1337 lldb::ModuleSP module_sp, 1338 const DataExtractor& opcodes, 1339 const lldb::offset_t opcodes_offset, 1340 const lldb::offset_t opcodes_length, 1341 const lldb::RegisterKind reg_kind, 1342 const Value* initial_value_ptr, 1343 Value& result, 1344 Error *error_ptr 1345 ) 1346 { 1347 1348 if (opcodes_length == 0) 1349 { 1350 if (error_ptr) 1351 error_ptr->SetErrorString ("no location, value may have been optimized out"); 1352 return false; 1353 } 1354 std::vector<Value> stack; 1355 1356 Process *process = NULL; 1357 StackFrame *frame = NULL; 1358 1359 if (exe_ctx) 1360 { 1361 process = exe_ctx->GetProcessPtr(); 1362 frame = exe_ctx->GetFramePtr(); 1363 } 1364 if (reg_ctx == NULL && frame) 1365 reg_ctx = frame->GetRegisterContext().get(); 1366 1367 if (initial_value_ptr) 1368 stack.push_back(*initial_value_ptr); 1369 1370 lldb::offset_t offset = opcodes_offset; 1371 const lldb::offset_t end_offset = opcodes_offset + opcodes_length; 1372 Value tmp; 1373 uint32_t reg_num; 1374 1375 /// Insertion point for evaluating multi-piece expression. 1376 uint64_t op_piece_offset = 0; 1377 Value pieces; // Used for DW_OP_piece 1378 1379 // Make sure all of the data is available in opcodes. 1380 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length)) 1381 { 1382 if (error_ptr) 1383 error_ptr->SetErrorString ("invalid offset and/or length for opcodes buffer."); 1384 return false; 1385 } 1386 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1387 1388 1389 while (opcodes.ValidOffset(offset) && offset < end_offset) 1390 { 1391 const lldb::offset_t op_offset = offset; 1392 const uint8_t op = opcodes.GetU8(&offset); 1393 1394 if (log && log->GetVerbose()) 1395 { 1396 size_t count = stack.size(); 1397 log->Printf("Stack before operation has %" PRIu64 " values:", (uint64_t)count); 1398 for (size_t i=0; i<count; ++i) 1399 { 1400 StreamString new_value; 1401 new_value.Printf("[%" PRIu64 "]", (uint64_t)i); 1402 stack[i].Dump(&new_value); 1403 log->Printf(" %s", new_value.GetData()); 1404 } 1405 log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op)); 1406 } 1407 switch (op) 1408 { 1409 //---------------------------------------------------------------------- 1410 // The DW_OP_addr operation has a single operand that encodes a machine 1411 // address and whose size is the size of an address on the target machine. 1412 //---------------------------------------------------------------------- 1413 case DW_OP_addr: 1414 stack.push_back(Scalar(opcodes.GetAddress(&offset))); 1415 stack.back().SetValueType (Value::eValueTypeFileAddress); 1416 break; 1417 1418 //---------------------------------------------------------------------- 1419 // The DW_OP_addr_sect_offset4 is used for any location expressions in 1420 // shared libraries that have a location like: 1421 // DW_OP_addr(0x1000) 1422 // If this address resides in a shared library, then this virtual 1423 // address won't make sense when it is evaluated in the context of a 1424 // running process where shared libraries have been slid. To account for 1425 // this, this new address type where we can store the section pointer 1426 // and a 4 byte offset. 1427 //---------------------------------------------------------------------- 1428 // case DW_OP_addr_sect_offset4: 1429 // { 1430 // result_type = eResultTypeFileAddress; 1431 // lldb::Section *sect = (lldb::Section *)opcodes.GetMaxU64(&offset, sizeof(void *)); 1432 // lldb::addr_t sect_offset = opcodes.GetU32(&offset); 1433 // 1434 // Address so_addr (sect, sect_offset); 1435 // lldb::addr_t load_addr = so_addr.GetLoadAddress(); 1436 // if (load_addr != LLDB_INVALID_ADDRESS) 1437 // { 1438 // // We successfully resolve a file address to a load 1439 // // address. 1440 // stack.push_back(load_addr); 1441 // break; 1442 // } 1443 // else 1444 // { 1445 // // We were able 1446 // if (error_ptr) 1447 // error_ptr->SetErrorStringWithFormat ("Section %s in %s is not currently loaded.\n", sect->GetName().AsCString(), sect->GetModule()->GetFileSpec().GetFilename().AsCString()); 1448 // return false; 1449 // } 1450 // } 1451 // break; 1452 1453 //---------------------------------------------------------------------- 1454 // OPCODE: DW_OP_deref 1455 // OPERANDS: none 1456 // DESCRIPTION: Pops the top stack entry and treats it as an address. 1457 // The value retrieved from that address is pushed. The size of the 1458 // data retrieved from the dereferenced address is the size of an 1459 // address on the target machine. 1460 //---------------------------------------------------------------------- 1461 case DW_OP_deref: 1462 { 1463 if (stack.empty()) 1464 { 1465 if (error_ptr) 1466 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref."); 1467 return false; 1468 } 1469 Value::ValueType value_type = stack.back().GetValueType(); 1470 switch (value_type) 1471 { 1472 case Value::eValueTypeHostAddress: 1473 { 1474 void *src = (void *)stack.back().GetScalar().ULongLong(); 1475 intptr_t ptr; 1476 ::memcpy (&ptr, src, sizeof(void *)); 1477 stack.back().GetScalar() = ptr; 1478 stack.back().ClearContext(); 1479 } 1480 break; 1481 case Value::eValueTypeLoadAddress: 1482 if (exe_ctx) 1483 { 1484 if (process) 1485 { 1486 lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1487 Error error; 1488 lldb::addr_t pointer_value = process->ReadPointerFromMemory(pointer_addr, error); 1489 if (pointer_value != LLDB_INVALID_ADDRESS) 1490 { 1491 stack.back().GetScalar() = pointer_value; 1492 stack.back().ClearContext(); 1493 } 1494 else 1495 { 1496 if (error_ptr) 1497 error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%" PRIx64 " for DW_OP_deref: %s\n", 1498 pointer_addr, 1499 error.AsCString()); 1500 return false; 1501 } 1502 } 1503 else 1504 { 1505 if (error_ptr) 1506 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n"); 1507 return false; 1508 } 1509 } 1510 else 1511 { 1512 if (error_ptr) 1513 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n"); 1514 return false; 1515 } 1516 break; 1517 1518 default: 1519 break; 1520 } 1521 1522 } 1523 break; 1524 1525 //---------------------------------------------------------------------- 1526 // OPCODE: DW_OP_deref_size 1527 // OPERANDS: 1 1528 // 1 - uint8_t that specifies the size of the data to dereference. 1529 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top 1530 // stack entry and treats it as an address. The value retrieved from that 1531 // address is pushed. In the DW_OP_deref_size operation, however, the 1532 // size in bytes of the data retrieved from the dereferenced address is 1533 // specified by the single operand. This operand is a 1-byte unsigned 1534 // integral constant whose value may not be larger than the size of an 1535 // address on the target machine. The data retrieved is zero extended 1536 // to the size of an address on the target machine before being pushed 1537 // on the expression stack. 1538 //---------------------------------------------------------------------- 1539 case DW_OP_deref_size: 1540 { 1541 if (stack.empty()) 1542 { 1543 if (error_ptr) 1544 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref_size."); 1545 return false; 1546 } 1547 uint8_t size = opcodes.GetU8(&offset); 1548 Value::ValueType value_type = stack.back().GetValueType(); 1549 switch (value_type) 1550 { 1551 case Value::eValueTypeHostAddress: 1552 { 1553 void *src = (void *)stack.back().GetScalar().ULongLong(); 1554 intptr_t ptr; 1555 ::memcpy (&ptr, src, sizeof(void *)); 1556 // I can't decide whether the size operand should apply to the bytes in their 1557 // lldb-host endianness or the target endianness.. I doubt this'll ever come up 1558 // but I'll opt for assuming big endian regardless. 1559 switch (size) 1560 { 1561 case 1: ptr = ptr & 0xff; break; 1562 case 2: ptr = ptr & 0xffff; break; 1563 case 3: ptr = ptr & 0xffffff; break; 1564 case 4: ptr = ptr & 0xffffffff; break; 1565 // the casts are added to work around the case where intptr_t is a 32 bit quantity; 1566 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this program. 1567 case 5: ptr = (intptr_t) ptr & 0xffffffffffULL; break; 1568 case 6: ptr = (intptr_t) ptr & 0xffffffffffffULL; break; 1569 case 7: ptr = (intptr_t) ptr & 0xffffffffffffffULL; break; 1570 default: break; 1571 } 1572 stack.back().GetScalar() = ptr; 1573 stack.back().ClearContext(); 1574 } 1575 break; 1576 case Value::eValueTypeLoadAddress: 1577 if (exe_ctx) 1578 { 1579 if (process) 1580 { 1581 lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1582 uint8_t addr_bytes[sizeof(lldb::addr_t)]; 1583 Error error; 1584 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) == size) 1585 { 1586 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), process->GetByteOrder(), size); 1587 lldb::offset_t addr_data_offset = 0; 1588 switch (size) 1589 { 1590 case 1: stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset); break; 1591 case 2: stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset); break; 1592 case 4: stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset); break; 1593 case 8: stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset); break; 1594 default: stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset); 1595 } 1596 stack.back().ClearContext(); 1597 } 1598 else 1599 { 1600 if (error_ptr) 1601 error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%" PRIx64 " for DW_OP_deref: %s\n", 1602 pointer_addr, 1603 error.AsCString()); 1604 return false; 1605 } 1606 } 1607 else 1608 { 1609 if (error_ptr) 1610 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n"); 1611 return false; 1612 } 1613 } 1614 else 1615 { 1616 if (error_ptr) 1617 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n"); 1618 return false; 1619 } 1620 break; 1621 1622 default: 1623 break; 1624 } 1625 1626 } 1627 break; 1628 1629 //---------------------------------------------------------------------- 1630 // OPCODE: DW_OP_xderef_size 1631 // OPERANDS: 1 1632 // 1 - uint8_t that specifies the size of the data to dereference. 1633 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at 1634 // the top of the stack is treated as an address. The second stack 1635 // entry is treated as an "address space identifier" for those 1636 // architectures that support multiple address spaces. The top two 1637 // stack elements are popped, a data item is retrieved through an 1638 // implementation-defined address calculation and pushed as the new 1639 // stack top. In the DW_OP_xderef_size operation, however, the size in 1640 // bytes of the data retrieved from the dereferenced address is 1641 // specified by the single operand. This operand is a 1-byte unsigned 1642 // integral constant whose value may not be larger than the size of an 1643 // address on the target machine. The data retrieved is zero extended 1644 // to the size of an address on the target machine before being pushed 1645 // on the expression stack. 1646 //---------------------------------------------------------------------- 1647 case DW_OP_xderef_size: 1648 if (error_ptr) 1649 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size."); 1650 return false; 1651 //---------------------------------------------------------------------- 1652 // OPCODE: DW_OP_xderef 1653 // OPERANDS: none 1654 // DESCRIPTION: Provides an extended dereference mechanism. The entry at 1655 // the top of the stack is treated as an address. The second stack entry 1656 // is treated as an "address space identifier" for those architectures 1657 // that support multiple address spaces. The top two stack elements are 1658 // popped, a data item is retrieved through an implementation-defined 1659 // address calculation and pushed as the new stack top. The size of the 1660 // data retrieved from the dereferenced address is the size of an address 1661 // on the target machine. 1662 //---------------------------------------------------------------------- 1663 case DW_OP_xderef: 1664 if (error_ptr) 1665 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef."); 1666 return false; 1667 1668 //---------------------------------------------------------------------- 1669 // All DW_OP_constXXX opcodes have a single operand as noted below: 1670 // 1671 // Opcode Operand 1 1672 // --------------- ---------------------------------------------------- 1673 // DW_OP_const1u 1-byte unsigned integer constant 1674 // DW_OP_const1s 1-byte signed integer constant 1675 // DW_OP_const2u 2-byte unsigned integer constant 1676 // DW_OP_const2s 2-byte signed integer constant 1677 // DW_OP_const4u 4-byte unsigned integer constant 1678 // DW_OP_const4s 4-byte signed integer constant 1679 // DW_OP_const8u 8-byte unsigned integer constant 1680 // DW_OP_const8s 8-byte signed integer constant 1681 // DW_OP_constu unsigned LEB128 integer constant 1682 // DW_OP_consts signed LEB128 integer constant 1683 //---------------------------------------------------------------------- 1684 case DW_OP_const1u : stack.push_back(Scalar(( uint8_t)opcodes.GetU8 (&offset))); break; 1685 case DW_OP_const1s : stack.push_back(Scalar(( int8_t)opcodes.GetU8 (&offset))); break; 1686 case DW_OP_const2u : stack.push_back(Scalar((uint16_t)opcodes.GetU16 (&offset))); break; 1687 case DW_OP_const2s : stack.push_back(Scalar(( int16_t)opcodes.GetU16 (&offset))); break; 1688 case DW_OP_const4u : stack.push_back(Scalar((uint32_t)opcodes.GetU32 (&offset))); break; 1689 case DW_OP_const4s : stack.push_back(Scalar(( int32_t)opcodes.GetU32 (&offset))); break; 1690 case DW_OP_const8u : stack.push_back(Scalar((uint64_t)opcodes.GetU64 (&offset))); break; 1691 case DW_OP_const8s : stack.push_back(Scalar(( int64_t)opcodes.GetU64 (&offset))); break; 1692 case DW_OP_constu : stack.push_back(Scalar(opcodes.GetULEB128 (&offset))); break; 1693 case DW_OP_consts : stack.push_back(Scalar(opcodes.GetSLEB128 (&offset))); break; 1694 1695 //---------------------------------------------------------------------- 1696 // OPCODE: DW_OP_dup 1697 // OPERANDS: none 1698 // DESCRIPTION: duplicates the value at the top of the stack 1699 //---------------------------------------------------------------------- 1700 case DW_OP_dup: 1701 if (stack.empty()) 1702 { 1703 if (error_ptr) 1704 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup."); 1705 return false; 1706 } 1707 else 1708 stack.push_back(stack.back()); 1709 break; 1710 1711 //---------------------------------------------------------------------- 1712 // OPCODE: DW_OP_drop 1713 // OPERANDS: none 1714 // DESCRIPTION: pops the value at the top of the stack 1715 //---------------------------------------------------------------------- 1716 case DW_OP_drop: 1717 if (stack.empty()) 1718 { 1719 if (error_ptr) 1720 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop."); 1721 return false; 1722 } 1723 else 1724 stack.pop_back(); 1725 break; 1726 1727 //---------------------------------------------------------------------- 1728 // OPCODE: DW_OP_over 1729 // OPERANDS: none 1730 // DESCRIPTION: Duplicates the entry currently second in the stack at 1731 // the top of the stack. 1732 //---------------------------------------------------------------------- 1733 case DW_OP_over: 1734 if (stack.size() < 2) 1735 { 1736 if (error_ptr) 1737 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_over."); 1738 return false; 1739 } 1740 else 1741 stack.push_back(stack[stack.size() - 2]); 1742 break; 1743 1744 1745 //---------------------------------------------------------------------- 1746 // OPCODE: DW_OP_pick 1747 // OPERANDS: uint8_t index into the current stack 1748 // DESCRIPTION: The stack entry with the specified index (0 through 255, 1749 // inclusive) is pushed on the stack 1750 //---------------------------------------------------------------------- 1751 case DW_OP_pick: 1752 { 1753 uint8_t pick_idx = opcodes.GetU8(&offset); 1754 if (pick_idx < stack.size()) 1755 stack.push_back(stack[pick_idx]); 1756 else 1757 { 1758 if (error_ptr) 1759 error_ptr->SetErrorStringWithFormat("Index %u out of range for DW_OP_pick.\n", pick_idx); 1760 return false; 1761 } 1762 } 1763 break; 1764 1765 //---------------------------------------------------------------------- 1766 // OPCODE: DW_OP_swap 1767 // OPERANDS: none 1768 // DESCRIPTION: swaps the top two stack entries. The entry at the top 1769 // of the stack becomes the second stack entry, and the second entry 1770 // becomes the top of the stack 1771 //---------------------------------------------------------------------- 1772 case DW_OP_swap: 1773 if (stack.size() < 2) 1774 { 1775 if (error_ptr) 1776 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_swap."); 1777 return false; 1778 } 1779 else 1780 { 1781 tmp = stack.back(); 1782 stack.back() = stack[stack.size() - 2]; 1783 stack[stack.size() - 2] = tmp; 1784 } 1785 break; 1786 1787 //---------------------------------------------------------------------- 1788 // OPCODE: DW_OP_rot 1789 // OPERANDS: none 1790 // DESCRIPTION: Rotates the first three stack entries. The entry at 1791 // the top of the stack becomes the third stack entry, the second 1792 // entry becomes the top of the stack, and the third entry becomes 1793 // the second entry. 1794 //---------------------------------------------------------------------- 1795 case DW_OP_rot: 1796 if (stack.size() < 3) 1797 { 1798 if (error_ptr) 1799 error_ptr->SetErrorString("Expression stack needs at least 3 items for DW_OP_rot."); 1800 return false; 1801 } 1802 else 1803 { 1804 size_t last_idx = stack.size() - 1; 1805 Value old_top = stack[last_idx]; 1806 stack[last_idx] = stack[last_idx - 1]; 1807 stack[last_idx - 1] = stack[last_idx - 2]; 1808 stack[last_idx - 2] = old_top; 1809 } 1810 break; 1811 1812 //---------------------------------------------------------------------- 1813 // OPCODE: DW_OP_abs 1814 // OPERANDS: none 1815 // DESCRIPTION: pops the top stack entry, interprets it as a signed 1816 // value and pushes its absolute value. If the absolute value can not be 1817 // represented, the result is undefined. 1818 //---------------------------------------------------------------------- 1819 case DW_OP_abs: 1820 if (stack.empty()) 1821 { 1822 if (error_ptr) 1823 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_abs."); 1824 return false; 1825 } 1826 else if (stack.back().ResolveValue(exe_ctx).AbsoluteValue() == false) 1827 { 1828 if (error_ptr) 1829 error_ptr->SetErrorString("Failed to take the absolute value of the first stack item."); 1830 return false; 1831 } 1832 break; 1833 1834 //---------------------------------------------------------------------- 1835 // OPCODE: DW_OP_and 1836 // OPERANDS: none 1837 // DESCRIPTION: pops the top two stack values, performs a bitwise and 1838 // operation on the two, and pushes the result. 1839 //---------------------------------------------------------------------- 1840 case DW_OP_and: 1841 if (stack.size() < 2) 1842 { 1843 if (error_ptr) 1844 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_and."); 1845 return false; 1846 } 1847 else 1848 { 1849 tmp = stack.back(); 1850 stack.pop_back(); 1851 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx); 1852 } 1853 break; 1854 1855 //---------------------------------------------------------------------- 1856 // OPCODE: DW_OP_div 1857 // OPERANDS: none 1858 // DESCRIPTION: pops the top two stack values, divides the former second 1859 // entry by the former top of the stack using signed division, and 1860 // pushes the result. 1861 //---------------------------------------------------------------------- 1862 case DW_OP_div: 1863 if (stack.size() < 2) 1864 { 1865 if (error_ptr) 1866 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_div."); 1867 return false; 1868 } 1869 else 1870 { 1871 tmp = stack.back(); 1872 if (tmp.ResolveValue(exe_ctx).IsZero()) 1873 { 1874 if (error_ptr) 1875 error_ptr->SetErrorString("Divide by zero."); 1876 return false; 1877 } 1878 else 1879 { 1880 stack.pop_back(); 1881 stack.back() = stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx); 1882 if (!stack.back().ResolveValue(exe_ctx).IsValid()) 1883 { 1884 if (error_ptr) 1885 error_ptr->SetErrorString("Divide failed."); 1886 return false; 1887 } 1888 } 1889 } 1890 break; 1891 1892 //---------------------------------------------------------------------- 1893 // OPCODE: DW_OP_minus 1894 // OPERANDS: none 1895 // DESCRIPTION: pops the top two stack values, subtracts the former top 1896 // of the stack from the former second entry, and pushes the result. 1897 //---------------------------------------------------------------------- 1898 case DW_OP_minus: 1899 if (stack.size() < 2) 1900 { 1901 if (error_ptr) 1902 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_minus."); 1903 return false; 1904 } 1905 else 1906 { 1907 tmp = stack.back(); 1908 stack.pop_back(); 1909 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx); 1910 } 1911 break; 1912 1913 //---------------------------------------------------------------------- 1914 // OPCODE: DW_OP_mod 1915 // OPERANDS: none 1916 // DESCRIPTION: pops the top two stack values and pushes the result of 1917 // the calculation: former second stack entry modulo the former top of 1918 // the stack. 1919 //---------------------------------------------------------------------- 1920 case DW_OP_mod: 1921 if (stack.size() < 2) 1922 { 1923 if (error_ptr) 1924 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mod."); 1925 return false; 1926 } 1927 else 1928 { 1929 tmp = stack.back(); 1930 stack.pop_back(); 1931 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx); 1932 } 1933 break; 1934 1935 1936 //---------------------------------------------------------------------- 1937 // OPCODE: DW_OP_mul 1938 // OPERANDS: none 1939 // DESCRIPTION: pops the top two stack entries, multiplies them 1940 // together, and pushes the result. 1941 //---------------------------------------------------------------------- 1942 case DW_OP_mul: 1943 if (stack.size() < 2) 1944 { 1945 if (error_ptr) 1946 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mul."); 1947 return false; 1948 } 1949 else 1950 { 1951 tmp = stack.back(); 1952 stack.pop_back(); 1953 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx); 1954 } 1955 break; 1956 1957 //---------------------------------------------------------------------- 1958 // OPCODE: DW_OP_neg 1959 // OPERANDS: none 1960 // DESCRIPTION: pops the top stack entry, and pushes its negation. 1961 //---------------------------------------------------------------------- 1962 case DW_OP_neg: 1963 if (stack.empty()) 1964 { 1965 if (error_ptr) 1966 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_neg."); 1967 return false; 1968 } 1969 else 1970 { 1971 if (stack.back().ResolveValue(exe_ctx).UnaryNegate() == false) 1972 { 1973 if (error_ptr) 1974 error_ptr->SetErrorString("Unary negate failed."); 1975 return false; 1976 } 1977 } 1978 break; 1979 1980 //---------------------------------------------------------------------- 1981 // OPCODE: DW_OP_not 1982 // OPERANDS: none 1983 // DESCRIPTION: pops the top stack entry, and pushes its bitwise 1984 // complement 1985 //---------------------------------------------------------------------- 1986 case DW_OP_not: 1987 if (stack.empty()) 1988 { 1989 if (error_ptr) 1990 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_not."); 1991 return false; 1992 } 1993 else 1994 { 1995 if (stack.back().ResolveValue(exe_ctx).OnesComplement() == false) 1996 { 1997 if (error_ptr) 1998 error_ptr->SetErrorString("Logical NOT failed."); 1999 return false; 2000 } 2001 } 2002 break; 2003 2004 //---------------------------------------------------------------------- 2005 // OPCODE: DW_OP_or 2006 // OPERANDS: none 2007 // DESCRIPTION: pops the top two stack entries, performs a bitwise or 2008 // operation on the two, and pushes the result. 2009 //---------------------------------------------------------------------- 2010 case DW_OP_or: 2011 if (stack.size() < 2) 2012 { 2013 if (error_ptr) 2014 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_or."); 2015 return false; 2016 } 2017 else 2018 { 2019 tmp = stack.back(); 2020 stack.pop_back(); 2021 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx); 2022 } 2023 break; 2024 2025 //---------------------------------------------------------------------- 2026 // OPCODE: DW_OP_plus 2027 // OPERANDS: none 2028 // DESCRIPTION: pops the top two stack entries, adds them together, and 2029 // pushes the result. 2030 //---------------------------------------------------------------------- 2031 case DW_OP_plus: 2032 if (stack.size() < 2) 2033 { 2034 if (error_ptr) 2035 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_plus."); 2036 return false; 2037 } 2038 else 2039 { 2040 tmp = stack.back(); 2041 stack.pop_back(); 2042 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) + tmp.ResolveValue(exe_ctx); 2043 } 2044 break; 2045 2046 //---------------------------------------------------------------------- 2047 // OPCODE: DW_OP_plus_uconst 2048 // OPERANDS: none 2049 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128 2050 // constant operand and pushes the result. 2051 //---------------------------------------------------------------------- 2052 case DW_OP_plus_uconst: 2053 if (stack.empty()) 2054 { 2055 if (error_ptr) 2056 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_plus_uconst."); 2057 return false; 2058 } 2059 else 2060 { 2061 const uint64_t uconst_value = opcodes.GetULEB128(&offset); 2062 // Implicit conversion from a UINT to a Scalar... 2063 stack.back().ResolveValue(exe_ctx) += uconst_value; 2064 if (!stack.back().ResolveValue(exe_ctx).IsValid()) 2065 { 2066 if (error_ptr) 2067 error_ptr->SetErrorString("DW_OP_plus_uconst failed."); 2068 return false; 2069 } 2070 } 2071 break; 2072 2073 //---------------------------------------------------------------------- 2074 // OPCODE: DW_OP_shl 2075 // OPERANDS: none 2076 // DESCRIPTION: pops the top two stack entries, shifts the former 2077 // second entry left by the number of bits specified by the former top 2078 // of the stack, and pushes the result. 2079 //---------------------------------------------------------------------- 2080 case DW_OP_shl: 2081 if (stack.size() < 2) 2082 { 2083 if (error_ptr) 2084 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shl."); 2085 return false; 2086 } 2087 else 2088 { 2089 tmp = stack.back(); 2090 stack.pop_back(); 2091 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx); 2092 } 2093 break; 2094 2095 //---------------------------------------------------------------------- 2096 // OPCODE: DW_OP_shr 2097 // OPERANDS: none 2098 // DESCRIPTION: pops the top two stack entries, shifts the former second 2099 // entry right logically (filling with zero bits) by the number of bits 2100 // specified by the former top of the stack, and pushes the result. 2101 //---------------------------------------------------------------------- 2102 case DW_OP_shr: 2103 if (stack.size() < 2) 2104 { 2105 if (error_ptr) 2106 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shr."); 2107 return false; 2108 } 2109 else 2110 { 2111 tmp = stack.back(); 2112 stack.pop_back(); 2113 if (stack.back().ResolveValue(exe_ctx).ShiftRightLogical(tmp.ResolveValue(exe_ctx)) == false) 2114 { 2115 if (error_ptr) 2116 error_ptr->SetErrorString("DW_OP_shr failed."); 2117 return false; 2118 } 2119 } 2120 break; 2121 2122 //---------------------------------------------------------------------- 2123 // OPCODE: DW_OP_shra 2124 // OPERANDS: none 2125 // DESCRIPTION: pops the top two stack entries, shifts the former second 2126 // entry right arithmetically (divide the magnitude by 2, keep the same 2127 // sign for the result) by the number of bits specified by the former 2128 // top of the stack, and pushes the result. 2129 //---------------------------------------------------------------------- 2130 case DW_OP_shra: 2131 if (stack.size() < 2) 2132 { 2133 if (error_ptr) 2134 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shra."); 2135 return false; 2136 } 2137 else 2138 { 2139 tmp = stack.back(); 2140 stack.pop_back(); 2141 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx); 2142 } 2143 break; 2144 2145 //---------------------------------------------------------------------- 2146 // OPCODE: DW_OP_xor 2147 // OPERANDS: none 2148 // DESCRIPTION: pops the top two stack entries, performs the bitwise 2149 // exclusive-or operation on the two, and pushes the result. 2150 //---------------------------------------------------------------------- 2151 case DW_OP_xor: 2152 if (stack.size() < 2) 2153 { 2154 if (error_ptr) 2155 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_xor."); 2156 return false; 2157 } 2158 else 2159 { 2160 tmp = stack.back(); 2161 stack.pop_back(); 2162 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx); 2163 } 2164 break; 2165 2166 2167 //---------------------------------------------------------------------- 2168 // OPCODE: DW_OP_skip 2169 // OPERANDS: int16_t 2170 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte 2171 // signed integer constant. The 2-byte constant is the number of bytes 2172 // of the DWARF expression to skip forward or backward from the current 2173 // operation, beginning after the 2-byte constant. 2174 //---------------------------------------------------------------------- 2175 case DW_OP_skip: 2176 { 2177 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset); 2178 lldb::offset_t new_offset = offset + skip_offset; 2179 if (new_offset >= opcodes_offset && new_offset < end_offset) 2180 offset = new_offset; 2181 else 2182 { 2183 if (error_ptr) 2184 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip."); 2185 return false; 2186 } 2187 } 2188 break; 2189 2190 //---------------------------------------------------------------------- 2191 // OPCODE: DW_OP_bra 2192 // OPERANDS: int16_t 2193 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte 2194 // signed integer constant. This operation pops the top of stack. If 2195 // the value popped is not the constant 0, the 2-byte constant operand 2196 // is the number of bytes of the DWARF expression to skip forward or 2197 // backward from the current operation, beginning after the 2-byte 2198 // constant. 2199 //---------------------------------------------------------------------- 2200 case DW_OP_bra: 2201 { 2202 tmp = stack.back(); 2203 stack.pop_back(); 2204 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset); 2205 Scalar zero(0); 2206 if (tmp.ResolveValue(exe_ctx) != zero) 2207 { 2208 lldb::offset_t new_offset = offset + bra_offset; 2209 if (new_offset >= opcodes_offset && new_offset < end_offset) 2210 offset = new_offset; 2211 else 2212 { 2213 if (error_ptr) 2214 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra."); 2215 return false; 2216 } 2217 } 2218 } 2219 break; 2220 2221 //---------------------------------------------------------------------- 2222 // OPCODE: DW_OP_eq 2223 // OPERANDS: none 2224 // DESCRIPTION: pops the top two stack values, compares using the 2225 // equals (==) operator. 2226 // STACK RESULT: push the constant value 1 onto the stack if the result 2227 // of the operation is true or the constant value 0 if the result of the 2228 // operation is false. 2229 //---------------------------------------------------------------------- 2230 case DW_OP_eq: 2231 if (stack.size() < 2) 2232 { 2233 if (error_ptr) 2234 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_eq."); 2235 return false; 2236 } 2237 else 2238 { 2239 tmp = stack.back(); 2240 stack.pop_back(); 2241 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx); 2242 } 2243 break; 2244 2245 //---------------------------------------------------------------------- 2246 // OPCODE: DW_OP_ge 2247 // OPERANDS: none 2248 // DESCRIPTION: pops the top two stack values, compares using the 2249 // greater than or equal to (>=) operator. 2250 // STACK RESULT: push the constant value 1 onto the stack if the result 2251 // of the operation is true or the constant value 0 if the result of the 2252 // operation is false. 2253 //---------------------------------------------------------------------- 2254 case DW_OP_ge: 2255 if (stack.size() < 2) 2256 { 2257 if (error_ptr) 2258 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ge."); 2259 return false; 2260 } 2261 else 2262 { 2263 tmp = stack.back(); 2264 stack.pop_back(); 2265 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx); 2266 } 2267 break; 2268 2269 //---------------------------------------------------------------------- 2270 // OPCODE: DW_OP_gt 2271 // OPERANDS: none 2272 // DESCRIPTION: pops the top two stack values, compares using the 2273 // greater than (>) operator. 2274 // STACK RESULT: push the constant value 1 onto the stack if the result 2275 // of the operation is true or the constant value 0 if the result of the 2276 // operation is false. 2277 //---------------------------------------------------------------------- 2278 case DW_OP_gt: 2279 if (stack.size() < 2) 2280 { 2281 if (error_ptr) 2282 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_gt."); 2283 return false; 2284 } 2285 else 2286 { 2287 tmp = stack.back(); 2288 stack.pop_back(); 2289 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx); 2290 } 2291 break; 2292 2293 //---------------------------------------------------------------------- 2294 // OPCODE: DW_OP_le 2295 // OPERANDS: none 2296 // DESCRIPTION: pops the top two stack values, compares using the 2297 // less than or equal to (<=) operator. 2298 // STACK RESULT: push the constant value 1 onto the stack if the result 2299 // of the operation is true or the constant value 0 if the result of the 2300 // operation is false. 2301 //---------------------------------------------------------------------- 2302 case DW_OP_le: 2303 if (stack.size() < 2) 2304 { 2305 if (error_ptr) 2306 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_le."); 2307 return false; 2308 } 2309 else 2310 { 2311 tmp = stack.back(); 2312 stack.pop_back(); 2313 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx); 2314 } 2315 break; 2316 2317 //---------------------------------------------------------------------- 2318 // OPCODE: DW_OP_lt 2319 // OPERANDS: none 2320 // DESCRIPTION: pops the top two stack values, compares using the 2321 // less than (<) operator. 2322 // STACK RESULT: push the constant value 1 onto the stack if the result 2323 // of the operation is true or the constant value 0 if the result of the 2324 // operation is false. 2325 //---------------------------------------------------------------------- 2326 case DW_OP_lt: 2327 if (stack.size() < 2) 2328 { 2329 if (error_ptr) 2330 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_lt."); 2331 return false; 2332 } 2333 else 2334 { 2335 tmp = stack.back(); 2336 stack.pop_back(); 2337 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx); 2338 } 2339 break; 2340 2341 //---------------------------------------------------------------------- 2342 // OPCODE: DW_OP_ne 2343 // OPERANDS: none 2344 // DESCRIPTION: pops the top two stack values, compares using the 2345 // not equal (!=) operator. 2346 // STACK RESULT: push the constant value 1 onto the stack if the result 2347 // of the operation is true or the constant value 0 if the result of the 2348 // operation is false. 2349 //---------------------------------------------------------------------- 2350 case DW_OP_ne: 2351 if (stack.size() < 2) 2352 { 2353 if (error_ptr) 2354 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ne."); 2355 return false; 2356 } 2357 else 2358 { 2359 tmp = stack.back(); 2360 stack.pop_back(); 2361 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx); 2362 } 2363 break; 2364 2365 //---------------------------------------------------------------------- 2366 // OPCODE: DW_OP_litn 2367 // OPERANDS: none 2368 // DESCRIPTION: encode the unsigned literal values from 0 through 31. 2369 // STACK RESULT: push the unsigned literal constant value onto the top 2370 // of the stack. 2371 //---------------------------------------------------------------------- 2372 case DW_OP_lit0: 2373 case DW_OP_lit1: 2374 case DW_OP_lit2: 2375 case DW_OP_lit3: 2376 case DW_OP_lit4: 2377 case DW_OP_lit5: 2378 case DW_OP_lit6: 2379 case DW_OP_lit7: 2380 case DW_OP_lit8: 2381 case DW_OP_lit9: 2382 case DW_OP_lit10: 2383 case DW_OP_lit11: 2384 case DW_OP_lit12: 2385 case DW_OP_lit13: 2386 case DW_OP_lit14: 2387 case DW_OP_lit15: 2388 case DW_OP_lit16: 2389 case DW_OP_lit17: 2390 case DW_OP_lit18: 2391 case DW_OP_lit19: 2392 case DW_OP_lit20: 2393 case DW_OP_lit21: 2394 case DW_OP_lit22: 2395 case DW_OP_lit23: 2396 case DW_OP_lit24: 2397 case DW_OP_lit25: 2398 case DW_OP_lit26: 2399 case DW_OP_lit27: 2400 case DW_OP_lit28: 2401 case DW_OP_lit29: 2402 case DW_OP_lit30: 2403 case DW_OP_lit31: 2404 stack.push_back(Scalar(op - DW_OP_lit0)); 2405 break; 2406 2407 //---------------------------------------------------------------------- 2408 // OPCODE: DW_OP_regN 2409 // OPERANDS: none 2410 // DESCRIPTION: Push the value in register n on the top of the stack. 2411 //---------------------------------------------------------------------- 2412 case DW_OP_reg0: 2413 case DW_OP_reg1: 2414 case DW_OP_reg2: 2415 case DW_OP_reg3: 2416 case DW_OP_reg4: 2417 case DW_OP_reg5: 2418 case DW_OP_reg6: 2419 case DW_OP_reg7: 2420 case DW_OP_reg8: 2421 case DW_OP_reg9: 2422 case DW_OP_reg10: 2423 case DW_OP_reg11: 2424 case DW_OP_reg12: 2425 case DW_OP_reg13: 2426 case DW_OP_reg14: 2427 case DW_OP_reg15: 2428 case DW_OP_reg16: 2429 case DW_OP_reg17: 2430 case DW_OP_reg18: 2431 case DW_OP_reg19: 2432 case DW_OP_reg20: 2433 case DW_OP_reg21: 2434 case DW_OP_reg22: 2435 case DW_OP_reg23: 2436 case DW_OP_reg24: 2437 case DW_OP_reg25: 2438 case DW_OP_reg26: 2439 case DW_OP_reg27: 2440 case DW_OP_reg28: 2441 case DW_OP_reg29: 2442 case DW_OP_reg30: 2443 case DW_OP_reg31: 2444 { 2445 reg_num = op - DW_OP_reg0; 2446 2447 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp)) 2448 stack.push_back(tmp); 2449 else 2450 return false; 2451 } 2452 break; 2453 //---------------------------------------------------------------------- 2454 // OPCODE: DW_OP_regx 2455 // OPERANDS: 2456 // ULEB128 literal operand that encodes the register. 2457 // DESCRIPTION: Push the value in register on the top of the stack. 2458 //---------------------------------------------------------------------- 2459 case DW_OP_regx: 2460 { 2461 reg_num = opcodes.GetULEB128(&offset); 2462 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp)) 2463 stack.push_back(tmp); 2464 else 2465 return false; 2466 } 2467 break; 2468 2469 //---------------------------------------------------------------------- 2470 // OPCODE: DW_OP_bregN 2471 // OPERANDS: 2472 // SLEB128 offset from register N 2473 // DESCRIPTION: Value is in memory at the address specified by register 2474 // N plus an offset. 2475 //---------------------------------------------------------------------- 2476 case DW_OP_breg0: 2477 case DW_OP_breg1: 2478 case DW_OP_breg2: 2479 case DW_OP_breg3: 2480 case DW_OP_breg4: 2481 case DW_OP_breg5: 2482 case DW_OP_breg6: 2483 case DW_OP_breg7: 2484 case DW_OP_breg8: 2485 case DW_OP_breg9: 2486 case DW_OP_breg10: 2487 case DW_OP_breg11: 2488 case DW_OP_breg12: 2489 case DW_OP_breg13: 2490 case DW_OP_breg14: 2491 case DW_OP_breg15: 2492 case DW_OP_breg16: 2493 case DW_OP_breg17: 2494 case DW_OP_breg18: 2495 case DW_OP_breg19: 2496 case DW_OP_breg20: 2497 case DW_OP_breg21: 2498 case DW_OP_breg22: 2499 case DW_OP_breg23: 2500 case DW_OP_breg24: 2501 case DW_OP_breg25: 2502 case DW_OP_breg26: 2503 case DW_OP_breg27: 2504 case DW_OP_breg28: 2505 case DW_OP_breg29: 2506 case DW_OP_breg30: 2507 case DW_OP_breg31: 2508 { 2509 reg_num = op - DW_OP_breg0; 2510 2511 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp)) 2512 { 2513 int64_t breg_offset = opcodes.GetSLEB128(&offset); 2514 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; 2515 tmp.ClearContext(); 2516 stack.push_back(tmp); 2517 stack.back().SetValueType (Value::eValueTypeLoadAddress); 2518 } 2519 else 2520 return false; 2521 } 2522 break; 2523 //---------------------------------------------------------------------- 2524 // OPCODE: DW_OP_bregx 2525 // OPERANDS: 2 2526 // ULEB128 literal operand that encodes the register. 2527 // SLEB128 offset from register N 2528 // DESCRIPTION: Value is in memory at the address specified by register 2529 // N plus an offset. 2530 //---------------------------------------------------------------------- 2531 case DW_OP_bregx: 2532 { 2533 reg_num = opcodes.GetULEB128(&offset); 2534 2535 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp)) 2536 { 2537 int64_t breg_offset = opcodes.GetSLEB128(&offset); 2538 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; 2539 tmp.ClearContext(); 2540 stack.push_back(tmp); 2541 stack.back().SetValueType (Value::eValueTypeLoadAddress); 2542 } 2543 else 2544 return false; 2545 } 2546 break; 2547 2548 case DW_OP_fbreg: 2549 if (exe_ctx) 2550 { 2551 if (frame) 2552 { 2553 Scalar value; 2554 if (frame->GetFrameBaseValue(value, error_ptr)) 2555 { 2556 int64_t fbreg_offset = opcodes.GetSLEB128(&offset); 2557 value += fbreg_offset; 2558 stack.push_back(value); 2559 stack.back().SetValueType (Value::eValueTypeLoadAddress); 2560 } 2561 else 2562 return false; 2563 } 2564 else 2565 { 2566 if (error_ptr) 2567 error_ptr->SetErrorString ("Invalid stack frame in context for DW_OP_fbreg opcode."); 2568 return false; 2569 } 2570 } 2571 else 2572 { 2573 if (error_ptr) 2574 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_fbreg.\n"); 2575 return false; 2576 } 2577 2578 break; 2579 2580 //---------------------------------------------------------------------- 2581 // OPCODE: DW_OP_nop 2582 // OPERANDS: none 2583 // DESCRIPTION: A place holder. It has no effect on the location stack 2584 // or any of its values. 2585 //---------------------------------------------------------------------- 2586 case DW_OP_nop: 2587 break; 2588 2589 //---------------------------------------------------------------------- 2590 // OPCODE: DW_OP_piece 2591 // OPERANDS: 1 2592 // ULEB128: byte size of the piece 2593 // DESCRIPTION: The operand describes the size in bytes of the piece of 2594 // the object referenced by the DWARF expression whose result is at the 2595 // top of the stack. If the piece is located in a register, but does not 2596 // occupy the entire register, the placement of the piece within that 2597 // register is defined by the ABI. 2598 // 2599 // Many compilers store a single variable in sets of registers, or store 2600 // a variable partially in memory and partially in registers. 2601 // DW_OP_piece provides a way of describing how large a part of a 2602 // variable a particular DWARF expression refers to. 2603 //---------------------------------------------------------------------- 2604 case DW_OP_piece: 2605 { 2606 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset); 2607 2608 if (piece_byte_size > 0) 2609 { 2610 Value curr_piece; 2611 2612 if (stack.empty()) 2613 { 2614 // In a multi-piece expression, this means that the current piece is not available. 2615 // Fill with zeros for now by resizing the data and appending it 2616 curr_piece.ResizeData(piece_byte_size); 2617 ::memset (curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size); 2618 pieces.AppendDataToHostBuffer(curr_piece); 2619 } 2620 else 2621 { 2622 Error error; 2623 // Extract the current piece into "curr_piece" 2624 Value curr_piece_source_value(stack.back()); 2625 stack.pop_back(); 2626 2627 const Value::ValueType curr_piece_source_value_type = curr_piece_source_value.GetValueType(); 2628 switch (curr_piece_source_value_type) 2629 { 2630 case Value::eValueTypeLoadAddress: 2631 if (process) 2632 { 2633 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) 2634 { 2635 lldb::addr_t load_addr = curr_piece_source_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 2636 if (process->ReadMemory(load_addr, curr_piece.GetBuffer().GetBytes(), piece_byte_size, error) != piece_byte_size) 2637 { 2638 if (error_ptr) 2639 error_ptr->SetErrorStringWithFormat ("failed to read memory DW_OP_piece(%" PRIu64 ") from 0x%" PRIx64, 2640 piece_byte_size, 2641 load_addr); 2642 return false; 2643 } 2644 } 2645 else 2646 { 2647 if (error_ptr) 2648 error_ptr->SetErrorStringWithFormat ("failed to resize the piece memory buffer for DW_OP_piece(%" PRIu64 ")", piece_byte_size); 2649 return false; 2650 } 2651 } 2652 break; 2653 2654 case Value::eValueTypeFileAddress: 2655 case Value::eValueTypeHostAddress: 2656 if (error_ptr) 2657 { 2658 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 2659 error_ptr->SetErrorStringWithFormat ("failed to read memory DW_OP_piece(%" PRIu64 ") from %s address 0x%" PRIx64, 2660 piece_byte_size, 2661 curr_piece_source_value.GetValueType() == Value::eValueTypeFileAddress ? "file" : "host", 2662 addr); 2663 } 2664 return false; 2665 2666 case Value::eValueTypeScalar: 2667 { 2668 uint32_t bit_size = piece_byte_size * 8; 2669 uint32_t bit_offset = 0; 2670 if (!curr_piece_source_value.GetScalar().ExtractBitfield (bit_size, bit_offset)) 2671 { 2672 if (error_ptr) 2673 error_ptr->SetErrorStringWithFormat("unable to extract %" PRIu64 " bytes from a %" PRIu64 " byte scalar value.", piece_byte_size, (uint64_t)curr_piece_source_value.GetScalar().GetByteSize()); 2674 return false; 2675 } 2676 curr_piece = curr_piece_source_value; 2677 } 2678 break; 2679 2680 case Value::eValueTypeVector: 2681 { 2682 if (curr_piece_source_value.GetVector().length >= piece_byte_size) 2683 curr_piece_source_value.GetVector().length = piece_byte_size; 2684 else 2685 { 2686 if (error_ptr) 2687 error_ptr->SetErrorStringWithFormat("unable to extract %" PRIu64 " bytes from a %" PRIu64 " byte vector value.", piece_byte_size, (uint64_t)curr_piece_source_value.GetVector().length); 2688 return false; 2689 } 2690 } 2691 break; 2692 2693 } 2694 2695 // Check if this is the first piece? 2696 if (op_piece_offset == 0) 2697 { 2698 // This is the first piece, we should push it back onto the stack so subsequent 2699 // pieces will be able to access this piece and add to it 2700 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) 2701 { 2702 if (error_ptr) 2703 error_ptr->SetErrorString("failed to append piece data"); 2704 return false; 2705 } 2706 } 2707 else if (!stack.empty()) 2708 { 2709 // If this is the second or later piece there should be a value on the stack 2710 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) 2711 { 2712 if (error_ptr) 2713 error_ptr->SetErrorStringWithFormat ("DW_OP_piece for offset %" PRIu64 " but top of stack is of size %" PRIu64, 2714 op_piece_offset, 2715 pieces.GetBuffer().GetByteSize()); 2716 return false; 2717 } 2718 2719 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) 2720 { 2721 if (error_ptr) 2722 error_ptr->SetErrorString("failed to append piece data"); 2723 return false; 2724 } 2725 } 2726 op_piece_offset += piece_byte_size; 2727 } 2728 } 2729 } 2730 break; 2731 2732 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3); 2733 if (stack.size() < 1) 2734 { 2735 if (error_ptr) 2736 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_bit_piece."); 2737 return false; 2738 } 2739 else 2740 { 2741 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset); 2742 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset); 2743 switch (stack.back().GetValueType()) 2744 { 2745 case Value::eValueTypeScalar: 2746 { 2747 if (!stack.back().GetScalar().ExtractBitfield (piece_bit_size, piece_bit_offset)) 2748 { 2749 if (error_ptr) 2750 error_ptr->SetErrorStringWithFormat("unable to extract %" PRIu64 " bit value with %" PRIu64 " bit offset from a %" PRIu64 " bit scalar value.", 2751 piece_bit_size, 2752 piece_bit_offset, 2753 (uint64_t)(stack.back().GetScalar().GetByteSize()*8)); 2754 return false; 2755 } 2756 } 2757 break; 2758 2759 case Value::eValueTypeFileAddress: 2760 case Value::eValueTypeLoadAddress: 2761 case Value::eValueTypeHostAddress: 2762 if (error_ptr) 2763 { 2764 error_ptr->SetErrorStringWithFormat ("unable to extract DW_OP_bit_piece(bit_size = %" PRIu64 ", bit_offset = %" PRIu64 ") from an addresss value.", 2765 piece_bit_size, 2766 piece_bit_offset); 2767 } 2768 return false; 2769 2770 case Value::eValueTypeVector: 2771 if (error_ptr) 2772 { 2773 error_ptr->SetErrorStringWithFormat ("unable to extract DW_OP_bit_piece(bit_size = %" PRIu64 ", bit_offset = %" PRIu64 ") from a vector value.", 2774 piece_bit_size, 2775 piece_bit_offset); 2776 } 2777 return false; 2778 } 2779 } 2780 break; 2781 2782 //---------------------------------------------------------------------- 2783 // OPCODE: DW_OP_push_object_address 2784 // OPERANDS: none 2785 // DESCRIPTION: Pushes the address of the object currently being 2786 // evaluated as part of evaluation of a user presented expression. 2787 // This object may correspond to an independent variable described by 2788 // its own DIE or it may be a component of an array, structure, or class 2789 // whose address has been dynamically determined by an earlier step 2790 // during user expression evaluation. 2791 //---------------------------------------------------------------------- 2792 case DW_OP_push_object_address: 2793 if (error_ptr) 2794 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_push_object_address."); 2795 return false; 2796 2797 //---------------------------------------------------------------------- 2798 // OPCODE: DW_OP_call2 2799 // OPERANDS: 2800 // uint16_t compile unit relative offset of a DIE 2801 // DESCRIPTION: Performs subroutine calls during evaluation 2802 // of a DWARF expression. The operand is the 2-byte unsigned offset 2803 // of a debugging information entry in the current compilation unit. 2804 // 2805 // Operand interpretation is exactly like that for DW_FORM_ref2. 2806 // 2807 // This operation transfers control of DWARF expression evaluation 2808 // to the DW_AT_location attribute of the referenced DIE. If there is 2809 // no such attribute, then there is no effect. Execution of the DWARF 2810 // expression of a DW_AT_location attribute may add to and/or remove from 2811 // values on the stack. Execution returns to the point following the call 2812 // when the end of the attribute is reached. Values on the stack at the 2813 // time of the call may be used as parameters by the called expression 2814 // and values left on the stack by the called expression may be used as 2815 // return values by prior agreement between the calling and called 2816 // expressions. 2817 //---------------------------------------------------------------------- 2818 case DW_OP_call2: 2819 if (error_ptr) 2820 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call2."); 2821 return false; 2822 //---------------------------------------------------------------------- 2823 // OPCODE: DW_OP_call4 2824 // OPERANDS: 1 2825 // uint32_t compile unit relative offset of a DIE 2826 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF 2827 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset 2828 // of a debugging information entry in the current compilation unit. 2829 // 2830 // Operand interpretation DW_OP_call4 is exactly like that for 2831 // DW_FORM_ref4. 2832 // 2833 // This operation transfers control of DWARF expression evaluation 2834 // to the DW_AT_location attribute of the referenced DIE. If there is 2835 // no such attribute, then there is no effect. Execution of the DWARF 2836 // expression of a DW_AT_location attribute may add to and/or remove from 2837 // values on the stack. Execution returns to the point following the call 2838 // when the end of the attribute is reached. Values on the stack at the 2839 // time of the call may be used as parameters by the called expression 2840 // and values left on the stack by the called expression may be used as 2841 // return values by prior agreement between the calling and called 2842 // expressions. 2843 //---------------------------------------------------------------------- 2844 case DW_OP_call4: 2845 if (error_ptr) 2846 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call4."); 2847 return false; 2848 2849 //---------------------------------------------------------------------- 2850 // OPCODE: DW_OP_stack_value 2851 // OPERANDS: None 2852 // DESCRIPTION: Specifies that the object does not exist in memory but 2853 // rather is a constant value. The value from the top of the stack is 2854 // the value to be used. This is the actual object value and not the 2855 // location. 2856 //---------------------------------------------------------------------- 2857 case DW_OP_stack_value: 2858 stack.back().SetValueType(Value::eValueTypeScalar); 2859 break; 2860 2861 //---------------------------------------------------------------------- 2862 // OPCODE: DW_OP_call_frame_cfa 2863 // OPERANDS: None 2864 // DESCRIPTION: Specifies a DWARF expression that pushes the value of 2865 // the canonical frame address consistent with the call frame information 2866 // located in .debug_frame (or in the FDEs of the eh_frame section). 2867 //---------------------------------------------------------------------- 2868 case DW_OP_call_frame_cfa: 2869 if (frame) 2870 { 2871 // Note that we don't have to parse FDEs because this DWARF expression 2872 // is commonly evaluated with a valid stack frame. 2873 StackID id = frame->GetStackID(); 2874 addr_t cfa = id.GetCallFrameAddress(); 2875 if (cfa != LLDB_INVALID_ADDRESS) 2876 { 2877 stack.push_back(Scalar(cfa)); 2878 stack.back().SetValueType (Value::eValueTypeLoadAddress); 2879 } 2880 else 2881 if (error_ptr) 2882 error_ptr->SetErrorString ("Stack frame does not include a canonical frame address for DW_OP_call_frame_cfa opcode."); 2883 } 2884 else 2885 { 2886 if (error_ptr) 2887 error_ptr->SetErrorString ("Invalid stack frame in context for DW_OP_call_frame_cfa opcode."); 2888 return false; 2889 } 2890 break; 2891 2892 //---------------------------------------------------------------------- 2893 // OPCODE: DW_OP_GNU_push_tls_address 2894 // OPERANDS: none 2895 // DESCRIPTION: Pops a TLS offset from the stack, converts it to 2896 // an absolute value, and pushes it back on. 2897 //---------------------------------------------------------------------- 2898 case DW_OP_GNU_push_tls_address: 2899 { 2900 if (stack.size() < 1) 2901 { 2902 if (error_ptr) 2903 error_ptr->SetErrorString("DW_OP_GNU_push_tls_address needs an argument."); 2904 return false; 2905 } 2906 2907 if (!exe_ctx || !module_sp) 2908 { 2909 if (error_ptr) 2910 error_ptr->SetErrorString("No context to evaluate TLS within."); 2911 return false; 2912 } 2913 2914 Thread *thread = exe_ctx->GetThreadPtr(); 2915 if (!thread) 2916 { 2917 if (error_ptr) 2918 error_ptr->SetErrorString("No thread to evaluate TLS within."); 2919 return false; 2920 } 2921 2922 // Lookup the TLS block address for this thread and module. 2923 addr_t tls_addr = thread->GetThreadLocalData (module_sp); 2924 2925 if (tls_addr == LLDB_INVALID_ADDRESS) 2926 { 2927 if (error_ptr) 2928 error_ptr->SetErrorString ("No TLS data currently exists for this thread."); 2929 return false; 2930 } 2931 2932 // Convert the TLS offset into the absolute address. 2933 Scalar tmp = stack.back().ResolveValue(exe_ctx); 2934 stack.back() = tmp + tls_addr; 2935 stack.back().SetValueType (Value::eValueTypeLoadAddress); 2936 } 2937 break; 2938 2939 default: 2940 if (log) 2941 log->Printf("Unhandled opcode %s in DWARFExpression.", DW_OP_value_to_name(op)); 2942 break; 2943 } 2944 } 2945 2946 if (stack.empty()) 2947 { 2948 // Nothing on the stack, check if we created a piece value from DW_OP_piece or DW_OP_bit_piece opcodes 2949 if (pieces.GetBuffer().GetByteSize()) 2950 { 2951 result = pieces; 2952 } 2953 else 2954 { 2955 if (error_ptr) 2956 error_ptr->SetErrorString ("Stack empty after evaluation."); 2957 return false; 2958 } 2959 } 2960 else 2961 { 2962 if (log && log->GetVerbose()) 2963 { 2964 size_t count = stack.size(); 2965 log->Printf("Stack after operation has %" PRIu64 " values:", (uint64_t)count); 2966 for (size_t i=0; i<count; ++i) 2967 { 2968 StreamString new_value; 2969 new_value.Printf("[%" PRIu64 "]", (uint64_t)i); 2970 stack[i].Dump(&new_value); 2971 log->Printf(" %s", new_value.GetData()); 2972 } 2973 } 2974 result = stack.back(); 2975 } 2976 return true; // Return true on success 2977 } 2978 2979