proc_maps_linux.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #ifndef BASE_DEBUG_PROC_MAPS_LINUX_H_
  5. #define BASE_DEBUG_PROC_MAPS_LINUX_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/base_export.h"
  10. namespace base {
  11. namespace debug {
  12. // Describes a region of mapped memory and the path of the file mapped.
  13. struct MappedMemoryRegion {
  14. enum Permission {
  15. READ = 1 << 0,
  16. WRITE = 1 << 1,
  17. EXECUTE = 1 << 2,
  18. PRIVATE = 1 << 3, // If set, region is private, otherwise it is shared.
  19. };
  20. // The address range [start,end) of mapped memory.
  21. uintptr_t start;
  22. uintptr_t end;
  23. // Byte offset into |path| of the range mapped into memory.
  24. unsigned long long offset;
  25. // Image base, if this mapping corresponds to an ELF image.
  26. uintptr_t base;
  27. // Bitmask of read/write/execute/private/shared permissions.
  28. uint8_t permissions;
  29. // Name of the file mapped into memory.
  30. //
  31. // NOTE: path names aren't guaranteed to point at valid files. For example,
  32. // "[heap]" and "[stack]" are used to represent the location of the process'
  33. // heap and stack, respectively.
  34. std::string path;
  35. };
  36. // Reads the data from /proc/self/maps and stores the result in |proc_maps|.
  37. // Returns true if successful, false otherwise.
  38. //
  39. // There is *NO* guarantee that the resulting contents will be free of
  40. // duplicates or even contain valid entries by time the method returns.
  41. //
  42. //
  43. // THE GORY DETAILS
  44. //
  45. // Did you know it's next-to-impossible to atomically read the whole contents
  46. // of /proc/<pid>/maps? You would think that if we passed in a large-enough
  47. // buffer to read() that It Should Just Work(tm), but sadly that's not the case.
  48. //
  49. // Linux's procfs uses seq_file [1] for handling iteration, text formatting,
  50. // and dealing with resulting data that is larger than the size of a page. That
  51. // last bit is especially important because it means that seq_file will never
  52. // return more than the size of a page in a single call to read().
  53. //
  54. // Unfortunately for a program like Chrome the size of /proc/self/maps is
  55. // larger than the size of page so we're forced to call read() multiple times.
  56. // If the virtual memory table changed in any way between calls to read() (e.g.,
  57. // a different thread calling mprotect()), it can make seq_file generate
  58. // duplicate entries or skip entries.
  59. //
  60. // Even if seq_file was changed to keep flushing the contents of its page-sized
  61. // buffer to the usermode buffer inside a single call to read(), it has to
  62. // release its lock on the virtual memory table to handle page faults while
  63. // copying data to usermode. This puts us in the same situation where the table
  64. // can change while we're copying data.
  65. //
  66. // Alternatives such as fork()-and-suspend-the-parent-while-child-reads were
  67. // attempted, but they present more subtle problems than it's worth. Depending
  68. // on your use case your best bet may be to read /proc/<pid>/maps prior to
  69. // starting other threads.
  70. //
  71. // [1] http://kernelnewbies.org/Documents/SeqFileHowTo
  72. BASE_EXPORT bool ReadProcMaps(std::string* proc_maps);
  73. // Parses /proc/<pid>/maps input data and stores in |regions|. Returns true
  74. // and updates |regions| if and only if all of |input| was successfully parsed.
  75. BASE_EXPORT bool ParseProcMaps(const std::string& input,
  76. std::vector<MappedMemoryRegion>* regions);
  77. } // namespace debug
  78. } // namespace base
  79. #endif // BASE_DEBUG_PROC_MAPS_LINUX_H_