Fix typo.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_procmaps_freebsd.cc
blobfbc55203ab4dc7b721e01dafdc01ef3858b7718c
1 //===-- sanitizer_procmaps_freebsd.cc -------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Information about the process mappings (FreeBSD-specific parts).
9 //===----------------------------------------------------------------------===//
11 #include "sanitizer_platform.h"
12 #if SANITIZER_FREEBSD
13 #include "sanitizer_common.h"
14 #include "sanitizer_freebsd.h"
15 #include "sanitizer_procmaps.h"
17 #include <unistd.h>
18 #include <sys/sysctl.h>
19 #include <sys/user.h>
21 // Fix 'kinfo_vmentry' definition on FreeBSD prior v9.2 in 32-bit mode.
22 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
23 # include <osreldate.h>
24 # if __FreeBSD_version <= 902001 // v9.2
25 # define kinfo_vmentry xkinfo_vmentry
26 # endif
27 #endif
29 namespace __sanitizer {
31 void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
32 const int Mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid() };
33 size_t Size = 0;
34 int Err = sysctl(Mib, 4, NULL, &Size, NULL, 0);
35 CHECK_EQ(Err, 0);
36 CHECK_GT(Size, 0);
38 size_t MmapedSize = Size * 4 / 3;
39 void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
40 Size = MmapedSize;
41 Err = sysctl(Mib, 4, VmMap, &Size, NULL, 0);
42 CHECK_EQ(Err, 0);
44 proc_maps->data = (char*)VmMap;
45 proc_maps->mmaped_size = MmapedSize;
46 proc_maps->len = Size;
49 bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
50 char filename[], uptr filename_size,
51 uptr *protection) {
52 char *last = proc_self_maps_.data + proc_self_maps_.len;
53 if (current_ >= last) return false;
54 uptr dummy;
55 if (!start) start = &dummy;
56 if (!end) end = &dummy;
57 if (!offset) offset = &dummy;
58 if (!protection) protection = &dummy;
59 struct kinfo_vmentry *VmEntry = (struct kinfo_vmentry*)current_;
61 *start = (uptr)VmEntry->kve_start;
62 *end = (uptr)VmEntry->kve_end;
63 *offset = (uptr)VmEntry->kve_offset;
65 *protection = 0;
66 if ((VmEntry->kve_protection & KVME_PROT_READ) != 0)
67 *protection |= kProtectionRead;
68 if ((VmEntry->kve_protection & KVME_PROT_WRITE) != 0)
69 *protection |= kProtectionWrite;
70 if ((VmEntry->kve_protection & KVME_PROT_EXEC) != 0)
71 *protection |= kProtectionExecute;
73 if (filename != NULL && filename_size > 0) {
74 internal_snprintf(filename,
75 Min(filename_size, (uptr)PATH_MAX),
76 "%s", VmEntry->kve_path);
79 current_ += VmEntry->kve_structsize;
81 return true;
84 } // namespace __sanitizer
86 #endif // SANITIZER_FREEBSD