HPCToolkit
VdsoSymbols.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //******************************************************************************
48 // system includes
49 //******************************************************************************
50 
51 #include <iostream>
52 #include <string.h>
53 
54 
55 
56 //******************************************************************************
57 // local includes
58 //******************************************************************************
59 
60 #include <lib/prof-lean/vdso.h>
61 
62 #include "dso_symbols.h"
63 #include "VdsoSymbols.hpp"
64 
65 
66 
67 //******************************************************************************
68 // private functions
69 //******************************************************************************
70 
73 (
74  dso_symbol_bind_t bind
75 )
76 {
77  switch(bind) {
78  case dso_symbol_bind_local: return SimpleSymbolBinding_Local;
79  case dso_symbol_bind_global: return SimpleSymbolBinding_Global;
80  case dso_symbol_bind_weak: return SimpleSymbolBinding_Weak;
81  case dso_symbol_bind_other: return SimpleSymbolBinding_Other;
82  }
83 }
84 
85 
86 static void
88 (
89  const char *sym_name,
90  int64_t sym_addr,
91  dso_symbol_bind_t binding,
92  void *callback_arg
93 )
94 {
95  VdsoSymbols *vdso_syms = (VdsoSymbols *) callback_arg;
96  SimpleSymbolBinding btype = binding_xlate(binding);
97 
98  // add name to the set of function symbols
99  vdso_syms->add((uint64_t) sym_addr, SimpleSymbolKind_Function,
100  btype, sym_name);
101 }
102 
103 
104 
105 //******************************************************************************
106 // interface operations
107 //******************************************************************************
108 
110 (
111  void
112 ) : SimpleSymbols(VDSO_SEGMENT_NAME_SHORT)
113 {
114 }
115 
116 
117 bool
118 VdsoSymbols::parse(const char *pathname)
119 {
120  int success = (pathname ?
121  dso_symbols(pathname, note_symbol, this) :
122  dso_symbols_vdso(note_symbol, this)) ;
123 
124  if (success) coalesce(chooseHighestBinding);
125 
126 #ifdef DEBUG_VDSOSYMBOLS
127  dump();
128 #endif
129 
130  return success ? true : false;
131 }
132 
133 
134 bool
136 (
137  const char *pathname
138 )
139 {
140  const char *slash = strrchr(pathname, '/');
141  const char *basename = (slash ? slash + 1 : pathname);
142  return strcmp(basename, VDSO_SEGMENT_NAME_SHORT) == 0;
143 }
144 
145 
148 (
149  void
150 )
151 {
152  return new VdsoSymbols;
153 }
154 
155 
156 
157 //******************************************************************************
158 // unit test
159 //******************************************************************************
160 // #define UNIT_TEST
161 
162 #ifdef UNIT_TEST
163 
164 int main(int argc, char **argv)
165 {
166  VdsoSymbols syms;
167  syms.parse();
168  syms.dump();
169 
170  uint64_t addr = 0;
171  if (argc == 2) {
172  sscanf(argv[1], "%lx", &addr);
173  std::string name;
174  SimpleSymbol *sym = syms.find(addr);
175  bool result = (sym != 0);
176  if (result) name = sym->name();
177  std::cout << "Lookup " << std::hex << "0x" << addr << std::dec
178  << " (" << result << ")" << " --> " << name << std::endl;
179  }
180 }
181 
182 #endif
SimpleSymbolBinding
void coalesce(SimpleSymbolsCoalesceCallback coalesce)
SimpleSymbol * find(uint64_t vma)
static SimpleSymbolBinding binding_xlate(dso_symbol_bind_t bind)
Definition: VdsoSymbols.cpp:73
void add(uint64_t addr, SimpleSymbolKind kind, SimpleSymbolBinding binding, const char *name)
bool match(const char *pathname)
SimpleSymbols * create()
int main(int argc, char *argv[])
Definition: main.cpp:125
const std::string & name() const
SimpleSymbolsCoalesceCallback chooseHighestBinding
string basename(const char *fName)
Definition: FileUtil.cpp:90
cct_addr_t * addr
Definition: cct.c:130
bool parse(const char *pathname)
static void note_symbol(const char *sym_name, int64_t sym_addr, dso_symbol_bind_t binding, void *callback_arg)
Definition: VdsoSymbols.cpp:88
const std::string & name()