Retro68/gcc/libsanitizer/sanitizer_common/sanitizer_procmaps_linux.cc

76 lines
2.7 KiB
C++
Raw Normal View History

2015-08-28 15:33:40 +00:00
//===-- sanitizer_procmaps_linux.cc ---------------------------------------===//
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Information about the process mappings (Linux-specific parts).
//===----------------------------------------------------------------------===//
#include "sanitizer_platform.h"
#if SANITIZER_LINUX
#include "sanitizer_common.h"
#include "sanitizer_procmaps.h"
namespace __sanitizer {
void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
2018-12-28 15:30:48 +00:00
ReadFileToBuffer("/proc/self/maps", &proc_maps->data, &proc_maps->mmaped_size,
&proc_maps->len);
2015-08-28 15:33:40 +00:00
}
static bool IsOneOf(char c, char c1, char c2) {
return c == c1 || c == c2;
}
2018-12-28 15:30:48 +00:00
bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
if (data_.current >= last) return false;
char *next_line =
(char *)internal_memchr(data_.current, '\n', last - data_.current);
2015-08-28 15:33:40 +00:00
if (next_line == 0)
next_line = last;
// Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar
2018-12-28 15:30:48 +00:00
segment->start = ParseHex(&data_.current);
CHECK_EQ(*data_.current++, '-');
segment->end = ParseHex(&data_.current);
CHECK_EQ(*data_.current++, ' ');
CHECK(IsOneOf(*data_.current, '-', 'r'));
segment->protection = 0;
if (*data_.current++ == 'r') segment->protection |= kProtectionRead;
CHECK(IsOneOf(*data_.current, '-', 'w'));
if (*data_.current++ == 'w') segment->protection |= kProtectionWrite;
CHECK(IsOneOf(*data_.current, '-', 'x'));
if (*data_.current++ == 'x') segment->protection |= kProtectionExecute;
CHECK(IsOneOf(*data_.current, 's', 'p'));
if (*data_.current++ == 's') segment->protection |= kProtectionShared;
CHECK_EQ(*data_.current++, ' ');
segment->offset = ParseHex(&data_.current);
CHECK_EQ(*data_.current++, ' ');
ParseHex(&data_.current);
CHECK_EQ(*data_.current++, ':');
ParseHex(&data_.current);
CHECK_EQ(*data_.current++, ' ');
while (IsDecimal(*data_.current)) data_.current++;
2015-08-28 15:33:40 +00:00
// Qemu may lack the trailing space.
// https://github.com/google/sanitizers/issues/160
2018-12-28 15:30:48 +00:00
// CHECK_EQ(*data_.current++, ' ');
2015-08-28 15:33:40 +00:00
// Skip spaces.
2018-12-28 15:30:48 +00:00
while (data_.current < next_line && *data_.current == ' ') data_.current++;
2015-08-28 15:33:40 +00:00
// Fill in the filename.
2018-12-28 15:30:48 +00:00
if (segment->filename) {
uptr len =
Min((uptr)(next_line - data_.current), segment->filename_size - 1);
internal_strncpy(segment->filename, data_.current, len);
segment->filename[len] = 0;
2015-08-28 15:33:40 +00:00
}
2018-12-28 15:30:48 +00:00
data_.current = next_line + 1;
2015-08-28 15:33:40 +00:00
return true;
}
} // namespace __sanitizer
#endif // SANITIZER_LINUX