bugreport-service: Add the ability to get coredump file from a report
[platform/core/system/crash-worker.git] / src / bugreport-service / diagnostics / diagnostics_dump.c
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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 #include <alloca.h>
17 #include <assert.h>
18 #include <diagnostics.h>
19 #include <dirent.h>
20 #include <getopt.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #include "diagnostics/diagnostics.h"
32 #include "diagnostics/diagnostics_dump.h"
33 #include "shared/log.h"
34 #include "shared/util.h"
35
36 enum BUGREPORT_REPORT_TYPE { BR_UNKNOWN, BR_BUGREPORT, BR_CRASHINFO, BR_CRASHINFO_JSON, BR_COREDUMP_TAR, BR_FULLREPORT };
37
38 struct report_file {
39         char *file_name;
40         char *file_path;
41         struct timespec ctime;
42 };
43
44 static bool get_reports_list(const char *path, struct report_file **list, size_t *list_count)
45 {
46         assert(list);
47         assert(list_count);
48
49         DIR *dp;
50         struct dirent *ep;
51         dp = opendir(path);
52         if (dp == NULL) {
53                 _E("Open directory %s error: %m", path);
54                 return false;
55         }
56
57         char file_path[PATH_MAX];
58         bool ret = false;
59
60         while ((ep = readdir(dp))) {
61                 if (ep->d_type != DT_REG)
62                         continue;
63                 struct stat st;
64                 int r = snprintf(file_path, sizeof file_path, "%s/%s", path, ep->d_name);
65                 if (r == -1 || r >= sizeof file_path) {
66                         _E("Error while preparing report path: %m");
67                         goto out;
68                 }
69                 if (stat(file_path, &st) != 0) {
70                         _E("Get stats about %s error: %m", path);
71                         goto out;
72                 }
73                 struct tm t;
74                 if (localtime_r(&(st.st_ctim.tv_sec), &t) == NULL) {
75                         _E("localtime_r error: %m");
76                         goto out;
77                 }
78
79                 struct report_file *tmp_list = realloc(*list, sizeof(struct report_file)*(*list_count + 1));
80                 char *fname = strdup(ep->d_name);
81                 char *fpath = strdup(file_path);
82                 if (tmp_list == NULL || fname == NULL || fpath == NULL) {
83                         _E("Out of memory");
84                         free(fname);
85                         free(fpath);
86                         free(tmp_list);
87                         goto out;
88                 }
89
90                 *list = tmp_list;
91                 (*list)[*list_count].file_name = fname;
92                 (*list)[*list_count].file_path = fpath;
93                 (*list)[*list_count].ctime = st.st_ctim;
94                 (*list_count)++;
95         }
96
97         ret = true;
98 out:
99         closedir(dp);
100         return ret;
101 }
102
103 static void free_record(struct report_file *record)
104 {
105         free(record->file_name);
106         free(record->file_path);
107 }
108
109 static void filter_time(struct report_file *list, size_t *list_count, long time_from, long time_to)
110 {
111         size_t dest = 0;
112         size_t n_list_count = *list_count;
113
114         struct timespec current_ts;
115         clock_gettime(CLOCK_REALTIME, &current_ts);
116
117         for (int i = 0; i < *list_count; i++) {
118                 if (list[i].ctime.tv_sec >= time_from && list[i].ctime.tv_sec < time_to) {
119                         if (dest != i)
120                                 list[dest] = list[i];
121                         dest++;
122                 } else {
123                         free_record(&list[i]);
124                         n_list_count--;
125                 }
126         }
127         *list_count = n_list_count;
128 }
129
130 static int ctime_compare(const void *a, const void *b)
131 {
132         return ((struct report_file*)a)->ctime.tv_sec - ((struct report_file*)b)->ctime.tv_sec;
133 }
134
135 static struct report_file* get_reports(size_t *count)
136 {
137         assert(count);
138
139         struct report_file *list = NULL;
140         *count = 0;
141
142         const char *dirs[] = {
143                 CRASH_ROOT_PATH CRASH_PATH_SUBDIR,
144                 CRASH_ROOT_PATH LIVE_PATH_SUBDIR
145         };
146
147         for (int i = 0; i < ARRAY_SIZE(dirs); i++) {
148                 if (file_exists(dirs[i]) && !get_reports_list(dirs[i], &list, count)) {
149                         _E("Can not read reports from: %s", dirs[i]);
150                         return NULL;
151                 }
152         }
153
154         qsort(list, *count, sizeof(struct report_file), ctime_compare);
155         return list;
156 }
157
158 static struct report_file* get_reports_from_range(long time_from, long time_to, size_t *count)
159 {
160         struct report_file *list = get_reports(count);
161
162         filter_time(list, count, time_from, time_to);
163         return list;
164 }
165
166 static void write_bugreport_log_file(int out_fd, const char *report_path)
167 {
168         int in_fd = diagnostics_report_get_file(report_path, LOG_FILE);
169         if (in_fd < 0) {
170                 _D("No LOG_FILE report in %s file", report_path);
171                 return;
172         }
173
174         if (dprintf(out_fd, "Begin systemtate-log\n") == -1) {
175                 _E("Write error: %m");
176                 goto out;
177         }
178
179         if (copy_bytes(out_fd, in_fd, NULL) == -1) {
180                 _E("Copy data error");
181                 goto out;
182         }
183
184 out:
185         close(in_fd);
186         if (dprintf(out_fd, "End systemtate-log\n") == -1)
187                 _E("Write error: %m");
188 }
189
190 static bool write_bugreport(int fd, long time_from, long time_to)
191 {
192         size_t list_count = 0;
193         bool result = true;
194
195         struct report_file *list = get_reports_from_range(time_from, time_to, &list_count);
196         if (list == NULL)
197                 return false;
198
199         for (int i = 0; i < list_count; i++) {
200                 if (dprintf(fd, "%sBegin bugreport <%s>\n", (i > 0) ? "\n" : "", list[i].file_path) == -1) {
201                         _E("Write error: %m");
202                         result = false;
203                         goto out;
204                 }
205
206                 write_bugreport_log_file(fd, list[i].file_path);
207
208                 if (dprintf(fd, "End bugreport <%s>\n", list[i].file_path) == -1) {
209                         _E("Write error: %m");
210                         result = false;
211                         goto out;
212                 }
213         }
214
215 out:
216         for (int i = 0; i < list_count; i++)
217                 free_record(&list[i]);
218         free(list);
219         return result;
220 }
221
222 static bool write_crash_info(int fd, long time_from, long time_to, bool as_json)
223 {
224         bool result = true;
225         size_t list_count = 0;
226
227         struct report_file *list = get_reports_from_range(time_from, time_to, &list_count);
228         if (list == NULL) {
229                 if (as_json)
230                         dprintf(fd, "{\"error\": \"Internal error.\"}");
231                 return false;
232         }
233
234
235         const enum diagnostics_entry entry_type = as_json ? INFO_JSON : INFO_FILE;
236         for (int i = 0, p = 0; i < list_count; i++) {
237                 int nfd = diagnostics_report_get_file(list[i].file_path, entry_type);
238                 if (nfd <= 0) {
239                         _I("No INFO_FILE in %s file", list[i].file_path);
240                         dprintf(nfd, "No INFO_FILE in %s\n",  list[i].file_path);
241                         continue;
242                 }
243
244                 int res = 0;
245                 if (as_json)
246                         res = dprintf(fd, "%c\n", (p == 0) ? '[' : ',');
247                 else
248                         res = dprintf(fd, "%sBegin crash-info <%s>\n", (i > 0) ? "\n" : "", list[i].file_path);
249
250                 if (res == -1) {
251                         _E("Write error: %m");
252                         result = false;
253                         goto out;
254                 }
255
256                 if (copy_bytes(fd, nfd, NULL) == -1) {
257                         _E("Copy data error");
258                         close(nfd);
259                         result = false;
260                         goto out;
261                 }
262                 p++;
263
264                 close(nfd);
265
266                 if (as_json) {
267                         if (i == list_count-1)
268                                 res = dprintf(fd, "]\n");
269                 } else {
270                         res = dprintf(fd, "End crash-info <%s>\n", list[i].file_path);
271                 }
272                 if (res == -1) {
273                         _E("Write error: %m");
274                         result = false;
275                         goto out;
276                 }
277         }
278
279 out:
280         for (int i = 0; i < list_count; i++)
281                 free_record(&list[i]);
282         free(list);
283         return result;
284 }
285
286 static struct report_file *find_report_file(const char *ident, struct report_file *list, size_t list_count)
287 {
288         assert(ident);
289         assert(list);
290
291         for (size_t i = 0; i < list_count; i++) {
292                 char *dot = rindex(list[i].file_name, '.');
293                 if (dot == NULL)
294                         continue;
295                 if (strncmp(ident, list[i].file_name, dot - list[i].file_name) == 0)
296                         return &list[i];
297         }
298         return NULL;
299 }
300
301 static bool write_single_file(int fd, enum BUGREPORT_REPORT_TYPE report_type, const char *ident)
302 {
303         assert(fd >= 0);
304         assert(ident);
305
306         bool result = true;
307         size_t list_count = 0;
308         struct report_file *list = get_reports(&list_count);
309         if (list == NULL) {
310                 if (report_type == BR_CRASHINFO_JSON)
311                         dprintf(fd, "{\"error\": \"Internal error.\"}");
312                 return false;
313         }
314
315         struct report_file *report = find_report_file(ident, list, list_count);
316         if (report == NULL) {
317                 result = false;
318                 goto out;
319         }
320
321         enum diagnostics_entry report_entry = INFO_FILE;
322         switch (report_type) {
323         case BR_BUGREPORT:
324                 report_entry = LOG_FILE;
325                 break;
326         case BR_CRASHINFO:
327                 report_entry = INFO_FILE;
328                 break;
329         case BR_CRASHINFO_JSON:
330                 report_entry = INFO_JSON;
331                 break;
332         case BR_COREDUMP_TAR:
333                 report_entry = COREDUMP_TAR;
334                 break;
335         case BR_FULLREPORT:
336                 report_entry = FULL_REPORT;
337                 break;
338         default:
339                 _E("Unsupported report type: %d", report_type);
340                 result = false;
341                 goto out;
342         }
343
344         int in_fd = diagnostics_report_get_file(report->file_path, report_entry);
345         if (in_fd < 0) {
346                 _E("Can not get report type: %d from: %s (report ident: %s)", report_type, report->file_path, ident);
347                 result = false;
348                 goto out;
349         }
350
351         if (copy_bytes(fd, in_fd, NULL) == -1) {
352                 _E("Copy data error");
353                 result = false;
354                 goto out;
355         }
356 out:
357         for (size_t i = 0; i < list_count; i++)
358                 free_record(&list[i]);
359         free(list);
360         return result;
361 }
362
363 struct diagnostics_call_options {
364         enum BUGREPORT_REPORT_TYPE report_type;
365         long from, to;
366         bool last_set, from_set, to_set;
367         char *arg;
368 };
369
370 static void diagnostics_print_help(int out_fd)
371 {
372         dprintf(out_fd, "Usage:\n\n"
373         "  dumpsys org.tizen.bugreport-service [--type=<report_type>] [[--last <seconds> |--from <timestamp> [--to <timestamp>]]|[report_ident]]\n\n"
374         "  --type <report_type>    Specify the type of report. Available types: bugreport, crash-info, crash-info-json. Additional types are available\n"
375         "                          if single report is specified by <report_ident>: fullreport, coredumptar (default: bugreport).\n"
376         "  --last <seconds>        Get reports generated in the last <seconds>.\n"
377         "  --from <timestamp>      Get reports generated after specified time.\n"
378         "  --to <timestamp>        Get reports generated before specified time (default: current time).\n"
379         "  report_ident            Get a specified report.\n\n"
380         "  If no time range or report_ident is specified, all reports will be returned.\n\n"
381         );
382 }
383
384 static bool diagnostics_call_parse_options(int out_fd, char **params, int params_size, struct diagnostics_call_options *dco)
385 {
386         struct timespec cur_time;
387         clock_gettime(CLOCK_REALTIME, &cur_time);
388         dco->report_type = BR_BUGREPORT;
389         dco->from = 0;
390         dco->to = cur_time.tv_sec;
391         dco->last_set = false;
392         dco->from_set = false;
393         dco->to_set = false;
394         dco->arg = NULL;
395
396         enum PARAM_NAME { PN_TYPE = 1, PN_LAST, PN_TO, PN_FROM, PN_HELP };
397
398         struct option long_options[] = {
399                 {"type", required_argument, NULL, PN_TYPE},
400                 {"last", required_argument, NULL, PN_LAST},
401                 {"to", required_argument, NULL, PN_TO},
402                 {"from", required_argument, NULL, PN_FROM},
403                 {"help", no_argument, NULL, PN_HELP},
404                 {0, 0, 0, 0},
405         };
406
407         int opt;
408         optind = 0;
409
410         char **nparams = alloca((params_size+1)*sizeof(char*));
411         memcpy(&nparams[1], params, params_size*sizeof(char*));
412         nparams[0] = "n";
413         while ((opt = getopt_long_only(params_size+1, nparams, "", long_options, NULL)) != -1) {
414                 switch(opt) {
415                 case PN_TYPE:
416                         if (strcmp(optarg, "bugreport") == 0) {
417                                 dco->report_type = BR_BUGREPORT;
418                         } else if (strcmp(optarg, "crash-info") == 0) {
419                                 dco->report_type = BR_CRASHINFO;
420                         } else if (strcmp(optarg, "crash-info-json") == 0) {
421                                 dco->report_type = BR_CRASHINFO_JSON;
422                         } else if (strcmp(optarg, "coredumptar") == 0) {
423                                 dco->report_type = BR_COREDUMP_TAR;
424                         } else if (strcmp(optarg, "fullreport") == 0) {
425                                 dco->report_type = BR_FULLREPORT;
426                         } else {
427                                 _E("Incorrect report type: %s", optarg);
428                                 dprintf(out_fd, "Incorrect report type: %s\n", optarg);
429                                 return false;
430                         }
431                         break;
432                 case PN_LAST:
433                         dco->last_set = true;
434                         dco->from = cur_time.tv_sec - strtol(optarg, NULL, 10);
435                         break;
436                 case PN_FROM:
437                         dco->from_set = true;
438                         dco->from = strtol(optarg, NULL, 10);
439                         break;
440                 case PN_TO:
441                         dco->to_set = true;
442                         dco->to = strtol(optarg, NULL, 10);
443                         break;
444                 case PN_HELP:
445                         diagnostics_print_help(out_fd);
446                         return false;
447                 default:
448                         _E("Incorrect option: %s", (optind > 0 && optind - 1 < params_size + 1) ? nparams[optind-1] : "<unknown>");
449                         dprintf(out_fd, "Incorrect option: %s\n", (optind > 0 && optind - 1 < params_size + 1) ? nparams[optind-1] : "<unknown>");
450                         return false;
451                 }
452         }
453
454         if (optind < params_size+1)
455                 dco->arg = nparams[optind];
456
457         return true;
458 }
459
460 static void diagnostics_callback(diagnostics_data_h data, char **params, int params_size, diagnostics_ctx_h ctx, void *user_data)
461 {
462         int fd;
463         int ret = diagnostics_data_get_fd(data, &fd);
464         if (ret < 0) {
465                 _E("Get data file descriptor error: %d", ret);
466                 return;
467         }
468         struct diagnostics_call_options dco;
469
470         if (!diagnostics_call_parse_options(fd, params, params_size, &dco))
471                 return;
472
473         if ((dco.last_set && (dco.from_set || dco.to_set)) || (dco.to_set && !dco.from_set)) {
474                 _E("Incorrect parameters set");
475                 dprintf(fd, "Incorrect parameters set.\n");
476                 return;
477         }
478
479         if (dco.arg != NULL) {
480                 if (dco.last_set || dco.from_set || dco.to_set) {
481                         _E("Incorrect parameters set.");
482                         dprintf(fd, "Incorrect parameters set.\n");
483                         return;
484                 }
485                 write_single_file(fd, dco.report_type, dco.arg);
486                 return;
487         }
488
489         switch(dco.report_type) {
490         case BR_CRASHINFO:
491                 write_crash_info(fd, dco.from, dco.to, false);
492                 break;
493         case BR_CRASHINFO_JSON:
494                 write_crash_info(fd, dco.from, dco.to, true);
495                 break;
496         case BR_BUGREPORT:
497                 write_bugreport(fd, dco.from, dco.to);
498                 break;
499         case BR_COREDUMP_TAR:
500                 dprintf(fd, "Report type \"coredumptar\" requires a specific report with report_ident argument.\n");
501                 break;
502         case BR_FULLREPORT:
503                 dprintf(fd, "Report type \"fullreport\" requires a specific report with report_ident argument.\n");
504                 break;
505         default:
506                 _E("Unknown report type\n");
507         }
508 }
509
510 bool diagnostics_init()
511 {
512         diagnostics_set_client_id("org.tizen.bugreport-service");
513         if (diagnostics_set_data_request_cb(&diagnostics_callback, NULL) != DIAGNOSTICS_ERROR_NONE) {
514                 _E("Unable to register diagnostics callback");
515                 return false;
516         }
517
518         return true;
519 }
520