summaryrefslogtreecommitdiff
path: root/tools/incident_report/main.cpp
blob: 250d1186c672cfbb642218fd67680bcc1d2dc69c (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
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
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "generic_message.h"
#include "printer.h"

#include <frameworks/base/core/proto/android/os/incident.pb.h>
#include <google/protobuf/wire_format.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

using namespace android::os;
using namespace google::protobuf;
using namespace google::protobuf::io;
using namespace google::protobuf::internal;

static bool read_message(CodedInputStream* in, Descriptor const* descriptor,
        GenericMessage* message);
static void print_message(Out* out, Descriptor const* descriptor, GenericMessage const* message);

// ================================================================================
static bool
read_length_delimited(CodedInputStream* in, uint32 fieldId, Descriptor const* descriptor,
        GenericMessage* message)
{
    uint32 size;
    if (!in->ReadVarint32(&size)) {
        return false;
    }

    FieldDescriptor const* field = descriptor->FindFieldByNumber(fieldId);
    if (field != NULL) {
        int type = field->type();
        if (type == FieldDescriptor::TYPE_MESSAGE) {
            GenericMessage* child = message->addMessage(fieldId);

            CodedInputStream::Limit limit = in->PushLimit(size);
            bool rv = read_message(in, field->message_type(), child);
            in->PopLimit(limit);
            return rv;
        } else if (type == FieldDescriptor::TYPE_STRING) {
            // TODO: do a version of readstring that just pumps the data
            // rather than allocating a string which we don't care about.
            string str;
            if (in->ReadString(&str, size)) {
                message->addString(fieldId, str);
                return true;
            } else {
                return false;
            }
        } else if (type == FieldDescriptor::TYPE_BYTES) {
            // TODO: Save bytes field.
            return in->Skip(size);
        }
    }
    return in->Skip(size);
}

// ================================================================================
static bool
read_message(CodedInputStream* in, Descriptor const* descriptor, GenericMessage* message)
{
    uint32 value32;
    uint64 value64;

    while (true) {
        uint32 tag = in->ReadTag();
        if (tag == 0) {
            return true;
        }
        int fieldId = WireFormatLite::GetTagFieldNumber(tag);
        switch (WireFormatLite::GetTagWireType(tag)) {
            case WireFormatLite::WIRETYPE_VARINT:
                if (in->ReadVarint64(&value64)) {
                    message->addInt64(fieldId, value64);
                    break;
                } else {
                    fprintf(stderr, "bad VARINT: 0x%x (%d) at index %d\n", tag, tag,
                                                    in->CurrentPosition());
                    return false;
                }
            case WireFormatLite::WIRETYPE_FIXED64:
                if (in->ReadLittleEndian64(&value64)) {
                    message->addInt64(fieldId, value64);
                    break;
                } else {
                    fprintf(stderr, "bad VARINT: 0x%x (%d) at index %d\n", tag, tag,
                            in->CurrentPosition());
                    return false;
                }
            case WireFormatLite::WIRETYPE_LENGTH_DELIMITED:
                if (!read_length_delimited(in, fieldId, descriptor, message)) {
                    fprintf(stderr, "bad LENGTH_DELIMITED: 0x%x (%d) at index %d\n",
                            tag, tag, in->CurrentPosition());
                    return false;
                }
                break;
            case WireFormatLite::WIRETYPE_FIXED32:
                if (in->ReadLittleEndian32(&value32)) {
                    message->addInt32(fieldId, value32);
                    break;
                } else {
                    fprintf(stderr, "bad FIXED32: 0x%x (%d) at index %d\n", tag, tag,
                            in->CurrentPosition());
                    return false;
                }
            default:
                fprintf(stderr, "bad tag: 0x%x (%d) at index %d\n", tag, tag,
                        in->CurrentPosition());
                return false;
        }
    }
}

// ================================================================================
static void
print_value(Out* out, FieldDescriptor const* field, GenericMessage::Node const& node)
{
    uint32_t val32;
    FieldDescriptor::Type type = field->type();

    switch (node.type) {
        case GenericMessage::TYPE_VALUE32:
            switch (type) {
                case FieldDescriptor::TYPE_FIXED32:
                    out->printf("%u", node.value32);
                    break;
                case FieldDescriptor::TYPE_SFIXED32:
                    out->printf("%d", node.value32);
                    break;
                case FieldDescriptor::TYPE_FLOAT:
                    out->printf("%f", *(float*)&node.value32);
                    break;
                default:
                    out->printf("(unexpected value32 %d (0x%x)", node.value32, node.value32);
                    break;
            }
            break;
        case GenericMessage::TYPE_VALUE64:
            switch (type) {
                case FieldDescriptor::TYPE_FIXED64:
                case FieldDescriptor::TYPE_SFIXED64:
                case FieldDescriptor::TYPE_DOUBLE:
                    out->printf("%f", *(double*)&node.value64);
                    break;
                case FieldDescriptor::TYPE_SINT32:
                case FieldDescriptor::TYPE_INT32:
                    val32 = (uint32_t)node.value32;
                    out->printf("%d", val32);
                    break;
                case FieldDescriptor::TYPE_INT64:
                case FieldDescriptor::TYPE_UINT32:
                    val32 = (uint32_t)node.value32;
                    out->printf("%u", val32);
                    break;
                case FieldDescriptor::TYPE_UINT64:
                case FieldDescriptor::TYPE_SINT64:
                case FieldDescriptor::TYPE_BOOL:
                    if (node.value64) {
                        out->printf("true");
                    } else {
                        out->printf("false");
                    }
                    break;
                case FieldDescriptor::TYPE_ENUM:
                    out->printf("%s", field->enum_type()->FindValueByNumber((int)node.value64)
                            ->name().c_str());
                    break;
                default:
                    out->printf("(unexpected value64 %ld (0x%x))", node.value64, node.value64);
                    break;
            }
            break;
        case GenericMessage::TYPE_MESSAGE:
            print_message(out, field->message_type(), node.message);
            break;
        case GenericMessage::TYPE_STRING:
            // TODO: custom format for multi-line strings.
            out->printf("%s", node.str->c_str());
            break;
        case GenericMessage::TYPE_DATA:
            out->printf("<bytes>");
            break;
    }
}

static void
print_message(Out* out, Descriptor const* descriptor, GenericMessage const* message)
{
    out->printf("%s {\n", descriptor->name().c_str());
    out->indent();

    int const N = descriptor->field_count();
    for (int i=0; i<N; i++) {
        FieldDescriptor const* field = descriptor->field(i);

        int fieldId = field->number();
        bool repeated = field->label() == FieldDescriptor::LABEL_REPEATED;
        FieldDescriptor::Type type = field->type();
        GenericMessage::const_iterator_pair it = message->find(fieldId);

        out->printf("%s=", field->name().c_str());
        if (repeated) {
            if (it.first != it.second) {
                out->printf("[");
                if (type == FieldDescriptor::TYPE_MESSAGE
                        || type == FieldDescriptor::TYPE_STRING
                        || type == FieldDescriptor::TYPE_BYTES) {
                    out->printf("\n");
                }
                out->indent();

                for (GenericMessage::const_iterator_pair it = message->find(fieldId);
                        it.first != it.second; it.first++) {
                    print_value(out, field, it.first->second);
                    if (type == FieldDescriptor::TYPE_MESSAGE
                            || type == FieldDescriptor::TYPE_STRING
                            || type == FieldDescriptor::TYPE_BYTES) {
                        out->printf("\n");
                    }
                }

                out->dedent();
                out->printf("]");
            } else {
                out->printf("[]");
            }
        } else {
            if (it.first != it.second) {
                print_value(out, field, it.first->second);
            } else {
                switch (type) {
                    case FieldDescriptor::TYPE_BOOL:
                        out->printf("false");
                        break;
                    case FieldDescriptor::TYPE_STRING:
                    case FieldDescriptor::TYPE_MESSAGE:
                        out->printf("");
                        break;
                    case FieldDescriptor::TYPE_ENUM:
                        out->printf("%s", field->default_value_enum()->name().c_str());
                        break;
                    default:
                        out->printf("0");
                        break;
                }
            }
        }
        out->printf("\n");
    }
    out->dedent();
    out->printf("}");
}

// ================================================================================
static uint8_t*
write_raw_varint(uint8_t* buf, uint32_t val)
{
    uint8_t* p = buf;
    while (true) {
        if ((val & ~0x7F) == 0) {
            *p++ = (uint8_t)val;
            return p;
        } else {
            *p++ = (uint8_t)((val & 0x7F) | 0x80);
            val >>= 7;
        }
    }
}

static int
write_all(int fd, uint8_t const* buf, size_t size)
{
    while (size > 0) {
        ssize_t amt = ::write(fd, buf, size);
        if (amt < 0) {
            return errno;
        }
        size -= amt;
        buf += amt;
    }
    return 0;
}

static int
adb_incident_workaround(const char* adbSerial, const vector<string>& sections)
{
    const int maxAllowedSize = 20 * 1024 * 1024; // 20MB
    unique_ptr<uint8_t[]> buffer(new uint8_t[maxAllowedSize]);

    for (vector<string>::const_iterator it=sections.begin(); it!=sections.end(); it++) {
        Descriptor const* descriptor = IncidentProto::descriptor();
        FieldDescriptor const* field;

        // Get the name and field id.
        string name = *it;
        char* end;
        int id = strtol(name.c_str(), &end, 0);
        if (*end == '\0') {
            // If it's an id, find out the string.
            field = descriptor->FindFieldByNumber(id);
            if (field == NULL) {
                fprintf(stderr, "Unable to find field number: %d\n", id);
                return 1;
            }
            name = field->name();
        } else {
            // If it's a string, find out the id.
            field = descriptor->FindFieldByName(name);
            if (field == NULL) {
                fprintf(stderr, "Unable to find field: %s\n", name.c_str());
                return 1;
            }
            id = field->number();
        }
        
        int pfd[2];
        if (pipe(pfd) != 0) {
            fprintf(stderr, "pipe failed: %s\n", strerror(errno));
            return 1;
        }

        pid_t pid = fork();
        if (pid == -1) {
            fprintf(stderr, "fork failed: %s\n", strerror(errno));
            return 1;
        } else if (pid == 0) {
            // child
            dup2(pfd[1], STDOUT_FILENO);
            close(pfd[0]);
            close(pfd[1]);

            char const** args = (char const**)malloc(sizeof(char*) * 8);
            int argpos = 0;
            args[argpos++] = "adb";
            if (adbSerial != NULL) {
                args[argpos++] = "-s";
                args[argpos++] = adbSerial;
            }
            args[argpos++] = "shell";
            args[argpos++] = "dumpsys";
            args[argpos++] = name.c_str();
            args[argpos++] = "--proto";
            args[argpos++] = NULL;
            execvp(args[0], (char*const*)args);
            fprintf(stderr, "execvp failed: %s\n", strerror(errno));
            free(args);
            return 1;
        } else {
            // parent
            close(pfd[1]);

            size_t size = 0;
            while (size < maxAllowedSize) {
                ssize_t amt = read(pfd[0], buffer.get() + size, maxAllowedSize - size);
                if (amt == 0) {
                    break;
                } else if (amt == -1) {
                    fprintf(stderr, "read error: %s\n", strerror(errno));
                    return 1;
                }
                size += amt;
            }

            int status;
            do {
                waitpid(pid, &status, 0);
            } while (!WIFEXITED(status));
            if (WEXITSTATUS(status) != 0) {
                return WEXITSTATUS(status);
            }

            if (size > 0) {
                uint8_t header[20];
                uint8_t* p = write_raw_varint(header, (id << 3) | 2);
                p = write_raw_varint(p, size);
                int err = write_all(STDOUT_FILENO, header, p-header);
                if (err != 0) {
                    fprintf(stderr, "write error: %s\n", strerror(err));
                    return 1;
                }
                err = write_all(STDOUT_FILENO, buffer.get(), size);
                if (err != 0) {
                    fprintf(stderr, "write error: %s\n", strerror(err));
                    return 1;
                }
            }

            close(pfd[0]);
        }
    }

    return 0;
}

// ================================================================================
static void
usage(FILE* out)
{
    fprintf(out, "usage: incident_report -i INPUT [-o OUTPUT]\n");
    fprintf(out, "\n");
    fprintf(out, "Pretty-prints an incident report protobuf file.\n");
    fprintf(out, "  -i INPUT    the input file. INPUT may be '-' to use stdin\n");
    fprintf(out, "  -o OUTPUT   the output file. OUTPUT may be '-' or omitted to use stdout\n");
    fprintf(out, "\n");
    fprintf(out, "\n");
    fprintf(out, "usage: incident_report [-o OUTPUT] [-t|b] [-s SERIAL] [SECTION...]\n");
    fprintf(out, "\n");
    fprintf(out, "Take an incident report over adb (which must be in the PATH).\n");
    fprintf(out, "  -b          output the incident report raw protobuf format\n");
    fprintf(out, "  -o OUTPUT   the output file. OUTPUT may be '-' or omitted to use stdout\n");
    fprintf(out, "  -s SERIAL   sent to adb to choose which device, instead of $ANDROID_SERIAL\n");
    fprintf(out, "  -t          output the incident report in pretty-printed text format\n");
    fprintf(out, "\n");
    fprintf(out, "  SECTION     which bugreport sections to print, either the int code of the\n");
    fprintf(out, "              section in the Incident proto or the field name.  If ommited,\n");
    fprintf(out, "              the report will contain all fields\n");
    fprintf(out, "\n");
}

int
main(int argc, char** argv)
{
    enum { OUTPUT_TEXT, OUTPUT_PROTO } outputFormat = OUTPUT_TEXT;
    const char* inFilename = NULL;
    const char* outFilename = NULL;
    const char* adbSerial = NULL;
    bool adbIncidentWorkaround = true;
    pid_t childPid = -1;
    vector<string> sections;

    int opt;
    while ((opt = getopt(argc, argv, "bhi:o:s:tw")) != -1) {
        switch (opt) {
            case 'b':
                outputFormat = OUTPUT_PROTO;
                break;
            case 'i':
                inFilename = optarg;
                break;
            case 'o':
                outFilename = optarg;
                break;
            case 's':
                adbSerial = optarg;
                break;
            case 't':
                outputFormat = OUTPUT_TEXT;
                break;
            case 'h':
                usage(stdout);
                return 0;
            case 'w':
                adbIncidentWorkaround = false;
                break;
            default:
                usage(stderr);
                return 1;
        }
    }

    while (optind < argc) {
        sections.push_back(argv[optind++]);
    }

    int inFd;
    if (inFilename != NULL) {
        // translate-only mode - oepn the file or use stdin.
        if (strcmp("-", inFilename) == 0) {
            inFd = STDIN_FILENO;
        } else {
            inFd = open(inFilename, O_RDONLY | O_CLOEXEC);
            if (inFd < 0) {
                fprintf(stderr, "unable to open file for read (%s): %s\n", strerror(errno),
                        inFilename);
                return 1;
            }
        }
    } else {
        // pipe mode - run adb shell incident ...
        int pfd[2];
        if (pipe(pfd) != 0) {
            fprintf(stderr, "pipe failed: %s\n", strerror(errno));
            return 1;
        }

        childPid = fork();
        if (childPid == -1) {
            fprintf(stderr, "fork failed: %s\n", strerror(errno));
            return 1;
        } else if (childPid == 0) {
            dup2(pfd[1], STDOUT_FILENO);
            close(pfd[0]);
            close(pfd[1]);
            // child
            if (adbIncidentWorkaround) {
                // TODO: Until the device side incident command is checked in,
                // the incident_report builds the outer Incident proto by hand
                // from individual adb shell dumpsys <service> --proto calls,
                // with a maximum allowed output size.
                return adb_incident_workaround(adbSerial, sections);
            }

            // TODO: This is what the real implementation will be...
            char const** args = (char const**)malloc(sizeof(char*) * (6 + sections.size()));
            int argpos = 0;
            args[argpos++] = "adb";
            if (adbSerial != NULL) {
                args[argpos++] = "-s";
                args[argpos++] = adbSerial;
            }
            args[argpos++] = "shell";
            args[argpos++] = "incident";
            for (vector<string>::const_iterator it=sections.begin(); it!=sections.end(); it++) {
                args[argpos++] = it->c_str();
            }
            args[argpos++] = NULL;
            execvp(args[0], (char*const*)args);
            fprintf(stderr, "execvp failed: %s\n", strerror(errno));
            return 0;
        } else {
            // parent
            inFd = pfd[0];
            close(pfd[1]);
        }
    }

    int outFd;
    if (outFilename == NULL || strcmp("-", outFilename) == 0) {
        outFd = STDOUT_FILENO;
    } else {
        outFd = open(outFilename, O_CREAT | O_RDWR, 0666);
        if (outFd < 0) {
            fprintf(stderr, "unable to open file for write: %s\n", outFilename);
            return 1;
        }
    }

    GenericMessage message;

    Descriptor const* descriptor = IncidentProto::descriptor();
    FileInputStream infile(inFd);
    CodedInputStream in(&infile);

    if (!read_message(&in, descriptor, &message)) {
        fprintf(stderr, "unable to read incident\n");
        return 1;
    }

    Out out(outFd);

    print_message(&out, descriptor, &message);
    out.printf("\n");

    if (childPid != -1) {
        int status;
        do {
            waitpid(childPid, &status, 0);
        } while (!WIFEXITED(status));
        if (WEXITSTATUS(status) != 0) {
            return WEXITSTATUS(status);
        }
    }

    return 0;
}