2
0

misc.c 58 KB

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