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