xref: /llvm-project/lldb/source/DataFormatters/CXXFunctionPointer.cpp (revision c4fb7180cbbe977f1ab1ce945a691550f8fdd1fb)
1 //===-- CXXFunctionPointer.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 
9 #include "lldb/DataFormatters/CXXFunctionPointer.h"
10 
11 #include "lldb/Target/ABI.h"
12 #include "lldb/Target/SectionLoadList.h"
13 #include "lldb/Target/Target.h"
14 #include "lldb/Utility/Stream.h"
15 #include "lldb/ValueObject/ValueObject.h"
16 #include "lldb/lldb-enumerations.h"
17 
18 #include <string>
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 using namespace lldb_private::formatters;
23 
24 bool lldb_private::formatters::CXXFunctionPointerSummaryProvider(
25     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
26   std::string destination;
27   StreamString sstr;
28   AddressType func_ptr_address_type = eAddressTypeInvalid;
29   addr_t func_ptr_address = valobj.GetPointerValue(&func_ptr_address_type);
30   if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS) {
31     switch (func_ptr_address_type) {
32     case eAddressTypeInvalid:
33     case eAddressTypeFile:
34     case eAddressTypeHost:
35       break;
36 
37     case eAddressTypeLoad: {
38       ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
39 
40       Address so_addr;
41       Target *target = exe_ctx.GetTargetPtr();
42       if (target && target->HasLoadedSections()) {
43         target->ResolveLoadAddress(func_ptr_address, so_addr);
44         if (so_addr.GetSection() == nullptr) {
45           // If we have an address that doesn't correspond to any symbol,
46           // it might have authentication bits.  Strip them & see if it
47           // now points to a symbol -- if so, do the SymbolContext lookup
48           // based on the stripped address.
49           // If we find a symbol with the ptrauth bits stripped, print the
50           // raw value into the stream, and replace the Address with the
51           // one that points to a symbol for a fuller description.
52           if (Process *process = exe_ctx.GetProcessPtr()) {
53             if (ABISP abi_sp = process->GetABI()) {
54               addr_t fixed_addr = abi_sp->FixCodeAddress(func_ptr_address);
55               if (fixed_addr != func_ptr_address) {
56                 Address test_address;
57                 test_address.SetLoadAddress(fixed_addr, target);
58                 if (test_address.GetSection() != nullptr) {
59                   int addrsize = target->GetArchitecture().GetAddressByteSize();
60                   sstr.Printf("actual=0x%*.*" PRIx64 " ", addrsize * 2,
61                               addrsize * 2, fixed_addr);
62                   so_addr = test_address;
63                 }
64               }
65             }
66           }
67         }
68 
69         if (so_addr.IsValid()) {
70           so_addr.Dump(&sstr, exe_ctx.GetBestExecutionContextScope(),
71                        Address::DumpStyleResolvedDescription,
72                        Address::DumpStyleSectionNameOffset);
73         }
74       }
75     } break;
76     }
77   }
78   if (sstr.GetSize() > 0) {
79     if (valobj.GetValueType() == lldb::eValueTypeVTableEntry)
80       stream.PutCString(sstr.GetData());
81     else
82       stream.Printf("(%s)", sstr.GetData());
83     return true;
84   } else
85     return false;
86 }
87