xref: /kvmtool/builtin-debug.c (revision 4b1addaeed6f8c717b2b818e3f867829849d8e86)
1 #include <kvm/util.h>
2 #include <kvm/kvm-cmd.h>
3 #include <kvm/builtin-debug.h>
4 #include <kvm/kvm.h>
5 #include <kvm/parse-options.h>
6 #include <kvm/kvm-ipc.h>
7 
8 #include <stdio.h>
9 #include <string.h>
10 #include <signal.h>
11 
12 static bool all;
13 static int instance;
14 static const char *instance_name;
15 
16 struct debug_cmd {
17 	u32 type;
18 	u32 len;
19 };
20 
21 static const char * const debug_usage[] = {
22 	"kvm debug [--all] [-n name]",
23 	NULL
24 };
25 
26 static const struct option debug_options[] = {
27 	OPT_GROUP("General options:"),
28 	OPT_BOOLEAN('a', "all", &all, "Debug all instances"),
29 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
30 	OPT_END()
31 };
32 
33 static void parse_debug_options(int argc, const char **argv)
34 {
35 	while (argc != 0) {
36 		argc = parse_options(argc, argv, debug_options, debug_usage,
37 				PARSE_OPT_STOP_AT_NON_OPTION);
38 		if (argc != 0)
39 			kvm_debug_help();
40 	}
41 }
42 
43 void kvm_debug_help(void)
44 {
45 	usage_with_options(debug_usage, debug_options);
46 }
47 
48 static int do_debug(const char *name, int sock)
49 {
50 	struct debug_cmd cmd = {KVM_IPC_DEBUG, 0};
51 	int r;
52 
53 	r = write(sock, &cmd, sizeof(cmd));
54 	if (r < 0)
55 		return r;
56 
57 	return 0;
58 }
59 
60 int kvm_cmd_debug(int argc, const char **argv, const char *prefix)
61 {
62 	parse_debug_options(argc, argv);
63 
64 	if (all)
65 		return kvm__enumerate_instances(do_debug);
66 
67 	if (instance_name == NULL &&
68 	    instance == 0)
69 		kvm_debug_help();
70 
71 	if (instance_name)
72 		instance = kvm__get_sock_by_instance(instance_name);
73 
74 	if (instance <= 0)
75 		die("Failed locating instance");
76 
77 	return do_debug(instance_name, instance);
78 }
79