blob: 987eb43b73242a76fb8ea0b20a537017bf101c5f [file] [log] [blame]
Dean Michael Berrisa0e3ae42018-05-02 00:43:17 +00001//===- xray-fc-account.cpp: XRay Function Call Accounting Tool ------------===//
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Implementation of the helper tools dealing with XRay-generated function ids.
10//
11//===----------------------------------------------------------------------===//
12
13#include "func-id-helper.h"
14#include "llvm/Support/Path.h"
15#include <sstream>
16
17using namespace llvm;
18using namespace xray;
19
20std::string FuncIdConversionHelper::SymbolOrNumber(int32_t FuncId) const {
Martin Pelikan86ed8e52018-03-01 01:59:24 +000021 auto CacheIt = CachedNames.find(FuncId);
22 if (CacheIt != CachedNames.end())
23 return CacheIt->second;
24
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000025 std::ostringstream F;
26 auto It = FunctionAddresses.find(FuncId);
27 if (It == FunctionAddresses.end()) {
28 F << "#" << FuncId;
29 return F.str();
30 }
31
32 if (auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, It->second)) {
33 auto &DI = *ResOrErr;
34 if (DI.FunctionName == "<invalid>")
35 F << "@(" << std::hex << It->second << ")";
36 else
37 F << DI.FunctionName;
38 } else
39 handleAllErrors(ResOrErr.takeError(), [&](const ErrorInfoBase &) {
40 F << "@(" << std::hex << It->second << ")";
41 });
42
Martin Pelikan86ed8e52018-03-01 01:59:24 +000043 auto S = F.str();
44 CachedNames[FuncId] = S;
45 return S;
Dean Michael Berrisf8f909f2017-01-10 02:38:11 +000046}
47
48std::string FuncIdConversionHelper::FileLineAndColumn(int32_t FuncId) const {
49 auto It = FunctionAddresses.find(FuncId);
50 if (It == FunctionAddresses.end())
51 return "(unknown)";
52
53 std::ostringstream F;
54 auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, It->second);
55 if (!ResOrErr) {
56 consumeError(ResOrErr.takeError());
57 return "(unknown)";
58 }
59
60 auto &DI = *ResOrErr;
61 F << sys::path::filename(DI.FileName).str() << ":" << DI.Line << ":"
62 << DI.Column;
63
64 return F.str();
65}