proc_maps_linux.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/debug/proc_maps_linux.h"
  5. #include <fcntl.h>
  6. #include <stddef.h>
  7. #include "base/files/file_util.h"
  8. #include "base/files/scoped_file.h"
  9. #include "base/logging.h"
  10. #include "base/strings/string_split.h"
  11. #include "build/build_config.h"
  12. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  13. #include <inttypes.h>
  14. #endif
  15. namespace base {
  16. namespace debug {
  17. // Scans |proc_maps| starting from |pos| returning true if the gate VMA was
  18. // found, otherwise returns false.
  19. static bool ContainsGateVMA(std::string* proc_maps, size_t pos) {
  20. #if defined(ARCH_CPU_ARM_FAMILY)
  21. // The gate VMA on ARM kernels is the interrupt vectors page.
  22. return proc_maps->find(" [vectors]\n", pos) != std::string::npos;
  23. #elif defined(ARCH_CPU_X86_64)
  24. // The gate VMA on x86 64-bit kernels is the virtual system call page.
  25. return proc_maps->find(" [vsyscall]\n", pos) != std::string::npos;
  26. #else
  27. // Otherwise assume there is no gate VMA in which case we shouldn't
  28. // get duplicate entires.
  29. return false;
  30. #endif
  31. }
  32. bool ReadProcMaps(std::string* proc_maps) {
  33. // seq_file only writes out a page-sized amount on each call. Refer to header
  34. // file for details.
  35. const size_t read_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
  36. base::ScopedFD fd(HANDLE_EINTR(open("/proc/self/maps", O_RDONLY)));
  37. if (!fd.is_valid()) {
  38. DPLOG(ERROR) << "Couldn't open /proc/self/maps";
  39. return false;
  40. }
  41. proc_maps->clear();
  42. while (true) {
  43. // To avoid a copy, resize |proc_maps| so read() can write directly into it.
  44. // Compute |buffer| afterwards since resize() may reallocate.
  45. size_t pos = proc_maps->size();
  46. proc_maps->resize(pos + read_size);
  47. void* buffer = &(*proc_maps)[pos];
  48. ssize_t bytes_read = HANDLE_EINTR(read(fd.get(), buffer, read_size));
  49. if (bytes_read < 0) {
  50. DPLOG(ERROR) << "Couldn't read /proc/self/maps";
  51. proc_maps->clear();
  52. return false;
  53. }
  54. // ... and don't forget to trim off excess bytes.
  55. proc_maps->resize(pos + static_cast<size_t>(bytes_read));
  56. if (bytes_read == 0)
  57. break;
  58. // The gate VMA is handled as a special case after seq_file has finished
  59. // iterating through all entries in the virtual memory table.
  60. //
  61. // Unfortunately, if additional entries are added at this point in time
  62. // seq_file gets confused and the next call to read() will return duplicate
  63. // entries including the gate VMA again.
  64. //
  65. // Avoid this by searching for the gate VMA and breaking early.
  66. if (ContainsGateVMA(proc_maps, pos))
  67. break;
  68. }
  69. return true;
  70. }
  71. bool ParseProcMaps(const std::string& input,
  72. std::vector<MappedMemoryRegion>* regions_out) {
  73. CHECK(regions_out);
  74. std::vector<MappedMemoryRegion> regions;
  75. // This isn't async safe nor terribly efficient, but it doesn't need to be at
  76. // this point in time.
  77. std::vector<std::string> lines = SplitString(
  78. input, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  79. for (size_t i = 0; i < lines.size(); ++i) {
  80. // Due to splitting on '\n' the last line should be empty.
  81. if (i == lines.size() - 1) {
  82. if (!lines[i].empty()) {
  83. DLOG(WARNING) << "Last line not empty";
  84. return false;
  85. }
  86. break;
  87. }
  88. MappedMemoryRegion region;
  89. const char* line = lines[i].c_str();
  90. char permissions[5] = {'\0'}; // Ensure NUL-terminated string.
  91. uint8_t dev_major = 0;
  92. uint8_t dev_minor = 0;
  93. long inode = 0;
  94. int path_index = 0;
  95. // Sample format from man 5 proc:
  96. //
  97. // address perms offset dev inode pathname
  98. // 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm
  99. //
  100. // The final %n term captures the offset in the input string, which is used
  101. // to determine the path name. It *does not* increment the return value.
  102. // Refer to man 3 sscanf for details.
  103. if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4c %llx %hhx:%hhx %ld %n",
  104. &region.start, &region.end, permissions, &region.offset,
  105. &dev_major, &dev_minor, &inode, &path_index) < 7) {
  106. DPLOG(WARNING) << "sscanf failed for line: " << line;
  107. return false;
  108. }
  109. region.permissions = 0;
  110. if (permissions[0] == 'r')
  111. region.permissions |= MappedMemoryRegion::READ;
  112. else if (permissions[0] != '-')
  113. return false;
  114. if (permissions[1] == 'w')
  115. region.permissions |= MappedMemoryRegion::WRITE;
  116. else if (permissions[1] != '-')
  117. return false;
  118. if (permissions[2] == 'x')
  119. region.permissions |= MappedMemoryRegion::EXECUTE;
  120. else if (permissions[2] != '-')
  121. return false;
  122. if (permissions[3] == 'p')
  123. region.permissions |= MappedMemoryRegion::PRIVATE;
  124. else if (permissions[3] != 's' && permissions[3] != 'S') // Shared memory.
  125. return false;
  126. // Pushing then assigning saves us a string copy.
  127. regions.push_back(region);
  128. regions.back().path.assign(line + path_index);
  129. }
  130. regions_out->swap(regions);
  131. return true;
  132. }
  133. } // namespace debug
  134. } // namespace base