ATLAS Offline Software
Loading...
Searching...
No Matches
procmaps.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include <algorithm>
6#include <cstdlib>
7#include <cstring>
8#include <cstdint>
9#include <fstream>
10#include "CxxUtils/procmaps.h"
11procmaps::Entry::Entry(const char* procMapsLine) :
13 readable(false), writable(false), executable(false), isPrivate(false),
14 offset(0), inode(0)
15{
16 dev[0]=0; dev[1]=0;
17 char pageProts[5];
18 memset(pageProts,' ', 4);
19 uint64_t inode_tmp;
20 char path[2048] = "";
21 sscanf(procMapsLine,
22 "%80lx-%80lx %4s %80x %4x:%4x %80lu %2047s",
23 &this->begAddress,
24 &this->endAddress,
25 pageProts,
26 &this->offset,
27 &this->dev[0],
28 &this->dev[1],
29 &inode_tmp,
30 path
31 );
32 this->inode = inode_tmp;
33 this->pathname = std::string (path);
34 //printf("pageProts %s pathname <%s> \n", pageProts, pathname);
35 this->readable = (pageProts[0] == 'r');
36 this->writable = (pageProts[1] == 'w');
37 this->executable = (pageProts[2] == 'x');
38 this->isPrivate = (pageProts[3] == 'p');
39}
40
42 m_pmaps.reserve(entries);
43 loadMaps(false);
44}
45
46void
48 m_pmaps.clear();
49 std::ifstream f("/proc/self/maps");
50 const int LMAX=256;
51 char line[LMAX];
52 while ( f.getline(line,LMAX) ) {
53 if (dump) printf("%s",line);
54 m_pmaps.emplace_back(line);
55 }
56}
57
58const procmaps::Entry*
59procmaps::getEntry(const void * address, bool refreshMaps) {
60 unsigned long toMatch = reinterpret_cast<unsigned long>(address);
61 auto i = std::upper_bound (this->begin(), this->end(),
62 toMatch,
63 [] (unsigned long addr, const Entry& ent)
64 { return addr < ent.begAddress; });
65
66 if (i > this->begin()) {
67 --i;
68 if (toMatch >= i->begAddress && toMatch < i->endAddress) {
69 return &*i;
70 }
71 }
72
73 if (refreshMaps) {
74 //if ! found recurse once by calling getEntry with refreshMaps false
75 loadMaps();
76 return getEntry(address,false);
77 }
78
79 return nullptr;
80}
void loadMaps(bool dump=false)
load/refresh info from /proc/self/map
Definition procmaps.cxx:47
const Entry * getEntry(const void *address, bool tryReloadMaps=true)
main entry point: get info for the page range containing address by default tries to reload /proc/sel...
Definition procmaps.cxx:59
const_iterator begin() const
Definition procmaps.h:46
procmaps(size_t entries=1024)
Definition procmaps.cxx:41
const_iterator end() const
Definition procmaps.h:47
procmaps_t m_pmaps
Definition procmaps.h:53
double entries
Definition listroot.cxx:49
-event-from-file
Entry(const char *line)
Definition procmaps.cxx:11
ino_t inode
dev[0] major, dev[1] minor
Definition procmaps.h:30
unsigned int offset
=true page is private(COW), =false page is shared
Definition procmaps.h:28
unsigned int dev[2]
Definition procmaps.h:29
unsigned long endAddress
Definition procmaps.h:23
std::string pathname
Definition procmaps.h:31
unsigned long begAddress
Definition procmaps.h:22