1 /*
2  * QEMU monitor
3  *
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "monitor/qdev.h"
28 #include "hw/usb.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
33 #include "net/net.h"
34 #include "net/slirp.h"
35 #include "ui/qemu-spice.h"
36 #include "qemu/config-file.h"
37 #include "qemu/ctype.h"
38 #include "ui/console.h"
39 #include "ui/input.h"
40 #include "audio/audio.h"
41 #include "disas/disas.h"
42 #include "sysemu/balloon.h"
43 #include "qemu/timer.h"
44 #include "sysemu/hw_accel.h"
45 #include "sysemu/runstate.h"
46 #include "authz/list.h"
47 #include "qapi/util.h"
48 #include "sysemu/blockdev.h"
49 #include "sysemu/sysemu.h"
50 #include "sysemu/tcg.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qdict.h"
53 #include "qapi/qmp/qerror.h"
54 #include "qapi/qmp/qstring.h"
55 #include "qom/object_interfaces.h"
56 #include "trace/control.h"
57 #include "monitor/hmp-target.h"
58 #include "monitor/hmp.h"
59 #ifdef CONFIG_TRACE_SIMPLE
60 #include "trace/simple.h"
61 #endif
62 #include "exec/memory.h"
63 #include "exec/exec-all.h"
64 #include "qemu/option.h"
65 #include "qemu/thread.h"
66 #include "block/qapi.h"
67 #include "block/block-hmp-cmds.h"
68 #include "qapi/qapi-commands-char.h"
69 #include "qapi/qapi-commands-control.h"
70 #include "qapi/qapi-commands-migration.h"
71 #include "qapi/qapi-commands-misc.h"
72 #include "qapi/qapi-commands-qom.h"
73 #include "qapi/qapi-commands-run-state.h"
74 #include "qapi/qapi-commands-trace.h"
75 #include "qapi/qapi-commands-machine.h"
76 #include "qapi/qapi-init-commands.h"
77 #include "qapi/error.h"
78 #include "qapi/qmp-event.h"
79 #include "sysemu/cpus.h"
80 #include "qemu/cutils.h"
81 
82 #if defined(TARGET_S390X)
83 #include "hw/s390x/storage-keys.h"
84 #include "hw/s390x/storage-attributes.h"
85 #endif
86 
87 /* file descriptors passed via SCM_RIGHTS */
88 typedef struct mon_fd_t mon_fd_t;
89 struct mon_fd_t {
90     char *name;
91     int fd;
92     QLIST_ENTRY(mon_fd_t) next;
93 };
94 
95 /* file descriptor associated with a file descriptor set */
96 typedef struct MonFdsetFd MonFdsetFd;
97 struct MonFdsetFd {
98     int fd;
99     bool removed;
100     char *opaque;
101     QLIST_ENTRY(MonFdsetFd) next;
102 };
103 
104 /* file descriptor set containing fds passed via SCM_RIGHTS */
105 typedef struct MonFdset MonFdset;
106 struct MonFdset {
107     int64_t id;
108     QLIST_HEAD(, MonFdsetFd) fds;
109     QLIST_HEAD(, MonFdsetFd) dup_fds;
110     QLIST_ENTRY(MonFdset) next;
111 };
112 
113 /* Protects mon_fdsets */
114 static QemuMutex mon_fdsets_lock;
115 static QLIST_HEAD(, MonFdset) mon_fdsets;
116 
117 static HMPCommand hmp_info_cmds[];
118 
qmp_human_monitor_command(const char * command_line,bool has_cpu_index,int64_t cpu_index,Error ** errp)119 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
120                                 int64_t cpu_index, Error **errp)
121 {
122     char *output = NULL;
123     MonitorHMP hmp = {};
124 
125     monitor_data_init(&hmp.common, false, true, false);
126 
127     if (has_cpu_index) {
128         int ret = monitor_set_cpu(&hmp.common, cpu_index);
129         if (ret < 0) {
130             error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
131                        "a CPU number");
132             goto out;
133         }
134     }
135 
136     handle_hmp_command(&hmp, command_line);
137 
138     WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
139         output = g_strdup(hmp.common.outbuf->str);
140     }
141 
142 out:
143     monitor_data_destroy(&hmp.common);
144     return output;
145 }
146 
147 /**
148  * Is @name in the '|' separated list of names @list?
149  */
hmp_compare_cmd(const char * name,const char * list)150 int hmp_compare_cmd(const char *name, const char *list)
151 {
152     const char *p, *pstart;
153     int len;
154     len = strlen(name);
155     p = list;
156     for (;;) {
157         pstart = p;
158         p = qemu_strchrnul(p, '|');
159         if ((p - pstart) == len && !memcmp(pstart, name, len)) {
160             return 1;
161         }
162         if (*p == '\0') {
163             break;
164         }
165         p++;
166     }
167     return 0;
168 }
169 
do_help_cmd(Monitor * mon,const QDict * qdict)170 static void do_help_cmd(Monitor *mon, const QDict *qdict)
171 {
172     help_cmd(mon, qdict_get_try_str(qdict, "name"));
173 }
174 
hmp_trace_event(Monitor * mon,const QDict * qdict)175 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
176 {
177     const char *tp_name = qdict_get_str(qdict, "name");
178     bool new_state = qdict_get_bool(qdict, "option");
179     bool has_vcpu = qdict_haskey(qdict, "vcpu");
180     int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
181     Error *local_err = NULL;
182 
183     if (vcpu < 0) {
184         monitor_printf(mon, "argument vcpu must be positive");
185         return;
186     }
187 
188     qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
189     if (local_err) {
190         error_report_err(local_err);
191     }
192 }
193 
194 #ifdef CONFIG_TRACE_SIMPLE
hmp_trace_file(Monitor * mon,const QDict * qdict)195 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
196 {
197     const char *op = qdict_get_try_str(qdict, "op");
198     const char *arg = qdict_get_try_str(qdict, "arg");
199 
200     if (!op) {
201         st_print_trace_file_status();
202     } else if (!strcmp(op, "on")) {
203         st_set_trace_file_enabled(true);
204     } else if (!strcmp(op, "off")) {
205         st_set_trace_file_enabled(false);
206     } else if (!strcmp(op, "flush")) {
207         st_flush_trace_buffer();
208     } else if (!strcmp(op, "set")) {
209         if (arg) {
210             st_set_trace_file(arg);
211         }
212     } else {
213         monitor_printf(mon, "unexpected argument \"%s\"\n", op);
214         help_cmd(mon, "trace-file");
215     }
216 }
217 #endif
218 
hmp_info_help(Monitor * mon,const QDict * qdict)219 static void hmp_info_help(Monitor *mon, const QDict *qdict)
220 {
221     help_cmd(mon, "info");
222 }
223 
monitor_init_qmp_commands(void)224 static void monitor_init_qmp_commands(void)
225 {
226     /*
227      * Two command lists:
228      * - qmp_commands contains all QMP commands
229      * - qmp_cap_negotiation_commands contains just
230      *   "qmp_capabilities", to enforce capability negotiation
231      */
232 
233     qmp_init_marshal(&qmp_commands);
234 
235     qmp_register_command(&qmp_commands, "device_add",
236                          qmp_device_add, 0, 0);
237 
238     QTAILQ_INIT(&qmp_cap_negotiation_commands);
239     qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
240                          qmp_marshal_qmp_capabilities,
241                          QCO_ALLOW_PRECONFIG, 0);
242 }
243 
244 /* Set the current CPU defined by the user. Callers must hold BQL. */
monitor_set_cpu(Monitor * mon,int cpu_index)245 int monitor_set_cpu(Monitor *mon, int cpu_index)
246 {
247     CPUState *cpu;
248 
249     cpu = qemu_get_cpu(cpu_index);
250     if (cpu == NULL) {
251         return -1;
252     }
253     g_free(mon->mon_cpu_path);
254     mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
255     return 0;
256 }
257 
258 /* Callers must hold BQL. */
mon_get_cpu_sync(Monitor * mon,bool synchronize)259 static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
260 {
261     CPUState *cpu = NULL;
262 
263     if (mon->mon_cpu_path) {
264         cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
265                                                     TYPE_CPU, NULL);
266         if (!cpu) {
267             g_free(mon->mon_cpu_path);
268             mon->mon_cpu_path = NULL;
269         }
270     }
271     if (!mon->mon_cpu_path) {
272         if (!first_cpu) {
273             return NULL;
274         }
275         monitor_set_cpu(mon, first_cpu->cpu_index);
276         cpu = first_cpu;
277     }
278     assert(cpu != NULL);
279     if (synchronize) {
280         cpu_synchronize_state(cpu);
281     }
282     return cpu;
283 }
284 
mon_get_cpu(Monitor * mon)285 CPUState *mon_get_cpu(Monitor *mon)
286 {
287     return mon_get_cpu_sync(mon, true);
288 }
289 
mon_get_cpu_env(Monitor * mon)290 CPUArchState *mon_get_cpu_env(Monitor *mon)
291 {
292     CPUState *cs = mon_get_cpu(mon);
293 
294     return cs ? cs->env_ptr : NULL;
295 }
296 
monitor_get_cpu_index(Monitor * mon)297 int monitor_get_cpu_index(Monitor *mon)
298 {
299     CPUState *cs = mon_get_cpu_sync(mon, false);
300 
301     return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
302 }
303 
hmp_info_registers(Monitor * mon,const QDict * qdict)304 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
305 {
306     bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
307     CPUState *cs;
308 
309     if (all_cpus) {
310         CPU_FOREACH(cs) {
311             monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
312             cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
313         }
314     } else {
315         cs = mon_get_cpu(mon);
316 
317         if (!cs) {
318             monitor_printf(mon, "No CPU available\n");
319             return;
320         }
321 
322         cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
323     }
324 }
325 
hmp_info_sync_profile(Monitor * mon,const QDict * qdict)326 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
327 {
328     int64_t max = qdict_get_try_int(qdict, "max", 10);
329     bool mean = qdict_get_try_bool(qdict, "mean", false);
330     bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
331     enum QSPSortBy sort_by;
332 
333     sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
334     qsp_report(max, sort_by, coalesce);
335 }
336 
hmp_info_history(Monitor * mon,const QDict * qdict)337 static void hmp_info_history(Monitor *mon, const QDict *qdict)
338 {
339     MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
340     int i;
341     const char *str;
342 
343     if (!hmp_mon->rs) {
344         return;
345     }
346     i = 0;
347     for(;;) {
348         str = readline_get_history(hmp_mon->rs, i);
349         if (!str) {
350             break;
351         }
352         monitor_printf(mon, "%d: '%s'\n", i, str);
353         i++;
354     }
355 }
356 
hmp_info_trace_events(Monitor * mon,const QDict * qdict)357 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
358 {
359     const char *name = qdict_get_try_str(qdict, "name");
360     bool has_vcpu = qdict_haskey(qdict, "vcpu");
361     int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
362     TraceEventInfoList *events;
363     TraceEventInfoList *elem;
364     Error *local_err = NULL;
365 
366     if (name == NULL) {
367         name = "*";
368     }
369     if (vcpu < 0) {
370         monitor_printf(mon, "argument vcpu must be positive");
371         return;
372     }
373 
374     events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
375     if (local_err) {
376         error_report_err(local_err);
377         return;
378     }
379 
380     for (elem = events; elem != NULL; elem = elem->next) {
381         monitor_printf(mon, "%s : state %u\n",
382                        elem->value->name,
383                        elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
384     }
385     qapi_free_TraceEventInfoList(events);
386 }
387 
qmp_client_migrate_info(const char * protocol,const char * hostname,bool has_port,int64_t port,bool has_tls_port,int64_t tls_port,bool has_cert_subject,const char * cert_subject,Error ** errp)388 void qmp_client_migrate_info(const char *protocol, const char *hostname,
389                              bool has_port, int64_t port,
390                              bool has_tls_port, int64_t tls_port,
391                              bool has_cert_subject, const char *cert_subject,
392                              Error **errp)
393 {
394     if (strcmp(protocol, "spice") == 0) {
395         if (!qemu_using_spice(errp)) {
396             return;
397         }
398 
399         if (!has_port && !has_tls_port) {
400             error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
401             return;
402         }
403 
404         if (qemu_spice.migrate_info(hostname,
405                                     has_port ? port : -1,
406                                     has_tls_port ? tls_port : -1,
407                                     cert_subject)) {
408             error_setg(errp, "Could not set up display for migration");
409             return;
410         }
411         return;
412     }
413 
414     error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
415 }
416 
hmp_logfile(Monitor * mon,const QDict * qdict)417 static void hmp_logfile(Monitor *mon, const QDict *qdict)
418 {
419     Error *err = NULL;
420 
421     qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
422     if (err) {
423         error_report_err(err);
424     }
425 }
426 
hmp_log(Monitor * mon,const QDict * qdict)427 static void hmp_log(Monitor *mon, const QDict *qdict)
428 {
429     int mask;
430     const char *items = qdict_get_str(qdict, "items");
431 
432     if (!strcmp(items, "none")) {
433         mask = 0;
434     } else {
435         mask = qemu_str_to_log_mask(items);
436         if (!mask) {
437             help_cmd(mon, "log");
438             return;
439         }
440     }
441     qemu_set_log(mask);
442 }
443 
hmp_singlestep(Monitor * mon,const QDict * qdict)444 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
445 {
446     const char *option = qdict_get_try_str(qdict, "option");
447     if (!option || !strcmp(option, "on")) {
448         singlestep = 1;
449     } else if (!strcmp(option, "off")) {
450         singlestep = 0;
451     } else {
452         monitor_printf(mon, "unexpected option %s\n", option);
453     }
454 }
455 
hmp_gdbserver(Monitor * mon,const QDict * qdict)456 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
457 {
458     const char *device = qdict_get_try_str(qdict, "device");
459     if (!device) {
460         device = "tcp::" DEFAULT_GDBSTUB_PORT;
461     }
462 
463     if (gdbserver_start(device) < 0) {
464         monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
465                        device);
466     } else if (strcmp(device, "none") == 0) {
467         monitor_printf(mon, "Disabled gdbserver\n");
468     } else {
469         monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
470                        device);
471     }
472 }
473 
hmp_watchdog_action(Monitor * mon,const QDict * qdict)474 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
475 {
476     Error *err = NULL;
477     WatchdogAction action;
478     char *qapi_value;
479 
480     qapi_value = g_ascii_strdown(qdict_get_str(qdict, "action"), -1);
481     action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, &err);
482     g_free(qapi_value);
483     if (err) {
484         hmp_handle_error(mon, err);
485         return;
486     }
487     qmp_watchdog_set_action(action, &error_abort);
488 }
489 
monitor_printc(Monitor * mon,int c)490 static void monitor_printc(Monitor *mon, int c)
491 {
492     monitor_printf(mon, "'");
493     switch(c) {
494     case '\'':
495         monitor_printf(mon, "\\'");
496         break;
497     case '\\':
498         monitor_printf(mon, "\\\\");
499         break;
500     case '\n':
501         monitor_printf(mon, "\\n");
502         break;
503     case '\r':
504         monitor_printf(mon, "\\r");
505         break;
506     default:
507         if (c >= 32 && c <= 126) {
508             monitor_printf(mon, "%c", c);
509         } else {
510             monitor_printf(mon, "\\x%02x", c);
511         }
512         break;
513     }
514     monitor_printf(mon, "'");
515 }
516 
memory_dump(Monitor * mon,int count,int format,int wsize,hwaddr addr,int is_physical)517 static void memory_dump(Monitor *mon, int count, int format, int wsize,
518                         hwaddr addr, int is_physical)
519 {
520     int l, line_size, i, max_digits, len;
521     uint8_t buf[16];
522     uint64_t v;
523     CPUState *cs = mon_get_cpu(mon);
524 
525     if (!cs && (format == 'i' || !is_physical)) {
526         monitor_printf(mon, "Can not dump without CPU\n");
527         return;
528     }
529 
530     if (format == 'i') {
531         monitor_disas(mon, cs, addr, count, is_physical);
532         return;
533     }
534 
535     len = wsize * count;
536     if (wsize == 1) {
537         line_size = 8;
538     } else {
539         line_size = 16;
540     }
541     max_digits = 0;
542 
543     switch(format) {
544     case 'o':
545         max_digits = DIV_ROUND_UP(wsize * 8, 3);
546         break;
547     default:
548     case 'x':
549         max_digits = (wsize * 8) / 4;
550         break;
551     case 'u':
552     case 'd':
553         max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
554         break;
555     case 'c':
556         wsize = 1;
557         break;
558     }
559 
560     while (len > 0) {
561         if (is_physical) {
562             monitor_printf(mon, TARGET_FMT_plx ":", addr);
563         } else {
564             monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
565         }
566         l = len;
567         if (l > line_size)
568             l = line_size;
569         if (is_physical) {
570             AddressSpace *as = cs ? cs->as : &address_space_memory;
571             MemTxResult r = address_space_read(as, addr,
572                                                MEMTXATTRS_UNSPECIFIED, buf, l);
573             if (r != MEMTX_OK) {
574                 monitor_printf(mon, " Cannot access memory\n");
575                 break;
576             }
577         } else {
578             if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
579                 monitor_printf(mon, " Cannot access memory\n");
580                 break;
581             }
582         }
583         i = 0;
584         while (i < l) {
585             switch(wsize) {
586             default:
587             case 1:
588                 v = ldub_p(buf + i);
589                 break;
590             case 2:
591                 v = lduw_p(buf + i);
592                 break;
593             case 4:
594                 v = (uint32_t)ldl_p(buf + i);
595                 break;
596             case 8:
597                 v = ldq_p(buf + i);
598                 break;
599             }
600             monitor_printf(mon, " ");
601             switch(format) {
602             case 'o':
603                 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
604                 break;
605             case 'x':
606                 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
607                 break;
608             case 'u':
609                 monitor_printf(mon, "%*" PRIu64, max_digits, v);
610                 break;
611             case 'd':
612                 monitor_printf(mon, "%*" PRId64, max_digits, v);
613                 break;
614             case 'c':
615                 monitor_printc(mon, v);
616                 break;
617             }
618             i += wsize;
619         }
620         monitor_printf(mon, "\n");
621         addr += l;
622         len -= l;
623     }
624 }
625 
hmp_memory_dump(Monitor * mon,const QDict * qdict)626 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
627 {
628     int count = qdict_get_int(qdict, "count");
629     int format = qdict_get_int(qdict, "format");
630     int size = qdict_get_int(qdict, "size");
631     target_long addr = qdict_get_int(qdict, "addr");
632 
633     memory_dump(mon, count, format, size, addr, 0);
634 }
635 
hmp_physical_memory_dump(Monitor * mon,const QDict * qdict)636 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
637 {
638     int count = qdict_get_int(qdict, "count");
639     int format = qdict_get_int(qdict, "format");
640     int size = qdict_get_int(qdict, "size");
641     hwaddr addr = qdict_get_int(qdict, "addr");
642 
643     memory_dump(mon, count, format, size, addr, 1);
644 }
645 
gpa2hva(MemoryRegion ** p_mr,hwaddr addr,uint64_t size,Error ** errp)646 void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
647 {
648     Int128 gpa_region_size;
649     MemoryRegionSection mrs = memory_region_find(get_system_memory(),
650                                                  addr, size);
651 
652     if (!mrs.mr) {
653         error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
654         return NULL;
655     }
656 
657     if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
658         error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
659         memory_region_unref(mrs.mr);
660         return NULL;
661     }
662 
663     gpa_region_size = int128_make64(size);
664     if (int128_lt(mrs.size, gpa_region_size)) {
665         error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
666                    " exceeded.", addr);
667         memory_region_unref(mrs.mr);
668         return NULL;
669     }
670 
671     *p_mr = mrs.mr;
672     return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
673 }
674 
hmp_gpa2hva(Monitor * mon,const QDict * qdict)675 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
676 {
677     hwaddr addr = qdict_get_int(qdict, "addr");
678     Error *local_err = NULL;
679     MemoryRegion *mr = NULL;
680     void *ptr;
681 
682     ptr = gpa2hva(&mr, addr, 1, &local_err);
683     if (local_err) {
684         error_report_err(local_err);
685         return;
686     }
687 
688     monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
689                    " (%s) is %p\n",
690                    addr, mr->name, ptr);
691 
692     memory_region_unref(mr);
693 }
694 
hmp_gva2gpa(Monitor * mon,const QDict * qdict)695 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
696 {
697     target_ulong addr = qdict_get_int(qdict, "addr");
698     MemTxAttrs attrs;
699     CPUState *cs = mon_get_cpu(mon);
700     hwaddr gpa;
701 
702     if (!cs) {
703         monitor_printf(mon, "No cpu\n");
704         return;
705     }
706 
707     gpa  = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
708     if (gpa == -1) {
709         monitor_printf(mon, "Unmapped\n");
710     } else {
711         monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
712                        gpa + (addr & ~TARGET_PAGE_MASK));
713     }
714 }
715 
716 #ifdef CONFIG_LINUX
vtop(void * ptr,Error ** errp)717 static uint64_t vtop(void *ptr, Error **errp)
718 {
719     uint64_t pinfo;
720     uint64_t ret = -1;
721     uintptr_t addr = (uintptr_t) ptr;
722     uintptr_t pagesize = qemu_real_host_page_size;
723     off_t offset = addr / pagesize * sizeof(pinfo);
724     int fd;
725 
726     fd = open("/proc/self/pagemap", O_RDONLY);
727     if (fd == -1) {
728         error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
729         return -1;
730     }
731 
732     /* Force copy-on-write if necessary.  */
733     qatomic_add((uint8_t *)ptr, 0);
734 
735     if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
736         error_setg_errno(errp, errno, "Cannot read pagemap");
737         goto out;
738     }
739     if ((pinfo & (1ull << 63)) == 0) {
740         error_setg(errp, "Page not present");
741         goto out;
742     }
743     ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
744 
745 out:
746     close(fd);
747     return ret;
748 }
749 
hmp_gpa2hpa(Monitor * mon,const QDict * qdict)750 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
751 {
752     hwaddr addr = qdict_get_int(qdict, "addr");
753     Error *local_err = NULL;
754     MemoryRegion *mr = NULL;
755     void *ptr;
756     uint64_t physaddr;
757 
758     ptr = gpa2hva(&mr, addr, 1, &local_err);
759     if (local_err) {
760         error_report_err(local_err);
761         return;
762     }
763 
764     physaddr = vtop(ptr, &local_err);
765     if (local_err) {
766         error_report_err(local_err);
767     } else {
768         monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
769                        " (%s) is 0x%" PRIx64 "\n",
770                        addr, mr->name, (uint64_t) physaddr);
771     }
772 
773     memory_region_unref(mr);
774 }
775 #endif
776 
do_print(Monitor * mon,const QDict * qdict)777 static void do_print(Monitor *mon, const QDict *qdict)
778 {
779     int format = qdict_get_int(qdict, "format");
780     hwaddr val = qdict_get_int(qdict, "val");
781 
782     switch(format) {
783     case 'o':
784         monitor_printf(mon, "%#" HWADDR_PRIo, val);
785         break;
786     case 'x':
787         monitor_printf(mon, "%#" HWADDR_PRIx, val);
788         break;
789     case 'u':
790         monitor_printf(mon, "%" HWADDR_PRIu, val);
791         break;
792     default:
793     case 'd':
794         monitor_printf(mon, "%" HWADDR_PRId, val);
795         break;
796     case 'c':
797         monitor_printc(mon, val);
798         break;
799     }
800     monitor_printf(mon, "\n");
801 }
802 
hmp_sum(Monitor * mon,const QDict * qdict)803 static void hmp_sum(Monitor *mon, const QDict *qdict)
804 {
805     uint32_t addr;
806     uint16_t sum;
807     uint32_t start = qdict_get_int(qdict, "start");
808     uint32_t size = qdict_get_int(qdict, "size");
809 
810     sum = 0;
811     for(addr = start; addr < (start + size); addr++) {
812         uint8_t val = address_space_ldub(&address_space_memory, addr,
813                                          MEMTXATTRS_UNSPECIFIED, NULL);
814         /* BSD sum algorithm ('sum' Unix command) */
815         sum = (sum >> 1) | (sum << 15);
816         sum += val;
817     }
818     monitor_printf(mon, "%05d\n", sum);
819 }
820 
821 static int mouse_button_state;
822 
hmp_mouse_move(Monitor * mon,const QDict * qdict)823 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
824 {
825     int dx, dy, dz, button;
826     const char *dx_str = qdict_get_str(qdict, "dx_str");
827     const char *dy_str = qdict_get_str(qdict, "dy_str");
828     const char *dz_str = qdict_get_try_str(qdict, "dz_str");
829 
830     dx = strtol(dx_str, NULL, 0);
831     dy = strtol(dy_str, NULL, 0);
832     qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
833     qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
834 
835     if (dz_str) {
836         dz = strtol(dz_str, NULL, 0);
837         if (dz != 0) {
838             button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
839             qemu_input_queue_btn(NULL, button, true);
840             qemu_input_event_sync();
841             qemu_input_queue_btn(NULL, button, false);
842         }
843     }
844     qemu_input_event_sync();
845 }
846 
hmp_mouse_button(Monitor * mon,const QDict * qdict)847 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
848 {
849     static uint32_t bmap[INPUT_BUTTON__MAX] = {
850         [INPUT_BUTTON_LEFT]       = MOUSE_EVENT_LBUTTON,
851         [INPUT_BUTTON_MIDDLE]     = MOUSE_EVENT_MBUTTON,
852         [INPUT_BUTTON_RIGHT]      = MOUSE_EVENT_RBUTTON,
853     };
854     int button_state = qdict_get_int(qdict, "button_state");
855 
856     if (mouse_button_state == button_state) {
857         return;
858     }
859     qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
860     qemu_input_event_sync();
861     mouse_button_state = button_state;
862 }
863 
hmp_ioport_read(Monitor * mon,const QDict * qdict)864 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
865 {
866     int size = qdict_get_int(qdict, "size");
867     int addr = qdict_get_int(qdict, "addr");
868     int has_index = qdict_haskey(qdict, "index");
869     uint32_t val;
870     int suffix;
871 
872     if (has_index) {
873         int index = qdict_get_int(qdict, "index");
874         cpu_outb(addr & IOPORTS_MASK, index & 0xff);
875         addr++;
876     }
877     addr &= 0xffff;
878 
879     switch(size) {
880     default:
881     case 1:
882         val = cpu_inb(addr);
883         suffix = 'b';
884         break;
885     case 2:
886         val = cpu_inw(addr);
887         suffix = 'w';
888         break;
889     case 4:
890         val = cpu_inl(addr);
891         suffix = 'l';
892         break;
893     }
894     monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
895                    suffix, addr, size * 2, val);
896 }
897 
hmp_ioport_write(Monitor * mon,const QDict * qdict)898 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
899 {
900     int size = qdict_get_int(qdict, "size");
901     int addr = qdict_get_int(qdict, "addr");
902     int val = qdict_get_int(qdict, "val");
903 
904     addr &= IOPORTS_MASK;
905 
906     switch (size) {
907     default:
908     case 1:
909         cpu_outb(addr, val);
910         break;
911     case 2:
912         cpu_outw(addr, val);
913         break;
914     case 4:
915         cpu_outl(addr, val);
916         break;
917     }
918 }
919 
hmp_boot_set(Monitor * mon,const QDict * qdict)920 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
921 {
922     Error *local_err = NULL;
923     const char *bootdevice = qdict_get_str(qdict, "bootdevice");
924 
925     qemu_boot_set(bootdevice, &local_err);
926     if (local_err) {
927         error_report_err(local_err);
928     } else {
929         monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
930     }
931 }
932 
hmp_info_mtree(Monitor * mon,const QDict * qdict)933 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
934 {
935     bool flatview = qdict_get_try_bool(qdict, "flatview", false);
936     bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
937     bool owner = qdict_get_try_bool(qdict, "owner", false);
938     bool disabled = qdict_get_try_bool(qdict, "disabled", false);
939 
940     mtree_info(flatview, dispatch_tree, owner, disabled);
941 }
942 
943 /* Capture support */
944 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
945 
hmp_info_capture(Monitor * mon,const QDict * qdict)946 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
947 {
948     int i;
949     CaptureState *s;
950 
951     for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
952         monitor_printf(mon, "[%d]: ", i);
953         s->ops.info (s->opaque);
954     }
955 }
956 
hmp_stopcapture(Monitor * mon,const QDict * qdict)957 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
958 {
959     int i;
960     int n = qdict_get_int(qdict, "n");
961     CaptureState *s;
962 
963     for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
964         if (i == n) {
965             s->ops.destroy (s->opaque);
966             QLIST_REMOVE (s, entries);
967             g_free (s);
968             return;
969         }
970     }
971 }
972 
hmp_wavcapture(Monitor * mon,const QDict * qdict)973 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
974 {
975     const char *path = qdict_get_str(qdict, "path");
976     int freq = qdict_get_try_int(qdict, "freq", 44100);
977     int bits = qdict_get_try_int(qdict, "bits", 16);
978     int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
979     const char *audiodev = qdict_get_str(qdict, "audiodev");
980     CaptureState *s;
981     AudioState *as = audio_state_by_name(audiodev);
982 
983     if (!as) {
984         monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
985         return;
986     }
987 
988     s = g_malloc0 (sizeof (*s));
989 
990     if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
991         monitor_printf(mon, "Failed to add wave capture\n");
992         g_free (s);
993         return;
994     }
995     QLIST_INSERT_HEAD (&capture_head, s, entries);
996 }
997 
qmp_getfd(const char * fdname,Error ** errp)998 void qmp_getfd(const char *fdname, Error **errp)
999 {
1000     Monitor *cur_mon = monitor_cur();
1001     mon_fd_t *monfd;
1002     int fd, tmp_fd;
1003 
1004     fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1005     if (fd == -1) {
1006         error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1007         return;
1008     }
1009 
1010     if (qemu_isdigit(fdname[0])) {
1011         close(fd);
1012         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1013                    "a name not starting with a digit");
1014         return;
1015     }
1016 
1017     QEMU_LOCK_GUARD(&cur_mon->mon_lock);
1018     QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1019         if (strcmp(monfd->name, fdname) != 0) {
1020             continue;
1021         }
1022 
1023         tmp_fd = monfd->fd;
1024         monfd->fd = fd;
1025         /* Make sure close() is outside critical section */
1026         close(tmp_fd);
1027         return;
1028     }
1029 
1030     monfd = g_malloc0(sizeof(mon_fd_t));
1031     monfd->name = g_strdup(fdname);
1032     monfd->fd = fd;
1033 
1034     QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1035 }
1036 
qmp_closefd(const char * fdname,Error ** errp)1037 void qmp_closefd(const char *fdname, Error **errp)
1038 {
1039     Monitor *cur_mon = monitor_cur();
1040     mon_fd_t *monfd;
1041     int tmp_fd;
1042 
1043     qemu_mutex_lock(&cur_mon->mon_lock);
1044     QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1045         if (strcmp(monfd->name, fdname) != 0) {
1046             continue;
1047         }
1048 
1049         QLIST_REMOVE(monfd, next);
1050         tmp_fd = monfd->fd;
1051         g_free(monfd->name);
1052         g_free(monfd);
1053         qemu_mutex_unlock(&cur_mon->mon_lock);
1054         /* Make sure close() is outside critical section */
1055         close(tmp_fd);
1056         return;
1057     }
1058 
1059     qemu_mutex_unlock(&cur_mon->mon_lock);
1060     error_setg(errp, "File descriptor named '%s' not found", fdname);
1061 }
1062 
monitor_get_fd(Monitor * mon,const char * fdname,Error ** errp)1063 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1064 {
1065     mon_fd_t *monfd;
1066 
1067     QEMU_LOCK_GUARD(&mon->mon_lock);
1068     QLIST_FOREACH(monfd, &mon->fds, next) {
1069         int fd;
1070 
1071         if (strcmp(monfd->name, fdname) != 0) {
1072             continue;
1073         }
1074 
1075         fd = monfd->fd;
1076 
1077         /* caller takes ownership of fd */
1078         QLIST_REMOVE(monfd, next);
1079         g_free(monfd->name);
1080         g_free(monfd);
1081 
1082         return fd;
1083     }
1084 
1085     error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1086     return -1;
1087 }
1088 
monitor_fdset_cleanup(MonFdset * mon_fdset)1089 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1090 {
1091     MonFdsetFd *mon_fdset_fd;
1092     MonFdsetFd *mon_fdset_fd_next;
1093 
1094     QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1095         if ((mon_fdset_fd->removed ||
1096                 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1097                 runstate_is_running()) {
1098             close(mon_fdset_fd->fd);
1099             g_free(mon_fdset_fd->opaque);
1100             QLIST_REMOVE(mon_fdset_fd, next);
1101             g_free(mon_fdset_fd);
1102         }
1103     }
1104 
1105     if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1106         QLIST_REMOVE(mon_fdset, next);
1107         g_free(mon_fdset);
1108     }
1109 }
1110 
monitor_fdsets_cleanup(void)1111 void monitor_fdsets_cleanup(void)
1112 {
1113     MonFdset *mon_fdset;
1114     MonFdset *mon_fdset_next;
1115 
1116     QEMU_LOCK_GUARD(&mon_fdsets_lock);
1117     QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1118         monitor_fdset_cleanup(mon_fdset);
1119     }
1120 }
1121 
qmp_add_fd(bool has_fdset_id,int64_t fdset_id,bool has_opaque,const char * opaque,Error ** errp)1122 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1123                       const char *opaque, Error **errp)
1124 {
1125     int fd;
1126     Monitor *mon = monitor_cur();
1127     AddfdInfo *fdinfo;
1128 
1129     fd = qemu_chr_fe_get_msgfd(&mon->chr);
1130     if (fd == -1) {
1131         error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1132         goto error;
1133     }
1134 
1135     fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1136                                   has_opaque, opaque, errp);
1137     if (fdinfo) {
1138         return fdinfo;
1139     }
1140 
1141 error:
1142     if (fd != -1) {
1143         close(fd);
1144     }
1145     return NULL;
1146 }
1147 
qmp_remove_fd(int64_t fdset_id,bool has_fd,int64_t fd,Error ** errp)1148 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1149 {
1150     MonFdset *mon_fdset;
1151     MonFdsetFd *mon_fdset_fd;
1152     char fd_str[60];
1153 
1154     QEMU_LOCK_GUARD(&mon_fdsets_lock);
1155     QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1156         if (mon_fdset->id != fdset_id) {
1157             continue;
1158         }
1159         QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1160             if (has_fd) {
1161                 if (mon_fdset_fd->fd != fd) {
1162                     continue;
1163                 }
1164                 mon_fdset_fd->removed = true;
1165                 break;
1166             } else {
1167                 mon_fdset_fd->removed = true;
1168             }
1169         }
1170         if (has_fd && !mon_fdset_fd) {
1171             goto error;
1172         }
1173         monitor_fdset_cleanup(mon_fdset);
1174         return;
1175     }
1176 
1177 error:
1178     if (has_fd) {
1179         snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1180                  fdset_id, fd);
1181     } else {
1182         snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1183     }
1184     error_setg(errp, "File descriptor named '%s' not found", fd_str);
1185 }
1186 
qmp_query_fdsets(Error ** errp)1187 FdsetInfoList *qmp_query_fdsets(Error **errp)
1188 {
1189     MonFdset *mon_fdset;
1190     MonFdsetFd *mon_fdset_fd;
1191     FdsetInfoList *fdset_list = NULL;
1192 
1193     QEMU_LOCK_GUARD(&mon_fdsets_lock);
1194     QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1195         FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
1196 
1197         fdset_info->fdset_id = mon_fdset->id;
1198 
1199         QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1200             FdsetFdInfo *fdsetfd_info;
1201 
1202             fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1203             fdsetfd_info->fd = mon_fdset_fd->fd;
1204             if (mon_fdset_fd->opaque) {
1205                 fdsetfd_info->has_opaque = true;
1206                 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
1207             } else {
1208                 fdsetfd_info->has_opaque = false;
1209             }
1210 
1211             QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
1212         }
1213 
1214         QAPI_LIST_PREPEND(fdset_list, fdset_info);
1215     }
1216 
1217     return fdset_list;
1218 }
1219 
monitor_fdset_add_fd(int fd,bool has_fdset_id,int64_t fdset_id,bool has_opaque,const char * opaque,Error ** errp)1220 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1221                                 bool has_opaque, const char *opaque,
1222                                 Error **errp)
1223 {
1224     MonFdset *mon_fdset = NULL;
1225     MonFdsetFd *mon_fdset_fd;
1226     AddfdInfo *fdinfo;
1227 
1228     QEMU_LOCK_GUARD(&mon_fdsets_lock);
1229     if (has_fdset_id) {
1230         QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1231             /* Break if match found or match impossible due to ordering by ID */
1232             if (fdset_id <= mon_fdset->id) {
1233                 if (fdset_id < mon_fdset->id) {
1234                     mon_fdset = NULL;
1235                 }
1236                 break;
1237             }
1238         }
1239     }
1240 
1241     if (mon_fdset == NULL) {
1242         int64_t fdset_id_prev = -1;
1243         MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1244 
1245         if (has_fdset_id) {
1246             if (fdset_id < 0) {
1247                 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1248                            "a non-negative value");
1249                 return NULL;
1250             }
1251             /* Use specified fdset ID */
1252             QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1253                 mon_fdset_cur = mon_fdset;
1254                 if (fdset_id < mon_fdset_cur->id) {
1255                     break;
1256                 }
1257             }
1258         } else {
1259             /* Use first available fdset ID */
1260             QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1261                 mon_fdset_cur = mon_fdset;
1262                 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1263                     fdset_id_prev = mon_fdset_cur->id;
1264                     continue;
1265                 }
1266                 break;
1267             }
1268         }
1269 
1270         mon_fdset = g_malloc0(sizeof(*mon_fdset));
1271         if (has_fdset_id) {
1272             mon_fdset->id = fdset_id;
1273         } else {
1274             mon_fdset->id = fdset_id_prev + 1;
1275         }
1276 
1277         /* The fdset list is ordered by fdset ID */
1278         if (!mon_fdset_cur) {
1279             QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1280         } else if (mon_fdset->id < mon_fdset_cur->id) {
1281             QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1282         } else {
1283             QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1284         }
1285     }
1286 
1287     mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1288     mon_fdset_fd->fd = fd;
1289     mon_fdset_fd->removed = false;
1290     if (has_opaque) {
1291         mon_fdset_fd->opaque = g_strdup(opaque);
1292     }
1293     QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1294 
1295     fdinfo = g_malloc0(sizeof(*fdinfo));
1296     fdinfo->fdset_id = mon_fdset->id;
1297     fdinfo->fd = mon_fdset_fd->fd;
1298 
1299     return fdinfo;
1300 }
1301 
monitor_fdset_dup_fd_add(int64_t fdset_id,int flags)1302 int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
1303 {
1304 #ifdef _WIN32
1305     return -ENOENT;
1306 #else
1307     MonFdset *mon_fdset;
1308 
1309     QEMU_LOCK_GUARD(&mon_fdsets_lock);
1310     QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1311         MonFdsetFd *mon_fdset_fd;
1312         MonFdsetFd *mon_fdset_fd_dup;
1313         int fd = -1;
1314         int dup_fd;
1315         int mon_fd_flags;
1316 
1317         if (mon_fdset->id != fdset_id) {
1318             continue;
1319         }
1320 
1321         QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1322             mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1323             if (mon_fd_flags == -1) {
1324                 return -1;
1325             }
1326 
1327             if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1328                 fd = mon_fdset_fd->fd;
1329                 break;
1330             }
1331         }
1332 
1333         if (fd == -1) {
1334             errno = EACCES;
1335             return -1;
1336         }
1337 
1338         dup_fd = qemu_dup_flags(fd, flags);
1339         if (dup_fd == -1) {
1340             return -1;
1341         }
1342 
1343         mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1344         mon_fdset_fd_dup->fd = dup_fd;
1345         QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1346         return dup_fd;
1347     }
1348 
1349     errno = ENOENT;
1350     return -1;
1351 #endif
1352 }
1353 
monitor_fdset_dup_fd_find_remove(int dup_fd,bool remove)1354 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1355 {
1356     MonFdset *mon_fdset;
1357     MonFdsetFd *mon_fdset_fd_dup;
1358 
1359     QEMU_LOCK_GUARD(&mon_fdsets_lock);
1360     QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1361         QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1362             if (mon_fdset_fd_dup->fd == dup_fd) {
1363                 if (remove) {
1364                     QLIST_REMOVE(mon_fdset_fd_dup, next);
1365                     g_free(mon_fdset_fd_dup);
1366                     if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1367                         monitor_fdset_cleanup(mon_fdset);
1368                     }
1369                     return -1;
1370                 } else {
1371                     return mon_fdset->id;
1372                 }
1373             }
1374         }
1375     }
1376 
1377     return -1;
1378 }
1379 
monitor_fdset_dup_fd_find(int dup_fd)1380 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1381 {
1382     return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1383 }
1384 
monitor_fdset_dup_fd_remove(int dup_fd)1385 void monitor_fdset_dup_fd_remove(int dup_fd)
1386 {
1387     monitor_fdset_dup_fd_find_remove(dup_fd, true);
1388 }
1389 
monitor_fd_param(Monitor * mon,const char * fdname,Error ** errp)1390 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1391 {
1392     int fd;
1393     Error *local_err = NULL;
1394 
1395     if (!qemu_isdigit(fdname[0]) && mon) {
1396         fd = monitor_get_fd(mon, fdname, &local_err);
1397     } else {
1398         fd = qemu_parse_fd(fdname);
1399         if (fd == -1) {
1400             error_setg(&local_err, "Invalid file descriptor number '%s'",
1401                        fdname);
1402         }
1403     }
1404     if (local_err) {
1405         error_propagate(errp, local_err);
1406         assert(fd == -1);
1407     } else {
1408         assert(fd != -1);
1409     }
1410 
1411     return fd;
1412 }
1413 
1414 /* Please update hmp-commands.hx when adding or changing commands */
1415 static HMPCommand hmp_info_cmds[] = {
1416 #include "hmp-commands-info.h"
1417     { NULL, NULL, },
1418 };
1419 
1420 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1421 HMPCommand hmp_cmds[] = {
1422 #include "hmp-commands.h"
1423     { NULL, NULL, },
1424 };
1425 
1426 /*
1427  * Set @pval to the value in the register identified by @name.
1428  * return 0 if OK, -1 if not found
1429  */
get_monitor_def(Monitor * mon,int64_t * pval,const char * name)1430 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1431 {
1432     const MonitorDef *md = target_monitor_defs();
1433     CPUState *cs = mon_get_cpu(mon);
1434     void *ptr;
1435     uint64_t tmp = 0;
1436     int ret;
1437 
1438     if (cs == NULL || md == NULL) {
1439         return -1;
1440     }
1441 
1442     for(; md->name != NULL; md++) {
1443         if (hmp_compare_cmd(name, md->name)) {
1444             if (md->get_value) {
1445                 *pval = md->get_value(mon, md, md->offset);
1446             } else {
1447                 CPUArchState *env = mon_get_cpu_env(mon);
1448                 ptr = (uint8_t *)env + md->offset;
1449                 switch(md->type) {
1450                 case MD_I32:
1451                     *pval = *(int32_t *)ptr;
1452                     break;
1453                 case MD_TLONG:
1454                     *pval = *(target_long *)ptr;
1455                     break;
1456                 default:
1457                     *pval = 0;
1458                     break;
1459                 }
1460             }
1461             return 0;
1462         }
1463     }
1464 
1465     ret = target_get_monitor_def(cs, name, &tmp);
1466     if (!ret) {
1467         *pval = (target_long) tmp;
1468     }
1469 
1470     return ret;
1471 }
1472 
add_completion_option(ReadLineState * rs,const char * str,const char * option)1473 static void add_completion_option(ReadLineState *rs, const char *str,
1474                                   const char *option)
1475 {
1476     if (!str || !option) {
1477         return;
1478     }
1479     if (!strncmp(option, str, strlen(str))) {
1480         readline_add_completion(rs, option);
1481     }
1482 }
1483 
chardev_add_completion(ReadLineState * rs,int nb_args,const char * str)1484 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1485 {
1486     size_t len;
1487     ChardevBackendInfoList *list, *start;
1488 
1489     if (nb_args != 2) {
1490         return;
1491     }
1492     len = strlen(str);
1493     readline_set_completion_index(rs, len);
1494 
1495     start = list = qmp_query_chardev_backends(NULL);
1496     while (list) {
1497         const char *chr_name = list->value->name;
1498 
1499         if (!strncmp(chr_name, str, len)) {
1500             readline_add_completion(rs, chr_name);
1501         }
1502         list = list->next;
1503     }
1504     qapi_free_ChardevBackendInfoList(start);
1505 }
1506 
netdev_add_completion(ReadLineState * rs,int nb_args,const char * str)1507 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1508 {
1509     size_t len;
1510     int i;
1511 
1512     if (nb_args != 2) {
1513         return;
1514     }
1515     len = strlen(str);
1516     readline_set_completion_index(rs, len);
1517     for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1518         add_completion_option(rs, str, NetClientDriver_str(i));
1519     }
1520 }
1521 
device_add_completion(ReadLineState * rs,int nb_args,const char * str)1522 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1523 {
1524     GSList *list, *elt;
1525     size_t len;
1526 
1527     if (nb_args != 2) {
1528         return;
1529     }
1530 
1531     len = strlen(str);
1532     readline_set_completion_index(rs, len);
1533     list = elt = object_class_get_list(TYPE_DEVICE, false);
1534     while (elt) {
1535         const char *name;
1536         DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1537                                              TYPE_DEVICE);
1538         name = object_class_get_name(OBJECT_CLASS(dc));
1539 
1540         if (dc->user_creatable
1541             && !strncmp(name, str, len)) {
1542             readline_add_completion(rs, name);
1543         }
1544         elt = elt->next;
1545     }
1546     g_slist_free(list);
1547 }
1548 
object_add_completion(ReadLineState * rs,int nb_args,const char * str)1549 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1550 {
1551     GSList *list, *elt;
1552     size_t len;
1553 
1554     if (nb_args != 2) {
1555         return;
1556     }
1557 
1558     len = strlen(str);
1559     readline_set_completion_index(rs, len);
1560     list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1561     while (elt) {
1562         const char *name;
1563 
1564         name = object_class_get_name(OBJECT_CLASS(elt->data));
1565         if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1566             readline_add_completion(rs, name);
1567         }
1568         elt = elt->next;
1569     }
1570     g_slist_free(list);
1571 }
1572 
qdev_add_hotpluggable_device(Object * obj,void * opaque)1573 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1574 {
1575     GSList **list = opaque;
1576     DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
1577 
1578     if (dev == NULL) {
1579         return 0;
1580     }
1581 
1582     if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1583         *list = g_slist_append(*list, dev);
1584     }
1585 
1586     return 0;
1587 }
1588 
qdev_build_hotpluggable_device_list(Object * peripheral)1589 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1590 {
1591     GSList *list = NULL;
1592 
1593     object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1594 
1595     return list;
1596 }
1597 
peripheral_device_del_completion(ReadLineState * rs,const char * str,size_t len)1598 static void peripheral_device_del_completion(ReadLineState *rs,
1599                                              const char *str, size_t len)
1600 {
1601     Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1602     GSList *list, *item;
1603 
1604     list = qdev_build_hotpluggable_device_list(peripheral);
1605     if (!list) {
1606         return;
1607     }
1608 
1609     for (item = list; item; item = g_slist_next(item)) {
1610         DeviceState *dev = item->data;
1611 
1612         if (dev->id && !strncmp(str, dev->id, len)) {
1613             readline_add_completion(rs, dev->id);
1614         }
1615     }
1616 
1617     g_slist_free(list);
1618 }
1619 
chardev_remove_completion(ReadLineState * rs,int nb_args,const char * str)1620 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1621 {
1622     size_t len;
1623     ChardevInfoList *list, *start;
1624 
1625     if (nb_args != 2) {
1626         return;
1627     }
1628     len = strlen(str);
1629     readline_set_completion_index(rs, len);
1630 
1631     start = list = qmp_query_chardev(NULL);
1632     while (list) {
1633         ChardevInfo *chr = list->value;
1634 
1635         if (!strncmp(chr->label, str, len)) {
1636             readline_add_completion(rs, chr->label);
1637         }
1638         list = list->next;
1639     }
1640     qapi_free_ChardevInfoList(start);
1641 }
1642 
ringbuf_completion(ReadLineState * rs,const char * str)1643 static void ringbuf_completion(ReadLineState *rs, const char *str)
1644 {
1645     size_t len;
1646     ChardevInfoList *list, *start;
1647 
1648     len = strlen(str);
1649     readline_set_completion_index(rs, len);
1650 
1651     start = list = qmp_query_chardev(NULL);
1652     while (list) {
1653         ChardevInfo *chr_info = list->value;
1654 
1655         if (!strncmp(chr_info->label, str, len)) {
1656             Chardev *chr = qemu_chr_find(chr_info->label);
1657             if (chr && CHARDEV_IS_RINGBUF(chr)) {
1658                 readline_add_completion(rs, chr_info->label);
1659             }
1660         }
1661         list = list->next;
1662     }
1663     qapi_free_ChardevInfoList(start);
1664 }
1665 
ringbuf_write_completion(ReadLineState * rs,int nb_args,const char * str)1666 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1667 {
1668     if (nb_args != 2) {
1669         return;
1670     }
1671     ringbuf_completion(rs, str);
1672 }
1673 
device_del_completion(ReadLineState * rs,int nb_args,const char * str)1674 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1675 {
1676     size_t len;
1677 
1678     if (nb_args != 2) {
1679         return;
1680     }
1681 
1682     len = strlen(str);
1683     readline_set_completion_index(rs, len);
1684     peripheral_device_del_completion(rs, str, len);
1685 }
1686 
object_del_completion(ReadLineState * rs,int nb_args,const char * str)1687 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
1688 {
1689     ObjectPropertyInfoList *list, *start;
1690     size_t len;
1691 
1692     if (nb_args != 2) {
1693         return;
1694     }
1695     len = strlen(str);
1696     readline_set_completion_index(rs, len);
1697 
1698     start = list = qmp_qom_list("/objects", NULL);
1699     while (list) {
1700         ObjectPropertyInfo *info = list->value;
1701 
1702         if (!strncmp(info->type, "child<", 5)
1703             && !strncmp(info->name, str, len)) {
1704             readline_add_completion(rs, info->name);
1705         }
1706         list = list->next;
1707     }
1708     qapi_free_ObjectPropertyInfoList(start);
1709 }
1710 
sendkey_completion(ReadLineState * rs,int nb_args,const char * str)1711 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1712 {
1713     int i;
1714     char *sep;
1715     size_t len;
1716 
1717     if (nb_args != 2) {
1718         return;
1719     }
1720     sep = strrchr(str, '-');
1721     if (sep) {
1722         str = sep + 1;
1723     }
1724     len = strlen(str);
1725     readline_set_completion_index(rs, len);
1726     for (i = 0; i < Q_KEY_CODE__MAX; i++) {
1727         if (!strncmp(str, QKeyCode_str(i), len)) {
1728             readline_add_completion(rs, QKeyCode_str(i));
1729         }
1730     }
1731 }
1732 
set_link_completion(ReadLineState * rs,int nb_args,const char * str)1733 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1734 {
1735     size_t len;
1736 
1737     len = strlen(str);
1738     readline_set_completion_index(rs, len);
1739     if (nb_args == 2) {
1740         NetClientState *ncs[MAX_QUEUE_NUM];
1741         int count, i;
1742         count = qemu_find_net_clients_except(NULL, ncs,
1743                                              NET_CLIENT_DRIVER_NONE,
1744                                              MAX_QUEUE_NUM);
1745         for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1746             const char *name = ncs[i]->name;
1747             if (!strncmp(str, name, len)) {
1748                 readline_add_completion(rs, name);
1749             }
1750         }
1751     } else if (nb_args == 3) {
1752         add_completion_option(rs, str, "on");
1753         add_completion_option(rs, str, "off");
1754     }
1755 }
1756 
netdev_del_completion(ReadLineState * rs,int nb_args,const char * str)1757 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1758 {
1759     int len, count, i;
1760     NetClientState *ncs[MAX_QUEUE_NUM];
1761 
1762     if (nb_args != 2) {
1763         return;
1764     }
1765 
1766     len = strlen(str);
1767     readline_set_completion_index(rs, len);
1768     count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
1769                                          MAX_QUEUE_NUM);
1770     for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1771         const char *name = ncs[i]->name;
1772         if (strncmp(str, name, len)) {
1773             continue;
1774         }
1775         if (ncs[i]->is_netdev) {
1776             readline_add_completion(rs, name);
1777         }
1778     }
1779 }
1780 
info_trace_events_completion(ReadLineState * rs,int nb_args,const char * str)1781 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1782 {
1783     size_t len;
1784 
1785     len = strlen(str);
1786     readline_set_completion_index(rs, len);
1787     if (nb_args == 2) {
1788         TraceEventIter iter;
1789         TraceEvent *ev;
1790         char *pattern = g_strdup_printf("%s*", str);
1791         trace_event_iter_init_pattern(&iter, pattern);
1792         while ((ev = trace_event_iter_next(&iter)) != NULL) {
1793             readline_add_completion(rs, trace_event_get_name(ev));
1794         }
1795         g_free(pattern);
1796     }
1797 }
1798 
trace_event_completion(ReadLineState * rs,int nb_args,const char * str)1799 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1800 {
1801     size_t len;
1802 
1803     len = strlen(str);
1804     readline_set_completion_index(rs, len);
1805     if (nb_args == 2) {
1806         TraceEventIter iter;
1807         TraceEvent *ev;
1808         char *pattern = g_strdup_printf("%s*", str);
1809         trace_event_iter_init_pattern(&iter, pattern);
1810         while ((ev = trace_event_iter_next(&iter)) != NULL) {
1811             readline_add_completion(rs, trace_event_get_name(ev));
1812         }
1813         g_free(pattern);
1814     } else if (nb_args == 3) {
1815         add_completion_option(rs, str, "on");
1816         add_completion_option(rs, str, "off");
1817     }
1818 }
1819 
watchdog_action_completion(ReadLineState * rs,int nb_args,const char * str)1820 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1821 {
1822     int i;
1823 
1824     if (nb_args != 2) {
1825         return;
1826     }
1827     readline_set_completion_index(rs, strlen(str));
1828     for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1829         add_completion_option(rs, str, WatchdogAction_str(i));
1830     }
1831 }
1832 
migrate_set_capability_completion(ReadLineState * rs,int nb_args,const char * str)1833 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1834                                        const char *str)
1835 {
1836     size_t len;
1837 
1838     len = strlen(str);
1839     readline_set_completion_index(rs, len);
1840     if (nb_args == 2) {
1841         int i;
1842         for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1843             const char *name = MigrationCapability_str(i);
1844             if (!strncmp(str, name, len)) {
1845                 readline_add_completion(rs, name);
1846             }
1847         }
1848     } else if (nb_args == 3) {
1849         add_completion_option(rs, str, "on");
1850         add_completion_option(rs, str, "off");
1851     }
1852 }
1853 
migrate_set_parameter_completion(ReadLineState * rs,int nb_args,const char * str)1854 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1855                                       const char *str)
1856 {
1857     size_t len;
1858 
1859     len = strlen(str);
1860     readline_set_completion_index(rs, len);
1861     if (nb_args == 2) {
1862         int i;
1863         for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1864             const char *name = MigrationParameter_str(i);
1865             if (!strncmp(str, name, len)) {
1866                 readline_add_completion(rs, name);
1867             }
1868         }
1869     }
1870 }
1871 
vm_completion(ReadLineState * rs,const char * str)1872 static void vm_completion(ReadLineState *rs, const char *str)
1873 {
1874     size_t len;
1875     BlockDriverState *bs;
1876     BdrvNextIterator it;
1877 
1878     len = strlen(str);
1879     readline_set_completion_index(rs, len);
1880 
1881     for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1882         SnapshotInfoList *snapshots, *snapshot;
1883         AioContext *ctx = bdrv_get_aio_context(bs);
1884         bool ok = false;
1885 
1886         aio_context_acquire(ctx);
1887         if (bdrv_can_snapshot(bs)) {
1888             ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1889         }
1890         aio_context_release(ctx);
1891         if (!ok) {
1892             continue;
1893         }
1894 
1895         snapshot = snapshots;
1896         while (snapshot) {
1897             char *completion = snapshot->value->name;
1898             if (!strncmp(str, completion, len)) {
1899                 readline_add_completion(rs, completion);
1900             }
1901             completion = snapshot->value->id;
1902             if (!strncmp(str, completion, len)) {
1903                 readline_add_completion(rs, completion);
1904             }
1905             snapshot = snapshot->next;
1906         }
1907         qapi_free_SnapshotInfoList(snapshots);
1908     }
1909 
1910 }
1911 
delvm_completion(ReadLineState * rs,int nb_args,const char * str)1912 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1913 {
1914     if (nb_args == 2) {
1915         vm_completion(rs, str);
1916     }
1917 }
1918 
loadvm_completion(ReadLineState * rs,int nb_args,const char * str)1919 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1920 {
1921     if (nb_args == 2) {
1922         vm_completion(rs, str);
1923     }
1924 }
1925 
1926 static int
compare_mon_cmd(const void * a,const void * b)1927 compare_mon_cmd(const void *a, const void *b)
1928 {
1929     return strcmp(((const HMPCommand *)a)->name,
1930             ((const HMPCommand *)b)->name);
1931 }
1932 
sortcmdlist(void)1933 static void sortcmdlist(void)
1934 {
1935     qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1936           sizeof(*hmp_cmds),
1937           compare_mon_cmd);
1938     qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1939           sizeof(*hmp_info_cmds),
1940           compare_mon_cmd);
1941 }
1942 
monitor_register_hmp(const char * name,bool info,void (* cmd)(Monitor * mon,const QDict * qdict))1943 void monitor_register_hmp(const char *name, bool info,
1944                           void (*cmd)(Monitor *mon, const QDict *qdict))
1945 {
1946     HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1947 
1948     while (table->name != NULL) {
1949         if (strcmp(table->name, name) == 0) {
1950             g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1951             table->cmd = cmd;
1952             return;
1953         }
1954         table++;
1955     }
1956     g_assert_not_reached();
1957 }
1958 
monitor_register_hmp_info_hrt(const char * name,HumanReadableText * (* handler)(Error ** errp))1959 void monitor_register_hmp_info_hrt(const char *name,
1960                                    HumanReadableText *(*handler)(Error **errp))
1961 {
1962     HMPCommand *table = hmp_info_cmds;
1963 
1964     while (table->name != NULL) {
1965         if (strcmp(table->name, name) == 0) {
1966             g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1967             table->cmd_info_hrt = handler;
1968             return;
1969         }
1970         table++;
1971     }
1972     g_assert_not_reached();
1973 }
1974 
monitor_init_globals(void)1975 void monitor_init_globals(void)
1976 {
1977     monitor_init_globals_core();
1978     monitor_init_qmp_commands();
1979     sortcmdlist();
1980     qemu_mutex_init(&mon_fdsets_lock);
1981 }
1982