misc.c 57 KB

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