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