misc.c 61 KB

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