1//===-- DumpRegisterValue.cpp -----------------------------------*- C++ -*-===//
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
9#include "lldb/Core/DumpRegisterValue.h"
10#include "lldb/Core/DumpDataExtractor.h"
11#include "lldb/Utility/DataExtractor.h"
12#include "lldb/Utility/RegisterValue.h"
13#include "lldb/Utility/StreamString.h"
14#include "lldb/lldb-private-types.h"
15
16using namespace lldb;
17
18bool lldb_private::DumpRegisterValue(const RegisterValue &reg_val, Stream *s,
19                                     const RegisterInfo *reg_info,
20                                     bool prefix_with_name,
21                                     bool prefix_with_alt_name, Format format,
22                                     uint32_t reg_name_right_align_at) {
23  DataExtractor data;
24  if (reg_val.GetData(data)) {
25    bool name_printed = false;
26    // For simplicity, alignment of the register name printing applies only in
27    // the most common case where:
28    //
29    //     prefix_with_name^prefix_with_alt_name is true
30    //
31    StreamString format_string;
32    if (reg_name_right_align_at && (prefix_with_name ^ prefix_with_alt_name))
33      format_string.Printf("%%%us", reg_name_right_align_at);
34    else
35      format_string.Printf("%%s");
36    std::string fmt = format_string.GetString();
37    if (prefix_with_name) {
38      if (reg_info->name) {
39        s->Printf(fmt.c_str(), reg_info->name);
40        name_printed = true;
41      } else if (reg_info->alt_name) {
42        s->Printf(fmt.c_str(), reg_info->alt_name);
43        prefix_with_alt_name = false;
44        name_printed = true;
45      }
46    }
47    if (prefix_with_alt_name) {
48      if (name_printed)
49        s->PutChar('/');
50      if (reg_info->alt_name) {
51        s->Printf(fmt.c_str(), reg_info->alt_name);
52        name_printed = true;
53      } else if (!name_printed) {
54        // No alternate name but we were asked to display a name, so show the
55        // main name
56        s->Printf(fmt.c_str(), reg_info->name);
57        name_printed = true;
58      }
59    }
60    if (name_printed)
61      s->PutCString(" = ");
62
63    if (format == eFormatDefault)
64      format = reg_info->format;
65
66    DumpDataExtractor(data, s,
67                      0,                    // Offset in "data"
68                      format,               // Format to use when dumping
69                      reg_info->byte_size,  // item_byte_size
70                      1,                    // item_count
71                      UINT32_MAX,           // num_per_line
72                      LLDB_INVALID_ADDRESS, // base_addr
73                      0,                    // item_bit_size
74                      0);                   // item_bit_offset
75    return true;
76  }
77  return false;
78}
79