1 /*
2  * Copyright (C) 2018-2022 Intel Corporation.
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <unistd.h>
11 #include "crash_dump.h"
12 #include "log_sys.h"
13 #include "version.h"
14 
15 /**
16  * Debugger can work without server when uses "debugger pid" commands to
17  * debug the running process. This function will dump the process info on the
18  * screen, also you can relocate the info to a file.
19  */
print_usage(void)20 static void print_usage(void)
21 {
22 	printf("debugger - tool to dump process info of a running process.\n");
23 	printf("[Usage]\n");
24 	printf("\t--shell cmd, debugger <pid>  (root role to run)\n");
25 	printf("[Option]\n");
26 	printf("\t-h: print this usage message\n");
27 	printf("\t-v: print debugger version\n");
28 }
29 
main(int argc,char * argv[])30 int main(int argc, char *argv[])
31 {
32 	int pid;
33 
34 	if (argc > 1) {
35 		if (strcmp(argv[1], "-v") == 0) {
36 			printf("version is %d.%d-%s, build by %s@%s\n",
37 				UC_MAJOR_VERSION, UC_MINOR_VERSION,
38 				UC_BUILD_VERSION, UC_BUILD_USER,
39 				UC_BUILD_TIME);
40 			return 0;
41 		}
42 		if (strcmp(argv[1], "-h") == 0) {
43 			print_usage();
44 			return 0;
45 		}
46 	} else
47 		print_usage();
48 
49 	if (getuid() != 0) {
50 		printf("failed to execute debugger, root is required\n");
51 		exit(EXIT_FAILURE);
52 	}
53 
54 	if (argc == 2) {
55 		/* it's from shell cmd */
56 		pid = (int)strtol(argv[1], NULL, 10);
57 		crash_dump(pid, 0, STDOUT_FILENO);
58 	} else {
59 		print_usage();
60 		return 0;
61 	}
62 
63 	return 0;
64 }
65