LLDB  mainline
DynamicRegisterInfo.cpp
Go to the documentation of this file.
1 //===-- DynamicRegisterInfo.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "lldb/Core/StreamFile.h"
13 #include "lldb/Utility/ArchSpec.h"
14 #include "lldb/Utility/Log.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 DynamicRegisterInfo::DynamicRegisterInfo(
24  const lldb_private::ArchSpec &arch) {
25  SetRegisterInfo(dict, arch);
26 }
27 
28 DynamicRegisterInfo::DynamicRegisterInfo(DynamicRegisterInfo &&info) {
29  MoveFrom(std::move(info));
30 }
31 
33 DynamicRegisterInfo::operator=(DynamicRegisterInfo &&info) {
34  MoveFrom(std::move(info));
35  return *this;
36 }
37 
38 void DynamicRegisterInfo::MoveFrom(DynamicRegisterInfo &&info) {
39  m_regs = std::move(info.m_regs);
40  m_sets = std::move(info.m_sets);
41  m_set_reg_nums = std::move(info.m_set_reg_nums);
42  m_set_names = std::move(info.m_set_names);
43  m_value_regs_map = std::move(info.m_value_regs_map);
44  m_invalidate_regs_map = std::move(info.m_invalidate_regs_map);
45 
46  m_reg_data_byte_size = info.m_reg_data_byte_size;
47  m_finalized = info.m_finalized;
48 
49  if (m_finalized) {
50  const size_t num_sets = m_sets.size();
51  for (size_t set = 0; set < num_sets; ++set)
52  m_sets[set].registers = m_set_reg_nums[set].data();
53  }
54 
55  info.Clear();
56 }
57 
58 llvm::Expected<uint32_t> DynamicRegisterInfo::ByteOffsetFromSlice(
59  uint32_t index, llvm::StringRef slice_str, lldb::ByteOrder byte_order) {
60  // Slices use the following format:
61  // REGNAME[MSBIT:LSBIT]
62  // REGNAME - name of the register to grab a slice of
63  // MSBIT - the most significant bit at which the current register value
64  // starts at
65  // LSBIT - the least significant bit at which the current register value
66  // ends at
67  static llvm::Regex g_bitfield_regex(
68  "([A-Za-z_][A-Za-z0-9_]*)\\[([0-9]+):([0-9]+)\\]");
69  llvm::SmallVector<llvm::StringRef, 4> matches;
70  if (!g_bitfield_regex.match(slice_str, &matches))
71  return llvm::createStringError(
72  llvm::inconvertibleErrorCode(),
73  "failed to match against register bitfield regex (slice: %s)",
74  slice_str.str().c_str());
75 
76  llvm::StringRef reg_name_str = matches[1];
77  llvm::StringRef msbit_str = matches[2];
78  llvm::StringRef lsbit_str = matches[3];
79  uint32_t msbit;
80  uint32_t lsbit;
81  if (!llvm::to_integer(msbit_str, msbit) ||
82  !llvm::to_integer(lsbit_str, lsbit))
83  return llvm::createStringError(
84  llvm::inconvertibleErrorCode(), "msbit (%s) or lsbit (%s) are invalid",
85  msbit_str.str().c_str(), lsbit_str.str().c_str());
86 
87  if (msbit <= lsbit)
88  return llvm::createStringError(llvm::inconvertibleErrorCode(),
89  "msbit (%u) must be greater than lsbit (%u)",
90  msbit, lsbit);
91 
92  const uint32_t msbyte = msbit / 8;
93  const uint32_t lsbyte = lsbit / 8;
94 
95  const RegisterInfo *containing_reg_info = GetRegisterInfo(reg_name_str);
96  if (!containing_reg_info)
97  return llvm::createStringError(llvm::inconvertibleErrorCode(),
98  "invalid concrete register \"%s\"",
99  reg_name_str.str().c_str());
100 
101  const uint32_t max_bit = containing_reg_info->byte_size * 8;
102 
103  if (msbit > max_bit)
104  return llvm::createStringError(
105  llvm::inconvertibleErrorCode(),
106  "msbit (%u) must be less than the bitsize of the register \"%s\" (%u)",
107  msbit, reg_name_str.str().c_str(), max_bit);
108  if (lsbit > max_bit)
109  return llvm::createStringError(
110  llvm::inconvertibleErrorCode(),
111  "lsbit (%u) must be less than the bitsize of the register \"%s\" (%u)",
112  lsbit, reg_name_str.str().c_str(), max_bit);
113 
114  m_invalidate_regs_map[containing_reg_info->kinds[eRegisterKindLLDB]]
115  .push_back(index);
116  m_value_regs_map[index].push_back(
117  containing_reg_info->kinds[eRegisterKindLLDB]);
118  m_invalidate_regs_map[index].push_back(
119  containing_reg_info->kinds[eRegisterKindLLDB]);
120 
121  if (byte_order == eByteOrderLittle)
122  return containing_reg_info->byte_offset + lsbyte;
123  if (byte_order == eByteOrderBig)
124  return containing_reg_info->byte_offset + msbyte;
125  llvm_unreachable("Invalid byte order");
126 }
127 
128 llvm::Expected<uint32_t> DynamicRegisterInfo::ByteOffsetFromComposite(
129  uint32_t index, StructuredData::Array &composite_reg_list,
130  lldb::ByteOrder byte_order) {
131  const size_t num_composite_regs = composite_reg_list.GetSize();
132  if (num_composite_regs == 0)
133  return llvm::createStringError(llvm::inconvertibleErrorCode(),
134  "\"composite\" list is empty");
135 
136  uint32_t composite_offset = UINT32_MAX;
137  for (uint32_t composite_idx = 0; composite_idx < num_composite_regs;
138  ++composite_idx) {
139  ConstString composite_reg_name;
140  if (!composite_reg_list.GetItemAtIndexAsString(composite_idx,
141  composite_reg_name, nullptr))
142  return llvm::createStringError(
143  llvm::inconvertibleErrorCode(),
144  "\"composite\" list value is not a Python string at index %d",
145  composite_idx);
146 
147  const RegisterInfo *composite_reg_info =
148  GetRegisterInfo(composite_reg_name.GetStringRef());
149  if (!composite_reg_info)
150  return llvm::createStringError(
151  llvm::inconvertibleErrorCode(),
152  "failed to find composite register by name: \"%s\"",
153  composite_reg_name.GetCString());
154 
155  composite_offset =
156  std::min(composite_offset, composite_reg_info->byte_offset);
157  m_value_regs_map[index].push_back(
158  composite_reg_info->kinds[eRegisterKindLLDB]);
159  m_invalidate_regs_map[composite_reg_info->kinds[eRegisterKindLLDB]]
160  .push_back(index);
161  m_invalidate_regs_map[index].push_back(
162  composite_reg_info->kinds[eRegisterKindLLDB]);
163  }
164 
165  return composite_offset;
166 }
167 
168 llvm::Expected<uint32_t> DynamicRegisterInfo::ByteOffsetFromRegInfoDict(
169  uint32_t index, StructuredData::Dictionary &reg_info_dict,
170  lldb::ByteOrder byte_order) {
171  uint32_t byte_offset;
172  if (reg_info_dict.GetValueForKeyAsInteger("offset", byte_offset))
173  return byte_offset;
174 
175  // No offset for this register, see if the register has a value
176  // expression which indicates this register is part of another register.
177  // Value expressions are things like "rax[31:0]" which state that the
178  // current register's value is in a concrete register "rax" in bits 31:0.
179  // If there is a value expression we can calculate the offset
180  llvm::StringRef slice_str;
181  if (reg_info_dict.GetValueForKeyAsString("slice", slice_str, nullptr))
182  return ByteOffsetFromSlice(index, slice_str, byte_order);
183 
184  StructuredData::Array *composite_reg_list;
185  if (reg_info_dict.GetValueForKeyAsArray("composite", composite_reg_list))
186  return ByteOffsetFromComposite(index, *composite_reg_list, byte_order);
187 
188  return llvm::createStringError(llvm::inconvertibleErrorCode(),
189  "insufficient data to calculate byte offset");
190 }
191 
192 size_t
193 DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
194  const ArchSpec &arch) {
196  assert(!m_finalized);
197  StructuredData::Array *sets = nullptr;
198  if (dict.GetValueForKeyAsArray("sets", sets)) {
199  const uint32_t num_sets = sets->GetSize();
200  for (uint32_t i = 0; i < num_sets; ++i) {
201  ConstString set_name;
202  if (sets->GetItemAtIndexAsString(i, set_name) && !set_name.IsEmpty()) {
203  m_sets.push_back({set_name.AsCString(), nullptr, 0, nullptr});
204  } else {
205  Clear();
206  printf("error: register sets must have valid names\n");
207  return 0;
208  }
209  }
210  m_set_reg_nums.resize(m_sets.size());
211  }
212 
213  StructuredData::Array *regs = nullptr;
214  if (!dict.GetValueForKeyAsArray("registers", regs))
215  return 0;
216 
217  const ByteOrder byte_order = arch.GetByteOrder();
218 
219  const uint32_t num_regs = regs->GetSize();
220  // typedef std::map<std::string, std::vector<std::string> >
221  // InvalidateNameMap;
222  // InvalidateNameMap invalidate_map;
223  for (uint32_t i = 0; i < num_regs; ++i) {
224  StructuredData::Dictionary *reg_info_dict = nullptr;
225  if (!regs->GetItemAtIndexAsDictionary(i, reg_info_dict)) {
226  Clear();
227  printf("error: items in the 'registers' array must be dictionaries\n");
228  regs->DumpToStdout();
229  return 0;
230  }
231 
232  // { 'name':'rcx' , 'bitsize' : 64, 'offset' : 16,
233  // 'encoding':'uint' , 'format':'hex' , 'set': 0, 'ehframe' : 2,
234  // 'dwarf' : 2, 'generic':'arg4', 'alt-name':'arg4', },
235  RegisterInfo reg_info;
236  std::vector<uint32_t> value_regs;
237  std::vector<uint32_t> invalidate_regs;
238  memset(&reg_info, 0, sizeof(reg_info));
239 
240  ConstString name_val;
241  ConstString alt_name_val;
242  if (!reg_info_dict->GetValueForKeyAsString("name", name_val, nullptr)) {
243  Clear();
244  printf("error: registers must have valid names and offsets\n");
245  reg_info_dict->DumpToStdout();
246  return 0;
247  }
248  reg_info.name = name_val.GetCString();
249  reg_info_dict->GetValueForKeyAsString("alt-name", alt_name_val, nullptr);
250  reg_info.alt_name = alt_name_val.GetCString();
251 
252  llvm::Expected<uint32_t> byte_offset =
253  ByteOffsetFromRegInfoDict(i, *reg_info_dict, byte_order);
254  if (byte_offset)
255  reg_info.byte_offset = byte_offset.get();
256  else {
257  LLDB_LOG_ERROR(log, byte_offset.takeError(),
258  "error while parsing register {1}: {0}", reg_info.name);
259  Clear();
260  reg_info_dict->DumpToStdout();
261  return 0;
262  }
263 
264  int64_t bitsize = 0;
265  if (!reg_info_dict->GetValueForKeyAsInteger("bitsize", bitsize)) {
266  Clear();
267  printf("error: invalid or missing 'bitsize' key/value pair in register "
268  "dictionary\n");
269  reg_info_dict->DumpToStdout();
270  return 0;
271  }
272 
273  reg_info.byte_size = bitsize / 8;
274 
275  llvm::StringRef format_str;
276  if (reg_info_dict->GetValueForKeyAsString("format", format_str, nullptr)) {
277  if (OptionArgParser::ToFormat(format_str.str().c_str(), reg_info.format,
278  nullptr)
279  .Fail()) {
280  Clear();
281  printf("error: invalid 'format' value in register dictionary\n");
282  reg_info_dict->DumpToStdout();
283  return 0;
284  }
285  } else {
286  reg_info_dict->GetValueForKeyAsInteger("format", reg_info.format,
287  eFormatHex);
288  }
289 
290  llvm::StringRef encoding_str;
291  if (reg_info_dict->GetValueForKeyAsString("encoding", encoding_str))
292  reg_info.encoding = Args::StringToEncoding(encoding_str, eEncodingUint);
293  else
294  reg_info_dict->GetValueForKeyAsInteger("encoding", reg_info.encoding,
295  eEncodingUint);
296 
297  size_t set = 0;
298  if (!reg_info_dict->GetValueForKeyAsInteger<size_t>("set", set, -1) ||
299  set >= m_sets.size()) {
300  Clear();
301  printf("error: invalid 'set' value in register dictionary, valid values "
302  "are 0 - %i\n",
303  (int)set);
304  reg_info_dict->DumpToStdout();
305  return 0;
306  }
307 
308  // Fill in the register numbers
309  reg_info.kinds[lldb::eRegisterKindLLDB] = i;
310  reg_info.kinds[lldb::eRegisterKindProcessPlugin] = i;
311  uint32_t eh_frame_regno = LLDB_INVALID_REGNUM;
312  reg_info_dict->GetValueForKeyAsInteger("gcc", eh_frame_regno,
314  if (eh_frame_regno == LLDB_INVALID_REGNUM)
315  reg_info_dict->GetValueForKeyAsInteger("ehframe", eh_frame_regno,
317  reg_info.kinds[lldb::eRegisterKindEHFrame] = eh_frame_regno;
318  reg_info_dict->GetValueForKeyAsInteger(
319  "dwarf", reg_info.kinds[lldb::eRegisterKindDWARF], LLDB_INVALID_REGNUM);
320  llvm::StringRef generic_str;
321  if (reg_info_dict->GetValueForKeyAsString("generic", generic_str))
322  reg_info.kinds[lldb::eRegisterKindGeneric] =
323  Args::StringToGenericRegister(generic_str);
324  else
325  reg_info_dict->GetValueForKeyAsInteger(
326  "generic", reg_info.kinds[lldb::eRegisterKindGeneric],
328 
329  // Check if this register invalidates any other register values when it is
330  // modified
331  StructuredData::Array *invalidate_reg_list = nullptr;
332  if (reg_info_dict->GetValueForKeyAsArray("invalidate-regs",
333  invalidate_reg_list)) {
334  const size_t num_regs = invalidate_reg_list->GetSize();
335  if (num_regs > 0) {
336  for (uint32_t idx = 0; idx < num_regs; ++idx) {
337  ConstString invalidate_reg_name;
338  uint64_t invalidate_reg_num;
339  if (invalidate_reg_list->GetItemAtIndexAsString(
340  idx, invalidate_reg_name)) {
341  const RegisterInfo *invalidate_reg_info =
342  GetRegisterInfo(invalidate_reg_name.GetStringRef());
343  if (invalidate_reg_info) {
344  m_invalidate_regs_map[i].push_back(
345  invalidate_reg_info->kinds[eRegisterKindLLDB]);
346  } else {
347  // TODO: print error invalid slice string that doesn't follow the
348  // format
349  printf("error: failed to find a 'invalidate-regs' register for "
350  "\"%s\" while parsing register \"%s\"\n",
351  invalidate_reg_name.GetCString(), reg_info.name);
352  }
353  } else if (invalidate_reg_list->GetItemAtIndexAsInteger(
354  idx, invalidate_reg_num)) {
355  if (invalidate_reg_num != UINT64_MAX)
356  m_invalidate_regs_map[i].push_back(invalidate_reg_num);
357  else
358  printf("error: 'invalidate-regs' list value wasn't a valid "
359  "integer\n");
360  } else {
361  printf("error: 'invalidate-regs' list value wasn't a python string "
362  "or integer\n");
363  }
364  }
365  } else {
366  printf("error: 'invalidate-regs' contained an empty list\n");
367  }
368  }
369 
370  // Calculate the register offset
371  const size_t end_reg_offset = reg_info.byte_offset + reg_info.byte_size;
372  if (m_reg_data_byte_size < end_reg_offset)
373  m_reg_data_byte_size = end_reg_offset;
374 
375  m_regs.push_back(reg_info);
376  m_set_reg_nums[set].push_back(i);
377  }
378  Finalize(arch);
379  return m_regs.size();
380 }
381 
382 size_t DynamicRegisterInfo::SetRegisterInfo(
383  std::vector<DynamicRegisterInfo::Register> &&regs,
384  const ArchSpec &arch) {
385  assert(!m_finalized);
386 
387  for (auto it : llvm::enumerate(regs)) {
388  uint32_t local_regnum = it.index();
389  const DynamicRegisterInfo::Register &reg = it.value();
390 
391  assert(reg.name);
392  assert(reg.set_name);
393 
394  if (!reg.value_regs.empty())
395  m_value_regs_map[local_regnum] = std::move(reg.value_regs);
396  if (!reg.invalidate_regs.empty())
397  m_invalidate_regs_map[local_regnum] = std::move(reg.invalidate_regs);
398  if (reg.value_reg_offset != 0) {
399  assert(reg.value_regs.size() == 1);
400  m_value_reg_offset_map[local_regnum] = reg.value_reg_offset;
401  }
402 
403  struct RegisterInfo reg_info {
404  reg.name.AsCString(), reg.alt_name.AsCString(), reg.byte_size,
405  reg.byte_offset, reg.encoding, reg.format,
407  reg.regnum_remote, local_regnum},
408  // value_regs and invalidate_regs are filled by Finalize()
409  nullptr, nullptr
410  };
411 
412  m_regs.push_back(reg_info);
413 
414  uint32_t set = GetRegisterSetIndexByName(reg.set_name, true);
415  assert(set < m_sets.size());
416  assert(set < m_set_reg_nums.size());
417  assert(set < m_set_names.size());
418  m_set_reg_nums[set].push_back(local_regnum);
419  };
420 
421  Finalize(arch);
422  return m_regs.size();
423 }
424 
425 void DynamicRegisterInfo::AddRegister(RegisterInfo reg_info,
426  ConstString &set_name) {
427  assert(!m_finalized);
428  const uint32_t reg_num = m_regs.size();
429  assert(reg_info.name);
430  uint32_t i;
431  if (reg_info.value_regs) {
432  for (i = 0; reg_info.value_regs[i] != LLDB_INVALID_REGNUM; ++i)
433  m_value_regs_map[reg_num].push_back(reg_info.value_regs[i]);
434 
435  // invalidate until Finalize() is called
436  reg_info.value_regs = nullptr;
437  }
438  if (reg_info.invalidate_regs) {
439  for (i = 0; reg_info.invalidate_regs[i] != LLDB_INVALID_REGNUM; ++i)
440  m_invalidate_regs_map[reg_num].push_back(reg_info.invalidate_regs[i]);
441 
442  // invalidate until Finalize() is called
443  reg_info.invalidate_regs = nullptr;
444  }
445 
446  m_regs.push_back(reg_info);
447  uint32_t set = GetRegisterSetIndexByName(set_name, true);
448  assert(set < m_sets.size());
449  assert(set < m_set_reg_nums.size());
450  assert(set < m_set_names.size());
451  m_set_reg_nums[set].push_back(reg_num);
452 }
453 
455  if (m_finalized)
456  return;
457 
458  m_finalized = true;
459  const size_t num_sets = m_sets.size();
460  for (size_t set = 0; set < num_sets; ++set) {
461  assert(m_sets.size() == m_set_reg_nums.size());
462  m_sets[set].num_registers = m_set_reg_nums[set].size();
463  m_sets[set].registers = m_set_reg_nums[set].data();
464  }
465 
466  // make sure value_regs are terminated with LLDB_INVALID_REGNUM
467 
468  for (reg_to_regs_map::iterator pos = m_value_regs_map.begin(),
469  end = m_value_regs_map.end();
470  pos != end; ++pos) {
471  if (pos->second.back() != LLDB_INVALID_REGNUM)
472  pos->second.push_back(LLDB_INVALID_REGNUM);
473  }
474 
475  // Now update all value_regs with each register info as needed
476  const size_t num_regs = m_regs.size();
477  for (size_t i = 0; i < num_regs; ++i) {
478  if (m_value_regs_map.find(i) != m_value_regs_map.end())
479  m_regs[i].value_regs = m_value_regs_map[i].data();
480  else
481  m_regs[i].value_regs = nullptr;
482  }
483 
484  // Expand all invalidation dependencies
485  for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(),
486  end = m_invalidate_regs_map.end();
487  pos != end; ++pos) {
488  const uint32_t reg_num = pos->first;
489 
490  if (m_regs[reg_num].value_regs) {
491  reg_num_collection extra_invalid_regs;
492  for (const uint32_t invalidate_reg_num : pos->second) {
493  reg_to_regs_map::iterator invalidate_pos =
494  m_invalidate_regs_map.find(invalidate_reg_num);
495  if (invalidate_pos != m_invalidate_regs_map.end()) {
496  for (const uint32_t concrete_invalidate_reg_num :
497  invalidate_pos->second) {
498  if (concrete_invalidate_reg_num != reg_num)
499  extra_invalid_regs.push_back(concrete_invalidate_reg_num);
500  }
501  }
502  }
503  pos->second.insert(pos->second.end(), extra_invalid_regs.begin(),
504  extra_invalid_regs.end());
505  }
506  }
507 
508  // sort and unique all invalidate registers and make sure each is terminated
509  // with LLDB_INVALID_REGNUM
510  for (reg_to_regs_map::iterator pos = m_invalidate_regs_map.begin(),
511  end = m_invalidate_regs_map.end();
512  pos != end; ++pos) {
513  if (pos->second.size() > 1) {
514  llvm::sort(pos->second.begin(), pos->second.end());
515  reg_num_collection::iterator unique_end =
516  std::unique(pos->second.begin(), pos->second.end());
517  if (unique_end != pos->second.end())
518  pos->second.erase(unique_end, pos->second.end());
519  }
520  assert(!pos->second.empty());
521  if (pos->second.back() != LLDB_INVALID_REGNUM)
522  pos->second.push_back(LLDB_INVALID_REGNUM);
523  }
524 
525  // Now update all invalidate_regs with each register info as needed
526  for (size_t i = 0; i < num_regs; ++i) {
527  if (m_invalidate_regs_map.find(i) != m_invalidate_regs_map.end())
528  m_regs[i].invalidate_regs = m_invalidate_regs_map[i].data();
529  else
530  m_regs[i].invalidate_regs = nullptr;
531  }
532 
533  // Check if we need to automatically set the generic registers in case they
534  // weren't set
535  bool generic_regs_specified = false;
536  for (const auto &reg : m_regs) {
537  if (reg.kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM) {
538  generic_regs_specified = true;
539  break;
540  }
541  }
542 
543  if (!generic_regs_specified) {
544  switch (arch.GetMachine()) {
545  case llvm::Triple::aarch64:
546  case llvm::Triple::aarch64_32:
547  case llvm::Triple::aarch64_be:
548  for (auto &reg : m_regs) {
549  if (strcmp(reg.name, "pc") == 0)
551  else if ((strcmp(reg.name, "fp") == 0) ||
552  (strcmp(reg.name, "x29") == 0))
554  else if ((strcmp(reg.name, "lr") == 0) ||
555  (strcmp(reg.name, "x30") == 0))
557  else if ((strcmp(reg.name, "sp") == 0) ||
558  (strcmp(reg.name, "x31") == 0))
560  else if (strcmp(reg.name, "cpsr") == 0)
562  }
563  break;
564 
565  case llvm::Triple::arm:
566  case llvm::Triple::armeb:
567  case llvm::Triple::thumb:
568  case llvm::Triple::thumbeb:
569  for (auto &reg : m_regs) {
570  if ((strcmp(reg.name, "pc") == 0) || (strcmp(reg.name, "r15") == 0))
572  else if ((strcmp(reg.name, "sp") == 0) ||
573  (strcmp(reg.name, "r13") == 0))
575  else if ((strcmp(reg.name, "lr") == 0) ||
576  (strcmp(reg.name, "r14") == 0))
578  else if ((strcmp(reg.name, "r7") == 0) &&
579  arch.GetTriple().getVendor() == llvm::Triple::Apple)
581  else if ((strcmp(reg.name, "r11") == 0) &&
582  arch.GetTriple().getVendor() != llvm::Triple::Apple)
584  else if (strcmp(reg.name, "fp") == 0)
586  else if (strcmp(reg.name, "cpsr") == 0)
588  }
589  break;
590 
591  case llvm::Triple::x86:
592  for (auto &reg : m_regs) {
593  if ((strcmp(reg.name, "eip") == 0) || (strcmp(reg.name, "pc") == 0))
595  else if ((strcmp(reg.name, "esp") == 0) ||
596  (strcmp(reg.name, "sp") == 0))
598  else if ((strcmp(reg.name, "ebp") == 0) ||
599  (strcmp(reg.name, "fp") == 0))
601  else if ((strcmp(reg.name, "eflags") == 0) ||
602  (strcmp(reg.name, "flags") == 0))
604  }
605  break;
606 
607  case llvm::Triple::x86_64:
608  for (auto &reg : m_regs) {
609  if ((strcmp(reg.name, "rip") == 0) || (strcmp(reg.name, "pc") == 0))
611  else if ((strcmp(reg.name, "rsp") == 0) ||
612  (strcmp(reg.name, "sp") == 0))
614  else if ((strcmp(reg.name, "rbp") == 0) ||
615  (strcmp(reg.name, "fp") == 0))
617  else if ((strcmp(reg.name, "rflags") == 0) ||
618  (strcmp(reg.name, "eflags") == 0) ||
619  (strcmp(reg.name, "flags") == 0))
621  }
622  break;
623 
624  default:
625  break;
626  }
627  }
628 
629  // At this stage call ConfigureOffsets to calculate register offsets for
630  // targets supporting dynamic offset calculation. It also calculates
631  // total byte size of register data.
632  ConfigureOffsets();
633 
634  // Check if register info is reconfigurable
635  // AArch64 SVE register set has configurable register sizes
636  if (arch.GetTriple().isAArch64()) {
637  for (const auto &reg : m_regs) {
638  if (strcmp(reg.name, "vg") == 0) {
639  m_is_reconfigurable = true;
640  break;
641  }
642  }
643  }
644 }
645 
646 void DynamicRegisterInfo::ConfigureOffsets() {
647  // We are going to create a map between remote (eRegisterKindProcessPlugin)
648  // and local (eRegisterKindLLDB) register numbers. This map will give us
649  // remote register numbers in increasing order for offset calculation.
650  std::map<uint32_t, uint32_t> remote_to_local_regnum_map;
651  for (const auto &reg : m_regs)
652  remote_to_local_regnum_map[reg.kinds[eRegisterKindProcessPlugin]] =
653  reg.kinds[eRegisterKindLLDB];
654 
655  // At this stage we manually calculate g/G packet offsets of all primary
656  // registers, only if target XML or qRegisterInfo packet did not send
657  // an offset explicitly.
658  uint32_t reg_offset = 0;
659  for (auto const &regnum_pair : remote_to_local_regnum_map) {
660  if (m_regs[regnum_pair.second].byte_offset == LLDB_INVALID_INDEX32 &&
661  m_regs[regnum_pair.second].value_regs == nullptr) {
662  m_regs[regnum_pair.second].byte_offset = reg_offset;
663 
664  reg_offset = m_regs[regnum_pair.second].byte_offset +
665  m_regs[regnum_pair.second].byte_size;
666  }
667  }
668 
669  // Now update all value_regs with each register info as needed
670  for (auto &reg : m_regs) {
671  if (reg.value_regs != nullptr) {
672  // Assign a valid offset to all pseudo registers that have only a single
673  // parent register in value_regs list, if not assigned by stub. Pseudo
674  // registers with value_regs list populated will share same offset as
675  // that of their corresponding parent register.
676  if (reg.byte_offset == LLDB_INVALID_INDEX32) {
677  uint32_t value_regnum = reg.value_regs[0];
678  if (value_regnum != LLDB_INVALID_INDEX32 &&
679  reg.value_regs[1] == LLDB_INVALID_INDEX32) {
680  reg.byte_offset =
681  GetRegisterInfoAtIndex(value_regnum)->byte_offset;
682  auto it = m_value_reg_offset_map.find(reg.kinds[eRegisterKindLLDB]);
683  if (it != m_value_reg_offset_map.end())
684  reg.byte_offset += it->second;
685  }
686  }
687  }
688 
689  reg_offset = reg.byte_offset + reg.byte_size;
690  if (m_reg_data_byte_size < reg_offset)
691  m_reg_data_byte_size = reg_offset;
692  }
693 }
694 
695 bool DynamicRegisterInfo::IsReconfigurable() { return m_is_reconfigurable; }
696 
697 size_t DynamicRegisterInfo::GetNumRegisters() const { return m_regs.size(); }
698 
699 size_t DynamicRegisterInfo::GetNumRegisterSets() const { return m_sets.size(); }
700 
701 size_t DynamicRegisterInfo::GetRegisterDataByteSize() const {
702  return m_reg_data_byte_size;
703 }
704 
705 const RegisterInfo *
706 DynamicRegisterInfo::GetRegisterInfoAtIndex(uint32_t i) const {
707  if (i < m_regs.size())
708  return &m_regs[i];
709  return nullptr;
710 }
711 
712 const RegisterInfo *DynamicRegisterInfo::GetRegisterInfo(uint32_t kind,
713  uint32_t num) const {
714  uint32_t reg_index = ConvertRegisterKindToRegisterNumber(kind, num);
715  if (reg_index != LLDB_INVALID_REGNUM)
716  return &m_regs[reg_index];
717  return nullptr;
718 }
719 
720 const RegisterSet *DynamicRegisterInfo::GetRegisterSet(uint32_t i) const {
721  if (i < m_sets.size())
722  return &m_sets[i];
723  return nullptr;
724 }
725 
726 uint32_t
727 DynamicRegisterInfo::GetRegisterSetIndexByName(const ConstString &set_name,
728  bool can_create) {
729  name_collection::iterator pos, end = m_set_names.end();
730  for (pos = m_set_names.begin(); pos != end; ++pos) {
731  if (*pos == set_name)
732  return std::distance(m_set_names.begin(), pos);
733  }
734 
735  m_set_names.push_back(set_name);
736  m_set_reg_nums.resize(m_set_reg_nums.size() + 1);
737  RegisterSet new_set = {set_name.AsCString(), nullptr, 0, nullptr};
738  m_sets.push_back(new_set);
739  return m_sets.size() - 1;
740 }
741 
742 uint32_t
743 DynamicRegisterInfo::ConvertRegisterKindToRegisterNumber(uint32_t kind,
744  uint32_t num) const {
745  reg_collection::const_iterator pos, end = m_regs.end();
746  for (pos = m_regs.begin(); pos != end; ++pos) {
747  if (pos->kinds[kind] == num)
748  return std::distance(m_regs.begin(), pos);
749  }
750 
751  return LLDB_INVALID_REGNUM;
752 }
753 
754 void DynamicRegisterInfo::Clear() {
755  m_regs.clear();
756  m_sets.clear();
757  m_set_reg_nums.clear();
758  m_set_names.clear();
759  m_value_regs_map.clear();
760  m_invalidate_regs_map.clear();
761  m_reg_data_byte_size = 0;
762  m_finalized = false;
763 }
764 
765 void DynamicRegisterInfo::Dump() const {
766  StreamFile s(stdout, false);
767  const size_t num_regs = m_regs.size();
768  s.Printf("%p: DynamicRegisterInfo contains %" PRIu64 " registers:\n",
769  static_cast<const void *>(this), static_cast<uint64_t>(num_regs));
770  for (size_t i = 0; i < num_regs; ++i) {
771  s.Printf("[%3" PRIu64 "] name = %-10s", (uint64_t)i, m_regs[i].name);
772  s.Printf(", size = %2u, offset = %4u, encoding = %u, format = %-10s",
773  m_regs[i].byte_size, m_regs[i].byte_offset, m_regs[i].encoding,
774  FormatManager::GetFormatAsCString(m_regs[i].format));
775  if (m_regs[i].kinds[eRegisterKindProcessPlugin] != LLDB_INVALID_REGNUM)
776  s.Printf(", process plugin = %3u",
777  m_regs[i].kinds[eRegisterKindProcessPlugin]);
778  if (m_regs[i].kinds[eRegisterKindDWARF] != LLDB_INVALID_REGNUM)
779  s.Printf(", dwarf = %3u", m_regs[i].kinds[eRegisterKindDWARF]);
780  if (m_regs[i].kinds[eRegisterKindEHFrame] != LLDB_INVALID_REGNUM)
781  s.Printf(", ehframe = %3u", m_regs[i].kinds[eRegisterKindEHFrame]);
782  if (m_regs[i].kinds[eRegisterKindGeneric] != LLDB_INVALID_REGNUM)
783  s.Printf(", generic = %3u", m_regs[i].kinds[eRegisterKindGeneric]);
784  if (m_regs[i].alt_name)
785  s.Printf(", alt-name = %s", m_regs[i].alt_name);
786  if (m_regs[i].value_regs) {
787  s.Printf(", value_regs = [ ");
788  for (size_t j = 0; m_regs[i].value_regs[j] != LLDB_INVALID_REGNUM; ++j) {
789  s.Printf("%s ", m_regs[m_regs[i].value_regs[j]].name);
790  }
791  s.Printf("]");
792  }
793  if (m_regs[i].invalidate_regs) {
794  s.Printf(", invalidate_regs = [ ");
795  for (size_t j = 0; m_regs[i].invalidate_regs[j] != LLDB_INVALID_REGNUM;
796  ++j) {
797  s.Printf("%s ", m_regs[m_regs[i].invalidate_regs[j]].name);
798  }
799  s.Printf("]");
800  }
801  s.EOL();
802  }
803 
804  const size_t num_sets = m_sets.size();
805  s.Printf("%p: DynamicRegisterInfo contains %" PRIu64 " register sets:\n",
806  static_cast<const void *>(this), static_cast<uint64_t>(num_sets));
807  for (size_t i = 0; i < num_sets; ++i) {
808  s.Printf("set[%" PRIu64 "] name = %s, regs = [", (uint64_t)i,
809  m_sets[i].name);
810  for (size_t idx = 0; idx < m_sets[i].num_registers; ++idx) {
811  s.Printf("%s ", m_regs[m_sets[i].registers[idx]].name);
812  }
813  s.Printf("]\n");
814  }
815 }
816 
817 const lldb_private::RegisterInfo *
818 DynamicRegisterInfo::GetRegisterInfo(llvm::StringRef reg_name) const {
819  for (auto &reg_info : m_regs)
820  if (reg_info.name == reg_name)
821  return &reg_info;
822  return nullptr;
823 }
824 
826  std::vector<DynamicRegisterInfo::Register> &regs,
827  DynamicRegisterInfo::Register new_reg_info) {
828  assert(!new_reg_info.value_regs.empty());
829  const uint32_t reg_num = regs.size();
830  regs.push_back(new_reg_info);
831 
832  std::map<uint32_t, std::vector<uint32_t>> new_invalidates;
833  for (uint32_t value_reg : new_reg_info.value_regs) {
834  // copy value_regs to invalidate_regs
835  new_invalidates[reg_num].push_back(value_reg);
836 
837  // copy invalidate_regs from the parent register
838  llvm::append_range(new_invalidates[reg_num],
839  regs[value_reg].invalidate_regs);
840 
841  // add reverse invalidate entries
842  for (uint32_t x : new_invalidates[reg_num])
843  new_invalidates[x].push_back(reg_num);
844  }
845 
846  for (const auto &x : new_invalidates)
847  llvm::append_range(regs[x.first].invalidate_regs, x.second);
848 }
RegularExpression.h
lldb_private::DynamicRegisterInfo::Register::byte_size
uint32_t byte_size
Definition: DynamicRegisterInfo.h:31
lldb_private::ArchSpec::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Returns the byte order for the architecture specification.
Definition: ArchSpec.cpp:730
lldb_private::StructuredData::Dictionary
Definition: StructuredData.h:352
lldb_private::ArchSpec
Definition: ArchSpec.h:33
LLDB_INVALID_REGNUM
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:91
lldb_private::ArchSpec::GetMachine
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:667
lldb_private::DynamicRegisterInfo::Register::alt_name
ConstString alt_name
Definition: DynamicRegisterInfo.h:29
lldb_private::StructuredData::Array
Definition: StructuredData.h:165
lldb::eRegisterKindDWARF
@ eRegisterKindDWARF
the register numbers seen DWARF
Definition: lldb-enumerations.h:229
lldb_private::DynamicRegisterInfo::Register::set_name
ConstString set_name
Definition: DynamicRegisterInfo.h:30
StructuredData.h
lldb::eRegisterKindGeneric
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target
Definition: lldb-enumerations.h:230
FormatManager.h
lldb_private::StructuredData::Array::GetItemAtIndexAsDictionary
bool GetItemAtIndexAsDictionary(size_t idx, Dictionary *&result) const
Definition: StructuredData.h:254
lldb_private::StructuredData::Object::DumpToStdout
void DumpToStdout(bool pretty_print=true) const
Definition: StructuredData.cpp:129
DynamicRegisterInfo.h
OptionArgParser.h
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:193
lldb_private::addSupplementaryRegister
void addSupplementaryRegister(std::vector< DynamicRegisterInfo::Register > &regs, DynamicRegisterInfo::Register new_reg_info)
Definition: DynamicRegisterInfo.cpp:825
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:444
lldb_private::StructuredData::Dictionary::GetValueForKeyAsInteger
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
Definition: StructuredData.h:403
lldb_private::DynamicRegisterInfo::Register::format
lldb::Format format
Definition: DynamicRegisterInfo.h:34
lldb::eRegisterKindEHFrame
@ eRegisterKindEHFrame
the register numbers seen in eh_frame
Definition: lldb-enumerations.h:228
LLDB_REGNUM_GENERIC_FLAGS
#define LLDB_REGNUM_GENERIC_FLAGS
Definition: lldb-defines.h:67
lldb_private::DynamicRegisterInfo::Register::encoding
lldb::Encoding encoding
Definition: DynamicRegisterInfo.h:33
lldb::eRegisterKindLLDB
@ eRegisterKindLLDB
lldb's internal register numbers
Definition: lldb-enumerations.h:234
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:202
lldb_private::DynamicRegisterInfo::Register::invalidate_regs
std::vector< uint32_t > invalidate_regs
Definition: DynamicRegisterInfo.h:40
Log.h
lldb_private::ConstString::IsEmpty
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:304
lldb::eRegisterKindProcessPlugin
@ eRegisterKindProcessPlugin
num used by the process plugin - e.g.
Definition: lldb-enumerations.h:232
lldb_private::DynamicRegisterInfo::Register::regnum_remote
uint32_t regnum_remote
Definition: DynamicRegisterInfo.h:38
lldb_private::DynamicRegisterInfo::Register::regnum_dwarf
uint32_t regnum_dwarf
Definition: DynamicRegisterInfo.h:35
lldb_private::repro::Finalize
llvm::Error Finalize(Loader *loader)
Definition: Reproducer.cpp:365
lldb::eEncodingUint
@ eEncodingUint
unsigned integer
Definition: lldb-enumerations.h:148
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::DynamicRegisterInfo::Register::byte_offset
uint32_t byte_offset
Definition: DynamicRegisterInfo.h:32
set
set(option_framework FRAMEWORK) endif() if(LLDB_ENABLE_PYTHON) get_target_property(python_bindings_dir swig_wrapper_python BINARY_DIR) set(lldb_python_wrapper $
Definition: API/CMakeLists.txt:9
lldb_private::DynamicRegisterInfo::Register
Definition: DynamicRegisterInfo.h:27
lldb_private::DynamicRegisterInfo::Register::value_regs
std::vector< uint32_t > value_regs
Definition: DynamicRegisterInfo.h:39
lldb_private::DynamicRegisterInfo::Register::value_reg_offset
uint32_t value_reg_offset
Definition: DynamicRegisterInfo.h:41
StreamFile.h
LIBLLDB_LOG_OBJECT
#define LIBLLDB_LOG_OBJECT
Definition: Logging.h:25
uint32_t
lldb_private::DynamicRegisterInfo::reg_num_collection
std::vector< uint32_t > reg_num_collection
Definition: DynamicRegisterInfo.h:101
lldb_private::DynamicRegisterInfo::Register::regnum_generic
uint32_t regnum_generic
Definition: DynamicRegisterInfo.h:37
LLDB_REGNUM_GENERIC_SP
#define LLDB_REGNUM_GENERIC_SP
Definition: lldb-defines.h:64
lldb_private::Stream::EOL
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
StringExtractor.h
lldb_private::StructuredData::Array::GetItemAtIndexAsInteger
bool GetItemAtIndexAsInteger(size_t idx, IntType &result) const
Definition: StructuredData.h:196
ArchSpec.h
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::DynamicRegisterInfo::Register::regnum_ehframe
uint32_t regnum_ehframe
Definition: DynamicRegisterInfo.h:36
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::eFormatHex
@ eFormatHex
Definition: lldb-enumerations.h:169
LLDB_REGNUM_GENERIC_FP
#define LLDB_REGNUM_GENERIC_FP
Definition: lldb-defines.h:65
lldb::eByteOrderBig
@ eByteOrderBig
Definition: lldb-enumerations.h:140
LLDB_REGNUM_GENERIC_PC
#define LLDB_REGNUM_GENERIC_PC
Definition: lldb-defines.h:63
lldb_private::StreamFile
Definition: StreamFile.h:22
LLDB_INVALID_INDEX32
#define LLDB_INVALID_INDEX32
Definition: lldb-defines.h:87
lldb_private::StructuredData::Array::GetItemAtIndexAsString
bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result) const
Definition: StructuredData.h:216
lldb_private::Log
Definition: Log.h:49
lldb_private::StructuredData::Dictionary::GetValueForKeyAsString
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
Definition: StructuredData.h:423
lldb_private::StructuredData::Dictionary::GetValueForKeyAsArray
bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
Definition: StructuredData.h:478
lldb_private::StructuredData::Array::GetSize
size_t GetSize() const
Definition: StructuredData.h:180
lldb_private::DynamicRegisterInfo
Definition: DynamicRegisterInfo.h:21
lldb_private::DynamicRegisterInfo::Register::name
ConstString name
Definition: DynamicRegisterInfo.h:28
lldb::eByteOrderLittle
@ eByteOrderLittle
Definition: lldb-enumerations.h:142
lldb
Definition: SBAddress.h:15
LLDB_LOG_ERROR
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:265
UINT64_MAX
#define UINT64_MAX
Definition: lldb-defines.h:35
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138
LLDB_REGNUM_GENERIC_RA
#define LLDB_REGNUM_GENERIC_RA
Definition: lldb-defines.h:66