1336823Sdim//===-- DumpRegisterValue.cpp -----------------------------------*- C++ -*-===//
2336823Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6336823Sdim//
7336823Sdim//===----------------------------------------------------------------------===//
8336823Sdim
9336823Sdim#include "lldb/Core/DumpRegisterValue.h"
10336823Sdim#include "lldb/Core/DumpDataExtractor.h"
11336823Sdim#include "lldb/Utility/DataExtractor.h"
12344779Sdim#include "lldb/Utility/RegisterValue.h"
13336823Sdim#include "lldb/Utility/StreamString.h"
14336823Sdim#include "lldb/lldb-private-types.h"
15336823Sdim
16336823Sdimusing namespace lldb;
17336823Sdim
18336823Sdimbool lldb_private::DumpRegisterValue(const RegisterValue &reg_val, Stream *s,
19336823Sdim                                     const RegisterInfo *reg_info,
20336823Sdim                                     bool prefix_with_name,
21336823Sdim                                     bool prefix_with_alt_name, Format format,
22336823Sdim                                     uint32_t reg_name_right_align_at) {
23336823Sdim  DataExtractor data;
24336823Sdim  if (reg_val.GetData(data)) {
25336823Sdim    bool name_printed = false;
26336823Sdim    // For simplicity, alignment of the register name printing applies only in
27336823Sdim    // the most common case where:
28336823Sdim    //
29336823Sdim    //     prefix_with_name^prefix_with_alt_name is true
30336823Sdim    //
31336823Sdim    StreamString format_string;
32336823Sdim    if (reg_name_right_align_at && (prefix_with_name ^ prefix_with_alt_name))
33336823Sdim      format_string.Printf("%%%us", reg_name_right_align_at);
34336823Sdim    else
35336823Sdim      format_string.Printf("%%s");
36336823Sdim    std::string fmt = format_string.GetString();
37336823Sdim    if (prefix_with_name) {
38336823Sdim      if (reg_info->name) {
39336823Sdim        s->Printf(fmt.c_str(), reg_info->name);
40336823Sdim        name_printed = true;
41336823Sdim      } else if (reg_info->alt_name) {
42336823Sdim        s->Printf(fmt.c_str(), reg_info->alt_name);
43336823Sdim        prefix_with_alt_name = false;
44336823Sdim        name_printed = true;
45336823Sdim      }
46336823Sdim    }
47336823Sdim    if (prefix_with_alt_name) {
48336823Sdim      if (name_printed)
49336823Sdim        s->PutChar('/');
50336823Sdim      if (reg_info->alt_name) {
51336823Sdim        s->Printf(fmt.c_str(), reg_info->alt_name);
52336823Sdim        name_printed = true;
53336823Sdim      } else if (!name_printed) {
54336823Sdim        // No alternate name but we were asked to display a name, so show the
55336823Sdim        // main name
56336823Sdim        s->Printf(fmt.c_str(), reg_info->name);
57336823Sdim        name_printed = true;
58336823Sdim      }
59336823Sdim    }
60336823Sdim    if (name_printed)
61336823Sdim      s->PutCString(" = ");
62336823Sdim
63336823Sdim    if (format == eFormatDefault)
64336823Sdim      format = reg_info->format;
65336823Sdim
66336823Sdim    DumpDataExtractor(data, s,
67336823Sdim                      0,                    // Offset in "data"
68336823Sdim                      format,               // Format to use when dumping
69336823Sdim                      reg_info->byte_size,  // item_byte_size
70336823Sdim                      1,                    // item_count
71336823Sdim                      UINT32_MAX,           // num_per_line
72336823Sdim                      LLDB_INVALID_ADDRESS, // base_addr
73336823Sdim                      0,                    // item_bit_size
74336823Sdim                      0);                   // item_bit_offset
75336823Sdim    return true;
76336823Sdim  }
77336823Sdim  return false;
78336823Sdim}
79