forked from LLNL/mitos
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvirtual_address_writer.h
42 lines (37 loc) · 1.37 KB
/
virtual_address_writer.h
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
//
// Created by maximilian on 07.03.23.
//
#ifndef SAMPLING_VIRTUAL_ADDRESS_WRITER_H
#define SAMPLING_VIRTUAL_ADDRESS_WRITER_H
#include <iostream>
#include "dlfcn.h"
#include "link.h"
#include "cassert"
#include "fstream"
/* This function saves the virtual address offset for the
executable (being run for sampling).
* This helps in locating the source code of the executable
using the instruction pointers obtained through the samples.
* This function must be included in the source code of the executable */
void Mitos_save_virtual_address_offset(std::string filename, bool verbose = false) {
// --------- get virtual address offset -------------------
void * const handle = dlopen(NULL, RTLD_LAZY);
assert(handle != 0);
// ------ Get the link map
const struct link_map* link_map = 0;
const int ret = dlinfo(handle, RTLD_DI_LINKMAP, &link_map);
const struct link_map * const loaded_link_map = link_map;
assert(ret == 0);
assert(link_map != nullptr);
std::ofstream fproc;
fproc << "link_map->l_addr " << (long long)link_map->l_addr << std::endl;
fproc.close();
std::ofstream f_addr;
if(verbose)
std::cout << "Output: " << filename << std::endl;
f_addr.open(filename);
f_addr << (long long)link_map->l_addr << std::endl;
f_addr.close();
// -------------------
}
#endif //SAMPLING_VIRTUAL_ADDRESS_WRITER_H