1 /*
2  * Copyright (C) 2020 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 <err.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 
22 #include <android-base/unique_fd.h>
23 #include <libdebuggerd/tombstone.h>
24 
25 #include "tombstone.pb.h"
26 
27 using android::base::unique_fd;
28 
usage(bool error)29 [[noreturn]] void usage(bool error) {
30   fprintf(stderr, "usage: pbtombstone TOMBSTONE.PB\n");
31   fprintf(stderr, "Convert a protobuf tombstone to text.\n");
32   exit(error);
33 }
34 
main(int argc,const char * argv[])35 int main(int argc, const char* argv[]) {
36   if (argc != 2) {
37     usage(true);
38   }
39 
40   if (strcmp("-h", argv[1]) == 0 || strcmp("--help", argv[1]) == 0) {
41     usage(false);
42   }
43 
44   unique_fd fd(open(argv[1], O_RDONLY | O_CLOEXEC));
45   if (fd == -1) {
46     err(1, "failed to open tombstone '%s'", argv[1]);
47   }
48 
49   Tombstone tombstone;
50   if (!tombstone.ParseFromFileDescriptor(fd.get())) {
51     err(1, "failed to parse tombstone");
52   }
53 
54   bool result = tombstone_proto_to_text(
55       tombstone, [](const std::string& line, bool) { printf("%s\n", line.c_str()); });
56 
57   if (!result) {
58     errx(1, "tombstone was malformed");
59   }
60 
61   return 0;
62 }
63