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