| 21 | #include "log.h" |
| 22 | |
| 23 | std::pair<dev_t, ino_t> devinobymap(const std::string& lib, bool useFind = false, unsigned int *ln = nullptr) { |
| 24 | std::ifstream maps("/proc/self/maps"); |
| 25 | unsigned int index = 0; |
| 26 | std::string line; |
| 27 | std::string needle = "/" + lib; |
| 28 | while (std::getline(maps, line)) { |
| 29 | if (ln && index < *ln) { |
| 30 | index++; |
| 31 | continue; |
| 32 | } |
| 33 | if (line.size() >= needle.size() && ((useFind && line.find(needle) != std::string::npos) || |
| 34 | line.compare(line.size() - needle.size(), needle.size(), needle) == 0)) { |
| 35 | std::istringstream iss(line); |
| 36 | std::string addr, perms, offset, dev, inode_str; |
| 37 | iss >> addr >> perms >> offset >> dev >> inode_str; |
| 38 | std::istringstream devsplit(dev); |
| 39 | std::string major_hex, minor_hex; |
| 40 | if (std::getline(devsplit, major_hex, ':') && |
| 41 | std::getline(devsplit, minor_hex)) { |
| 42 | int major = std::stoi(major_hex, nullptr, 16); |
| 43 | int minor = std::stoi(minor_hex, nullptr, 16); |
| 44 | dev_t devnum = makedev(major, minor); |
| 45 | ino_t inode = std::stoul(inode_str); |
| 46 | if (ln) |
| 47 | *ln = index; |
| 48 | return {devnum, inode}; |
| 49 | } |
| 50 | } |
| 51 | index++; |
| 52 | } |
| 53 | if (ln) |
| 54 | *ln = -1; |
| 55 | return {dev_t(0), ino_t(0)}; |
| 56 | } |
| 57 | |
| 58 | std::optional<std::pair<dev_t, ino_t>> devinoby(const char* lib) { |
| 59 | struct State { |