1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
/* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
* Copyright (c) 2024, gperftools Contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "base/proc_maps_iterator.h"
#include <ctype.h> // for isspace()
#include <stdlib.h> // for getenv()
#include <stdio.h> // for snprintf(), sscanf()
#include <string.h> // for memmove(), memchr(), etc.
#include <fcntl.h> // for open()
#include <errno.h> // for errno
#include <limits.h> // for PATH_MAX
#ifdef HAVE_UNISTD_H
#include <unistd.h> // for read()
#endif
#if (defined(_WIN32) || defined(__MINGW32__)) && !defined(__CYGWIN__) && !defined(__CYGWIN32)
# define PLATFORM_WINDOWS 1
#endif
#if (defined(_WIN32) || defined(__MINGW32__)) && (!defined(__CYGWIN__) && !defined(__CYGWIN32__))
#include <windows.h> // for DWORD etc
#include <tlhelp32.h> // for Module32First()
#endif
#if defined __MACH__ // Mac OS X, almost certainly
#include <mach-o/dyld.h> // for iterating over dll's in ProcMapsIter
#include <mach-o/loader.h> // for iterating over dll's in ProcMapsIter
#include <sys/types.h>
#include <sys/sysctl.h> // how we figure out numcpu's on OS X
#elif defined __FreeBSD__
#include <sys/sysctl.h>
#elif defined __sun__ // Solaris
#include <procfs.h> // for, e.g., prmap_t
#elif defined(PLATFORM_WINDOWS)
#include <process.h> // for getpid() (actually, _getpid())
#include <shlwapi.h> // for SHGetValueA()
#include <tlhelp32.h> // for Module32First()
#elif defined(__QNXNTO__)
#include <sys/mman.h>
#include <sys/sysmacros.h>
#endif
#include "base/logging.h"
#ifdef PLATFORM_WINDOWS
#ifdef MODULEENTRY32
// In a change from the usual W-A pattern, there is no A variant of
// MODULEENTRY32. Tlhelp32.h #defines the W variant, but not the A.
// In unicode mode, tlhelp32.h #defines MODULEENTRY32 to be
// MODULEENTRY32W. These #undefs are the only way I see to get back
// access to the original, ascii struct (and related functions).
#undef MODULEENTRY32
#undef Module32First
#undef Module32Next
#undef PMODULEENTRY32
#undef LPMODULEENTRY32
#endif /* MODULEENTRY32 */
// MinGW doesn't seem to define this, perhaps some windowsen don't either.
#ifndef TH32CS_SNAPMODULE32
#define TH32CS_SNAPMODULE32 0
#endif /* TH32CS_SNAPMODULE32 */
#endif /* PLATFORM_WINDOWS */
// Re-run fn until it doesn't cause EINTR.
#define NO_INTR(fn) do {} while ((fn) < 0 && errno == EINTR)
namespace tcmalloc {
namespace {
// Finds |c| in |text|, and assign '\0' at the found position.
// The original character at the modified position should be |c|.
// A pointer to the modified position is stored in |endptr|.
// |endptr| should not be NULL.
bool ExtractUntilChar(char *text, int c, char **endptr) {
CHECK_NE(text, NULL);
CHECK_NE(endptr, NULL);
char *found;
found = strchr(text, c);
if (found == NULL) {
*endptr = NULL;
return false;
}
*endptr = found;
*found = '\0';
return true;
}
// Increments |*text_pointer| while it points a whitespace character.
// It is to follow sscanf's whilespace handling.
void SkipWhileWhitespace(char **text_pointer, int c) {
if (isspace(c)) {
while (isspace(**text_pointer) && isspace(*((*text_pointer) + 1))) {
++(*text_pointer);
}
}
}
template<class T>
T StringToInteger(char *text, char **endptr, int base) {
assert(false);
return T();
}
template<>
inline int StringToInteger<int>(char *text, char **endptr, int base) {
return strtol(text, endptr, base);
}
template<>
inline int64_t StringToInteger<int64_t>(char *text, char **endptr, int base) {
return strtoll(text, endptr, base);
}
template<>
inline uint64_t StringToInteger<uint64_t>(char *text, char **endptr, int base) {
return strtoull(text, endptr, base);
}
template<typename T>
T StringToIntegerUntilChar(
char *text, int base, int c, char **endptr_result) {
CHECK_NE(endptr_result, NULL);
*endptr_result = NULL;
char *endptr_extract;
if (!ExtractUntilChar(text, c, &endptr_extract))
return 0;
T result;
char *endptr_strto;
result = StringToInteger<T>(text, &endptr_strto, base);
*endptr_extract = c;
if (endptr_extract != endptr_strto)
return 0;
*endptr_result = endptr_extract;
SkipWhileWhitespace(endptr_result, c);
return result;
}
char *CopyStringUntilChar(
char *text, unsigned out_len, int c, char *out) {
char *endptr;
if (!ExtractUntilChar(text, c, &endptr))
return NULL;
strncpy(out, text, out_len);
out[out_len-1] = '\0';
*endptr = c;
SkipWhileWhitespace(&endptr, c);
return endptr;
}
template<typename T>
bool StringToIntegerUntilCharWithCheck(
T *outptr, char *text, int base, int c, char **endptr) {
*outptr = StringToIntegerUntilChar<T>(*endptr, base, c, endptr);
if (*endptr == NULL || **endptr == '\0') return false;
++(*endptr);
return true;
}
bool ParseProcMapsLine(char *text, uint64_t *start, uint64_t *end,
char *flags, uint64_t *offset,
int64_t *inode,
unsigned *filename_offset) {
#if defined(__linux__) || defined(__NetBSD__)
/*
* It's similar to:
* sscanf(text, "%"SCNx64"-%"SCNx64" %4s %"SCNx64" %x:%x %"SCNd64" %n",
* start, end, flags, offset, major, minor, inode, filename_offset)
*/
char *endptr = text;
if (endptr == NULL || *endptr == '\0') return false;
if (!StringToIntegerUntilCharWithCheck(start, endptr, 16, '-', &endptr))
return false;
if (!StringToIntegerUntilCharWithCheck(end, endptr, 16, ' ', &endptr))
return false;
endptr = CopyStringUntilChar(endptr, 5, ' ', flags);
if (endptr == NULL || *endptr == '\0') return false;
++endptr;
if (!StringToIntegerUntilCharWithCheck(offset, endptr, 16, ' ', &endptr))
return false;
int64_t dummy;
if (!StringToIntegerUntilCharWithCheck(&dummy, endptr, 16, ':', &endptr))
return false;
if (!StringToIntegerUntilCharWithCheck(&dummy, endptr, 16, ' ', &endptr))
return false;
if (!StringToIntegerUntilCharWithCheck(inode, endptr, 10, ' ', &endptr))
return false;
*filename_offset = (endptr - text);
return true;
#else
return false;
#endif
}
template <typename Body>
bool ForEachLine(const char* path, const Body& body) {
#ifdef __FreeBSD__
// FreeBSD requires us to read all of the maps file at once, so
// we have to make a buffer that's "always" big enough
// TODO(alk): thats not good enough. We can do better.
static constexpr size_t kBufSize = 102400;
#else // a one-line buffer is good enough
static constexpr size_t kBufSize = PATH_MAX + 1024;
#endif
char buf[kBufSize];
char* const buf_end = buf + sizeof(buf) - 1;
int fd;
NO_INTR(fd = open(path, O_RDONLY));
if (fd < 0) {
return false;
}
char* sbuf = buf; // note, initial value could be nullptr, but
// memmove actually insists to get non-null
// arguments (even when count is 0)
char* ebuf = sbuf;
bool eof = false;
for (;;) {
char* nextline = static_cast<char*>(memchr(sbuf, '\n', ebuf - sbuf));
if (nextline != nullptr) {
RAW_CHECK(nextline < ebuf, "BUG");
*nextline = 0; // Turn newline into '\0'.
if (!body(sbuf, nextline)) {
break;
}
sbuf = nextline + 1;
continue;
}
int count = ebuf - sbuf;
// TODO: what if we face way too long line ?
if (eof) {
if (count == 0) {
break; // done
}
// Last read ended up without trailing newline. Lets add
// it. Note, we left one byte margin above, so we're able to
// write this and not get past end of buf.
*ebuf++ = '\n';
continue;
}
// Move the current text to the start of the buffer
memmove(buf, sbuf, count);
sbuf = buf;
ebuf = sbuf + count;
int nread;
NO_INTR(nread = read(fd, ebuf, buf_end - ebuf));
// Read failures are not expected, but lets not crash if this
// happens in non-debug mode.
DCHECK_GE(nread, 0);
if (nread < 0) {
nread = 0;
}
if (nread == 0) {
eof = true;
}
ebuf += nread;
// Retry memchr above.
}
close(fd);
return true;
}
inline
bool DoIterateLinux(const char* path, void (*body)(const ProcMapping& mapping, void* arg), void* arg) {
// TODO: line_end is unused ?
return ForEachLine(
path,
[&] (char* line_start, char* line_end) {
unsigned filename_offset;
char flags_tmp[10];
ProcMapping mapping;
if (!ParseProcMapsLine(line_start,
&mapping.start, &mapping.end,
flags_tmp,
&mapping.offset, &mapping.inode,
&filename_offset)) {
int c = line_end - line_start;
fprintf(stderr, "bad line %d:\n%.*s\n----\n", c, c, line_start);
return false;
}
mapping.filename = line_start + filename_offset;
mapping.flags = flags_tmp;
body(mapping, arg);
return true;
});
}
#if defined(__QNXNTO__)
inline
bool DoIterateQNX(void (*body)(const ProcMapping& mapping, void* arg), void* arg) {
return ForEachLine(
"proc/self/pmap",
[&] (char* line_start, char* line_end) {
// https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.sys_arch/topic/vm_calculations.html
// vaddr,size,flags,prot,maxprot,dev,ino,offset,rsv,guardsize,refcnt,mapcnt,path
// 0x00000025e9df9000,0x0000000000053000,0x00000071,0x05,0x0f,0x0000040b,0x0000000000000094,
// 0x0000000000000000,0x0000000000000000,0x00000000,0x00000005,0x00000003,/system/xbin/cat
ProcMapping mapping;
unsigned filename_offset;
uint64_t q_vaddr, q_size, q_ino, q_offset;
uint32_t q_flags, q_dev, q_prot;
if (sscanf(line_start,
"0x%" SCNx64 ",0x%" SCNx64 ",0x%" SCNx32 \
",0x%" SCNx32 ",0x%*x,0x%" SCNx32 ",0x%" SCNx64 \
",0x%" SCNx64 ",0x%*x,0x%*x,0x%*x,0x%*x,%n",
&q_vaddr,
&q_size,
&q_flags,
&q_prot,
&q_dev,
&q_ino,
&q_offset,
&filename_offset) != 7) {
return false;
}
mapping.start = q_vaddr;
mapping.end = q_vaddr + q_size;
mapping.offset = q_offset;
mapping.inode = q_ino;
// right shifted by 8 bits, restore it
q_prot <<= 8;
char flags_tmp[5] = {
q_prot & PROT_READ ? 'r' : '-',
q_prot & PROT_WRITE ? 'w' : '-',
q_prot & PROT_EXEC ? 'x' : '-',
q_flags & MAP_SHARED ? 's' : 'p',
'\0'
};
mapping.flags = flags_tmp;
mapping.filename = line_start + filename_offset;
body(mapping, arg);
return true;
});
}
#endif
inline
bool DoIterateFreeBSD(void (*body)(const ProcMapping& mapping, void* arg), void* arg) {
return ForEachLine(
"/proc/curproc/map",
[&] (char* line_start, char* line_end) {
if (line_start == line_end) {
return true; // freebsd is weird
}
ProcMapping mapping;
memset(&mapping, 0, sizeof(mapping));
unsigned filename_offset;
char flags_tmp[10];
// start end resident privateresident obj(?) prot refcnt shadowcnt
// flags copy_on_write needs_copy type filename:
// 0x8048000 0x804a000 2 0 0xc104ce70 r-x 1 0 0x0 COW NC vnode /bin/cat
if (sscanf(line_start, "0x%" SCNx64 " 0x%" SCNx64 " %*d %*d %*p %3s %*d %*d 0x%*x %*s %*s %*s %n",
&mapping.start,
&mapping.end,
flags_tmp,
&filename_offset) != 3) {
return false;
}
mapping.flags = flags_tmp;
mapping.filename = line_start + filename_offset;
body(mapping, arg);
return true;
});
}
#if defined(__sun__)
inline
bool DoIterateSolaris(void (*body)(const ProcMapping& mapping, void* arg), void* arg) {
int fd;
NO_INTR(fd = open("/proc/self/map", O_RDONLY));
// This is based on MA_READ == 4, MA_WRITE == 2, MA_EXEC == 1
static char kPerms[8][4] = {
"---", "--x", "-w-", "-wx",
"r--", "r-x", "rw-", "rwx" };
static_assert(MA_READ == 4, "solaris MA_READ must equal 4");
static_assert(MA_WRITE == 2, "solaris MA_WRITE must equal 2");
static_assert(MA_EXEC == 1, "solaris MA_EXEC must equal 1");
constexpr ssize_t kFilenameLen = PATH_MAX;
char current_filename[kFilenameLen];
int nread = 0; // fill up buffer with text
prmap_t mapinfo;
for (;;) {
NO_INTR(nread = read(fd, &mapinfo, sizeof(prmap_t)));
if (nread != sizeof(prmap_t)) {
CHECK_EQ(nread, 0);
break;
}
char object_path[PATH_MAX + 1000];
CHECK_LT(snprintf(object_path, sizeof(object_path),
"/proc/self/path/%s", mapinfo.pr_mapname),
sizeof(object_path));
ssize_t len = readlink(object_path, current_filename, kFilenameLen);
if (len < 0) {
len = 0;
}
CHECK_LT(len, kFilenameLen);
current_filename[len] = '\0';
ProcMapping mapping;
memset(&mapping, 0, sizeof(mapping));
mapping.start = mapinfo.pr_vaddr;
mapping.end = mapinfo.pr_vaddr + mapinfo.pr_size;
mapping.flags = kPerms[mapinfo.pr_mflags & 7];
mapping.offset = mapinfo.pr_offset;
mapping.filename = current_filename;
body(mapping, arg);
}
close(fd);
return true;
}
#endif
#if defined(PLATFORM_WINDOWS)
inline
bool DoIterateWindows(void (*body)(const ProcMapping& mapping, void* arg), void* arg) {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE |
TH32CS_SNAPMODULE32,
GetCurrentProcessId());
if (snapshot == INVALID_HANDLE_VALUE) {
return false;
}
MODULEENTRY32 mod_entry;
memset(&mod_entry, 0, sizeof(mod_entry));
ProcMapping mapping;
memset(&mapping, 0, sizeof(mapping));
static char kDefaultPerms[5] = "r-xp";
BOOL ok;
for (;;) {
if (mod_entry.dwSize == 0) { // only possible before first call
mod_entry.dwSize = sizeof(mod_entry);
ok = Module32First(snapshot, &mod_entry);
} else {
ok = Module32Next(snapshot, &mod_entry);
}
if (!ok) {
break;
}
uint64_t base_addr = reinterpret_cast<DWORD_PTR>(mod_entry.modBaseAddr);
mapping.start = base_addr;
mapping.end = base_addr + mod_entry.modBaseSize;
mapping.flags = kDefaultPerms;
mapping.filename = mod_entry.szExePath;
body(mapping, arg);
}
CloseHandle(snapshot);
return true;
}
#endif // defined(PLATFORM_WINDOWS)
#if defined(__MACH__)
// A templatized helper function instantiated for Mach (OS X) only.
// It can handle finding info for both 32 bits and 64 bits.
// Returns true if it successfully handled the hdr, false else.
template<uint32_t kMagic, uint32_t kLCSegment,
typename MachHeader, typename SegmentCommand>
bool NextExtMachHelper(const mach_header* hdr,
int current_image, int current_load_cmd,
uint64_t *start, uint64_t *end, const char **flags,
uint64_t *offset, int64_t *inode, const char **filename) {
static char kDefaultPerms[5] = "r-xp";
if (hdr->magic != kMagic)
return false;
const char* lc = (const char *)hdr + sizeof(MachHeader);
// TODO(csilvers): make this not-quadradic (increment and hold state)
for (int j = 0; j < current_load_cmd; j++) // advance to *our* load_cmd
lc += ((const load_command *)lc)->cmdsize;
if (((const load_command *)lc)->cmd == kLCSegment) {
const intptr_t dlloff = _dyld_get_image_vmaddr_slide(current_image);
const SegmentCommand* sc = (const SegmentCommand *)lc;
if (start) *start = sc->vmaddr + dlloff;
if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
if (flags) *flags = kDefaultPerms; // can we do better?
if (offset) *offset = sc->fileoff;
if (inode) *inode = 0;
if (filename)
*filename = const_cast<char*>(_dyld_get_image_name(current_image));
return true;
}
return false;
}
bool DoIterateOSX(void (*body)(const ProcMapping& mapping, void* arg), void* arg) {
int current_image = _dyld_image_count(); // count down from the top
int current_load_cmd = -1;
ProcMapping mapping;
memset(&mapping, 0, sizeof(mapping));
reenter:
for (; current_image >= 0; current_image--) {
const mach_header* hdr = _dyld_get_image_header(current_image);
if (!hdr) continue;
if (current_load_cmd < 0) // set up for this image
current_load_cmd = hdr->ncmds; // again, go from the top down
// We start with the next load command (we've already looked at this one).
for (current_load_cmd--; current_load_cmd >= 0; current_load_cmd--) {
#ifdef MH_MAGIC_64
if (NextExtMachHelper<MH_MAGIC_64, LC_SEGMENT_64,
struct mach_header_64, struct segment_command_64>(
hdr, current_image, current_load_cmd,
&mapping.start, &mapping.end,
&mapping.flags,
&mapping.offset, &mapping.inode, &mapping.filename)) {
body(mapping, arg);
goto reenter;
}
#endif
if (NextExtMachHelper<MH_MAGIC, LC_SEGMENT,
struct mach_header, struct segment_command>(
hdr, current_image, current_load_cmd,
&mapping.start, &mapping.end,
&mapping.flags,
&mapping.offset, &mapping.inode, &mapping.filename)) {
body(mapping, arg);
goto reenter;
}
}
// If we get here, no more load_cmd's in this image talk about
// segments. Go on to the next image.
}
return true;
}
#endif // __MACH__
void FormatLine(tcmalloc::GenericWriter* writer,
uint64_t start, uint64_t end, const char *flags,
uint64_t offset, int64_t inode,
const char *filename, dev_t dev) {
// We assume 'flags' looks like 'rwxp' or 'rwx'.
char r = (flags && flags[0] == 'r') ? 'r' : '-';
char w = (flags && flags[0] && flags[1] == 'w') ? 'w' : '-';
char x = (flags && flags[0] && flags[1] && flags[2] == 'x') ? 'x' : '-';
// p always seems set on linux, so we set the default to 'p', not '-'
char p = (flags && flags[0] && flags[1] && flags[2] && flags[3] != 'p')
? '-' : 'p';
writer->AppendF("%08" PRIx64 "-%08" PRIx64 " %c%c%c%c %08" PRIx64 " %02x:%02x %-11" PRId64,
start, end, r,w,x,p, offset,
static_cast<int>(dev/256), static_cast<int>(dev%256),
inode);
writer->AppendStr(filename);
writer->AppendStr("\n");
}
} // namespace
bool DoForEachProcMapping(void (*body)(const ProcMapping& mapping, void* arg), void* arg) {
#if defined(PLATFORM_WINDOWS)
return DoIterateWindows(body, arg);
#elif defined(__MACH__)
return DoIterateOSX(body, arg);
#elif defined(__sun__)
return DoIterateSolaris(body, arg);
#elif defined(__QNXNTO__)
return DoIterateQNX(body, arg);
#elif defined(__FreeBSD__)
return DoIterateFreeBSD(body, arg);
#else
return DoIterateLinux("/proc/self/maps", body, arg);
#endif
}
void SaveProcSelfMaps(GenericWriter* writer) {
ForEachProcMapping([writer] (const ProcMapping& mapping) {
FormatLine(writer,
mapping.start, mapping.end, mapping.flags,
mapping.offset, mapping.inode,
mapping.filename, 0);
});
}
void SaveProcSelfMapsToRawFD(RawFD fd) {
RawFDGenericWriter<> writer(fd);
SaveProcSelfMaps(&writer);
}
} // namespace tcmalloc
|