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 7 #include <stdio.h> 8 #include <string.h> 9 #include <signal.h> 10 11 static bool all; 12 static u64 instance_pid; 13 static const char *instance_name; 14 15 static const char * const debug_usage[] = { 16 "kvm debug [--all] [-n name] [-p pid]", 17 NULL 18 }; 19 20 static const struct option debug_options[] = { 21 OPT_GROUP("General options:"), 22 OPT_BOOLEAN('a', "all", &all, "Debug all instances"), 23 OPT_STRING('n', "name", &instance_name, "name", "Instance name"), 24 OPT_U64('p', "pid", &instance_pid, "Instance pid"), 25 OPT_END() 26 }; 27 28 static void parse_debug_options(int argc, const char **argv) 29 { 30 while (argc != 0) { 31 argc = parse_options(argc, argv, debug_options, debug_usage, 32 PARSE_OPT_STOP_AT_NON_OPTION); 33 if (argc != 0) 34 kvm_debug_help(); 35 } 36 } 37 38 void kvm_debug_help(void) 39 { 40 usage_with_options(debug_usage, debug_options); 41 } 42 43 static int do_debug(const char *name, int pid) 44 { 45 return kill(pid, SIGQUIT); 46 } 47 48 int kvm_cmd_debug(int argc, const char **argv, const char *prefix) 49 { 50 parse_debug_options(argc, argv); 51 52 if (all) 53 return kvm__enumerate_instances(do_debug); 54 55 if (instance_name == NULL && 56 instance_pid == 0) 57 kvm_debug_help(); 58 59 if (instance_name) 60 instance_pid = kvm__get_pid_by_instance(argv[0]); 61 62 if (instance_pid <= 0) 63 die("Failed locating instance"); 64 65 return kill(instance_pid, SIGQUIT); 66 } 67