 Chromium Code Reviews
 Chromium Code Reviews Issue 18178015:
  Implement /proc/self/maps/ parsing code.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 18178015:
  Implement /proc/self/maps/ parsing code.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| Index: base/debug/proc_maps.cc | 
| diff --git a/base/debug/proc_maps.cc b/base/debug/proc_maps.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..f478afd59a3c55795864e22045e8a73a76e75dcb | 
| --- /dev/null | 
| +++ b/base/debug/proc_maps.cc | 
| @@ -0,0 +1,66 @@ | 
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "base/debug/proc_maps.h" | 
| + | 
| +#include <inttypes.h> | 
| + | 
| +#include "base/file_util.h" | 
| +#include "base/strings/string_split.h" | 
| + | 
| +namespace base { | 
| +namespace debug { | 
| + | 
| +#if defined(OS_LINUX) || defined(OS_ANDROID) | 
| + | 
| +std::string ReadProcMaps() { | 
| + std::string proc_maps; | 
| + FilePath proc_maps_path("/proc/self/maps"); | 
| + if (!file_util::ReadFileToString(proc_maps_path, &proc_maps)) | 
| + proc_maps.clear(); | 
| + return proc_maps; | 
| +} | 
| + | 
| +void ParseProcMaps(const std::string& input, | 
| + std::vector<MappedMemoryRegion>* regions) { | 
| + // Sample format from man 5 proc: | 
| + // | 
| + // address perms offset dev inode pathname | 
| + // 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm | 
| + // | 
| + // The final %n term captures the offset in the input string, which is used | 
| + // to determine the path name. | 
| + static const char kProcMapFormat[] = | 
| 
satorux1
2013/07/01 03:59:56
remove static? function-local 'static' is not guar
 
scherkus (not reviewing)
2013/07/01 21:59:45
Done.
 | 
| + "%" SCNxPTR "-%" SCNxPTR " %*s %zx %*x:%*x %*d %n"; | 
| 
satorux1
2013/07/01 03:59:56
didn't know about SCNxPTR. :)
 
scherkus (not reviewing)
2013/07/01 21:59:45
yeah I didn't either until I starting looking thro
 | 
| + | 
| + regions->clear(); | 
| + | 
| + // This isn't async safe nor terribly efficient, but it doesn't need to be at | 
| + // this point in time. | 
| + std::vector<std::string> lines; | 
| + SplitString(input, '\n', &lines); | 
| + | 
| + for (size_t i = 0; i < lines.size(); ++i) { | 
| + const char* line = lines[i].c_str(); | 
| + uintptr_t start = 0; | 
| + uintptr_t end = 0; | 
| + size_t offset = 0; | 
| + int path_index = 0; | 
| + | 
| + if (sscanf(line, kProcMapFormat, &start, &end, &offset, &path_index) < 3) | 
| + continue; | 
| + | 
| + MappedMemoryRegion region; | 
| + region.start = start; | 
| + region.end = end; | 
| + region.offset = offset; | 
| + region.path = FilePath(line + path_index); | 
| + regions->push_back(region); | 
| + } | 
| +} | 
| + | 
| +#endif | 
| + | 
| +} // namespace debug | 
| +} // namespace base |