2
0

monitor-internal.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #ifndef MONITOR_INTERNAL_H
  25. #define MONITOR_INTERNAL_H
  26. #include "chardev/char-fe.h"
  27. #include "monitor/monitor.h"
  28. #include "qapi/qapi-types-control.h"
  29. #include "qapi/qmp/dispatch.h"
  30. #include "qapi/qmp/json-parser.h"
  31. #include "qemu/readline.h"
  32. #include "sysemu/iothread.h"
  33. /*
  34. * Supported types:
  35. *
  36. * 'F' filename
  37. * 'B' block device name
  38. * 's' string (accept optional quote)
  39. * 'S' it just appends the rest of the string (accept optional quote)
  40. * 'O' option string of the form NAME=VALUE,...
  41. * parsed according to QemuOptsList given by its name
  42. * Example: 'device:O' uses qemu_device_opts.
  43. * Restriction: only lists with empty desc are supported
  44. * TODO lift the restriction
  45. * 'i' 32 bit integer
  46. * 'l' target long (32 or 64 bit)
  47. * 'M' Non-negative target long (32 or 64 bit), in user mode the
  48. * value is multiplied by 2^20 (think Mebibyte)
  49. * 'o' octets (aka bytes)
  50. * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
  51. * K, k suffix, which multiplies the value by 2^60 for suffixes E
  52. * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
  53. * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
  54. * 'T' double
  55. * user mode accepts an optional ms, us, ns suffix,
  56. * which divides the value by 1e3, 1e6, 1e9, respectively
  57. * '/' optional gdb-like print format (like "/10x")
  58. *
  59. * '?' optional type (for all types, except '/')
  60. * '.' other form of optional type (for 'i' and 'l')
  61. * 'b' boolean
  62. * user mode accepts "on" or "off"
  63. * '-' optional parameter (eg. '-f'); if followed by a 's', it
  64. * specifies an optional string param (e.g. '-fs' allows '-f foo')
  65. *
  66. */
  67. typedef struct HMPCommand {
  68. const char *name;
  69. const char *args_type;
  70. const char *params;
  71. const char *help;
  72. const char *flags; /* p=preconfig */
  73. void (*cmd)(Monitor *mon, const QDict *qdict);
  74. /*
  75. * If implementing a command that takes no arguments and simply
  76. * prints formatted data, then leave @cmd NULL, and then set
  77. * @cmd_info_hrt to the corresponding QMP handler that returns
  78. * the formatted text.
  79. */
  80. HumanReadableText *(*cmd_info_hrt)(Error **errp);
  81. bool coroutine;
  82. /*
  83. * @sub_table is a list of 2nd level of commands. If it does not exist,
  84. * cmd should be used. If it exists, sub_table[?].cmd should be
  85. * used, and cmd of 1st level plays the role of help function.
  86. */
  87. struct HMPCommand *sub_table;
  88. void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
  89. } HMPCommand;
  90. struct Monitor {
  91. CharBackend chr;
  92. int suspend_cnt; /* Needs to be accessed atomically */
  93. bool is_qmp;
  94. bool skip_flush;
  95. bool use_io_thread;
  96. char *mon_cpu_path;
  97. QTAILQ_ENTRY(Monitor) entry;
  98. /*
  99. * The per-monitor lock. We can't access guest memory when holding
  100. * the lock.
  101. */
  102. QemuMutex mon_lock;
  103. /*
  104. * Members that are protected by the per-monitor lock
  105. */
  106. QLIST_HEAD(, mon_fd_t) fds;
  107. GString *outbuf;
  108. guint out_watch;
  109. int mux_out;
  110. int reset_seen;
  111. };
  112. struct MonitorHMP {
  113. Monitor common;
  114. bool use_readline;
  115. /*
  116. * State used only in the thread "owning" the monitor.
  117. * If @use_io_thread, this is @mon_iothread. (This does not actually happen
  118. * in the current state of the code.)
  119. * Else, it's the main thread.
  120. * These members can be safely accessed without locks.
  121. */
  122. ReadLineState *rs;
  123. };
  124. typedef struct {
  125. Monitor common;
  126. JSONMessageParser parser;
  127. bool pretty;
  128. /*
  129. * When a client connects, we're in capabilities negotiation mode.
  130. * @commands is &qmp_cap_negotiation_commands then. When command
  131. * qmp_capabilities succeeds, we go into command mode, and
  132. * @command becomes &qmp_commands.
  133. */
  134. const QmpCommandList *commands;
  135. bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */
  136. bool capab[QMP_CAPABILITY__MAX]; /* offered and accepted */
  137. /*
  138. * Protects qmp request/response queue.
  139. * Take monitor_lock first when you need both.
  140. */
  141. QemuMutex qmp_queue_lock;
  142. /* Input queue that holds all the parsed QMP requests */
  143. GQueue *qmp_requests;
  144. } MonitorQMP;
  145. /**
  146. * Is @mon a QMP monitor?
  147. */
  148. static inline bool monitor_is_qmp(const Monitor *mon)
  149. {
  150. return mon->is_qmp;
  151. }
  152. typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList;
  153. extern IOThread *mon_iothread;
  154. extern Coroutine *qmp_dispatcher_co;
  155. extern bool qmp_dispatcher_co_shutdown;
  156. extern QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
  157. extern QemuMutex monitor_lock;
  158. extern MonitorList mon_list;
  159. extern HMPCommand hmp_cmds[];
  160. void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
  161. bool use_io_thread);
  162. void monitor_data_destroy(Monitor *mon);
  163. int monitor_can_read(void *opaque);
  164. void monitor_list_append(Monitor *mon);
  165. void monitor_fdsets_cleanup(void);
  166. void qmp_send_response(MonitorQMP *mon, const QDict *rsp);
  167. void monitor_data_destroy_qmp(MonitorQMP *mon);
  168. void coroutine_fn monitor_qmp_dispatcher_co(void *data);
  169. void qmp_dispatcher_co_wake(void);
  170. int get_monitor_def(Monitor *mon, int64_t *pval, const char *name);
  171. void handle_hmp_command(MonitorHMP *mon, const char *cmdline);
  172. int hmp_compare_cmd(const char *name, const char *list);
  173. #endif