1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <getopt.h>
18 #include <inttypes.h>
19 #include <signal.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/mman.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 #include <iomanip>
27 #include <iostream>
28 #include <map>
29 #include <memory>
30 #include <string>
31 
32 #include <android-base/stringprintf.h>
33 #include <android-base/strings.h>
34 #include <meminfo/procmeminfo.h>
35 
36 #include <processrecord.h>
37 #include <smapinfo.h>
38 
39 using ::android::meminfo::Format;
40 using ::android::meminfo::GetFormat;
41 
usage(int exit_status)42 [[noreturn]] static void usage(int exit_status) {
43     std::cerr << "showmap [-aqtv] [-f FILE] PID\n"
44               << "-a\taddresses (show virtual memory map)\n"
45               << "-q\tquiet (don't show error if map could not be read)\n"
46               << "-t\tterse (show only items with private pages)\n"
47               << "-v\tverbose (don't coalesce maps with the same name)\n"
48               << "-f\tFILE (read from input from FILE instead of PID)\n"
49               << "-o\t[raw][json][csv] Print output in the specified format.\n"
50               << "  \tDefault output format is raw text. All memory in KB.)\n";
51 
52     exit(exit_status);
53 }
54 
main(int argc,char * argv[])55 int main(int argc, char* argv[]) {
56     signal(SIGPIPE, SIG_IGN);
57     struct option longopts[] = {
58             {"help", no_argument, nullptr, 'h'},
59             {0, 0, nullptr, 0},
60     };
61 
62     // Printing options.
63     bool terse = false;
64     bool quiet = false;
65 
66     // Output options.
67     bool show_addr = false;
68     bool verbose = false;
69     Format format = Format::RAW;
70 
71     // 'pid' will be ignored if a file is specified.
72     std::string filename;
73     pid_t pid = 0;
74 
75     int opt;
76     while ((opt = getopt_long(argc, argv, "tvaqf:o:h", longopts, nullptr)) != -1) {
77         switch (opt) {
78             case 't':
79                 terse = true;
80                 break;
81             case 'a':
82                 show_addr = true;
83                 break;
84             case 'v':
85                 verbose = true;
86                 break;
87             case 'q':
88                 quiet = true;
89                 break;
90             case 'f':
91                 filename = optarg;
92                 break;
93             case 'o':
94                 format = GetFormat(optarg);
95                 if (format == Format::INVALID) {
96                     std::cerr << "Invalid format.\n";
97                     usage(EXIT_FAILURE);
98                 }
99                 break;
100             case 'h':
101                 usage(EXIT_SUCCESS);
102             default:
103                 usage(EXIT_FAILURE);
104         }
105     }
106 
107     if (filename.empty()) {
108         if ((argc - 1) < optind) {
109             std::cerr << "Invalid arguments: Must provide <pid> at the end\n";
110             usage(EXIT_FAILURE);
111         }
112 
113         pid = atoi(argv[optind]);
114         if (pid <= 0) {
115             std::cerr << "Invalid process id " << argv[optind] << "\n";
116             usage(EXIT_FAILURE);
117         }
118         // run_showmap will read directly from this file and ignore the pid argument.
119         filename = ::android::base::StringPrintf("/proc/%d/smaps", pid);
120     }
121 
122     bool success = ::android::smapinfo::run_showmap(pid, filename, terse, verbose, show_addr, quiet,
123                                                     format, nullptr, std::cout, std::cerr);
124     if (!success) {
125         exit(EXIT_FAILURE);
126     }
127     return 0;
128 }
129