blob: c2bef6ddfb3966c3f35c41b39f75313699bb1aa1 [file] [log] [blame]
Dean Michael Berrisf4f861b2018-05-02 00:43:17 +00001//===- xray-fc-account.cpp: XRay Function Call Accounting Tool ------------===//
Dean Michael Berris6e1f0662017-01-10 02:38:11 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implementation of the helper tools dealing with XRay-generated function ids.
11//
12//===----------------------------------------------------------------------===//
13
14#include "func-id-helper.h"
15#include "llvm/Support/Path.h"
16#include <sstream>
17
18using namespace llvm;
19using namespace xray;
20
21std::string FuncIdConversionHelper::SymbolOrNumber(int32_t FuncId) const {
Martin Pelikan26e6f682018-03-01 01:59:24 +000022 auto CacheIt = CachedNames.find(FuncId);
23 if (CacheIt != CachedNames.end())
24 return CacheIt->second;
25
Dean Michael Berris6e1f0662017-01-10 02:38:11 +000026 std::ostringstream F;
27 auto It = FunctionAddresses.find(FuncId);
28 if (It == FunctionAddresses.end()) {
29 F << "#" << FuncId;
30 return F.str();
31 }
32
33 if (auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, It->second)) {
34 auto &DI = *ResOrErr;
35 if (DI.FunctionName == "<invalid>")
36 F << "@(" << std::hex << It->second << ")";
37 else
38 F << DI.FunctionName;
39 } else
40 handleAllErrors(ResOrErr.takeError(), [&](const ErrorInfoBase &) {
41 F << "@(" << std::hex << It->second << ")";
42 });
43
Martin Pelikan26e6f682018-03-01 01:59:24 +000044 auto S = F.str();
45 CachedNames[FuncId] = S;
46 return S;
Dean Michael Berris6e1f0662017-01-10 02:38:11 +000047}
48
49std::string FuncIdConversionHelper::FileLineAndColumn(int32_t FuncId) const {
50 auto It = FunctionAddresses.find(FuncId);
51 if (It == FunctionAddresses.end())
52 return "(unknown)";
53
54 std::ostringstream F;
55 auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, It->second);
56 if (!ResOrErr) {
57 consumeError(ResOrErr.takeError());
58 return "(unknown)";
59 }
60
61 auto &DI = *ResOrErr;
62 F << sys::path::filename(DI.FileName).str() << ":" << DI.Line << ":"
63 << DI.Column;
64
65 return F.str();
66}