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