2
0

monitor.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. #include "qemu/osdep.h"
  25. #include "monitor-internal.h"
  26. #include "qapi/error.h"
  27. #include "qapi/opts-visitor.h"
  28. #include "qapi/qapi-emit-events.h"
  29. #include "qapi/qapi-visit-control.h"
  30. #include "qapi/qmp/qdict.h"
  31. #include "qapi/qmp/qstring.h"
  32. #include "qemu/error-report.h"
  33. #include "qemu/option.h"
  34. #include "sysemu/qtest.h"
  35. #include "sysemu/sysemu.h"
  36. #include "trace.h"
  37. /*
  38. * To prevent flooding clients, events can be throttled. The
  39. * throttling is calculated globally, rather than per-Monitor
  40. * instance.
  41. */
  42. typedef struct MonitorQAPIEventState {
  43. QAPIEvent event; /* Throttling state for this event type and... */
  44. QDict *data; /* ... data, see qapi_event_throttle_equal() */
  45. QEMUTimer *timer; /* Timer for handling delayed events */
  46. QDict *qdict; /* Delayed event (if any) */
  47. } MonitorQAPIEventState;
  48. typedef struct {
  49. int64_t rate; /* Minimum time (in ns) between two events */
  50. } MonitorQAPIEventConf;
  51. /* Shared monitor I/O thread */
  52. IOThread *mon_iothread;
  53. /* Bottom half to dispatch the requests received from I/O thread */
  54. QEMUBH *qmp_dispatcher_bh;
  55. /* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */
  56. QemuMutex monitor_lock;
  57. static GHashTable *monitor_qapi_event_state;
  58. MonitorList mon_list;
  59. int mon_refcount;
  60. static bool monitor_destroyed;
  61. __thread Monitor *cur_mon;
  62. /**
  63. * Is the current monitor, if any, a QMP monitor?
  64. */
  65. bool monitor_cur_is_qmp(void)
  66. {
  67. return cur_mon && monitor_is_qmp(cur_mon);
  68. }
  69. /**
  70. * Is @mon is using readline?
  71. * Note: not all HMP monitors use readline, e.g., gdbserver has a
  72. * non-interactive HMP monitor, so readline is not used there.
  73. */
  74. static inline bool monitor_uses_readline(const MonitorHMP *mon)
  75. {
  76. return mon->use_readline;
  77. }
  78. static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
  79. {
  80. if (monitor_is_qmp(mon)) {
  81. return false;
  82. }
  83. return !monitor_uses_readline(container_of(mon, MonitorHMP, common));
  84. }
  85. static void monitor_flush_locked(Monitor *mon);
  86. static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
  87. void *opaque)
  88. {
  89. Monitor *mon = opaque;
  90. qemu_mutex_lock(&mon->mon_lock);
  91. mon->out_watch = 0;
  92. monitor_flush_locked(mon);
  93. qemu_mutex_unlock(&mon->mon_lock);
  94. return FALSE;
  95. }
  96. /* Caller must hold mon->mon_lock */
  97. static void monitor_flush_locked(Monitor *mon)
  98. {
  99. int rc;
  100. size_t len;
  101. const char *buf;
  102. if (mon->skip_flush) {
  103. return;
  104. }
  105. buf = qstring_get_str(mon->outbuf);
  106. len = qstring_get_length(mon->outbuf);
  107. if (len && !mon->mux_out) {
  108. rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
  109. if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
  110. /* all flushed or error */
  111. qobject_unref(mon->outbuf);
  112. mon->outbuf = qstring_new();
  113. return;
  114. }
  115. if (rc > 0) {
  116. /* partial write */
  117. QString *tmp = qstring_from_str(buf + rc);
  118. qobject_unref(mon->outbuf);
  119. mon->outbuf = tmp;
  120. }
  121. if (mon->out_watch == 0) {
  122. mon->out_watch =
  123. qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
  124. monitor_unblocked, mon);
  125. }
  126. }
  127. }
  128. void monitor_flush(Monitor *mon)
  129. {
  130. qemu_mutex_lock(&mon->mon_lock);
  131. monitor_flush_locked(mon);
  132. qemu_mutex_unlock(&mon->mon_lock);
  133. }
  134. /* flush at every end of line */
  135. int monitor_puts(Monitor *mon, const char *str)
  136. {
  137. int i;
  138. char c;
  139. qemu_mutex_lock(&mon->mon_lock);
  140. for (i = 0; str[i]; i++) {
  141. c = str[i];
  142. if (c == '\n') {
  143. qstring_append_chr(mon->outbuf, '\r');
  144. }
  145. qstring_append_chr(mon->outbuf, c);
  146. if (c == '\n') {
  147. monitor_flush_locked(mon);
  148. }
  149. }
  150. qemu_mutex_unlock(&mon->mon_lock);
  151. return i;
  152. }
  153. int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
  154. {
  155. char *buf;
  156. int n;
  157. if (!mon) {
  158. return -1;
  159. }
  160. if (monitor_is_qmp(mon)) {
  161. return -1;
  162. }
  163. buf = g_strdup_vprintf(fmt, ap);
  164. n = monitor_puts(mon, buf);
  165. g_free(buf);
  166. return n;
  167. }
  168. int monitor_printf(Monitor *mon, const char *fmt, ...)
  169. {
  170. int ret;
  171. va_list ap;
  172. va_start(ap, fmt);
  173. ret = monitor_vprintf(mon, fmt, ap);
  174. va_end(ap);
  175. return ret;
  176. }
  177. /*
  178. * Print to current monitor if we have one, else to stderr.
  179. */
  180. int error_vprintf(const char *fmt, va_list ap)
  181. {
  182. if (cur_mon && !monitor_cur_is_qmp()) {
  183. return monitor_vprintf(cur_mon, fmt, ap);
  184. }
  185. return vfprintf(stderr, fmt, ap);
  186. }
  187. int error_vprintf_unless_qmp(const char *fmt, va_list ap)
  188. {
  189. if (!cur_mon) {
  190. return vfprintf(stderr, fmt, ap);
  191. }
  192. if (!monitor_cur_is_qmp()) {
  193. return monitor_vprintf(cur_mon, fmt, ap);
  194. }
  195. return -1;
  196. }
  197. static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
  198. /* Limit guest-triggerable events to 1 per second */
  199. [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
  200. [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
  201. [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
  202. [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
  203. [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
  204. [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
  205. [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS },
  206. };
  207. /*
  208. * Return the clock to use for recording an event's time.
  209. * It's QEMU_CLOCK_REALTIME, except for qtests it's
  210. * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
  211. * Beware: result is invalid before configure_accelerator().
  212. */
  213. static inline QEMUClockType monitor_get_event_clock(void)
  214. {
  215. return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME;
  216. }
  217. /*
  218. * Broadcast an event to all monitors.
  219. * @qdict is the event object. Its member "event" must match @event.
  220. * Caller must hold monitor_lock.
  221. */
  222. static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
  223. {
  224. Monitor *mon;
  225. MonitorQMP *qmp_mon;
  226. trace_monitor_protocol_event_emit(event, qdict);
  227. QTAILQ_FOREACH(mon, &mon_list, entry) {
  228. if (!monitor_is_qmp(mon)) {
  229. continue;
  230. }
  231. qmp_mon = container_of(mon, MonitorQMP, common);
  232. if (qmp_mon->commands != &qmp_cap_negotiation_commands) {
  233. qmp_send_response(qmp_mon, qdict);
  234. }
  235. }
  236. }
  237. static void monitor_qapi_event_handler(void *opaque);
  238. /*
  239. * Queue a new event for emission to Monitor instances,
  240. * applying any rate limiting if required.
  241. */
  242. static void
  243. monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)
  244. {
  245. MonitorQAPIEventConf *evconf;
  246. MonitorQAPIEventState *evstate;
  247. assert(event < QAPI_EVENT__MAX);
  248. evconf = &monitor_qapi_event_conf[event];
  249. trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
  250. qemu_mutex_lock(&monitor_lock);
  251. if (!evconf->rate) {
  252. /* Unthrottled event */
  253. monitor_qapi_event_emit(event, qdict);
  254. } else {
  255. QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
  256. MonitorQAPIEventState key = { .event = event, .data = data };
  257. evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
  258. assert(!evstate || timer_pending(evstate->timer));
  259. if (evstate) {
  260. /*
  261. * Timer is pending for (at least) evconf->rate ns after
  262. * last send. Store event for sending when timer fires,
  263. * replacing a prior stored event if any.
  264. */
  265. qobject_unref(evstate->qdict);
  266. evstate->qdict = qobject_ref(qdict);
  267. } else {
  268. /*
  269. * Last send was (at least) evconf->rate ns ago.
  270. * Send immediately, and arm the timer to call
  271. * monitor_qapi_event_handler() in evconf->rate ns. Any
  272. * events arriving before then will be delayed until then.
  273. */
  274. int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
  275. monitor_qapi_event_emit(event, qdict);
  276. evstate = g_new(MonitorQAPIEventState, 1);
  277. evstate->event = event;
  278. evstate->data = qobject_ref(data);
  279. evstate->qdict = NULL;
  280. evstate->timer = timer_new_ns(monitor_get_event_clock(),
  281. monitor_qapi_event_handler,
  282. evstate);
  283. g_hash_table_add(monitor_qapi_event_state, evstate);
  284. timer_mod_ns(evstate->timer, now + evconf->rate);
  285. }
  286. }
  287. qemu_mutex_unlock(&monitor_lock);
  288. }
  289. void qapi_event_emit(QAPIEvent event, QDict *qdict)
  290. {
  291. /*
  292. * monitor_qapi_event_queue_no_reenter() is not reentrant: it
  293. * would deadlock on monitor_lock. Work around by queueing
  294. * events in thread-local storage.
  295. * TODO: remove this, make it re-enter safe.
  296. */
  297. typedef struct MonitorQapiEvent {
  298. QAPIEvent event;
  299. QDict *qdict;
  300. QSIMPLEQ_ENTRY(MonitorQapiEvent) entry;
  301. } MonitorQapiEvent;
  302. static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue;
  303. static __thread bool reentered;
  304. MonitorQapiEvent *ev;
  305. if (!reentered) {
  306. QSIMPLEQ_INIT(&event_queue);
  307. }
  308. ev = g_new(MonitorQapiEvent, 1);
  309. ev->qdict = qobject_ref(qdict);
  310. ev->event = event;
  311. QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry);
  312. if (reentered) {
  313. return;
  314. }
  315. reentered = true;
  316. while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) {
  317. QSIMPLEQ_REMOVE_HEAD(&event_queue, entry);
  318. monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict);
  319. qobject_unref(ev->qdict);
  320. g_free(ev);
  321. }
  322. reentered = false;
  323. }
  324. /*
  325. * This function runs evconf->rate ns after sending a throttled
  326. * event.
  327. * If another event has since been stored, send it.
  328. */
  329. static void monitor_qapi_event_handler(void *opaque)
  330. {
  331. MonitorQAPIEventState *evstate = opaque;
  332. MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
  333. trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
  334. qemu_mutex_lock(&monitor_lock);
  335. if (evstate->qdict) {
  336. int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
  337. monitor_qapi_event_emit(evstate->event, evstate->qdict);
  338. qobject_unref(evstate->qdict);
  339. evstate->qdict = NULL;
  340. timer_mod_ns(evstate->timer, now + evconf->rate);
  341. } else {
  342. g_hash_table_remove(monitor_qapi_event_state, evstate);
  343. qobject_unref(evstate->data);
  344. timer_free(evstate->timer);
  345. g_free(evstate);
  346. }
  347. qemu_mutex_unlock(&monitor_lock);
  348. }
  349. static unsigned int qapi_event_throttle_hash(const void *key)
  350. {
  351. const MonitorQAPIEventState *evstate = key;
  352. unsigned int hash = evstate->event * 255;
  353. if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
  354. hash += g_str_hash(qdict_get_str(evstate->data, "id"));
  355. }
  356. if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
  357. hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
  358. }
  359. return hash;
  360. }
  361. static gboolean qapi_event_throttle_equal(const void *a, const void *b)
  362. {
  363. const MonitorQAPIEventState *eva = a;
  364. const MonitorQAPIEventState *evb = b;
  365. if (eva->event != evb->event) {
  366. return FALSE;
  367. }
  368. if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
  369. return !strcmp(qdict_get_str(eva->data, "id"),
  370. qdict_get_str(evb->data, "id"));
  371. }
  372. if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
  373. return !strcmp(qdict_get_str(eva->data, "node-name"),
  374. qdict_get_str(evb->data, "node-name"));
  375. }
  376. return TRUE;
  377. }
  378. int monitor_suspend(Monitor *mon)
  379. {
  380. if (monitor_is_hmp_non_interactive(mon)) {
  381. return -ENOTTY;
  382. }
  383. atomic_inc(&mon->suspend_cnt);
  384. if (mon->use_io_thread) {
  385. /*
  386. * Kick I/O thread to make sure this takes effect. It'll be
  387. * evaluated again in prepare() of the watch object.
  388. */
  389. aio_notify(iothread_get_aio_context(mon_iothread));
  390. }
  391. trace_monitor_suspend(mon, 1);
  392. return 0;
  393. }
  394. static void monitor_accept_input(void *opaque)
  395. {
  396. Monitor *mon = opaque;
  397. qemu_chr_fe_accept_input(&mon->chr);
  398. }
  399. void monitor_resume(Monitor *mon)
  400. {
  401. if (monitor_is_hmp_non_interactive(mon)) {
  402. return;
  403. }
  404. if (atomic_dec_fetch(&mon->suspend_cnt) == 0) {
  405. AioContext *ctx;
  406. if (mon->use_io_thread) {
  407. ctx = iothread_get_aio_context(mon_iothread);
  408. } else {
  409. ctx = qemu_get_aio_context();
  410. }
  411. if (!monitor_is_qmp(mon)) {
  412. MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
  413. assert(hmp_mon->rs);
  414. readline_show_prompt(hmp_mon->rs);
  415. }
  416. aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon);
  417. }
  418. trace_monitor_suspend(mon, -1);
  419. }
  420. int monitor_can_read(void *opaque)
  421. {
  422. Monitor *mon = opaque;
  423. return !atomic_mb_read(&mon->suspend_cnt);
  424. }
  425. void monitor_list_append(Monitor *mon)
  426. {
  427. qemu_mutex_lock(&monitor_lock);
  428. /*
  429. * This prevents inserting new monitors during monitor_cleanup().
  430. * A cleaner solution would involve the main thread telling other
  431. * threads to terminate, waiting for their termination.
  432. */
  433. if (!monitor_destroyed) {
  434. QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
  435. mon = NULL;
  436. }
  437. qemu_mutex_unlock(&monitor_lock);
  438. if (mon) {
  439. monitor_data_destroy(mon);
  440. g_free(mon);
  441. }
  442. }
  443. static void monitor_iothread_init(void)
  444. {
  445. mon_iothread = iothread_create("mon_iothread", &error_abort);
  446. }
  447. void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
  448. bool use_io_thread)
  449. {
  450. if (use_io_thread && !mon_iothread) {
  451. monitor_iothread_init();
  452. }
  453. qemu_mutex_init(&mon->mon_lock);
  454. mon->is_qmp = is_qmp;
  455. mon->outbuf = qstring_new();
  456. mon->skip_flush = skip_flush;
  457. mon->use_io_thread = use_io_thread;
  458. }
  459. void monitor_data_destroy(Monitor *mon)
  460. {
  461. g_free(mon->mon_cpu_path);
  462. qemu_chr_fe_deinit(&mon->chr, false);
  463. if (monitor_is_qmp(mon)) {
  464. monitor_data_destroy_qmp(container_of(mon, MonitorQMP, common));
  465. } else {
  466. readline_free(container_of(mon, MonitorHMP, common)->rs);
  467. }
  468. qobject_unref(mon->outbuf);
  469. qemu_mutex_destroy(&mon->mon_lock);
  470. }
  471. void monitor_cleanup(void)
  472. {
  473. /*
  474. * We need to explicitly stop the I/O thread (but not destroy it),
  475. * clean up the monitor resources, then destroy the I/O thread since
  476. * we need to unregister from chardev below in
  477. * monitor_data_destroy(), and chardev is not thread-safe yet
  478. */
  479. if (mon_iothread) {
  480. iothread_stop(mon_iothread);
  481. }
  482. /* Flush output buffers and destroy monitors */
  483. qemu_mutex_lock(&monitor_lock);
  484. monitor_destroyed = true;
  485. while (!QTAILQ_EMPTY(&mon_list)) {
  486. Monitor *mon = QTAILQ_FIRST(&mon_list);
  487. QTAILQ_REMOVE(&mon_list, mon, entry);
  488. /* Permit QAPI event emission from character frontend release */
  489. qemu_mutex_unlock(&monitor_lock);
  490. monitor_flush(mon);
  491. monitor_data_destroy(mon);
  492. qemu_mutex_lock(&monitor_lock);
  493. g_free(mon);
  494. }
  495. qemu_mutex_unlock(&monitor_lock);
  496. /* QEMUBHs needs to be deleted before destroying the I/O thread */
  497. qemu_bh_delete(qmp_dispatcher_bh);
  498. qmp_dispatcher_bh = NULL;
  499. if (mon_iothread) {
  500. iothread_destroy(mon_iothread);
  501. mon_iothread = NULL;
  502. }
  503. }
  504. static void monitor_qapi_event_init(void)
  505. {
  506. monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
  507. qapi_event_throttle_equal);
  508. }
  509. void monitor_init_globals_core(void)
  510. {
  511. monitor_qapi_event_init();
  512. qemu_mutex_init(&monitor_lock);
  513. /*
  514. * The dispatcher BH must run in the main loop thread, since we
  515. * have commands assuming that context. It would be nice to get
  516. * rid of those assumptions.
  517. */
  518. qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
  519. monitor_qmp_bh_dispatcher,
  520. NULL);
  521. }
  522. int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
  523. {
  524. Chardev *chr;
  525. Error *local_err = NULL;
  526. chr = qemu_chr_find(opts->chardev);
  527. if (chr == NULL) {
  528. error_setg(errp, "chardev \"%s\" not found", opts->chardev);
  529. return -1;
  530. }
  531. if (!opts->has_mode) {
  532. opts->mode = allow_hmp ? MONITOR_MODE_READLINE : MONITOR_MODE_CONTROL;
  533. }
  534. switch (opts->mode) {
  535. case MONITOR_MODE_CONTROL:
  536. monitor_init_qmp(chr, opts->pretty, &local_err);
  537. break;
  538. case MONITOR_MODE_READLINE:
  539. if (!allow_hmp) {
  540. error_setg(errp, "Only QMP is supported");
  541. return -1;
  542. }
  543. if (opts->pretty) {
  544. warn_report("'pretty' is deprecated for HMP monitors, it has no "
  545. "effect and will be removed in future versions");
  546. }
  547. monitor_init_hmp(chr, true, &local_err);
  548. break;
  549. default:
  550. g_assert_not_reached();
  551. }
  552. if (local_err) {
  553. error_propagate(errp, local_err);
  554. return -1;
  555. }
  556. return 0;
  557. }
  558. int monitor_init_opts(QemuOpts *opts, Error **errp)
  559. {
  560. Visitor *v;
  561. MonitorOptions *options;
  562. int ret;
  563. v = opts_visitor_new(opts);
  564. visit_type_MonitorOptions(v, NULL, &options, errp);
  565. visit_free(v);
  566. if (!options) {
  567. return -1;
  568. }
  569. ret = monitor_init(options, true, errp);
  570. qapi_free_MonitorOptions(options);
  571. return ret;
  572. }
  573. QemuOptsList qemu_mon_opts = {
  574. .name = "mon",
  575. .implied_opt_name = "chardev",
  576. .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
  577. .desc = {
  578. {
  579. .name = "mode",
  580. .type = QEMU_OPT_STRING,
  581. },{
  582. .name = "chardev",
  583. .type = QEMU_OPT_STRING,
  584. },{
  585. .name = "pretty",
  586. .type = QEMU_OPT_BOOL,
  587. },
  588. { /* end of list */ }
  589. },
  590. };