misc.c 58 KB

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