summaryrefslogtreecommitdiff
path: root/pinner/pintool.cpp
blob: 9aa47b7b298eb938e25f3f257a589caac7f99d4d (plain)
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
#include <android-base/parseint.h>
#include <fcntl.h>
#include <sys/endian.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <vector>

#include <meminspect.h>
#include <pin_utils.h>

using namespace std;
using namespace android::base;

enum ToolMode {
    MAPPED_FILE,  // Files that are mapped in memory
    PINLIST,      // pinlist.meta style file
    UNKNOWN
};

void print_pinlist_ranges(const std::vector<VmaRange>& ranges) {
    cout << "--pinlist memory ranges--" << endl;
    for (auto&& range : ranges) {
        cout << "start=" << range.offset << " bytes=" << range.length << endl;
    }
}

void print_pinlist_summary(const std::vector<VmaRange>& ranges) {
    cout << "--pinlist summary--" << endl;
    uint64_t total_bytes = 0;
    for (auto&& range : ranges) {
        total_bytes += range.length;
    }
    cout << "total_bytes_to_pin=" << total_bytes << endl;
}

int perform_file_action(const vector<string>& options) {
    std::string custom_probe_file;
    std::string output_file;
    std::string pinconfig_file;

    bool verbose = false;
    bool is_zip = false;
    bool dump_results = false;
    ProbeType probe_type = UNSET;
    int64_t write_quota = -1;  // unbounded by default

    if (options.empty()) {
        cerr << "Missing filename for file action, see usage for details." << endl;
        return 1;
    }

    std::string input_file = options[0];

    // Validate that the file exists.
    if (get_file_size(input_file) == -1) {
        cerr << "Error: Could not read file: " << input_file << endl;
        return 1;
    }

    if (input_file.empty()) {
        cerr << "Error: Should specify an input file." << endl;
        return 1;
    }

    // Parse flags
    for (int i = 1; i < options.size(); ++i) {
        string option = options[i];
        if (option == "--gen-probe") {
            if (probe_type != ProbeType::UNSET) {
                cerr << "Should only specify one probe treatment. See usage for details." << endl;
                return 1;
            }
            probe_type = ProbeType::GENERATE;
            continue;
        }

        if (option == "--use-probe") {
            if (probe_type != ProbeType::UNSET) {
                cerr << "Should only specify one probe treatment. See usage for details." << endl;
                return 1;
            }
            probe_type = ProbeType::CUSTOM;
            ++i;
            custom_probe_file = options[i];
            continue;
        }
        if (option == "--pinconfig") {
            ++i;
            pinconfig_file = options[i];
            continue;
        }
        if (option == "-o") {
            ++i;
            output_file = options[i];
            continue;
        }
        if (option == "--quota") {
            ++i;
            android::base::ParseInt(options[i], &write_quota);
            continue;
        }
        if (option == "-v") {
            verbose = true;
            continue;
        }
        if (option == "--zip") {
            is_zip = true;
            continue;
        }
        if (option == "--dump") {
            dump_results = true;
            continue;
        }
    }

    if (verbose) {
        cout << "Setting output pinlist file: " << output_file.c_str() << endl;
        cout << "Setting input file: " << input_file.c_str() << endl;
        cout << "Setting pinconfig file: " << pinconfig_file.c_str() << endl;
        cout << "Setting custom probe file: " << custom_probe_file.c_str() << endl;
        cout << "Setting probe type: " << probe_type << endl;
        cout << "Dump enabled: " << dump_results << endl;
        cout << "Is Zip file: " << is_zip << endl;
        if (write_quota != -1) {
            cout << "Set Write quota: " << write_quota << endl;
        }
    }

    PinTool pintool(input_file);

    if (is_zip) {
        pintool.set_verbose_output(verbose);
        if (probe_type == ProbeType::CUSTOM) {
            if (verbose) {
                cout << "Using custom probe file: " << custom_probe_file << endl;
            }
            pintool.read_probe_from_pinlist(custom_probe_file);
        } else if (probe_type == ProbeType::GENERATE) {
            if (verbose) {
                cout << "Generating probe" << endl;
            }
            int res = pintool.probe_resident();
            if (res > 0) {
                cerr << "Failed to generate probe. Error Code: " << res << endl;
                return 1;
            }
        }
        pintool.compute_zip_entry_coverages();

        if (pinconfig_file.length() > 0) {
            // We have provided a pinconfig file so perform filtering
            // of computed coverages based on it.
            pintool.filter_zip_entry_coverages(pinconfig_file);
        }

        if (dump_results) {
            cout << endl << "----Unfiltered file coverages----" << endl << endl;
            pintool.dump_coverages(PinTool::DumpType::FILE_COVERAGE);

            if (pinconfig_file.length() > 0) {
                cout << endl << "----Filtered file coverages----" << endl << endl;
                pintool.dump_coverages(PinTool::DumpType::FILTERED);
            }
        }

        if (output_file.length() > 0) {
            pintool.write_coverages_as_pinlist(output_file, write_quota);
        }

        return 0;
    } else {
        if (probe_type != ProbeType::GENERATE) {
            cerr << "Only generating probes is supported for non-zip files, please include "
                    "--gen-probe on your command"
                 << endl;
            return 1;
        }

        // Generic file probing will just return resident memory and offsets
        // without more contextual information.
        VmaRangeGroup resident;

        int res = pintool.probe_resident();
        if (res > 0) {
            cerr << "Failed to generate probe. Error Code: " << res << endl;
            return 1;
        }

        pintool.dump_coverages(PinTool::DumpType::PROBE);

        if (output_file.length() > 0) {
            res = write_pinlist_file(output_file, resident.ranges, write_quota);
            if (res > 0) {
                cerr << "Failed to write pin file at: " << output_file << endl;
            } else if (verbose) {
                cout << "Finished writing pin file at: " << output_file << endl;
            }
        }
        return res;
    }
    return 0;
}

int perform_pinlist_action(const vector<string>& options) {
    string pinner_file;
    bool verbose = false;
    bool dump = false;
    bool summary = false;

    if (options.size() < 1) {
        cerr << "Missing arguments for pinlist mode. See usage for details << endl";
        return 1;
    }
    pinner_file = options[0];
    for (int i = 1; i < options.size(); ++i) {
        string option = options[i];

        if (option == "-v") {
            verbose = true;
        }

        if (option == "--dump") {
            dump = true;
        }

        if (option == "--summary") {
            summary = true;
        }
    }

    if (pinner_file.empty()) {
        cerr << "Error: Pinlist file to dump is missing. Specify it with '-p <file>'" << endl;
        return 1;
    }

    if (verbose) {
        cout << "Setting file to dump: " << pinner_file.c_str() << endl;
    }

    vector<VmaRange> vma_ranges;
    if (read_pinlist_file(pinner_file, vma_ranges) == 1) {
        cerr << "Failed reading pinlist file" << endl;
    }

    if (dump) {
        print_pinlist_ranges(vma_ranges);
    }

    if (summary) {
        print_pinlist_summary(vma_ranges);
    }

    return 0;
}

void print_usage() {
    const string usage = R"(
    Expected usage: pintool <mode> <required> [option]
    where:
    ./pintool <MODE>
    <MODE>
        file <filename> [option]
            [option]
                --gen-probe
                    Generate a probe from current resident memory based on provided "file"
                --use-probe <path_to_input_pinlist.meta>
                    Use a previously generated pinlist.meta style file as the probe to match against.
                --dump
                    Dump output contents to console.
                --zip
                    Treat the file as a zip/apk file required for doing per-file coverage analysis and generation.
                --pinconfig <path_to_pinconfig.txt>
                    Filter output coverage ranges using a provided pinconfig.txt style file. See README.md for samples
                    on the format of that file.
                -v
                    Enable verbose output.

        pinlist <pinlist_file> [option]
            <pinlist_file>
                this is the file that will be used for reading and it should follow the pinlist.meta format.
            [option]
                --dump
                    Dump <pinlist_file> contents to console output.
                -v
                    Enable verbose output.
                --summary
                    Summary results for the pinlist.meta file
    )";
    cout << usage.c_str();
}

int main(int argc, char** argv) {
    if (argc == 1) {
        print_usage();
        return 0;
    }

    if (argc < 2) {
        cerr << "<mode> is missing";
        return 1;
    }

    if (strcmp(argv[1], "--help") == 0) {
        print_usage();
        return 0;
    }

    ToolMode mode = ToolMode::UNKNOWN;
    if (strcmp(argv[1], "file") == 0) {
        mode = ToolMode::MAPPED_FILE;
    } else if (strcmp(argv[1], "pinlist") == 0) {
        mode = ToolMode::PINLIST;
    }

    if (mode == ToolMode::UNKNOWN) {
        cerr << "Failed to find mode: " << argv[1] << ". See usage for available modes." << endl;
        return 1;
    }

    vector<string> options;
    for (int i = 2; i < argc; ++i) {
        options.push_back(argv[i]);
    }

    int res;
    switch (mode) {
        case ToolMode::MAPPED_FILE:
            res = perform_file_action(options);
            break;
        case ToolMode::PINLIST:
            res = perform_pinlist_action(options);
            break;
        case ToolMode::UNKNOWN:
            cerr << "Unknown <MODE> see usage for details." << endl;
            return 1;
    }

    return res;
}