1 //===-- sanitizer_procmaps_freebsd.cc -------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Information about the process mappings (FreeBSD-specific parts).
9 //===----------------------------------------------------------------------===//
11 #include "sanitizer_platform.h"
13 #include "sanitizer_common.h"
14 #include "sanitizer_freebsd.h"
15 #include "sanitizer_procmaps.h"
18 #include <sys/sysctl.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
29 namespace __sanitizer
{
31 void ReadProcMaps(ProcSelfMapsBuff
*proc_maps
) {
32 const int Mib
[4] = { CTL_KERN
, KERN_PROC
, KERN_PROC_VMMAP
, getpid() };
34 int Err
= sysctl(Mib
, 4, NULL
, &Size
, NULL
, 0);
38 size_t MmapedSize
= Size
* 4 / 3;
39 void *VmMap
= MmapOrDie(MmapedSize
, "ReadProcMaps()");
41 Err
= sysctl(Mib
, 4, VmMap
, &Size
, NULL
, 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
,
52 char *last
= proc_self_maps_
.data
+ proc_self_maps_
.len
;
53 if (current_
>= last
) return false;
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
;
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
;
84 } // namespace __sanitizer
86 #endif // SANITIZER_FREEBSD