|
| 1 | +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ |
| 2 | +│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ |
| 3 | +╞══════════════════════════════════════════════════════════════════════════════╡ |
| 4 | +│ Copyright 2024 Justine Alexandra Roberts Tunney │ |
| 5 | +│ │ |
| 6 | +│ Permission to use, copy, modify, and/or distribute this software for │ |
| 7 | +│ any purpose with or without fee is hereby granted, provided that the │ |
| 8 | +│ above copyright notice and this permission notice appear in all copies. │ |
| 9 | +│ │ |
| 10 | +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ |
| 11 | +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ |
| 12 | +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ |
| 13 | +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ |
| 14 | +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ |
| 15 | +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ |
| 16 | +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ |
| 17 | +│ PERFORMANCE OF THIS SOFTWARE. │ |
| 18 | +╚─────────────────────────────────────────────────────────────────────────────*/ |
| 19 | +#include "libc/fmt/conv.h" |
| 20 | +#include "libc/intrin/describeflags.h" |
| 21 | +#include "libc/intrin/kprintf.h" |
| 22 | +#include "libc/macros.h" |
| 23 | +#include "libc/nt/enum/memflags.h" |
| 24 | +#include "libc/nt/memory.h" |
| 25 | +#include "libc/runtime/runtime.h" |
| 26 | +#include "libc/str/str.h" |
| 27 | + |
| 28 | +static const struct DescribeFlags kNtMemState[] = { |
| 29 | + {kNtMemCommit, "Commit"}, // |
| 30 | + {kNtMemFree, "Free"}, // |
| 31 | + {kNtMemReserve, "Reserve"}, // |
| 32 | +}; |
| 33 | + |
| 34 | +const char *DescribeNtMemState(char buf[64], uint32_t x) { |
| 35 | + return _DescribeFlags(buf, 64, kNtMemState, ARRAYLEN(kNtMemState), "kNtMem", |
| 36 | + x); |
| 37 | +} |
| 38 | + |
| 39 | +static const struct DescribeFlags kNtMemType[] = { |
| 40 | + {kNtMemImage, "Image"}, // |
| 41 | + {kNtMemMapped, "Mapped"}, // |
| 42 | + {kNtMemPrivate, "Private"}, // |
| 43 | +}; |
| 44 | + |
| 45 | +const char *DescribeNtMemType(char buf[64], uint32_t x) { |
| 46 | + return _DescribeFlags(buf, 64, kNtMemType, ARRAYLEN(kNtMemType), "kNtMem", x); |
| 47 | +} |
| 48 | + |
| 49 | +void __print_maps_win32(void) { |
| 50 | + char *p, b[5][64]; |
| 51 | + struct NtMemoryBasicInformation mi; |
| 52 | + kprintf("%-12s %-12s %10s %16s %16s %32s %32s\n", "Allocation", "BaseAddress", |
| 53 | + "RegionSize", "State", "Type", "AllocationProtect", "Protect"); |
| 54 | + for (p = 0;; p = (char *)mi.BaseAddress + mi.RegionSize) { |
| 55 | + bzero(&mi, sizeof(mi)); |
| 56 | + if (!VirtualQuery(p, &mi, sizeof(mi))) |
| 57 | + break; |
| 58 | + sizefmt(b[0], mi.RegionSize, 1024); |
| 59 | + kprintf("%.12lx %.12lx %10s %16s %16s %32s %32s\n", mi.AllocationBase, |
| 60 | + mi.BaseAddress, b[0], DescribeNtMemState(b[1], mi.State), |
| 61 | + DescribeNtMemType(b[2], mi.Type), |
| 62 | + _DescribeNtPageFlags(b[3], mi.AllocationProtect), |
| 63 | + _DescribeNtPageFlags(b[4], mi.Protect)); |
| 64 | + } |
| 65 | +} |
0 commit comments