Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 1 | //===-- UnwindPlan.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/Symbol/UnwindPlan.h" |
Greg Clayton | 79ea878 | 2011-04-26 23:48:45 +0000 | [diff] [blame] | 11 | |
| 12 | #include "lldb/Core/ConstString.h" |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 13 | #include "lldb/Target/Process.h" |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 14 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 15 | #include "lldb/Target/Thread.h" |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace lldb; |
| 18 | using namespace lldb_private; |
| 19 | |
| 20 | bool |
| 21 | UnwindPlan::Row::RegisterLocation::operator == (const UnwindPlan::Row::RegisterLocation& rhs) const |
| 22 | { |
| 23 | if (m_type != rhs.m_type) |
| 24 | return false; |
| 25 | if (m_type == atCFAPlusOffset || m_type == isCFAPlusOffset) |
| 26 | return m_location.offset == rhs.m_location.offset; |
| 27 | if (m_type == inOtherRegister) |
| 28 | return m_location.reg_num == rhs.m_location.reg_num; |
| 29 | if (m_type == atDWARFExpression || m_type == isDWARFExpression) |
| 30 | if (m_location.expr.length == rhs.m_location.expr.length) |
| 31 | return !memcmp (m_location.expr.opcodes, rhs.m_location.expr.opcodes, m_location.expr.length); |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | // This function doesn't copy the dwarf expression bytes; they must remain in allocated |
| 36 | // memory for the lifespan of this UnwindPlan object. |
| 37 | void |
| 38 | UnwindPlan::Row::RegisterLocation::SetAtDWARFExpression (const uint8_t *opcodes, uint32_t len) |
| 39 | { |
| 40 | m_type = atDWARFExpression; |
| 41 | m_location.expr.opcodes = opcodes; |
| 42 | m_location.expr.length = len; |
| 43 | } |
| 44 | |
| 45 | // This function doesn't copy the dwarf expression bytes; they must remain in allocated |
| 46 | // memory for the lifespan of this UnwindPlan object. |
| 47 | void |
| 48 | UnwindPlan::Row::RegisterLocation::SetIsDWARFExpression (const uint8_t *opcodes, uint32_t len) |
| 49 | { |
| 50 | m_type = isDWARFExpression; |
| 51 | m_location.expr.opcodes = opcodes; |
| 52 | m_location.expr.length = len; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | UnwindPlan::Row::RegisterLocation::SetUnspecified () |
| 57 | { |
| 58 | m_type = unspecified; |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | UnwindPlan::Row::RegisterLocation::SetUndefined () |
| 63 | { |
| 64 | m_type = isUndefined; |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | UnwindPlan::Row::RegisterLocation::SetSame () |
| 69 | { |
| 70 | m_type = isSame; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void |
| 75 | UnwindPlan::Row::RegisterLocation::SetAtCFAPlusOffset (int32_t offset) |
| 76 | { |
| 77 | m_type = atCFAPlusOffset; |
| 78 | m_location.offset = offset; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | UnwindPlan::Row::RegisterLocation::SetIsCFAPlusOffset (int32_t offset) |
| 83 | { |
| 84 | m_type = isCFAPlusOffset; |
| 85 | m_location.offset = offset; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | UnwindPlan::Row::RegisterLocation::SetInRegister (uint32_t reg_num) |
| 90 | { |
| 91 | m_type = inOtherRegister; |
| 92 | m_location.reg_num = reg_num; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | UnwindPlan::Row::RegisterLocation::Dump (Stream &s) const |
| 97 | { |
| 98 | switch (m_type) |
| 99 | { |
| 100 | case unspecified: |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 101 | s.PutCString ("unspecified"); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 102 | break; |
| 103 | case isUndefined: |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 104 | s.PutCString ("isUndefined"); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 105 | break; |
| 106 | case isSame: |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 107 | s.PutCString ("isSame"); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 108 | break; |
| 109 | case atCFAPlusOffset: |
| 110 | s.Printf ("atCFAPlusOffset %d", m_location.offset); |
| 111 | break; |
| 112 | case isCFAPlusOffset: |
| 113 | s.Printf ("isCFAPlusOffset %d", m_location.offset); |
| 114 | break; |
| 115 | case inOtherRegister: |
| 116 | s.Printf ("inOtherRegister %d", m_location.reg_num); |
| 117 | break; |
| 118 | case atDWARFExpression: |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 119 | s.PutCString ("atDWARFExpression"); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 120 | break; |
| 121 | case isDWARFExpression: |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 122 | s.PutCString ("isDWARFExpression"); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | UnwindPlan::Row::Clear () |
| 129 | { |
| 130 | m_offset = 0; |
| 131 | m_cfa_reg_num = 0; |
| 132 | m_cfa_offset = 0; |
| 133 | m_register_locations.clear(); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | UnwindPlan::Row::Dump (Stream& s, int register_kind, Thread* thread) const |
| 138 | { |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 139 | RegisterContext *reg_ctx = NULL; |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 140 | const RegisterInfo *rinfo = NULL; |
| 141 | int translated_regnum; |
| 142 | if (thread && thread->GetRegisterContext()) |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 143 | reg_ctx = thread->GetRegisterContext().get(); |
| 144 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 145 | s.Printf ("offset %ld, CFA reg ", (long) GetOffset()); |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 146 | if (reg_ctx |
| 147 | && (translated_regnum = reg_ctx->ConvertRegisterKindToRegisterNumber (register_kind, GetCFARegister())) != -1 |
| 148 | && (rinfo = reg_ctx->GetRegisterInfoAtIndex (translated_regnum)) != NULL |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 149 | && rinfo->name != NULL |
| 150 | && rinfo->name[0] != '\0') |
| 151 | { |
| 152 | s.Printf ("%s, ", rinfo->name); |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | s.Printf ("%d, ", (int)(int) GetCFARegister()); |
| 157 | } |
| 158 | s.Printf ("CFA offset %d", (int) GetCFAOffset ()); |
| 159 | for (collection::const_iterator idx = m_register_locations.begin (); idx != m_register_locations.end (); ++idx) |
| 160 | { |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 161 | s.PutCString (" ["); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 162 | bool printed_name = false; |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 163 | if (reg_ctx) |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 164 | { |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 165 | translated_regnum = reg_ctx->ConvertRegisterKindToRegisterNumber (register_kind, idx->first); |
| 166 | rinfo = reg_ctx->GetRegisterInfoAtIndex (translated_regnum); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 167 | if (rinfo && rinfo->name) |
| 168 | { |
| 169 | s.Printf ("%s ", rinfo->name); |
| 170 | printed_name = true; |
| 171 | } |
| 172 | } |
| 173 | if (!printed_name) |
| 174 | { |
| 175 | s.Printf ("reg %d ", idx->first); |
| 176 | } |
| 177 | idx->second.Dump(s); |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 178 | s.PutCString ("]"); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 179 | } |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 180 | s.EOL(); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | UnwindPlan::Row::Row() : |
| 184 | m_offset(0), |
| 185 | m_cfa_reg_num(0), |
| 186 | m_cfa_offset(0), |
| 187 | m_register_locations() |
| 188 | { |
| 189 | } |
| 190 | |
| 191 | bool |
| 192 | UnwindPlan::Row::GetRegisterInfo (uint32_t reg_num, UnwindPlan::Row::RegisterLocation& register_location) const |
| 193 | { |
| 194 | collection::const_iterator pos = m_register_locations.find(reg_num); |
| 195 | if (pos != m_register_locations.end()) |
| 196 | { |
| 197 | register_location = pos->second; |
| 198 | return true; |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | UnwindPlan::Row::SetRegisterInfo (uint32_t reg_num, const UnwindPlan::Row::RegisterLocation register_location) |
| 205 | { |
| 206 | m_register_locations[reg_num] = register_location; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void |
| 211 | UnwindPlan::AppendRow (const UnwindPlan::Row &row) |
| 212 | { |
| 213 | if (m_row_list.empty() || m_row_list.back().GetOffset() != row.GetOffset()) |
| 214 | m_row_list.push_back(row); |
| 215 | else |
| 216 | m_row_list.back() = row; |
| 217 | } |
| 218 | |
| 219 | const UnwindPlan::Row * |
| 220 | UnwindPlan::GetRowForFunctionOffset (int offset) const |
| 221 | { |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 222 | const UnwindPlan::Row *row_ptr = NULL; |
| 223 | if (!m_row_list.empty()) |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 224 | { |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 225 | if (offset == -1) |
| 226 | row_ptr = &m_row_list.back(); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 227 | else |
| 228 | { |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 229 | collection::const_iterator pos, end = m_row_list.end(); |
| 230 | for (pos = m_row_list.begin(); pos != end; ++pos) |
| 231 | { |
| 232 | if (pos->GetOffset() <= offset) |
| 233 | row_ptr = &*pos; |
| 234 | else |
| 235 | break; |
| 236 | } |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 239 | return row_ptr; |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | bool |
| 243 | UnwindPlan::IsValidRowIndex (uint32_t idx) const |
| 244 | { |
| 245 | return idx < m_row_list.size(); |
| 246 | } |
| 247 | |
| 248 | const UnwindPlan::Row& |
| 249 | UnwindPlan::GetRowAtIndex (uint32_t idx) const |
| 250 | { |
| 251 | // You must call IsValidRowIndex(idx) first before calling this!!! |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 252 | assert (idx < m_row_list.size()); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 253 | return m_row_list[idx]; |
| 254 | } |
| 255 | |
| 256 | int |
| 257 | UnwindPlan::GetRowCount () const |
| 258 | { |
| 259 | return m_row_list.size (); |
| 260 | } |
| 261 | |
| 262 | void |
| 263 | UnwindPlan::SetRegisterKind (uint32_t rk) |
| 264 | { |
| 265 | m_register_kind = rk; |
| 266 | } |
| 267 | |
| 268 | uint32_t |
| 269 | UnwindPlan::GetRegisterKind (void) const |
| 270 | { |
| 271 | return m_register_kind; |
| 272 | } |
| 273 | |
| 274 | void |
| 275 | UnwindPlan::SetPlanValidAddressRange (const AddressRange& range) |
| 276 | { |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 277 | if (range.GetBaseAddress().IsValid() && range.GetByteSize() != 0) |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 278 | m_plan_valid_address_range = range; |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | bool |
| 282 | UnwindPlan::PlanValidAtAddress (Address addr) |
| 283 | { |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 284 | if (!m_plan_valid_address_range.GetBaseAddress().IsValid() || m_plan_valid_address_range.GetByteSize() == 0) |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 285 | return true; |
| 286 | |
Jason Molenda | 5976200 | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 287 | if (!addr.IsValid()) |
| 288 | return true; |
| 289 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 290 | if (m_plan_valid_address_range.ContainsFileAddress (addr)) |
| 291 | return true; |
| 292 | |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | void |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 297 | UnwindPlan::Dump (Stream& s, Thread *thread) const |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 298 | { |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 299 | if (!m_source_name.IsEmpty()) |
| 300 | { |
| 301 | s.Printf ("This UnwindPlan originally sourced from %s\n", m_source_name.GetCString()); |
| 302 | } |
| 303 | if (m_plan_valid_address_range.GetBaseAddress().IsValid() && m_plan_valid_address_range.GetByteSize() > 0) |
| 304 | { |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 305 | s.PutCString ("Address range of this UnwindPlan: "); |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 306 | m_plan_valid_address_range.Dump (&s, &thread->GetProcess().GetTarget(), Address::DumpStyleSectionNameOffset); |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 307 | s.EOL(); |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 308 | } |
| 309 | else |
| 310 | { |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 311 | s.PutCString ("No valid address range recorded for this UnwindPlan.\n"); |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 312 | } |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 313 | s.Printf ("UnwindPlan register kind %d", m_register_kind); |
| 314 | switch (m_register_kind) |
| 315 | { |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 316 | case eRegisterKindGCC: s.PutCString (" [eRegisterKindGCC]"); break; |
| 317 | case eRegisterKindDWARF: s.PutCString (" [eRegisterKindDWARF]"); break; |
| 318 | case eRegisterKindGeneric: s.PutCString (" [eRegisterKindGeneric]"); break; |
| 319 | case eRegisterKindGDB: s.PutCString (" [eRegisterKindGDB]"); break; |
| 320 | case eRegisterKindLLDB: s.PutCString (" [eRegisterKindLLDB]"); break; |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 321 | default: break; |
| 322 | } |
Greg Clayton | 877aaa5 | 2011-01-08 21:19:00 +0000 | [diff] [blame] | 323 | s.EOL(); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 324 | for (int i = 0; IsValidRowIndex (i); i++) |
| 325 | { |
| 326 | s.Printf ("UnwindPlan row at index %d: ", i); |
| 327 | m_row_list[i].Dump(s, m_register_kind, thread); |
| 328 | } |
| 329 | } |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 330 | |
| 331 | void |
| 332 | UnwindPlan::SetSourceName (const char *source) |
| 333 | { |
| 334 | m_source_name = ConstString (source); |
| 335 | } |
| 336 | |
| 337 | ConstString |
| 338 | UnwindPlan::GetSourceName () const |
| 339 | { |
| 340 | return m_source_name; |
| 341 | } |