|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2023, Red Hat, Inc. and/or its affiliates. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | + * or visit www.oracle.com if you need additional information or have any |
| 22 | + * questions. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +#include "precompiled.hpp" |
| 27 | + |
| 28 | +#include "runtime/os.hpp" |
| 29 | +#include "nmt/memMapPrinter.hpp" |
| 30 | +#include "utilities/globalDefinitions.hpp" |
| 31 | +#include <limits.h> |
| 32 | + |
| 33 | +struct ProcMapsInfo { |
| 34 | + void* from = 0; |
| 35 | + void* to = 0; |
| 36 | + char prot[20 + 1]; |
| 37 | + char offset[20 + 1]; |
| 38 | + char dev[20 + 1]; |
| 39 | + char inode[20 + 1]; |
| 40 | + char filename[1024 + 1]; |
| 41 | + |
| 42 | + bool scan_proc_maps_line(const char* line) { |
| 43 | + prot[0] = offset[0] = dev[0] = inode[0] = filename[0] = '\0'; |
| 44 | + const int items_read = ::sscanf(line, "%p-%p %20s %20s %20s %20s %1024s", |
| 45 | + &from, &to, prot, offset, dev, inode, filename); |
| 46 | + return items_read >= 2; // need at least from and to |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +class LinuxMappingPrintInformation : public MappingPrintInformation { |
| 51 | + const ProcMapsInfo _info; |
| 52 | +public: |
| 53 | + |
| 54 | + LinuxMappingPrintInformation(const void* from, const void* to, const ProcMapsInfo* info) : |
| 55 | + MappingPrintInformation(from, to), _info(*info) {} |
| 56 | + |
| 57 | + void print_OS_specific_details(outputStream* st) const override { |
| 58 | + st->print("%s %s ", _info.prot, _info.offset); |
| 59 | + } |
| 60 | + |
| 61 | + const char* filename() const override { return _info.filename; } |
| 62 | +}; |
| 63 | + |
| 64 | +void MemMapPrinter::pd_print_header(outputStream* st) { |
| 65 | + st->print_cr("size prot offset What"); |
| 66 | +} |
| 67 | + |
| 68 | +void MemMapPrinter::pd_iterate_all_mappings(MappingPrintClosure& closure) { |
| 69 | + FILE* f = os::fopen("/proc/self/maps", "r"); |
| 70 | + if (f == nullptr) { |
| 71 | + return; |
| 72 | + } |
| 73 | + constexpr size_t linesize = sizeof(ProcMapsInfo); |
| 74 | + char line[linesize]; |
| 75 | + while (fgets(line, sizeof(line), f) == line) { |
| 76 | + line[sizeof(line) - 1] = '\0'; |
| 77 | + ProcMapsInfo info; |
| 78 | + if (info.scan_proc_maps_line(line)) { |
| 79 | + LinuxMappingPrintInformation mapinfo(info.from, info.to, &info); |
| 80 | + closure.do_it(&mapinfo); |
| 81 | + } |
| 82 | + } |
| 83 | + ::fclose(f); |
| 84 | +} |
0 commit comments