misc.c 57 KB

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