| 43 | } |
| 44 | |
| 45 | bool ReadProcMaps(std::string* proc_maps) { |
| 46 | // seq_file only writes out a page-sized amount on each call. Refer to header |
| 47 | // file for details. |
| 48 | const long kReadSize = sysconf(_SC_PAGESIZE); |
| 49 | |
| 50 | butil::ScopedFD fd(HANDLE_EINTR(open("/proc/self/maps", O_RDONLY))); |
| 51 | if (!fd.is_valid()) { |
| 52 | DPLOG(ERROR) << "Couldn't open /proc/self/maps"; |
| 53 | return false; |
| 54 | } |
| 55 | proc_maps->clear(); |
| 56 | |
| 57 | while (true) { |
| 58 | // To avoid a copy, resize |proc_maps| so read() can write directly into it. |
| 59 | // Compute |buffer| afterwards since resize() may reallocate. |
| 60 | size_t pos = proc_maps->size(); |
| 61 | proc_maps->resize(pos + kReadSize); |
| 62 | void* buffer = &(*proc_maps)[pos]; |
| 63 | |
| 64 | ssize_t bytes_read = HANDLE_EINTR(read(fd.get(), buffer, kReadSize)); |
| 65 | if (bytes_read < 0) { |
| 66 | DPLOG(ERROR) << "Couldn't read /proc/self/maps"; |
| 67 | proc_maps->clear(); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // ... and don't forget to trim off excess bytes. |
| 72 | proc_maps->resize(pos + bytes_read); |
| 73 | |
| 74 | if (bytes_read == 0) |
| 75 | break; |
| 76 | |
| 77 | // The gate VMA is handled as a special case after seq_file has finished |
| 78 | // iterating through all entries in the virtual memory table. |
| 79 | // |
| 80 | // Unfortunately, if additional entries are added at this point in time |
| 81 | // seq_file gets confused and the next call to read() will return duplicate |
| 82 | // entries including the gate VMA again. |
| 83 | // |
| 84 | // Avoid this by searching for the gate VMA and breaking early. |
| 85 | if (ContainsGateVMA(proc_maps, pos)) |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool ParseProcMaps(const std::string& input, |
| 93 | std::vector<MappedMemoryRegion>* regions_out) { |