-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.cc
82 lines (72 loc) · 2.51 KB
/
util.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018, Lawrence Livermore National Security, LLC.
// Produced at the Lawrence Livermore National Laboratory.
//
// Written by Emilio Castillo <[email protected]>.
// LLNL-CODE-745958. All rights reserved.
//
// This file is part of Loupe. For details, see:
// https://github.com/LLNL/loupe
// Please also read the LICENSE file for the MIT License notice.
//////////////////////////////////////////////////////////////////////////////
#define UNW_LOCAL_ONLY
#include <cxxabi.h>
#include <libunwind.h>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream>
#include "util.hh"
std::map<uint64_t, std::string> g_symbols;
// Call this function to get a backtrace.
void backtrace(uint64_t *pc_val) {
char sym[256];
char aux[256];
unw_cursor_t cursor;
unw_context_t context;
// Initialize cursor to current frame for local unwinding.
unw_getcontext(&context);
unw_init_local(&cursor, &context);
// Unwind frames until we get to the caller
unw_step(&cursor);
unw_step(&cursor);
unw_step(&cursor);
//while (unw_step(&cursor) > 0) {
unw_word_t offset, pc;
unw_get_reg(&cursor, UNW_REG_IP, &pc);
*pc_val = pc;
if(g_symbols.find(pc)==g_symbols.end()){
if (unw_get_proc_name(&cursor, aux, sizeof(aux), &offset) == 0) {
int status;
char* demangled = abi::__cxa_demangle(aux, nullptr, nullptr, &status);
if(demangled){
sprintf(sym,"(%s+0x%lx)", demangled, offset);
std::free(demangled);
} else {
sprintf(sym,"(%s+0x%lx)", aux, offset);
}
} else {
sprintf(sym,"(UNKNOWN+0x%lx)", offset);
}
g_symbols[*pc_val] = sym;
}
}
// Creates a dict file for the PC and the calls name
void dump_symbols(std::string* callnames, const std::string& name){
//Prepare all the data in the symbols
//We will need to receive the symbols from other ranks too
//ALL GATHER
std::ostringstream filename;
filename << name << ".sym";
std::ofstream myfile (filename.str());
for(auto it=g_symbols.begin();it!=g_symbols.end();it++){
myfile << it->first << "\t"<< it->second <<std::endl;
}
//TODO only output the ids of the calls we did
//356 is the number of mpi calls generated by wrap
for(int i=0;i<356;i++){
myfile << i << "\t" << callnames[i] <<std::endl;
}
myfile.close();
}