monitor.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961
  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 "hw/hw.h"
  25. #include "hw/usb.h"
  26. #include "hw/pcmcia.h"
  27. #include "hw/pc.h"
  28. #include "hw/pci.h"
  29. #include "gdbstub.h"
  30. #include "net.h"
  31. #include "qemu-char.h"
  32. #include "sysemu.h"
  33. #include "console.h"
  34. #include "block.h"
  35. #include "audio/audio.h"
  36. #include "disas.h"
  37. #include "balloon.h"
  38. #include <dirent.h>
  39. #include "qemu-timer.h"
  40. #include "migration.h"
  41. #include "kvm.h"
  42. //#define DEBUG
  43. //#define DEBUG_COMPLETION
  44. /*
  45. * Supported types:
  46. *
  47. * 'F' filename
  48. * 'B' block device name
  49. * 's' string (accept optional quote)
  50. * 'i' 32 bit integer
  51. * 'l' target long (32 or 64 bit)
  52. * '/' optional gdb-like print format (like "/10x")
  53. *
  54. * '?' optional type (for 'F', 's' and 'i')
  55. *
  56. */
  57. typedef struct term_cmd_t {
  58. const char *name;
  59. const char *args_type;
  60. void *handler;
  61. const char *params;
  62. const char *help;
  63. } term_cmd_t;
  64. #define MAX_MON 4
  65. static CharDriverState *monitor_hd[MAX_MON];
  66. static int hide_banner;
  67. static const term_cmd_t term_cmds[];
  68. static const term_cmd_t info_cmds[];
  69. static uint8_t term_outbuf[1024];
  70. static int term_outbuf_index;
  71. static void monitor_start_input(void);
  72. static void monitor_readline(const char *prompt, int is_password,
  73. char *buf, int buf_size);
  74. static CPUState *mon_cpu = NULL;
  75. void term_flush(void)
  76. {
  77. int i;
  78. if (term_outbuf_index > 0) {
  79. for (i = 0; i < MAX_MON; i++)
  80. if (monitor_hd[i] && monitor_hd[i]->focus == 0)
  81. qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
  82. term_outbuf_index = 0;
  83. }
  84. }
  85. /* flush at every end of line or if the buffer is full */
  86. void term_puts(const char *str)
  87. {
  88. char c;
  89. for(;;) {
  90. c = *str++;
  91. if (c == '\0')
  92. break;
  93. if (c == '\n')
  94. term_outbuf[term_outbuf_index++] = '\r';
  95. term_outbuf[term_outbuf_index++] = c;
  96. if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
  97. c == '\n')
  98. term_flush();
  99. }
  100. }
  101. void term_vprintf(const char *fmt, va_list ap)
  102. {
  103. char buf[4096];
  104. vsnprintf(buf, sizeof(buf), fmt, ap);
  105. term_puts(buf);
  106. }
  107. void term_printf(const char *fmt, ...)
  108. {
  109. va_list ap;
  110. va_start(ap, fmt);
  111. term_vprintf(fmt, ap);
  112. va_end(ap);
  113. }
  114. void term_print_filename(const char *filename)
  115. {
  116. int i;
  117. for (i = 0; filename[i]; i++) {
  118. switch (filename[i]) {
  119. case ' ':
  120. case '"':
  121. case '\\':
  122. term_printf("\\%c", filename[i]);
  123. break;
  124. case '\t':
  125. term_printf("\\t");
  126. break;
  127. case '\r':
  128. term_printf("\\r");
  129. break;
  130. case '\n':
  131. term_printf("\\n");
  132. break;
  133. default:
  134. term_printf("%c", filename[i]);
  135. break;
  136. }
  137. }
  138. }
  139. static int monitor_fprintf(FILE *stream, const char *fmt, ...)
  140. {
  141. va_list ap;
  142. va_start(ap, fmt);
  143. term_vprintf(fmt, ap);
  144. va_end(ap);
  145. return 0;
  146. }
  147. static int compare_cmd(const char *name, const char *list)
  148. {
  149. const char *p, *pstart;
  150. int len;
  151. len = strlen(name);
  152. p = list;
  153. for(;;) {
  154. pstart = p;
  155. p = strchr(p, '|');
  156. if (!p)
  157. p = pstart + strlen(pstart);
  158. if ((p - pstart) == len && !memcmp(pstart, name, len))
  159. return 1;
  160. if (*p == '\0')
  161. break;
  162. p++;
  163. }
  164. return 0;
  165. }
  166. static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
  167. {
  168. const term_cmd_t *cmd;
  169. for(cmd = cmds; cmd->name != NULL; cmd++) {
  170. if (!name || !strcmp(name, cmd->name))
  171. term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
  172. }
  173. }
  174. static void help_cmd(const char *name)
  175. {
  176. if (name && !strcmp(name, "info")) {
  177. help_cmd1(info_cmds, "info ", NULL);
  178. } else {
  179. help_cmd1(term_cmds, "", name);
  180. if (name && !strcmp(name, "log")) {
  181. const CPULogItem *item;
  182. term_printf("Log items (comma separated):\n");
  183. term_printf("%-10s %s\n", "none", "remove all logs");
  184. for(item = cpu_log_items; item->mask != 0; item++) {
  185. term_printf("%-10s %s\n", item->name, item->help);
  186. }
  187. }
  188. }
  189. }
  190. static void do_help(const char *name)
  191. {
  192. help_cmd(name);
  193. }
  194. static void do_commit(const char *device)
  195. {
  196. int i, all_devices;
  197. all_devices = !strcmp(device, "all");
  198. for (i = 0; i < nb_drives; i++) {
  199. if (all_devices ||
  200. !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
  201. bdrv_commit(drives_table[i].bdrv);
  202. }
  203. }
  204. static void do_info(const char *item)
  205. {
  206. const term_cmd_t *cmd;
  207. void (*handler)(void);
  208. if (!item)
  209. goto help;
  210. for(cmd = info_cmds; cmd->name != NULL; cmd++) {
  211. if (compare_cmd(item, cmd->name))
  212. goto found;
  213. }
  214. help:
  215. help_cmd("info");
  216. return;
  217. found:
  218. handler = cmd->handler;
  219. handler();
  220. }
  221. static void do_info_version(void)
  222. {
  223. term_printf("%s\n", QEMU_VERSION);
  224. }
  225. static void do_info_name(void)
  226. {
  227. if (qemu_name)
  228. term_printf("%s\n", qemu_name);
  229. }
  230. #if defined(TARGET_I386)
  231. static void do_info_hpet(void)
  232. {
  233. term_printf("HPET is %s by QEMU\n", (no_hpet) ? "disabled" : "enabled");
  234. }
  235. #endif
  236. static void do_info_uuid(void)
  237. {
  238. term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
  239. qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
  240. qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
  241. qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
  242. qemu_uuid[15]);
  243. }
  244. static void do_info_block(void)
  245. {
  246. bdrv_info();
  247. }
  248. static void do_info_blockstats(void)
  249. {
  250. bdrv_info_stats();
  251. }
  252. /* get the current CPU defined by the user */
  253. static int mon_set_cpu(int cpu_index)
  254. {
  255. CPUState *env;
  256. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  257. if (env->cpu_index == cpu_index) {
  258. mon_cpu = env;
  259. return 0;
  260. }
  261. }
  262. return -1;
  263. }
  264. static CPUState *mon_get_cpu(void)
  265. {
  266. if (!mon_cpu) {
  267. mon_set_cpu(0);
  268. }
  269. return mon_cpu;
  270. }
  271. static void do_info_registers(void)
  272. {
  273. CPUState *env;
  274. env = mon_get_cpu();
  275. if (!env)
  276. return;
  277. #ifdef TARGET_I386
  278. cpu_dump_state(env, NULL, monitor_fprintf,
  279. X86_DUMP_FPU);
  280. #else
  281. cpu_dump_state(env, NULL, monitor_fprintf,
  282. 0);
  283. #endif
  284. }
  285. static void do_info_cpus(void)
  286. {
  287. CPUState *env;
  288. /* just to set the default cpu if not already done */
  289. mon_get_cpu();
  290. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  291. term_printf("%c CPU #%d:",
  292. (env == mon_cpu) ? '*' : ' ',
  293. env->cpu_index);
  294. #if defined(TARGET_I386)
  295. term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
  296. #elif defined(TARGET_PPC)
  297. term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
  298. #elif defined(TARGET_SPARC)
  299. term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
  300. #elif defined(TARGET_MIPS)
  301. term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
  302. #endif
  303. if (env->halted)
  304. term_printf(" (halted)");
  305. term_printf("\n");
  306. }
  307. }
  308. static void do_cpu_set(int index)
  309. {
  310. if (mon_set_cpu(index) < 0)
  311. term_printf("Invalid CPU index\n");
  312. }
  313. static void do_info_jit(void)
  314. {
  315. dump_exec_info(NULL, monitor_fprintf);
  316. }
  317. static void do_info_history (void)
  318. {
  319. int i;
  320. const char *str;
  321. i = 0;
  322. for(;;) {
  323. str = readline_get_history(i);
  324. if (!str)
  325. break;
  326. term_printf("%d: '%s'\n", i, str);
  327. i++;
  328. }
  329. }
  330. #if defined(TARGET_PPC)
  331. /* XXX: not implemented in other targets */
  332. static void do_info_cpu_stats (void)
  333. {
  334. CPUState *env;
  335. env = mon_get_cpu();
  336. cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
  337. }
  338. #endif
  339. static void do_quit(void)
  340. {
  341. exit(0);
  342. }
  343. static int eject_device(BlockDriverState *bs, int force)
  344. {
  345. if (bdrv_is_inserted(bs)) {
  346. if (!force) {
  347. if (!bdrv_is_removable(bs)) {
  348. term_printf("device is not removable\n");
  349. return -1;
  350. }
  351. if (bdrv_is_locked(bs)) {
  352. term_printf("device is locked\n");
  353. return -1;
  354. }
  355. }
  356. bdrv_close(bs);
  357. }
  358. return 0;
  359. }
  360. static void do_eject(int force, const char *filename)
  361. {
  362. BlockDriverState *bs;
  363. bs = bdrv_find(filename);
  364. if (!bs) {
  365. term_printf("device not found\n");
  366. return;
  367. }
  368. eject_device(bs, force);
  369. }
  370. static void do_change_block(const char *device, const char *filename, const char *fmt)
  371. {
  372. BlockDriverState *bs;
  373. BlockDriver *drv = NULL;
  374. bs = bdrv_find(device);
  375. if (!bs) {
  376. term_printf("device not found\n");
  377. return;
  378. }
  379. if (fmt) {
  380. drv = bdrv_find_format(fmt);
  381. if (!drv) {
  382. term_printf("invalid format %s\n", fmt);
  383. return;
  384. }
  385. }
  386. if (eject_device(bs, 0) < 0)
  387. return;
  388. bdrv_open2(bs, filename, 0, drv);
  389. monitor_read_bdrv_key(bs);
  390. }
  391. static void do_change_vnc(const char *target, const char *arg)
  392. {
  393. if (strcmp(target, "passwd") == 0 ||
  394. strcmp(target, "password") == 0) {
  395. char password[9];
  396. if (arg) {
  397. strncpy(password, arg, sizeof(password));
  398. password[sizeof(password) - 1] = '\0';
  399. } else
  400. monitor_readline("Password: ", 1, password, sizeof(password));
  401. if (vnc_display_password(NULL, password) < 0)
  402. term_printf("could not set VNC server password\n");
  403. } else {
  404. if (vnc_display_open(NULL, target) < 0)
  405. term_printf("could not start VNC server on %s\n", target);
  406. }
  407. }
  408. static void do_change(const char *device, const char *target, const char *arg)
  409. {
  410. if (strcmp(device, "vnc") == 0) {
  411. do_change_vnc(target, arg);
  412. } else {
  413. do_change_block(device, target, arg);
  414. }
  415. }
  416. static void do_screen_dump(const char *filename)
  417. {
  418. vga_hw_screen_dump(filename);
  419. }
  420. static void do_logfile(const char *filename)
  421. {
  422. cpu_set_log_filename(filename);
  423. }
  424. static void do_log(const char *items)
  425. {
  426. int mask;
  427. if (!strcmp(items, "none")) {
  428. mask = 0;
  429. } else {
  430. mask = cpu_str_to_log_mask(items);
  431. if (!mask) {
  432. help_cmd("log");
  433. return;
  434. }
  435. }
  436. cpu_set_log(mask);
  437. }
  438. static void do_stop(void)
  439. {
  440. vm_stop(EXCP_INTERRUPT);
  441. }
  442. static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
  443. {
  444. int *err = opaque;
  445. if (bdrv_key_required(bs))
  446. *err = monitor_read_bdrv_key(bs);
  447. else
  448. *err = 0;
  449. }
  450. static void do_cont(void)
  451. {
  452. int err = 0;
  453. bdrv_iterate(encrypted_bdrv_it, &err);
  454. /* only resume the vm if all keys are set and valid */
  455. if (!err)
  456. vm_start();
  457. }
  458. #ifdef CONFIG_GDBSTUB
  459. static void do_gdbserver(const char *port)
  460. {
  461. if (!port)
  462. port = DEFAULT_GDBSTUB_PORT;
  463. if (gdbserver_start(port) < 0) {
  464. qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
  465. } else {
  466. qemu_printf("Waiting gdb connection on port '%s'\n", port);
  467. }
  468. }
  469. #endif
  470. static void term_printc(int c)
  471. {
  472. term_printf("'");
  473. switch(c) {
  474. case '\'':
  475. term_printf("\\'");
  476. break;
  477. case '\\':
  478. term_printf("\\\\");
  479. break;
  480. case '\n':
  481. term_printf("\\n");
  482. break;
  483. case '\r':
  484. term_printf("\\r");
  485. break;
  486. default:
  487. if (c >= 32 && c <= 126) {
  488. term_printf("%c", c);
  489. } else {
  490. term_printf("\\x%02x", c);
  491. }
  492. break;
  493. }
  494. term_printf("'");
  495. }
  496. static void memory_dump(int count, int format, int wsize,
  497. target_phys_addr_t addr, int is_physical)
  498. {
  499. CPUState *env;
  500. int nb_per_line, l, line_size, i, max_digits, len;
  501. uint8_t buf[16];
  502. uint64_t v;
  503. if (format == 'i') {
  504. int flags;
  505. flags = 0;
  506. env = mon_get_cpu();
  507. if (!env && !is_physical)
  508. return;
  509. #ifdef TARGET_I386
  510. if (wsize == 2) {
  511. flags = 1;
  512. } else if (wsize == 4) {
  513. flags = 0;
  514. } else {
  515. /* as default we use the current CS size */
  516. flags = 0;
  517. if (env) {
  518. #ifdef TARGET_X86_64
  519. if ((env->efer & MSR_EFER_LMA) &&
  520. (env->segs[R_CS].flags & DESC_L_MASK))
  521. flags = 2;
  522. else
  523. #endif
  524. if (!(env->segs[R_CS].flags & DESC_B_MASK))
  525. flags = 1;
  526. }
  527. }
  528. #endif
  529. monitor_disas(env, addr, count, is_physical, flags);
  530. return;
  531. }
  532. len = wsize * count;
  533. if (wsize == 1)
  534. line_size = 8;
  535. else
  536. line_size = 16;
  537. nb_per_line = line_size / wsize;
  538. max_digits = 0;
  539. switch(format) {
  540. case 'o':
  541. max_digits = (wsize * 8 + 2) / 3;
  542. break;
  543. default:
  544. case 'x':
  545. max_digits = (wsize * 8) / 4;
  546. break;
  547. case 'u':
  548. case 'd':
  549. max_digits = (wsize * 8 * 10 + 32) / 33;
  550. break;
  551. case 'c':
  552. wsize = 1;
  553. break;
  554. }
  555. while (len > 0) {
  556. if (is_physical)
  557. term_printf(TARGET_FMT_plx ":", addr);
  558. else
  559. term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
  560. l = len;
  561. if (l > line_size)
  562. l = line_size;
  563. if (is_physical) {
  564. cpu_physical_memory_rw(addr, buf, l, 0);
  565. } else {
  566. env = mon_get_cpu();
  567. if (!env)
  568. break;
  569. if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
  570. term_printf(" Cannot access memory\n");
  571. break;
  572. }
  573. }
  574. i = 0;
  575. while (i < l) {
  576. switch(wsize) {
  577. default:
  578. case 1:
  579. v = ldub_raw(buf + i);
  580. break;
  581. case 2:
  582. v = lduw_raw(buf + i);
  583. break;
  584. case 4:
  585. v = (uint32_t)ldl_raw(buf + i);
  586. break;
  587. case 8:
  588. v = ldq_raw(buf + i);
  589. break;
  590. }
  591. term_printf(" ");
  592. switch(format) {
  593. case 'o':
  594. term_printf("%#*" PRIo64, max_digits, v);
  595. break;
  596. case 'x':
  597. term_printf("0x%0*" PRIx64, max_digits, v);
  598. break;
  599. case 'u':
  600. term_printf("%*" PRIu64, max_digits, v);
  601. break;
  602. case 'd':
  603. term_printf("%*" PRId64, max_digits, v);
  604. break;
  605. case 'c':
  606. term_printc(v);
  607. break;
  608. }
  609. i += wsize;
  610. }
  611. term_printf("\n");
  612. addr += l;
  613. len -= l;
  614. }
  615. }
  616. #if TARGET_LONG_BITS == 64
  617. #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
  618. #else
  619. #define GET_TLONG(h, l) (l)
  620. #endif
  621. static void do_memory_dump(int count, int format, int size,
  622. uint32_t addrh, uint32_t addrl)
  623. {
  624. target_long addr = GET_TLONG(addrh, addrl);
  625. memory_dump(count, format, size, addr, 0);
  626. }
  627. #if TARGET_PHYS_ADDR_BITS > 32
  628. #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
  629. #else
  630. #define GET_TPHYSADDR(h, l) (l)
  631. #endif
  632. static void do_physical_memory_dump(int count, int format, int size,
  633. uint32_t addrh, uint32_t addrl)
  634. {
  635. target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
  636. memory_dump(count, format, size, addr, 1);
  637. }
  638. static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
  639. {
  640. target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
  641. #if TARGET_PHYS_ADDR_BITS == 32
  642. switch(format) {
  643. case 'o':
  644. term_printf("%#o", val);
  645. break;
  646. case 'x':
  647. term_printf("%#x", val);
  648. break;
  649. case 'u':
  650. term_printf("%u", val);
  651. break;
  652. default:
  653. case 'd':
  654. term_printf("%d", val);
  655. break;
  656. case 'c':
  657. term_printc(val);
  658. break;
  659. }
  660. #else
  661. switch(format) {
  662. case 'o':
  663. term_printf("%#" PRIo64, val);
  664. break;
  665. case 'x':
  666. term_printf("%#" PRIx64, val);
  667. break;
  668. case 'u':
  669. term_printf("%" PRIu64, val);
  670. break;
  671. default:
  672. case 'd':
  673. term_printf("%" PRId64, val);
  674. break;
  675. case 'c':
  676. term_printc(val);
  677. break;
  678. }
  679. #endif
  680. term_printf("\n");
  681. }
  682. static void do_memory_save(unsigned int valh, unsigned int vall,
  683. uint32_t size, const char *filename)
  684. {
  685. FILE *f;
  686. target_long addr = GET_TLONG(valh, vall);
  687. uint32_t l;
  688. CPUState *env;
  689. uint8_t buf[1024];
  690. env = mon_get_cpu();
  691. if (!env)
  692. return;
  693. f = fopen(filename, "wb");
  694. if (!f) {
  695. term_printf("could not open '%s'\n", filename);
  696. return;
  697. }
  698. while (size != 0) {
  699. l = sizeof(buf);
  700. if (l > size)
  701. l = size;
  702. cpu_memory_rw_debug(env, addr, buf, l, 0);
  703. fwrite(buf, 1, l, f);
  704. addr += l;
  705. size -= l;
  706. }
  707. fclose(f);
  708. }
  709. static void do_physical_memory_save(unsigned int valh, unsigned int vall,
  710. uint32_t size, const char *filename)
  711. {
  712. FILE *f;
  713. uint32_t l;
  714. uint8_t buf[1024];
  715. target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
  716. f = fopen(filename, "wb");
  717. if (!f) {
  718. term_printf("could not open '%s'\n", filename);
  719. return;
  720. }
  721. while (size != 0) {
  722. l = sizeof(buf);
  723. if (l > size)
  724. l = size;
  725. cpu_physical_memory_rw(addr, buf, l, 0);
  726. fwrite(buf, 1, l, f);
  727. fflush(f);
  728. addr += l;
  729. size -= l;
  730. }
  731. fclose(f);
  732. }
  733. static void do_sum(uint32_t start, uint32_t size)
  734. {
  735. uint32_t addr;
  736. uint8_t buf[1];
  737. uint16_t sum;
  738. sum = 0;
  739. for(addr = start; addr < (start + size); addr++) {
  740. cpu_physical_memory_rw(addr, buf, 1, 0);
  741. /* BSD sum algorithm ('sum' Unix command) */
  742. sum = (sum >> 1) | (sum << 15);
  743. sum += buf[0];
  744. }
  745. term_printf("%05d\n", sum);
  746. }
  747. typedef struct {
  748. int keycode;
  749. const char *name;
  750. } KeyDef;
  751. static const KeyDef key_defs[] = {
  752. { 0x2a, "shift" },
  753. { 0x36, "shift_r" },
  754. { 0x38, "alt" },
  755. { 0xb8, "alt_r" },
  756. { 0x64, "altgr" },
  757. { 0xe4, "altgr_r" },
  758. { 0x1d, "ctrl" },
  759. { 0x9d, "ctrl_r" },
  760. { 0xdd, "menu" },
  761. { 0x01, "esc" },
  762. { 0x02, "1" },
  763. { 0x03, "2" },
  764. { 0x04, "3" },
  765. { 0x05, "4" },
  766. { 0x06, "5" },
  767. { 0x07, "6" },
  768. { 0x08, "7" },
  769. { 0x09, "8" },
  770. { 0x0a, "9" },
  771. { 0x0b, "0" },
  772. { 0x0c, "minus" },
  773. { 0x0d, "equal" },
  774. { 0x0e, "backspace" },
  775. { 0x0f, "tab" },
  776. { 0x10, "q" },
  777. { 0x11, "w" },
  778. { 0x12, "e" },
  779. { 0x13, "r" },
  780. { 0x14, "t" },
  781. { 0x15, "y" },
  782. { 0x16, "u" },
  783. { 0x17, "i" },
  784. { 0x18, "o" },
  785. { 0x19, "p" },
  786. { 0x1c, "ret" },
  787. { 0x1e, "a" },
  788. { 0x1f, "s" },
  789. { 0x20, "d" },
  790. { 0x21, "f" },
  791. { 0x22, "g" },
  792. { 0x23, "h" },
  793. { 0x24, "j" },
  794. { 0x25, "k" },
  795. { 0x26, "l" },
  796. { 0x2c, "z" },
  797. { 0x2d, "x" },
  798. { 0x2e, "c" },
  799. { 0x2f, "v" },
  800. { 0x30, "b" },
  801. { 0x31, "n" },
  802. { 0x32, "m" },
  803. { 0x33, "comma" },
  804. { 0x34, "dot" },
  805. { 0x35, "slash" },
  806. { 0x37, "asterisk" },
  807. { 0x39, "spc" },
  808. { 0x3a, "caps_lock" },
  809. { 0x3b, "f1" },
  810. { 0x3c, "f2" },
  811. { 0x3d, "f3" },
  812. { 0x3e, "f4" },
  813. { 0x3f, "f5" },
  814. { 0x40, "f6" },
  815. { 0x41, "f7" },
  816. { 0x42, "f8" },
  817. { 0x43, "f9" },
  818. { 0x44, "f10" },
  819. { 0x45, "num_lock" },
  820. { 0x46, "scroll_lock" },
  821. { 0xb5, "kp_divide" },
  822. { 0x37, "kp_multiply" },
  823. { 0x4a, "kp_subtract" },
  824. { 0x4e, "kp_add" },
  825. { 0x9c, "kp_enter" },
  826. { 0x53, "kp_decimal" },
  827. { 0x54, "sysrq" },
  828. { 0x52, "kp_0" },
  829. { 0x4f, "kp_1" },
  830. { 0x50, "kp_2" },
  831. { 0x51, "kp_3" },
  832. { 0x4b, "kp_4" },
  833. { 0x4c, "kp_5" },
  834. { 0x4d, "kp_6" },
  835. { 0x47, "kp_7" },
  836. { 0x48, "kp_8" },
  837. { 0x49, "kp_9" },
  838. { 0x56, "<" },
  839. { 0x57, "f11" },
  840. { 0x58, "f12" },
  841. { 0xb7, "print" },
  842. { 0xc7, "home" },
  843. { 0xc9, "pgup" },
  844. { 0xd1, "pgdn" },
  845. { 0xcf, "end" },
  846. { 0xcb, "left" },
  847. { 0xc8, "up" },
  848. { 0xd0, "down" },
  849. { 0xcd, "right" },
  850. { 0xd2, "insert" },
  851. { 0xd3, "delete" },
  852. #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
  853. { 0xf0, "stop" },
  854. { 0xf1, "again" },
  855. { 0xf2, "props" },
  856. { 0xf3, "undo" },
  857. { 0xf4, "front" },
  858. { 0xf5, "copy" },
  859. { 0xf6, "open" },
  860. { 0xf7, "paste" },
  861. { 0xf8, "find" },
  862. { 0xf9, "cut" },
  863. { 0xfa, "lf" },
  864. { 0xfb, "help" },
  865. { 0xfc, "meta_l" },
  866. { 0xfd, "meta_r" },
  867. { 0xfe, "compose" },
  868. #endif
  869. { 0, NULL },
  870. };
  871. static int get_keycode(const char *key)
  872. {
  873. const KeyDef *p;
  874. char *endp;
  875. int ret;
  876. for(p = key_defs; p->name != NULL; p++) {
  877. if (!strcmp(key, p->name))
  878. return p->keycode;
  879. }
  880. if (strstart(key, "0x", NULL)) {
  881. ret = strtoul(key, &endp, 0);
  882. if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
  883. return ret;
  884. }
  885. return -1;
  886. }
  887. #define MAX_KEYCODES 16
  888. static uint8_t keycodes[MAX_KEYCODES];
  889. static int nb_pending_keycodes;
  890. static QEMUTimer *key_timer;
  891. static void release_keys(void *opaque)
  892. {
  893. int keycode;
  894. while (nb_pending_keycodes > 0) {
  895. nb_pending_keycodes--;
  896. keycode = keycodes[nb_pending_keycodes];
  897. if (keycode & 0x80)
  898. kbd_put_keycode(0xe0);
  899. kbd_put_keycode(keycode | 0x80);
  900. }
  901. }
  902. static void do_sendkey(const char *string, int has_hold_time, int hold_time)
  903. {
  904. char keyname_buf[16];
  905. char *separator;
  906. int keyname_len, keycode, i;
  907. if (nb_pending_keycodes > 0) {
  908. qemu_del_timer(key_timer);
  909. release_keys(NULL);
  910. }
  911. if (!has_hold_time)
  912. hold_time = 100;
  913. i = 0;
  914. while (1) {
  915. separator = strchr(string, '-');
  916. keyname_len = separator ? separator - string : strlen(string);
  917. if (keyname_len > 0) {
  918. pstrcpy(keyname_buf, sizeof(keyname_buf), string);
  919. if (keyname_len > sizeof(keyname_buf) - 1) {
  920. term_printf("invalid key: '%s...'\n", keyname_buf);
  921. return;
  922. }
  923. if (i == MAX_KEYCODES) {
  924. term_printf("too many keys\n");
  925. return;
  926. }
  927. keyname_buf[keyname_len] = 0;
  928. keycode = get_keycode(keyname_buf);
  929. if (keycode < 0) {
  930. term_printf("unknown key: '%s'\n", keyname_buf);
  931. return;
  932. }
  933. keycodes[i++] = keycode;
  934. }
  935. if (!separator)
  936. break;
  937. string = separator + 1;
  938. }
  939. nb_pending_keycodes = i;
  940. /* key down events */
  941. for (i = 0; i < nb_pending_keycodes; i++) {
  942. keycode = keycodes[i];
  943. if (keycode & 0x80)
  944. kbd_put_keycode(0xe0);
  945. kbd_put_keycode(keycode & 0x7f);
  946. }
  947. /* delayed key up events */
  948. qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
  949. muldiv64(ticks_per_sec, hold_time, 1000));
  950. }
  951. static int mouse_button_state;
  952. static void do_mouse_move(const char *dx_str, const char *dy_str,
  953. const char *dz_str)
  954. {
  955. int dx, dy, dz;
  956. dx = strtol(dx_str, NULL, 0);
  957. dy = strtol(dy_str, NULL, 0);
  958. dz = 0;
  959. if (dz_str)
  960. dz = strtol(dz_str, NULL, 0);
  961. kbd_mouse_event(dx, dy, dz, mouse_button_state);
  962. }
  963. static void do_mouse_button(int button_state)
  964. {
  965. mouse_button_state = button_state;
  966. kbd_mouse_event(0, 0, 0, mouse_button_state);
  967. }
  968. static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
  969. {
  970. uint32_t val;
  971. int suffix;
  972. if (has_index) {
  973. cpu_outb(NULL, addr & 0xffff, index & 0xff);
  974. addr++;
  975. }
  976. addr &= 0xffff;
  977. switch(size) {
  978. default:
  979. case 1:
  980. val = cpu_inb(NULL, addr);
  981. suffix = 'b';
  982. break;
  983. case 2:
  984. val = cpu_inw(NULL, addr);
  985. suffix = 'w';
  986. break;
  987. case 4:
  988. val = cpu_inl(NULL, addr);
  989. suffix = 'l';
  990. break;
  991. }
  992. term_printf("port%c[0x%04x] = %#0*x\n",
  993. suffix, addr, size * 2, val);
  994. }
  995. /* boot_set handler */
  996. static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
  997. static void *boot_opaque;
  998. void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
  999. {
  1000. qemu_boot_set_handler = func;
  1001. boot_opaque = opaque;
  1002. }
  1003. static void do_boot_set(const char *bootdevice)
  1004. {
  1005. int res;
  1006. if (qemu_boot_set_handler) {
  1007. res = qemu_boot_set_handler(boot_opaque, bootdevice);
  1008. if (res == 0)
  1009. term_printf("boot device list now set to %s\n", bootdevice);
  1010. else
  1011. term_printf("setting boot device list failed with error %i\n", res);
  1012. } else {
  1013. term_printf("no function defined to set boot device list for this architecture\n");
  1014. }
  1015. }
  1016. static void do_system_reset(void)
  1017. {
  1018. qemu_system_reset_request();
  1019. }
  1020. static void do_system_powerdown(void)
  1021. {
  1022. qemu_system_powerdown_request();
  1023. }
  1024. #if defined(TARGET_I386)
  1025. static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
  1026. {
  1027. term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
  1028. addr,
  1029. pte & mask,
  1030. pte & PG_GLOBAL_MASK ? 'G' : '-',
  1031. pte & PG_PSE_MASK ? 'P' : '-',
  1032. pte & PG_DIRTY_MASK ? 'D' : '-',
  1033. pte & PG_ACCESSED_MASK ? 'A' : '-',
  1034. pte & PG_PCD_MASK ? 'C' : '-',
  1035. pte & PG_PWT_MASK ? 'T' : '-',
  1036. pte & PG_USER_MASK ? 'U' : '-',
  1037. pte & PG_RW_MASK ? 'W' : '-');
  1038. }
  1039. static void tlb_info(void)
  1040. {
  1041. CPUState *env;
  1042. int l1, l2;
  1043. uint32_t pgd, pde, pte;
  1044. env = mon_get_cpu();
  1045. if (!env)
  1046. return;
  1047. if (!(env->cr[0] & CR0_PG_MASK)) {
  1048. term_printf("PG disabled\n");
  1049. return;
  1050. }
  1051. pgd = env->cr[3] & ~0xfff;
  1052. for(l1 = 0; l1 < 1024; l1++) {
  1053. cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
  1054. pde = le32_to_cpu(pde);
  1055. if (pde & PG_PRESENT_MASK) {
  1056. if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
  1057. print_pte((l1 << 22), pde, ~((1 << 20) - 1));
  1058. } else {
  1059. for(l2 = 0; l2 < 1024; l2++) {
  1060. cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
  1061. (uint8_t *)&pte, 4);
  1062. pte = le32_to_cpu(pte);
  1063. if (pte & PG_PRESENT_MASK) {
  1064. print_pte((l1 << 22) + (l2 << 12),
  1065. pte & ~PG_PSE_MASK,
  1066. ~0xfff);
  1067. }
  1068. }
  1069. }
  1070. }
  1071. }
  1072. }
  1073. static void mem_print(uint32_t *pstart, int *plast_prot,
  1074. uint32_t end, int prot)
  1075. {
  1076. int prot1;
  1077. prot1 = *plast_prot;
  1078. if (prot != prot1) {
  1079. if (*pstart != -1) {
  1080. term_printf("%08x-%08x %08x %c%c%c\n",
  1081. *pstart, end, end - *pstart,
  1082. prot1 & PG_USER_MASK ? 'u' : '-',
  1083. 'r',
  1084. prot1 & PG_RW_MASK ? 'w' : '-');
  1085. }
  1086. if (prot != 0)
  1087. *pstart = end;
  1088. else
  1089. *pstart = -1;
  1090. *plast_prot = prot;
  1091. }
  1092. }
  1093. static void mem_info(void)
  1094. {
  1095. CPUState *env;
  1096. int l1, l2, prot, last_prot;
  1097. uint32_t pgd, pde, pte, start, end;
  1098. env = mon_get_cpu();
  1099. if (!env)
  1100. return;
  1101. if (!(env->cr[0] & CR0_PG_MASK)) {
  1102. term_printf("PG disabled\n");
  1103. return;
  1104. }
  1105. pgd = env->cr[3] & ~0xfff;
  1106. last_prot = 0;
  1107. start = -1;
  1108. for(l1 = 0; l1 < 1024; l1++) {
  1109. cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
  1110. pde = le32_to_cpu(pde);
  1111. end = l1 << 22;
  1112. if (pde & PG_PRESENT_MASK) {
  1113. if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
  1114. prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
  1115. mem_print(&start, &last_prot, end, prot);
  1116. } else {
  1117. for(l2 = 0; l2 < 1024; l2++) {
  1118. cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
  1119. (uint8_t *)&pte, 4);
  1120. pte = le32_to_cpu(pte);
  1121. end = (l1 << 22) + (l2 << 12);
  1122. if (pte & PG_PRESENT_MASK) {
  1123. prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
  1124. } else {
  1125. prot = 0;
  1126. }
  1127. mem_print(&start, &last_prot, end, prot);
  1128. }
  1129. }
  1130. } else {
  1131. prot = 0;
  1132. mem_print(&start, &last_prot, end, prot);
  1133. }
  1134. }
  1135. }
  1136. #endif
  1137. #if defined(TARGET_SH4)
  1138. static void print_tlb(int idx, tlb_t *tlb)
  1139. {
  1140. term_printf(" tlb%i:\t"
  1141. "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
  1142. "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
  1143. "dirty=%hhu writethrough=%hhu\n",
  1144. idx,
  1145. tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
  1146. tlb->v, tlb->sh, tlb->c, tlb->pr,
  1147. tlb->d, tlb->wt);
  1148. }
  1149. static void tlb_info(void)
  1150. {
  1151. CPUState *env = mon_get_cpu();
  1152. int i;
  1153. term_printf ("ITLB:\n");
  1154. for (i = 0 ; i < ITLB_SIZE ; i++)
  1155. print_tlb (i, &env->itlb[i]);
  1156. term_printf ("UTLB:\n");
  1157. for (i = 0 ; i < UTLB_SIZE ; i++)
  1158. print_tlb (i, &env->utlb[i]);
  1159. }
  1160. #endif
  1161. static void do_info_kqemu(void)
  1162. {
  1163. #ifdef USE_KQEMU
  1164. CPUState *env;
  1165. int val;
  1166. val = 0;
  1167. env = mon_get_cpu();
  1168. if (!env) {
  1169. term_printf("No cpu initialized yet");
  1170. return;
  1171. }
  1172. val = env->kqemu_enabled;
  1173. term_printf("kqemu support: ");
  1174. switch(val) {
  1175. default:
  1176. case 0:
  1177. term_printf("disabled\n");
  1178. break;
  1179. case 1:
  1180. term_printf("enabled for user code\n");
  1181. break;
  1182. case 2:
  1183. term_printf("enabled for user and kernel code\n");
  1184. break;
  1185. }
  1186. #else
  1187. term_printf("kqemu support: not compiled\n");
  1188. #endif
  1189. }
  1190. static void do_info_kvm(void)
  1191. {
  1192. #ifdef CONFIG_KVM
  1193. term_printf("kvm support: ");
  1194. if (kvm_enabled())
  1195. term_printf("enabled\n");
  1196. else
  1197. term_printf("disabled\n");
  1198. #else
  1199. term_printf("kvm support: not compiled\n");
  1200. #endif
  1201. }
  1202. #ifdef CONFIG_PROFILER
  1203. int64_t kqemu_time;
  1204. int64_t qemu_time;
  1205. int64_t kqemu_exec_count;
  1206. int64_t dev_time;
  1207. int64_t kqemu_ret_int_count;
  1208. int64_t kqemu_ret_excp_count;
  1209. int64_t kqemu_ret_intr_count;
  1210. static void do_info_profile(void)
  1211. {
  1212. int64_t total;
  1213. total = qemu_time;
  1214. if (total == 0)
  1215. total = 1;
  1216. term_printf("async time %" PRId64 " (%0.3f)\n",
  1217. dev_time, dev_time / (double)ticks_per_sec);
  1218. term_printf("qemu time %" PRId64 " (%0.3f)\n",
  1219. qemu_time, qemu_time / (double)ticks_per_sec);
  1220. term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
  1221. kqemu_time, kqemu_time / (double)ticks_per_sec,
  1222. kqemu_time / (double)total * 100.0,
  1223. kqemu_exec_count,
  1224. kqemu_ret_int_count,
  1225. kqemu_ret_excp_count,
  1226. kqemu_ret_intr_count);
  1227. qemu_time = 0;
  1228. kqemu_time = 0;
  1229. kqemu_exec_count = 0;
  1230. dev_time = 0;
  1231. kqemu_ret_int_count = 0;
  1232. kqemu_ret_excp_count = 0;
  1233. kqemu_ret_intr_count = 0;
  1234. #ifdef USE_KQEMU
  1235. kqemu_record_dump();
  1236. #endif
  1237. }
  1238. #else
  1239. static void do_info_profile(void)
  1240. {
  1241. term_printf("Internal profiler not compiled\n");
  1242. }
  1243. #endif
  1244. /* Capture support */
  1245. static LIST_HEAD (capture_list_head, CaptureState) capture_head;
  1246. static void do_info_capture (void)
  1247. {
  1248. int i;
  1249. CaptureState *s;
  1250. for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
  1251. term_printf ("[%d]: ", i);
  1252. s->ops.info (s->opaque);
  1253. }
  1254. }
  1255. static void do_stop_capture (int n)
  1256. {
  1257. int i;
  1258. CaptureState *s;
  1259. for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
  1260. if (i == n) {
  1261. s->ops.destroy (s->opaque);
  1262. LIST_REMOVE (s, entries);
  1263. qemu_free (s);
  1264. return;
  1265. }
  1266. }
  1267. }
  1268. #ifdef HAS_AUDIO
  1269. static void do_wav_capture (const char *path,
  1270. int has_freq, int freq,
  1271. int has_bits, int bits,
  1272. int has_channels, int nchannels)
  1273. {
  1274. CaptureState *s;
  1275. s = qemu_mallocz (sizeof (*s));
  1276. freq = has_freq ? freq : 44100;
  1277. bits = has_bits ? bits : 16;
  1278. nchannels = has_channels ? nchannels : 2;
  1279. if (wav_start_capture (s, path, freq, bits, nchannels)) {
  1280. term_printf ("Faied to add wave capture\n");
  1281. qemu_free (s);
  1282. }
  1283. LIST_INSERT_HEAD (&capture_head, s, entries);
  1284. }
  1285. #endif
  1286. #if defined(TARGET_I386)
  1287. static void do_inject_nmi(int cpu_index)
  1288. {
  1289. CPUState *env;
  1290. for (env = first_cpu; env != NULL; env = env->next_cpu)
  1291. if (env->cpu_index == cpu_index) {
  1292. cpu_interrupt(env, CPU_INTERRUPT_NMI);
  1293. break;
  1294. }
  1295. }
  1296. #endif
  1297. static void do_info_status(void)
  1298. {
  1299. if (vm_running)
  1300. term_printf("VM status: running\n");
  1301. else
  1302. term_printf("VM status: paused\n");
  1303. }
  1304. static void do_balloon(int value)
  1305. {
  1306. ram_addr_t target = value;
  1307. qemu_balloon(target << 20);
  1308. }
  1309. static void do_info_balloon(void)
  1310. {
  1311. ram_addr_t actual;
  1312. actual = qemu_balloon_status();
  1313. if (kvm_enabled() && !kvm_has_sync_mmu())
  1314. term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
  1315. else if (actual == 0)
  1316. term_printf("Ballooning not activated in VM\n");
  1317. else
  1318. term_printf("balloon: actual=%d\n", (int)(actual >> 20));
  1319. }
  1320. /* Please update qemu-doc.texi when adding or changing commands */
  1321. static const term_cmd_t term_cmds[] = {
  1322. { "help|?", "s?", do_help,
  1323. "[cmd]", "show the help" },
  1324. { "commit", "s", do_commit,
  1325. "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
  1326. { "info", "s?", do_info,
  1327. "subcommand", "show various information about the system state" },
  1328. { "q|quit", "", do_quit,
  1329. "", "quit the emulator" },
  1330. { "eject", "-fB", do_eject,
  1331. "[-f] device", "eject a removable medium (use -f to force it)" },
  1332. { "change", "BFs?", do_change,
  1333. "device filename [format]", "change a removable medium, optional format" },
  1334. { "screendump", "F", do_screen_dump,
  1335. "filename", "save screen into PPM image 'filename'" },
  1336. { "logfile", "F", do_logfile,
  1337. "filename", "output logs to 'filename'" },
  1338. { "log", "s", do_log,
  1339. "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
  1340. { "savevm", "s?", do_savevm,
  1341. "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
  1342. { "loadvm", "s", do_loadvm,
  1343. "tag|id", "restore a VM snapshot from its tag or id" },
  1344. { "delvm", "s", do_delvm,
  1345. "tag|id", "delete a VM snapshot from its tag or id" },
  1346. { "stop", "", do_stop,
  1347. "", "stop emulation", },
  1348. { "c|cont", "", do_cont,
  1349. "", "resume emulation", },
  1350. #ifdef CONFIG_GDBSTUB
  1351. { "gdbserver", "s?", do_gdbserver,
  1352. "[port]", "start gdbserver session (default port=1234)", },
  1353. #endif
  1354. { "x", "/l", do_memory_dump,
  1355. "/fmt addr", "virtual memory dump starting at 'addr'", },
  1356. { "xp", "/l", do_physical_memory_dump,
  1357. "/fmt addr", "physical memory dump starting at 'addr'", },
  1358. { "p|print", "/l", do_print,
  1359. "/fmt expr", "print expression value (use $reg for CPU register access)", },
  1360. { "i", "/ii.", do_ioport_read,
  1361. "/fmt addr", "I/O port read" },
  1362. { "sendkey", "si?", do_sendkey,
  1363. "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
  1364. { "system_reset", "", do_system_reset,
  1365. "", "reset the system" },
  1366. { "system_powerdown", "", do_system_powerdown,
  1367. "", "send system power down event" },
  1368. { "sum", "ii", do_sum,
  1369. "addr size", "compute the checksum of a memory region" },
  1370. { "usb_add", "s", do_usb_add,
  1371. "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
  1372. { "usb_del", "s", do_usb_del,
  1373. "device", "remove USB device 'bus.addr'" },
  1374. { "cpu", "i", do_cpu_set,
  1375. "index", "set the default CPU" },
  1376. { "mouse_move", "sss?", do_mouse_move,
  1377. "dx dy [dz]", "send mouse move events" },
  1378. { "mouse_button", "i", do_mouse_button,
  1379. "state", "change mouse button state (1=L, 2=M, 4=R)" },
  1380. { "mouse_set", "i", do_mouse_set,
  1381. "index", "set which mouse device receives events" },
  1382. #ifdef HAS_AUDIO
  1383. { "wavcapture", "si?i?i?", do_wav_capture,
  1384. "path [frequency bits channels]",
  1385. "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
  1386. #endif
  1387. { "stopcapture", "i", do_stop_capture,
  1388. "capture index", "stop capture" },
  1389. { "memsave", "lis", do_memory_save,
  1390. "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
  1391. { "pmemsave", "lis", do_physical_memory_save,
  1392. "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
  1393. { "boot_set", "s", do_boot_set,
  1394. "bootdevice", "define new values for the boot device list" },
  1395. #if defined(TARGET_I386)
  1396. { "nmi", "i", do_inject_nmi,
  1397. "cpu", "inject an NMI on the given CPU", },
  1398. #endif
  1399. { "migrate", "-ds", do_migrate,
  1400. "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
  1401. { "migrate_cancel", "", do_migrate_cancel,
  1402. "", "cancel the current VM migration" },
  1403. { "migrate_set_speed", "s", do_migrate_set_speed,
  1404. "value", "set maximum speed (in bytes) for migrations" },
  1405. #if defined(TARGET_I386)
  1406. { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
  1407. "[file=file][,if=type][,bus=n]\n"
  1408. "[,unit=m][,media=d][index=i]\n"
  1409. "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
  1410. "[snapshot=on|off][,cache=on|off]",
  1411. "add drive to PCI storage controller" },
  1412. { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
  1413. { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
  1414. { "host_net_add", "ss", net_host_device_add,
  1415. "[tap,user,socket,vde] options", "add host VLAN client" },
  1416. { "host_net_remove", "is", net_host_device_remove,
  1417. "vlan_id name", "remove host VLAN client" },
  1418. #endif
  1419. { "balloon", "i", do_balloon,
  1420. "target", "request VM to change it's memory allocation (in MB)" },
  1421. { "set_link", "ss", do_set_link,
  1422. "name [up|down]", "change the link status of a network adapter" },
  1423. { NULL, NULL, },
  1424. };
  1425. /* Please update qemu-doc.texi when adding or changing commands */
  1426. static const term_cmd_t info_cmds[] = {
  1427. { "version", "", do_info_version,
  1428. "", "show the version of QEMU" },
  1429. { "network", "", do_info_network,
  1430. "", "show the network state" },
  1431. { "chardev", "", qemu_chr_info,
  1432. "", "show the character devices" },
  1433. { "block", "", do_info_block,
  1434. "", "show the block devices" },
  1435. { "blockstats", "", do_info_blockstats,
  1436. "", "show block device statistics" },
  1437. { "registers", "", do_info_registers,
  1438. "", "show the cpu registers" },
  1439. { "cpus", "", do_info_cpus,
  1440. "", "show infos for each CPU" },
  1441. { "history", "", do_info_history,
  1442. "", "show the command line history", },
  1443. { "irq", "", irq_info,
  1444. "", "show the interrupts statistics (if available)", },
  1445. { "pic", "", pic_info,
  1446. "", "show i8259 (PIC) state", },
  1447. { "pci", "", pci_info,
  1448. "", "show PCI info", },
  1449. #if defined(TARGET_I386) || defined(TARGET_SH4)
  1450. { "tlb", "", tlb_info,
  1451. "", "show virtual to physical memory mappings", },
  1452. #endif
  1453. #if defined(TARGET_I386)
  1454. { "mem", "", mem_info,
  1455. "", "show the active virtual memory mappings", },
  1456. { "hpet", "", do_info_hpet,
  1457. "", "show state of HPET", },
  1458. #endif
  1459. { "jit", "", do_info_jit,
  1460. "", "show dynamic compiler info", },
  1461. { "kqemu", "", do_info_kqemu,
  1462. "", "show KQEMU information", },
  1463. { "kvm", "", do_info_kvm,
  1464. "", "show KVM information", },
  1465. { "usb", "", usb_info,
  1466. "", "show guest USB devices", },
  1467. { "usbhost", "", usb_host_info,
  1468. "", "show host USB devices", },
  1469. { "profile", "", do_info_profile,
  1470. "", "show profiling information", },
  1471. { "capture", "", do_info_capture,
  1472. "", "show capture information" },
  1473. { "snapshots", "", do_info_snapshots,
  1474. "", "show the currently saved VM snapshots" },
  1475. { "status", "", do_info_status,
  1476. "", "show the current VM status (running|paused)" },
  1477. { "pcmcia", "", pcmcia_info,
  1478. "", "show guest PCMCIA status" },
  1479. { "mice", "", do_info_mice,
  1480. "", "show which guest mouse is receiving events" },
  1481. { "vnc", "", do_info_vnc,
  1482. "", "show the vnc server status"},
  1483. { "name", "", do_info_name,
  1484. "", "show the current VM name" },
  1485. { "uuid", "", do_info_uuid,
  1486. "", "show the current VM UUID" },
  1487. #if defined(TARGET_PPC)
  1488. { "cpustats", "", do_info_cpu_stats,
  1489. "", "show CPU statistics", },
  1490. #endif
  1491. #if defined(CONFIG_SLIRP)
  1492. { "slirp", "", do_info_slirp,
  1493. "", "show SLIRP statistics", },
  1494. #endif
  1495. { "migrate", "", do_info_migrate, "", "show migration status" },
  1496. { "balloon", "", do_info_balloon,
  1497. "", "show balloon information" },
  1498. { NULL, NULL, },
  1499. };
  1500. /*******************************************************************/
  1501. static const char *pch;
  1502. static jmp_buf expr_env;
  1503. #define MD_TLONG 0
  1504. #define MD_I32 1
  1505. typedef struct MonitorDef {
  1506. const char *name;
  1507. int offset;
  1508. target_long (*get_value)(const struct MonitorDef *md, int val);
  1509. int type;
  1510. } MonitorDef;
  1511. #if defined(TARGET_I386)
  1512. static target_long monitor_get_pc (const struct MonitorDef *md, int val)
  1513. {
  1514. CPUState *env = mon_get_cpu();
  1515. if (!env)
  1516. return 0;
  1517. return env->eip + env->segs[R_CS].base;
  1518. }
  1519. #endif
  1520. #if defined(TARGET_PPC)
  1521. static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
  1522. {
  1523. CPUState *env = mon_get_cpu();
  1524. unsigned int u;
  1525. int i;
  1526. if (!env)
  1527. return 0;
  1528. u = 0;
  1529. for (i = 0; i < 8; i++)
  1530. u |= env->crf[i] << (32 - (4 * i));
  1531. return u;
  1532. }
  1533. static target_long monitor_get_msr (const struct MonitorDef *md, int val)
  1534. {
  1535. CPUState *env = mon_get_cpu();
  1536. if (!env)
  1537. return 0;
  1538. return env->msr;
  1539. }
  1540. static target_long monitor_get_xer (const struct MonitorDef *md, int val)
  1541. {
  1542. CPUState *env = mon_get_cpu();
  1543. if (!env)
  1544. return 0;
  1545. return env->xer;
  1546. }
  1547. static target_long monitor_get_decr (const struct MonitorDef *md, int val)
  1548. {
  1549. CPUState *env = mon_get_cpu();
  1550. if (!env)
  1551. return 0;
  1552. return cpu_ppc_load_decr(env);
  1553. }
  1554. static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
  1555. {
  1556. CPUState *env = mon_get_cpu();
  1557. if (!env)
  1558. return 0;
  1559. return cpu_ppc_load_tbu(env);
  1560. }
  1561. static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
  1562. {
  1563. CPUState *env = mon_get_cpu();
  1564. if (!env)
  1565. return 0;
  1566. return cpu_ppc_load_tbl(env);
  1567. }
  1568. #endif
  1569. #if defined(TARGET_SPARC)
  1570. #ifndef TARGET_SPARC64
  1571. static target_long monitor_get_psr (const struct MonitorDef *md, int val)
  1572. {
  1573. CPUState *env = mon_get_cpu();
  1574. if (!env)
  1575. return 0;
  1576. return GET_PSR(env);
  1577. }
  1578. #endif
  1579. static target_long monitor_get_reg(const struct MonitorDef *md, int val)
  1580. {
  1581. CPUState *env = mon_get_cpu();
  1582. if (!env)
  1583. return 0;
  1584. return env->regwptr[val];
  1585. }
  1586. #endif
  1587. static const MonitorDef monitor_defs[] = {
  1588. #ifdef TARGET_I386
  1589. #define SEG(name, seg) \
  1590. { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
  1591. { name ".base", offsetof(CPUState, segs[seg].base) },\
  1592. { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
  1593. { "eax", offsetof(CPUState, regs[0]) },
  1594. { "ecx", offsetof(CPUState, regs[1]) },
  1595. { "edx", offsetof(CPUState, regs[2]) },
  1596. { "ebx", offsetof(CPUState, regs[3]) },
  1597. { "esp|sp", offsetof(CPUState, regs[4]) },
  1598. { "ebp|fp", offsetof(CPUState, regs[5]) },
  1599. { "esi", offsetof(CPUState, regs[6]) },
  1600. { "edi", offsetof(CPUState, regs[7]) },
  1601. #ifdef TARGET_X86_64
  1602. { "r8", offsetof(CPUState, regs[8]) },
  1603. { "r9", offsetof(CPUState, regs[9]) },
  1604. { "r10", offsetof(CPUState, regs[10]) },
  1605. { "r11", offsetof(CPUState, regs[11]) },
  1606. { "r12", offsetof(CPUState, regs[12]) },
  1607. { "r13", offsetof(CPUState, regs[13]) },
  1608. { "r14", offsetof(CPUState, regs[14]) },
  1609. { "r15", offsetof(CPUState, regs[15]) },
  1610. #endif
  1611. { "eflags", offsetof(CPUState, eflags) },
  1612. { "eip", offsetof(CPUState, eip) },
  1613. SEG("cs", R_CS)
  1614. SEG("ds", R_DS)
  1615. SEG("es", R_ES)
  1616. SEG("ss", R_SS)
  1617. SEG("fs", R_FS)
  1618. SEG("gs", R_GS)
  1619. { "pc", 0, monitor_get_pc, },
  1620. #elif defined(TARGET_PPC)
  1621. /* General purpose registers */
  1622. { "r0", offsetof(CPUState, gpr[0]) },
  1623. { "r1", offsetof(CPUState, gpr[1]) },
  1624. { "r2", offsetof(CPUState, gpr[2]) },
  1625. { "r3", offsetof(CPUState, gpr[3]) },
  1626. { "r4", offsetof(CPUState, gpr[4]) },
  1627. { "r5", offsetof(CPUState, gpr[5]) },
  1628. { "r6", offsetof(CPUState, gpr[6]) },
  1629. { "r7", offsetof(CPUState, gpr[7]) },
  1630. { "r8", offsetof(CPUState, gpr[8]) },
  1631. { "r9", offsetof(CPUState, gpr[9]) },
  1632. { "r10", offsetof(CPUState, gpr[10]) },
  1633. { "r11", offsetof(CPUState, gpr[11]) },
  1634. { "r12", offsetof(CPUState, gpr[12]) },
  1635. { "r13", offsetof(CPUState, gpr[13]) },
  1636. { "r14", offsetof(CPUState, gpr[14]) },
  1637. { "r15", offsetof(CPUState, gpr[15]) },
  1638. { "r16", offsetof(CPUState, gpr[16]) },
  1639. { "r17", offsetof(CPUState, gpr[17]) },
  1640. { "r18", offsetof(CPUState, gpr[18]) },
  1641. { "r19", offsetof(CPUState, gpr[19]) },
  1642. { "r20", offsetof(CPUState, gpr[20]) },
  1643. { "r21", offsetof(CPUState, gpr[21]) },
  1644. { "r22", offsetof(CPUState, gpr[22]) },
  1645. { "r23", offsetof(CPUState, gpr[23]) },
  1646. { "r24", offsetof(CPUState, gpr[24]) },
  1647. { "r25", offsetof(CPUState, gpr[25]) },
  1648. { "r26", offsetof(CPUState, gpr[26]) },
  1649. { "r27", offsetof(CPUState, gpr[27]) },
  1650. { "r28", offsetof(CPUState, gpr[28]) },
  1651. { "r29", offsetof(CPUState, gpr[29]) },
  1652. { "r30", offsetof(CPUState, gpr[30]) },
  1653. { "r31", offsetof(CPUState, gpr[31]) },
  1654. /* Floating point registers */
  1655. { "f0", offsetof(CPUState, fpr[0]) },
  1656. { "f1", offsetof(CPUState, fpr[1]) },
  1657. { "f2", offsetof(CPUState, fpr[2]) },
  1658. { "f3", offsetof(CPUState, fpr[3]) },
  1659. { "f4", offsetof(CPUState, fpr[4]) },
  1660. { "f5", offsetof(CPUState, fpr[5]) },
  1661. { "f6", offsetof(CPUState, fpr[6]) },
  1662. { "f7", offsetof(CPUState, fpr[7]) },
  1663. { "f8", offsetof(CPUState, fpr[8]) },
  1664. { "f9", offsetof(CPUState, fpr[9]) },
  1665. { "f10", offsetof(CPUState, fpr[10]) },
  1666. { "f11", offsetof(CPUState, fpr[11]) },
  1667. { "f12", offsetof(CPUState, fpr[12]) },
  1668. { "f13", offsetof(CPUState, fpr[13]) },
  1669. { "f14", offsetof(CPUState, fpr[14]) },
  1670. { "f15", offsetof(CPUState, fpr[15]) },
  1671. { "f16", offsetof(CPUState, fpr[16]) },
  1672. { "f17", offsetof(CPUState, fpr[17]) },
  1673. { "f18", offsetof(CPUState, fpr[18]) },
  1674. { "f19", offsetof(CPUState, fpr[19]) },
  1675. { "f20", offsetof(CPUState, fpr[20]) },
  1676. { "f21", offsetof(CPUState, fpr[21]) },
  1677. { "f22", offsetof(CPUState, fpr[22]) },
  1678. { "f23", offsetof(CPUState, fpr[23]) },
  1679. { "f24", offsetof(CPUState, fpr[24]) },
  1680. { "f25", offsetof(CPUState, fpr[25]) },
  1681. { "f26", offsetof(CPUState, fpr[26]) },
  1682. { "f27", offsetof(CPUState, fpr[27]) },
  1683. { "f28", offsetof(CPUState, fpr[28]) },
  1684. { "f29", offsetof(CPUState, fpr[29]) },
  1685. { "f30", offsetof(CPUState, fpr[30]) },
  1686. { "f31", offsetof(CPUState, fpr[31]) },
  1687. { "fpscr", offsetof(CPUState, fpscr) },
  1688. /* Next instruction pointer */
  1689. { "nip|pc", offsetof(CPUState, nip) },
  1690. { "lr", offsetof(CPUState, lr) },
  1691. { "ctr", offsetof(CPUState, ctr) },
  1692. { "decr", 0, &monitor_get_decr, },
  1693. { "ccr", 0, &monitor_get_ccr, },
  1694. /* Machine state register */
  1695. { "msr", 0, &monitor_get_msr, },
  1696. { "xer", 0, &monitor_get_xer, },
  1697. { "tbu", 0, &monitor_get_tbu, },
  1698. { "tbl", 0, &monitor_get_tbl, },
  1699. #if defined(TARGET_PPC64)
  1700. /* Address space register */
  1701. { "asr", offsetof(CPUState, asr) },
  1702. #endif
  1703. /* Segment registers */
  1704. { "sdr1", offsetof(CPUState, sdr1) },
  1705. { "sr0", offsetof(CPUState, sr[0]) },
  1706. { "sr1", offsetof(CPUState, sr[1]) },
  1707. { "sr2", offsetof(CPUState, sr[2]) },
  1708. { "sr3", offsetof(CPUState, sr[3]) },
  1709. { "sr4", offsetof(CPUState, sr[4]) },
  1710. { "sr5", offsetof(CPUState, sr[5]) },
  1711. { "sr6", offsetof(CPUState, sr[6]) },
  1712. { "sr7", offsetof(CPUState, sr[7]) },
  1713. { "sr8", offsetof(CPUState, sr[8]) },
  1714. { "sr9", offsetof(CPUState, sr[9]) },
  1715. { "sr10", offsetof(CPUState, sr[10]) },
  1716. { "sr11", offsetof(CPUState, sr[11]) },
  1717. { "sr12", offsetof(CPUState, sr[12]) },
  1718. { "sr13", offsetof(CPUState, sr[13]) },
  1719. { "sr14", offsetof(CPUState, sr[14]) },
  1720. { "sr15", offsetof(CPUState, sr[15]) },
  1721. /* Too lazy to put BATs and SPRs ... */
  1722. #elif defined(TARGET_SPARC)
  1723. { "g0", offsetof(CPUState, gregs[0]) },
  1724. { "g1", offsetof(CPUState, gregs[1]) },
  1725. { "g2", offsetof(CPUState, gregs[2]) },
  1726. { "g3", offsetof(CPUState, gregs[3]) },
  1727. { "g4", offsetof(CPUState, gregs[4]) },
  1728. { "g5", offsetof(CPUState, gregs[5]) },
  1729. { "g6", offsetof(CPUState, gregs[6]) },
  1730. { "g7", offsetof(CPUState, gregs[7]) },
  1731. { "o0", 0, monitor_get_reg },
  1732. { "o1", 1, monitor_get_reg },
  1733. { "o2", 2, monitor_get_reg },
  1734. { "o3", 3, monitor_get_reg },
  1735. { "o4", 4, monitor_get_reg },
  1736. { "o5", 5, monitor_get_reg },
  1737. { "o6", 6, monitor_get_reg },
  1738. { "o7", 7, monitor_get_reg },
  1739. { "l0", 8, monitor_get_reg },
  1740. { "l1", 9, monitor_get_reg },
  1741. { "l2", 10, monitor_get_reg },
  1742. { "l3", 11, monitor_get_reg },
  1743. { "l4", 12, monitor_get_reg },
  1744. { "l5", 13, monitor_get_reg },
  1745. { "l6", 14, monitor_get_reg },
  1746. { "l7", 15, monitor_get_reg },
  1747. { "i0", 16, monitor_get_reg },
  1748. { "i1", 17, monitor_get_reg },
  1749. { "i2", 18, monitor_get_reg },
  1750. { "i3", 19, monitor_get_reg },
  1751. { "i4", 20, monitor_get_reg },
  1752. { "i5", 21, monitor_get_reg },
  1753. { "i6", 22, monitor_get_reg },
  1754. { "i7", 23, monitor_get_reg },
  1755. { "pc", offsetof(CPUState, pc) },
  1756. { "npc", offsetof(CPUState, npc) },
  1757. { "y", offsetof(CPUState, y) },
  1758. #ifndef TARGET_SPARC64
  1759. { "psr", 0, &monitor_get_psr, },
  1760. { "wim", offsetof(CPUState, wim) },
  1761. #endif
  1762. { "tbr", offsetof(CPUState, tbr) },
  1763. { "fsr", offsetof(CPUState, fsr) },
  1764. { "f0", offsetof(CPUState, fpr[0]) },
  1765. { "f1", offsetof(CPUState, fpr[1]) },
  1766. { "f2", offsetof(CPUState, fpr[2]) },
  1767. { "f3", offsetof(CPUState, fpr[3]) },
  1768. { "f4", offsetof(CPUState, fpr[4]) },
  1769. { "f5", offsetof(CPUState, fpr[5]) },
  1770. { "f6", offsetof(CPUState, fpr[6]) },
  1771. { "f7", offsetof(CPUState, fpr[7]) },
  1772. { "f8", offsetof(CPUState, fpr[8]) },
  1773. { "f9", offsetof(CPUState, fpr[9]) },
  1774. { "f10", offsetof(CPUState, fpr[10]) },
  1775. { "f11", offsetof(CPUState, fpr[11]) },
  1776. { "f12", offsetof(CPUState, fpr[12]) },
  1777. { "f13", offsetof(CPUState, fpr[13]) },
  1778. { "f14", offsetof(CPUState, fpr[14]) },
  1779. { "f15", offsetof(CPUState, fpr[15]) },
  1780. { "f16", offsetof(CPUState, fpr[16]) },
  1781. { "f17", offsetof(CPUState, fpr[17]) },
  1782. { "f18", offsetof(CPUState, fpr[18]) },
  1783. { "f19", offsetof(CPUState, fpr[19]) },
  1784. { "f20", offsetof(CPUState, fpr[20]) },
  1785. { "f21", offsetof(CPUState, fpr[21]) },
  1786. { "f22", offsetof(CPUState, fpr[22]) },
  1787. { "f23", offsetof(CPUState, fpr[23]) },
  1788. { "f24", offsetof(CPUState, fpr[24]) },
  1789. { "f25", offsetof(CPUState, fpr[25]) },
  1790. { "f26", offsetof(CPUState, fpr[26]) },
  1791. { "f27", offsetof(CPUState, fpr[27]) },
  1792. { "f28", offsetof(CPUState, fpr[28]) },
  1793. { "f29", offsetof(CPUState, fpr[29]) },
  1794. { "f30", offsetof(CPUState, fpr[30]) },
  1795. { "f31", offsetof(CPUState, fpr[31]) },
  1796. #ifdef TARGET_SPARC64
  1797. { "f32", offsetof(CPUState, fpr[32]) },
  1798. { "f34", offsetof(CPUState, fpr[34]) },
  1799. { "f36", offsetof(CPUState, fpr[36]) },
  1800. { "f38", offsetof(CPUState, fpr[38]) },
  1801. { "f40", offsetof(CPUState, fpr[40]) },
  1802. { "f42", offsetof(CPUState, fpr[42]) },
  1803. { "f44", offsetof(CPUState, fpr[44]) },
  1804. { "f46", offsetof(CPUState, fpr[46]) },
  1805. { "f48", offsetof(CPUState, fpr[48]) },
  1806. { "f50", offsetof(CPUState, fpr[50]) },
  1807. { "f52", offsetof(CPUState, fpr[52]) },
  1808. { "f54", offsetof(CPUState, fpr[54]) },
  1809. { "f56", offsetof(CPUState, fpr[56]) },
  1810. { "f58", offsetof(CPUState, fpr[58]) },
  1811. { "f60", offsetof(CPUState, fpr[60]) },
  1812. { "f62", offsetof(CPUState, fpr[62]) },
  1813. { "asi", offsetof(CPUState, asi) },
  1814. { "pstate", offsetof(CPUState, pstate) },
  1815. { "cansave", offsetof(CPUState, cansave) },
  1816. { "canrestore", offsetof(CPUState, canrestore) },
  1817. { "otherwin", offsetof(CPUState, otherwin) },
  1818. { "wstate", offsetof(CPUState, wstate) },
  1819. { "cleanwin", offsetof(CPUState, cleanwin) },
  1820. { "fprs", offsetof(CPUState, fprs) },
  1821. #endif
  1822. #endif
  1823. { NULL },
  1824. };
  1825. static void expr_error(const char *msg)
  1826. {
  1827. term_printf("%s\n", msg);
  1828. longjmp(expr_env, 1);
  1829. }
  1830. /* return 0 if OK, -1 if not found, -2 if no CPU defined */
  1831. static int get_monitor_def(target_long *pval, const char *name)
  1832. {
  1833. const MonitorDef *md;
  1834. void *ptr;
  1835. for(md = monitor_defs; md->name != NULL; md++) {
  1836. if (compare_cmd(name, md->name)) {
  1837. if (md->get_value) {
  1838. *pval = md->get_value(md, md->offset);
  1839. } else {
  1840. CPUState *env = mon_get_cpu();
  1841. if (!env)
  1842. return -2;
  1843. ptr = (uint8_t *)env + md->offset;
  1844. switch(md->type) {
  1845. case MD_I32:
  1846. *pval = *(int32_t *)ptr;
  1847. break;
  1848. case MD_TLONG:
  1849. *pval = *(target_long *)ptr;
  1850. break;
  1851. default:
  1852. *pval = 0;
  1853. break;
  1854. }
  1855. }
  1856. return 0;
  1857. }
  1858. }
  1859. return -1;
  1860. }
  1861. static void next(void)
  1862. {
  1863. if (pch != '\0') {
  1864. pch++;
  1865. while (qemu_isspace(*pch))
  1866. pch++;
  1867. }
  1868. }
  1869. static int64_t expr_sum(void);
  1870. static int64_t expr_unary(void)
  1871. {
  1872. int64_t n;
  1873. char *p;
  1874. int ret;
  1875. switch(*pch) {
  1876. case '+':
  1877. next();
  1878. n = expr_unary();
  1879. break;
  1880. case '-':
  1881. next();
  1882. n = -expr_unary();
  1883. break;
  1884. case '~':
  1885. next();
  1886. n = ~expr_unary();
  1887. break;
  1888. case '(':
  1889. next();
  1890. n = expr_sum();
  1891. if (*pch != ')') {
  1892. expr_error("')' expected");
  1893. }
  1894. next();
  1895. break;
  1896. case '\'':
  1897. pch++;
  1898. if (*pch == '\0')
  1899. expr_error("character constant expected");
  1900. n = *pch;
  1901. pch++;
  1902. if (*pch != '\'')
  1903. expr_error("missing terminating \' character");
  1904. next();
  1905. break;
  1906. case '$':
  1907. {
  1908. char buf[128], *q;
  1909. target_long reg=0;
  1910. pch++;
  1911. q = buf;
  1912. while ((*pch >= 'a' && *pch <= 'z') ||
  1913. (*pch >= 'A' && *pch <= 'Z') ||
  1914. (*pch >= '0' && *pch <= '9') ||
  1915. *pch == '_' || *pch == '.') {
  1916. if ((q - buf) < sizeof(buf) - 1)
  1917. *q++ = *pch;
  1918. pch++;
  1919. }
  1920. while (qemu_isspace(*pch))
  1921. pch++;
  1922. *q = 0;
  1923. ret = get_monitor_def(&reg, buf);
  1924. if (ret == -1)
  1925. expr_error("unknown register");
  1926. else if (ret == -2)
  1927. expr_error("no cpu defined");
  1928. n = reg;
  1929. }
  1930. break;
  1931. case '\0':
  1932. expr_error("unexpected end of expression");
  1933. n = 0;
  1934. break;
  1935. default:
  1936. #if TARGET_PHYS_ADDR_BITS > 32
  1937. n = strtoull(pch, &p, 0);
  1938. #else
  1939. n = strtoul(pch, &p, 0);
  1940. #endif
  1941. if (pch == p) {
  1942. expr_error("invalid char in expression");
  1943. }
  1944. pch = p;
  1945. while (qemu_isspace(*pch))
  1946. pch++;
  1947. break;
  1948. }
  1949. return n;
  1950. }
  1951. static int64_t expr_prod(void)
  1952. {
  1953. int64_t val, val2;
  1954. int op;
  1955. val = expr_unary();
  1956. for(;;) {
  1957. op = *pch;
  1958. if (op != '*' && op != '/' && op != '%')
  1959. break;
  1960. next();
  1961. val2 = expr_unary();
  1962. switch(op) {
  1963. default:
  1964. case '*':
  1965. val *= val2;
  1966. break;
  1967. case '/':
  1968. case '%':
  1969. if (val2 == 0)
  1970. expr_error("division by zero");
  1971. if (op == '/')
  1972. val /= val2;
  1973. else
  1974. val %= val2;
  1975. break;
  1976. }
  1977. }
  1978. return val;
  1979. }
  1980. static int64_t expr_logic(void)
  1981. {
  1982. int64_t val, val2;
  1983. int op;
  1984. val = expr_prod();
  1985. for(;;) {
  1986. op = *pch;
  1987. if (op != '&' && op != '|' && op != '^')
  1988. break;
  1989. next();
  1990. val2 = expr_prod();
  1991. switch(op) {
  1992. default:
  1993. case '&':
  1994. val &= val2;
  1995. break;
  1996. case '|':
  1997. val |= val2;
  1998. break;
  1999. case '^':
  2000. val ^= val2;
  2001. break;
  2002. }
  2003. }
  2004. return val;
  2005. }
  2006. static int64_t expr_sum(void)
  2007. {
  2008. int64_t val, val2;
  2009. int op;
  2010. val = expr_logic();
  2011. for(;;) {
  2012. op = *pch;
  2013. if (op != '+' && op != '-')
  2014. break;
  2015. next();
  2016. val2 = expr_logic();
  2017. if (op == '+')
  2018. val += val2;
  2019. else
  2020. val -= val2;
  2021. }
  2022. return val;
  2023. }
  2024. static int get_expr(int64_t *pval, const char **pp)
  2025. {
  2026. pch = *pp;
  2027. if (setjmp(expr_env)) {
  2028. *pp = pch;
  2029. return -1;
  2030. }
  2031. while (qemu_isspace(*pch))
  2032. pch++;
  2033. *pval = expr_sum();
  2034. *pp = pch;
  2035. return 0;
  2036. }
  2037. static int get_str(char *buf, int buf_size, const char **pp)
  2038. {
  2039. const char *p;
  2040. char *q;
  2041. int c;
  2042. q = buf;
  2043. p = *pp;
  2044. while (qemu_isspace(*p))
  2045. p++;
  2046. if (*p == '\0') {
  2047. fail:
  2048. *q = '\0';
  2049. *pp = p;
  2050. return -1;
  2051. }
  2052. if (*p == '\"') {
  2053. p++;
  2054. while (*p != '\0' && *p != '\"') {
  2055. if (*p == '\\') {
  2056. p++;
  2057. c = *p++;
  2058. switch(c) {
  2059. case 'n':
  2060. c = '\n';
  2061. break;
  2062. case 'r':
  2063. c = '\r';
  2064. break;
  2065. case '\\':
  2066. case '\'':
  2067. case '\"':
  2068. break;
  2069. default:
  2070. qemu_printf("unsupported escape code: '\\%c'\n", c);
  2071. goto fail;
  2072. }
  2073. if ((q - buf) < buf_size - 1) {
  2074. *q++ = c;
  2075. }
  2076. } else {
  2077. if ((q - buf) < buf_size - 1) {
  2078. *q++ = *p;
  2079. }
  2080. p++;
  2081. }
  2082. }
  2083. if (*p != '\"') {
  2084. qemu_printf("unterminated string\n");
  2085. goto fail;
  2086. }
  2087. p++;
  2088. } else {
  2089. while (*p != '\0' && !qemu_isspace(*p)) {
  2090. if ((q - buf) < buf_size - 1) {
  2091. *q++ = *p;
  2092. }
  2093. p++;
  2094. }
  2095. }
  2096. *q = '\0';
  2097. *pp = p;
  2098. return 0;
  2099. }
  2100. static int default_fmt_format = 'x';
  2101. static int default_fmt_size = 4;
  2102. #define MAX_ARGS 16
  2103. static void monitor_handle_command(const char *cmdline)
  2104. {
  2105. const char *p, *pstart, *typestr;
  2106. char *q;
  2107. int c, nb_args, len, i, has_arg;
  2108. const term_cmd_t *cmd;
  2109. char cmdname[256];
  2110. char buf[1024];
  2111. void *str_allocated[MAX_ARGS];
  2112. void *args[MAX_ARGS];
  2113. void (*handler_0)(void);
  2114. void (*handler_1)(void *arg0);
  2115. void (*handler_2)(void *arg0, void *arg1);
  2116. void (*handler_3)(void *arg0, void *arg1, void *arg2);
  2117. void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
  2118. void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
  2119. void *arg4);
  2120. void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
  2121. void *arg4, void *arg5);
  2122. void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
  2123. void *arg4, void *arg5, void *arg6);
  2124. #ifdef DEBUG
  2125. term_printf("command='%s'\n", cmdline);
  2126. #endif
  2127. /* extract the command name */
  2128. p = cmdline;
  2129. q = cmdname;
  2130. while (qemu_isspace(*p))
  2131. p++;
  2132. if (*p == '\0')
  2133. return;
  2134. pstart = p;
  2135. while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
  2136. p++;
  2137. len = p - pstart;
  2138. if (len > sizeof(cmdname) - 1)
  2139. len = sizeof(cmdname) - 1;
  2140. memcpy(cmdname, pstart, len);
  2141. cmdname[len] = '\0';
  2142. /* find the command */
  2143. for(cmd = term_cmds; cmd->name != NULL; cmd++) {
  2144. if (compare_cmd(cmdname, cmd->name))
  2145. goto found;
  2146. }
  2147. term_printf("unknown command: '%s'\n", cmdname);
  2148. return;
  2149. found:
  2150. for(i = 0; i < MAX_ARGS; i++)
  2151. str_allocated[i] = NULL;
  2152. /* parse the parameters */
  2153. typestr = cmd->args_type;
  2154. nb_args = 0;
  2155. for(;;) {
  2156. c = *typestr;
  2157. if (c == '\0')
  2158. break;
  2159. typestr++;
  2160. switch(c) {
  2161. case 'F':
  2162. case 'B':
  2163. case 's':
  2164. {
  2165. int ret;
  2166. char *str;
  2167. while (qemu_isspace(*p))
  2168. p++;
  2169. if (*typestr == '?') {
  2170. typestr++;
  2171. if (*p == '\0') {
  2172. /* no optional string: NULL argument */
  2173. str = NULL;
  2174. goto add_str;
  2175. }
  2176. }
  2177. ret = get_str(buf, sizeof(buf), &p);
  2178. if (ret < 0) {
  2179. switch(c) {
  2180. case 'F':
  2181. term_printf("%s: filename expected\n", cmdname);
  2182. break;
  2183. case 'B':
  2184. term_printf("%s: block device name expected\n", cmdname);
  2185. break;
  2186. default:
  2187. term_printf("%s: string expected\n", cmdname);
  2188. break;
  2189. }
  2190. goto fail;
  2191. }
  2192. str = qemu_malloc(strlen(buf) + 1);
  2193. pstrcpy(str, sizeof(buf), buf);
  2194. str_allocated[nb_args] = str;
  2195. add_str:
  2196. if (nb_args >= MAX_ARGS) {
  2197. error_args:
  2198. term_printf("%s: too many arguments\n", cmdname);
  2199. goto fail;
  2200. }
  2201. args[nb_args++] = str;
  2202. }
  2203. break;
  2204. case '/':
  2205. {
  2206. int count, format, size;
  2207. while (qemu_isspace(*p))
  2208. p++;
  2209. if (*p == '/') {
  2210. /* format found */
  2211. p++;
  2212. count = 1;
  2213. if (qemu_isdigit(*p)) {
  2214. count = 0;
  2215. while (qemu_isdigit(*p)) {
  2216. count = count * 10 + (*p - '0');
  2217. p++;
  2218. }
  2219. }
  2220. size = -1;
  2221. format = -1;
  2222. for(;;) {
  2223. switch(*p) {
  2224. case 'o':
  2225. case 'd':
  2226. case 'u':
  2227. case 'x':
  2228. case 'i':
  2229. case 'c':
  2230. format = *p++;
  2231. break;
  2232. case 'b':
  2233. size = 1;
  2234. p++;
  2235. break;
  2236. case 'h':
  2237. size = 2;
  2238. p++;
  2239. break;
  2240. case 'w':
  2241. size = 4;
  2242. p++;
  2243. break;
  2244. case 'g':
  2245. case 'L':
  2246. size = 8;
  2247. p++;
  2248. break;
  2249. default:
  2250. goto next;
  2251. }
  2252. }
  2253. next:
  2254. if (*p != '\0' && !qemu_isspace(*p)) {
  2255. term_printf("invalid char in format: '%c'\n", *p);
  2256. goto fail;
  2257. }
  2258. if (format < 0)
  2259. format = default_fmt_format;
  2260. if (format != 'i') {
  2261. /* for 'i', not specifying a size gives -1 as size */
  2262. if (size < 0)
  2263. size = default_fmt_size;
  2264. default_fmt_size = size;
  2265. }
  2266. default_fmt_format = format;
  2267. } else {
  2268. count = 1;
  2269. format = default_fmt_format;
  2270. if (format != 'i') {
  2271. size = default_fmt_size;
  2272. } else {
  2273. size = -1;
  2274. }
  2275. }
  2276. if (nb_args + 3 > MAX_ARGS)
  2277. goto error_args;
  2278. args[nb_args++] = (void*)(long)count;
  2279. args[nb_args++] = (void*)(long)format;
  2280. args[nb_args++] = (void*)(long)size;
  2281. }
  2282. break;
  2283. case 'i':
  2284. case 'l':
  2285. {
  2286. int64_t val;
  2287. while (qemu_isspace(*p))
  2288. p++;
  2289. if (*typestr == '?' || *typestr == '.') {
  2290. if (*typestr == '?') {
  2291. if (*p == '\0')
  2292. has_arg = 0;
  2293. else
  2294. has_arg = 1;
  2295. } else {
  2296. if (*p == '.') {
  2297. p++;
  2298. while (qemu_isspace(*p))
  2299. p++;
  2300. has_arg = 1;
  2301. } else {
  2302. has_arg = 0;
  2303. }
  2304. }
  2305. typestr++;
  2306. if (nb_args >= MAX_ARGS)
  2307. goto error_args;
  2308. args[nb_args++] = (void *)(long)has_arg;
  2309. if (!has_arg) {
  2310. if (nb_args >= MAX_ARGS)
  2311. goto error_args;
  2312. val = -1;
  2313. goto add_num;
  2314. }
  2315. }
  2316. if (get_expr(&val, &p))
  2317. goto fail;
  2318. add_num:
  2319. if (c == 'i') {
  2320. if (nb_args >= MAX_ARGS)
  2321. goto error_args;
  2322. args[nb_args++] = (void *)(long)val;
  2323. } else {
  2324. if ((nb_args + 1) >= MAX_ARGS)
  2325. goto error_args;
  2326. #if TARGET_PHYS_ADDR_BITS > 32
  2327. args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
  2328. #else
  2329. args[nb_args++] = (void *)0;
  2330. #endif
  2331. args[nb_args++] = (void *)(long)(val & 0xffffffff);
  2332. }
  2333. }
  2334. break;
  2335. case '-':
  2336. {
  2337. int has_option;
  2338. /* option */
  2339. c = *typestr++;
  2340. if (c == '\0')
  2341. goto bad_type;
  2342. while (qemu_isspace(*p))
  2343. p++;
  2344. has_option = 0;
  2345. if (*p == '-') {
  2346. p++;
  2347. if (*p != c) {
  2348. term_printf("%s: unsupported option -%c\n",
  2349. cmdname, *p);
  2350. goto fail;
  2351. }
  2352. p++;
  2353. has_option = 1;
  2354. }
  2355. if (nb_args >= MAX_ARGS)
  2356. goto error_args;
  2357. args[nb_args++] = (void *)(long)has_option;
  2358. }
  2359. break;
  2360. default:
  2361. bad_type:
  2362. term_printf("%s: unknown type '%c'\n", cmdname, c);
  2363. goto fail;
  2364. }
  2365. }
  2366. /* check that all arguments were parsed */
  2367. while (qemu_isspace(*p))
  2368. p++;
  2369. if (*p != '\0') {
  2370. term_printf("%s: extraneous characters at the end of line\n",
  2371. cmdname);
  2372. goto fail;
  2373. }
  2374. switch(nb_args) {
  2375. case 0:
  2376. handler_0 = cmd->handler;
  2377. handler_0();
  2378. break;
  2379. case 1:
  2380. handler_1 = cmd->handler;
  2381. handler_1(args[0]);
  2382. break;
  2383. case 2:
  2384. handler_2 = cmd->handler;
  2385. handler_2(args[0], args[1]);
  2386. break;
  2387. case 3:
  2388. handler_3 = cmd->handler;
  2389. handler_3(args[0], args[1], args[2]);
  2390. break;
  2391. case 4:
  2392. handler_4 = cmd->handler;
  2393. handler_4(args[0], args[1], args[2], args[3]);
  2394. break;
  2395. case 5:
  2396. handler_5 = cmd->handler;
  2397. handler_5(args[0], args[1], args[2], args[3], args[4]);
  2398. break;
  2399. case 6:
  2400. handler_6 = cmd->handler;
  2401. handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
  2402. break;
  2403. case 7:
  2404. handler_7 = cmd->handler;
  2405. handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
  2406. break;
  2407. default:
  2408. term_printf("unsupported number of arguments: %d\n", nb_args);
  2409. goto fail;
  2410. }
  2411. fail:
  2412. for(i = 0; i < MAX_ARGS; i++)
  2413. qemu_free(str_allocated[i]);
  2414. return;
  2415. }
  2416. static void cmd_completion(const char *name, const char *list)
  2417. {
  2418. const char *p, *pstart;
  2419. char cmd[128];
  2420. int len;
  2421. p = list;
  2422. for(;;) {
  2423. pstart = p;
  2424. p = strchr(p, '|');
  2425. if (!p)
  2426. p = pstart + strlen(pstart);
  2427. len = p - pstart;
  2428. if (len > sizeof(cmd) - 2)
  2429. len = sizeof(cmd) - 2;
  2430. memcpy(cmd, pstart, len);
  2431. cmd[len] = '\0';
  2432. if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
  2433. add_completion(cmd);
  2434. }
  2435. if (*p == '\0')
  2436. break;
  2437. p++;
  2438. }
  2439. }
  2440. static void file_completion(const char *input)
  2441. {
  2442. DIR *ffs;
  2443. struct dirent *d;
  2444. char path[1024];
  2445. char file[1024], file_prefix[1024];
  2446. int input_path_len;
  2447. const char *p;
  2448. p = strrchr(input, '/');
  2449. if (!p) {
  2450. input_path_len = 0;
  2451. pstrcpy(file_prefix, sizeof(file_prefix), input);
  2452. pstrcpy(path, sizeof(path), ".");
  2453. } else {
  2454. input_path_len = p - input + 1;
  2455. memcpy(path, input, input_path_len);
  2456. if (input_path_len > sizeof(path) - 1)
  2457. input_path_len = sizeof(path) - 1;
  2458. path[input_path_len] = '\0';
  2459. pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
  2460. }
  2461. #ifdef DEBUG_COMPLETION
  2462. term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
  2463. #endif
  2464. ffs = opendir(path);
  2465. if (!ffs)
  2466. return;
  2467. for(;;) {
  2468. struct stat sb;
  2469. d = readdir(ffs);
  2470. if (!d)
  2471. break;
  2472. if (strstart(d->d_name, file_prefix, NULL)) {
  2473. memcpy(file, input, input_path_len);
  2474. if (input_path_len < sizeof(file))
  2475. pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
  2476. d->d_name);
  2477. /* stat the file to find out if it's a directory.
  2478. * In that case add a slash to speed up typing long paths
  2479. */
  2480. stat(file, &sb);
  2481. if(S_ISDIR(sb.st_mode))
  2482. pstrcat(file, sizeof(file), "/");
  2483. add_completion(file);
  2484. }
  2485. }
  2486. closedir(ffs);
  2487. }
  2488. static void block_completion_it(void *opaque, BlockDriverState *bs)
  2489. {
  2490. const char *name = bdrv_get_device_name(bs);
  2491. const char *input = opaque;
  2492. if (input[0] == '\0' ||
  2493. !strncmp(name, (char *)input, strlen(input))) {
  2494. add_completion(name);
  2495. }
  2496. }
  2497. /* NOTE: this parser is an approximate form of the real command parser */
  2498. static void parse_cmdline(const char *cmdline,
  2499. int *pnb_args, char **args)
  2500. {
  2501. const char *p;
  2502. int nb_args, ret;
  2503. char buf[1024];
  2504. p = cmdline;
  2505. nb_args = 0;
  2506. for(;;) {
  2507. while (qemu_isspace(*p))
  2508. p++;
  2509. if (*p == '\0')
  2510. break;
  2511. if (nb_args >= MAX_ARGS)
  2512. break;
  2513. ret = get_str(buf, sizeof(buf), &p);
  2514. args[nb_args] = qemu_strdup(buf);
  2515. nb_args++;
  2516. if (ret < 0)
  2517. break;
  2518. }
  2519. *pnb_args = nb_args;
  2520. }
  2521. void readline_find_completion(const char *cmdline)
  2522. {
  2523. const char *cmdname;
  2524. char *args[MAX_ARGS];
  2525. int nb_args, i, len;
  2526. const char *ptype, *str;
  2527. const term_cmd_t *cmd;
  2528. const KeyDef *key;
  2529. parse_cmdline(cmdline, &nb_args, args);
  2530. #ifdef DEBUG_COMPLETION
  2531. for(i = 0; i < nb_args; i++) {
  2532. term_printf("arg%d = '%s'\n", i, (char *)args[i]);
  2533. }
  2534. #endif
  2535. /* if the line ends with a space, it means we want to complete the
  2536. next arg */
  2537. len = strlen(cmdline);
  2538. if (len > 0 && qemu_isspace(cmdline[len - 1])) {
  2539. if (nb_args >= MAX_ARGS)
  2540. return;
  2541. args[nb_args++] = qemu_strdup("");
  2542. }
  2543. if (nb_args <= 1) {
  2544. /* command completion */
  2545. if (nb_args == 0)
  2546. cmdname = "";
  2547. else
  2548. cmdname = args[0];
  2549. completion_index = strlen(cmdname);
  2550. for(cmd = term_cmds; cmd->name != NULL; cmd++) {
  2551. cmd_completion(cmdname, cmd->name);
  2552. }
  2553. } else {
  2554. /* find the command */
  2555. for(cmd = term_cmds; cmd->name != NULL; cmd++) {
  2556. if (compare_cmd(args[0], cmd->name))
  2557. goto found;
  2558. }
  2559. return;
  2560. found:
  2561. ptype = cmd->args_type;
  2562. for(i = 0; i < nb_args - 2; i++) {
  2563. if (*ptype != '\0') {
  2564. ptype++;
  2565. while (*ptype == '?')
  2566. ptype++;
  2567. }
  2568. }
  2569. str = args[nb_args - 1];
  2570. switch(*ptype) {
  2571. case 'F':
  2572. /* file completion */
  2573. completion_index = strlen(str);
  2574. file_completion(str);
  2575. break;
  2576. case 'B':
  2577. /* block device name completion */
  2578. completion_index = strlen(str);
  2579. bdrv_iterate(block_completion_it, (void *)str);
  2580. break;
  2581. case 's':
  2582. /* XXX: more generic ? */
  2583. if (!strcmp(cmd->name, "info")) {
  2584. completion_index = strlen(str);
  2585. for(cmd = info_cmds; cmd->name != NULL; cmd++) {
  2586. cmd_completion(str, cmd->name);
  2587. }
  2588. } else if (!strcmp(cmd->name, "sendkey")) {
  2589. completion_index = strlen(str);
  2590. for(key = key_defs; key->name != NULL; key++) {
  2591. cmd_completion(str, key->name);
  2592. }
  2593. }
  2594. break;
  2595. default:
  2596. break;
  2597. }
  2598. }
  2599. for(i = 0; i < nb_args; i++)
  2600. qemu_free(args[i]);
  2601. }
  2602. static int term_can_read(void *opaque)
  2603. {
  2604. return 128;
  2605. }
  2606. static void term_read(void *opaque, const uint8_t *buf, int size)
  2607. {
  2608. int i;
  2609. for(i = 0; i < size; i++)
  2610. readline_handle_byte(buf[i]);
  2611. }
  2612. static int monitor_suspended;
  2613. static void monitor_handle_command1(void *opaque, const char *cmdline)
  2614. {
  2615. monitor_handle_command(cmdline);
  2616. if (!monitor_suspended)
  2617. monitor_start_input();
  2618. else
  2619. monitor_suspended = 2;
  2620. }
  2621. void monitor_suspend(void)
  2622. {
  2623. monitor_suspended = 1;
  2624. }
  2625. void monitor_resume(void)
  2626. {
  2627. if (monitor_suspended == 2)
  2628. monitor_start_input();
  2629. monitor_suspended = 0;
  2630. }
  2631. static void monitor_start_input(void)
  2632. {
  2633. readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
  2634. }
  2635. static void term_event(void *opaque, int event)
  2636. {
  2637. if (event != CHR_EVENT_RESET)
  2638. return;
  2639. if (!hide_banner)
  2640. term_printf("QEMU %s monitor - type 'help' for more information\n",
  2641. QEMU_VERSION);
  2642. monitor_start_input();
  2643. }
  2644. static int is_first_init = 1;
  2645. void monitor_init(CharDriverState *hd, int show_banner)
  2646. {
  2647. int i;
  2648. if (is_first_init) {
  2649. key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
  2650. if (!key_timer)
  2651. return;
  2652. for (i = 0; i < MAX_MON; i++) {
  2653. monitor_hd[i] = NULL;
  2654. }
  2655. is_first_init = 0;
  2656. }
  2657. for (i = 0; i < MAX_MON; i++) {
  2658. if (monitor_hd[i] == NULL) {
  2659. monitor_hd[i] = hd;
  2660. break;
  2661. }
  2662. }
  2663. hide_banner = !show_banner;
  2664. qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
  2665. readline_start("", 0, monitor_handle_command1, NULL);
  2666. }
  2667. /* XXX: use threads ? */
  2668. /* modal monitor readline */
  2669. static int monitor_readline_started;
  2670. static char *monitor_readline_buf;
  2671. static int monitor_readline_buf_size;
  2672. static void monitor_readline_cb(void *opaque, const char *input)
  2673. {
  2674. pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
  2675. monitor_readline_started = 0;
  2676. }
  2677. static void monitor_readline(const char *prompt, int is_password,
  2678. char *buf, int buf_size)
  2679. {
  2680. int i;
  2681. int old_focus[MAX_MON];
  2682. if (is_password) {
  2683. for (i = 0; i < MAX_MON; i++) {
  2684. old_focus[i] = 0;
  2685. if (monitor_hd[i]) {
  2686. old_focus[i] = monitor_hd[i]->focus;
  2687. monitor_hd[i]->focus = 0;
  2688. qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
  2689. }
  2690. }
  2691. }
  2692. readline_start(prompt, is_password, monitor_readline_cb, NULL);
  2693. monitor_readline_buf = buf;
  2694. monitor_readline_buf_size = buf_size;
  2695. monitor_readline_started = 1;
  2696. while (monitor_readline_started) {
  2697. main_loop_wait(10);
  2698. }
  2699. /* restore original focus */
  2700. if (is_password) {
  2701. for (i = 0; i < MAX_MON; i++)
  2702. if (old_focus[i])
  2703. monitor_hd[i]->focus = old_focus[i];
  2704. }
  2705. }
  2706. int monitor_read_bdrv_key(BlockDriverState *bs)
  2707. {
  2708. char password[256];
  2709. int i;
  2710. if (!bdrv_is_encrypted(bs))
  2711. return 0;
  2712. term_printf("%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
  2713. bdrv_get_encrypted_filename(bs));
  2714. for(i = 0; i < 3; i++) {
  2715. monitor_readline("Password: ", 1, password, sizeof(password));
  2716. if (bdrv_set_key(bs, password) == 0)
  2717. return 0;
  2718. term_printf("invalid password\n");
  2719. }
  2720. return -EPERM;
  2721. }