2
0

misc.c 52 KB

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