monitor.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830
  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 <dirent.h>
  25. #include "hw/hw.h"
  26. #include "hw/qdev.h"
  27. #include "hw/usb.h"
  28. #include "hw/pcmcia.h"
  29. #include "hw/pc.h"
  30. #include "hw/pci/pci.h"
  31. #include "hw/watchdog.h"
  32. #include "hw/loader.h"
  33. #include "exec/gdbstub.h"
  34. #include "net/net.h"
  35. #include "net/slirp.h"
  36. #include "char/char.h"
  37. #include "ui/qemu-spice.h"
  38. #include "sysemu/sysemu.h"
  39. #include "monitor/monitor.h"
  40. #include "monitor/readline.h"
  41. #include "ui/console.h"
  42. #include "sysemu/blockdev.h"
  43. #include "audio/audio.h"
  44. #include "disas/disas.h"
  45. #include "sysemu/balloon.h"
  46. #include "qemu/timer.h"
  47. #include "migration/migration.h"
  48. #include "sysemu/kvm.h"
  49. #include "qemu/acl.h"
  50. #include "qapi/qmp/qint.h"
  51. #include "qapi/qmp/qfloat.h"
  52. #include "qapi/qmp/qlist.h"
  53. #include "qapi/qmp/qbool.h"
  54. #include "qapi/qmp/qstring.h"
  55. #include "qapi/qmp/qjson.h"
  56. #include "qapi/qmp/json-streamer.h"
  57. #include "qapi/qmp/json-parser.h"
  58. #include "qemu/osdep.h"
  59. #include "cpu.h"
  60. #include "trace.h"
  61. #include "trace/control.h"
  62. #ifdef CONFIG_TRACE_SIMPLE
  63. #include "trace/simple.h"
  64. #endif
  65. #include "ui/qemu-spice.h"
  66. #include "exec/memory.h"
  67. #include "qmp-commands.h"
  68. #include "hmp.h"
  69. #include "qemu/thread.h"
  70. /* for pic/irq_info */
  71. #if defined(TARGET_SPARC)
  72. #include "hw/sun4m.h"
  73. #endif
  74. #include "hw/lm32_pic.h"
  75. //#define DEBUG
  76. //#define DEBUG_COMPLETION
  77. /*
  78. * Supported types:
  79. *
  80. * 'F' filename
  81. * 'B' block device name
  82. * 's' string (accept optional quote)
  83. * 'O' option string of the form NAME=VALUE,...
  84. * parsed according to QemuOptsList given by its name
  85. * Example: 'device:O' uses qemu_device_opts.
  86. * Restriction: only lists with empty desc are supported
  87. * TODO lift the restriction
  88. * 'i' 32 bit integer
  89. * 'l' target long (32 or 64 bit)
  90. * 'M' Non-negative target long (32 or 64 bit), in user mode the
  91. * value is multiplied by 2^20 (think Mebibyte)
  92. * 'o' octets (aka bytes)
  93. * user mode accepts an optional T, t, G, g, M, m, K, k
  94. * suffix, which multiplies the value by 2^40 for
  95. * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
  96. * M and m, 2^10 for K and k
  97. * 'T' double
  98. * user mode accepts an optional ms, us, ns suffix,
  99. * which divides the value by 1e3, 1e6, 1e9, respectively
  100. * '/' optional gdb-like print format (like "/10x")
  101. *
  102. * '?' optional type (for all types, except '/')
  103. * '.' other form of optional type (for 'i' and 'l')
  104. * 'b' boolean
  105. * user mode accepts "on" or "off"
  106. * '-' optional parameter (eg. '-f')
  107. *
  108. */
  109. typedef struct MonitorCompletionData MonitorCompletionData;
  110. struct MonitorCompletionData {
  111. Monitor *mon;
  112. void (*user_print)(Monitor *mon, const QObject *data);
  113. };
  114. typedef struct mon_cmd_t {
  115. const char *name;
  116. const char *args_type;
  117. const char *params;
  118. const char *help;
  119. void (*user_print)(Monitor *mon, const QObject *data);
  120. union {
  121. void (*cmd)(Monitor *mon, const QDict *qdict);
  122. int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
  123. int (*cmd_async)(Monitor *mon, const QDict *params,
  124. MonitorCompletion *cb, void *opaque);
  125. } mhandler;
  126. int flags;
  127. /* @sub_table is a list of 2nd level of commands. If it do not exist,
  128. * mhandler should be used. If it exist, sub_table[?].mhandler should be
  129. * used, and mhandler of 1st level plays the role of help function.
  130. */
  131. struct mon_cmd_t *sub_table;
  132. } mon_cmd_t;
  133. /* file descriptors passed via SCM_RIGHTS */
  134. typedef struct mon_fd_t mon_fd_t;
  135. struct mon_fd_t {
  136. char *name;
  137. int fd;
  138. QLIST_ENTRY(mon_fd_t) next;
  139. };
  140. /* file descriptor associated with a file descriptor set */
  141. typedef struct MonFdsetFd MonFdsetFd;
  142. struct MonFdsetFd {
  143. int fd;
  144. bool removed;
  145. char *opaque;
  146. QLIST_ENTRY(MonFdsetFd) next;
  147. };
  148. /* file descriptor set containing fds passed via SCM_RIGHTS */
  149. typedef struct MonFdset MonFdset;
  150. struct MonFdset {
  151. int64_t id;
  152. QLIST_HEAD(, MonFdsetFd) fds;
  153. QLIST_HEAD(, MonFdsetFd) dup_fds;
  154. QLIST_ENTRY(MonFdset) next;
  155. };
  156. typedef struct MonitorControl {
  157. QObject *id;
  158. JSONMessageParser parser;
  159. int command_mode;
  160. } MonitorControl;
  161. /*
  162. * To prevent flooding clients, events can be throttled. The
  163. * throttling is calculated globally, rather than per-Monitor
  164. * instance.
  165. */
  166. typedef struct MonitorEventState {
  167. MonitorEvent event; /* Event being tracked */
  168. int64_t rate; /* Period over which to throttle. 0 to disable */
  169. int64_t last; /* Time at which event was last emitted */
  170. QEMUTimer *timer; /* Timer for handling delayed events */
  171. QObject *data; /* Event pending delayed dispatch */
  172. } MonitorEventState;
  173. struct Monitor {
  174. CharDriverState *chr;
  175. int mux_out;
  176. int reset_seen;
  177. int flags;
  178. int suspend_cnt;
  179. uint8_t outbuf[1024];
  180. int outbuf_index;
  181. ReadLineState *rs;
  182. MonitorControl *mc;
  183. CPUArchState *mon_cpu;
  184. BlockDriverCompletionFunc *password_completion_cb;
  185. void *password_opaque;
  186. QError *error;
  187. QLIST_HEAD(,mon_fd_t) fds;
  188. QLIST_ENTRY(Monitor) entry;
  189. };
  190. /* QMP checker flags */
  191. #define QMP_ACCEPT_UNKNOWNS 1
  192. static QLIST_HEAD(mon_list, Monitor) mon_list;
  193. static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
  194. static int mon_refcount;
  195. static mon_cmd_t mon_cmds[];
  196. static mon_cmd_t info_cmds[];
  197. static const mon_cmd_t qmp_cmds[];
  198. Monitor *cur_mon;
  199. Monitor *default_mon;
  200. static void monitor_command_cb(Monitor *mon, const char *cmdline,
  201. void *opaque);
  202. static inline int qmp_cmd_mode(const Monitor *mon)
  203. {
  204. return (mon->mc ? mon->mc->command_mode : 0);
  205. }
  206. /* Return true if in control mode, false otherwise */
  207. static inline int monitor_ctrl_mode(const Monitor *mon)
  208. {
  209. return (mon->flags & MONITOR_USE_CONTROL);
  210. }
  211. /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
  212. int monitor_cur_is_qmp(void)
  213. {
  214. return cur_mon && monitor_ctrl_mode(cur_mon);
  215. }
  216. void monitor_read_command(Monitor *mon, int show_prompt)
  217. {
  218. if (!mon->rs)
  219. return;
  220. readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
  221. if (show_prompt)
  222. readline_show_prompt(mon->rs);
  223. }
  224. int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
  225. void *opaque)
  226. {
  227. if (monitor_ctrl_mode(mon)) {
  228. qerror_report(QERR_MISSING_PARAMETER, "password");
  229. return -EINVAL;
  230. } else if (mon->rs) {
  231. readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
  232. /* prompt is printed on return from the command handler */
  233. return 0;
  234. } else {
  235. monitor_printf(mon, "terminal does not support password prompting\n");
  236. return -ENOTTY;
  237. }
  238. }
  239. void monitor_flush(Monitor *mon)
  240. {
  241. if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
  242. qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
  243. mon->outbuf_index = 0;
  244. }
  245. }
  246. /* flush at every end of line or if the buffer is full */
  247. static void monitor_puts(Monitor *mon, const char *str)
  248. {
  249. char c;
  250. for(;;) {
  251. assert(mon->outbuf_index < sizeof(mon->outbuf) - 1);
  252. c = *str++;
  253. if (c == '\0')
  254. break;
  255. if (c == '\n')
  256. mon->outbuf[mon->outbuf_index++] = '\r';
  257. mon->outbuf[mon->outbuf_index++] = c;
  258. if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
  259. || c == '\n')
  260. monitor_flush(mon);
  261. }
  262. }
  263. void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
  264. {
  265. char buf[4096];
  266. if (!mon)
  267. return;
  268. if (monitor_ctrl_mode(mon)) {
  269. return;
  270. }
  271. vsnprintf(buf, sizeof(buf), fmt, ap);
  272. monitor_puts(mon, buf);
  273. }
  274. void monitor_printf(Monitor *mon, const char *fmt, ...)
  275. {
  276. va_list ap;
  277. va_start(ap, fmt);
  278. monitor_vprintf(mon, fmt, ap);
  279. va_end(ap);
  280. }
  281. void monitor_print_filename(Monitor *mon, const char *filename)
  282. {
  283. int i;
  284. for (i = 0; filename[i]; i++) {
  285. switch (filename[i]) {
  286. case ' ':
  287. case '"':
  288. case '\\':
  289. monitor_printf(mon, "\\%c", filename[i]);
  290. break;
  291. case '\t':
  292. monitor_printf(mon, "\\t");
  293. break;
  294. case '\r':
  295. monitor_printf(mon, "\\r");
  296. break;
  297. case '\n':
  298. monitor_printf(mon, "\\n");
  299. break;
  300. default:
  301. monitor_printf(mon, "%c", filename[i]);
  302. break;
  303. }
  304. }
  305. }
  306. static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
  307. const char *fmt, ...)
  308. {
  309. va_list ap;
  310. va_start(ap, fmt);
  311. monitor_vprintf((Monitor *)stream, fmt, ap);
  312. va_end(ap);
  313. return 0;
  314. }
  315. static void monitor_user_noop(Monitor *mon, const QObject *data) { }
  316. static inline int handler_is_qobject(const mon_cmd_t *cmd)
  317. {
  318. return cmd->user_print != NULL;
  319. }
  320. static inline bool handler_is_async(const mon_cmd_t *cmd)
  321. {
  322. return cmd->flags & MONITOR_CMD_ASYNC;
  323. }
  324. static inline int monitor_has_error(const Monitor *mon)
  325. {
  326. return mon->error != NULL;
  327. }
  328. static void monitor_json_emitter(Monitor *mon, const QObject *data)
  329. {
  330. QString *json;
  331. json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
  332. qobject_to_json(data);
  333. assert(json != NULL);
  334. qstring_append_chr(json, '\n');
  335. monitor_puts(mon, qstring_get_str(json));
  336. QDECREF(json);
  337. }
  338. static QDict *build_qmp_error_dict(const QError *err)
  339. {
  340. QObject *obj;
  341. obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
  342. ErrorClass_lookup[err->err_class],
  343. qerror_human(err));
  344. return qobject_to_qdict(obj);
  345. }
  346. static void monitor_protocol_emitter(Monitor *mon, QObject *data)
  347. {
  348. QDict *qmp;
  349. trace_monitor_protocol_emitter(mon);
  350. if (!monitor_has_error(mon)) {
  351. /* success response */
  352. qmp = qdict_new();
  353. if (data) {
  354. qobject_incref(data);
  355. qdict_put_obj(qmp, "return", data);
  356. } else {
  357. /* return an empty QDict by default */
  358. qdict_put(qmp, "return", qdict_new());
  359. }
  360. } else {
  361. /* error response */
  362. qmp = build_qmp_error_dict(mon->error);
  363. QDECREF(mon->error);
  364. mon->error = NULL;
  365. }
  366. if (mon->mc->id) {
  367. qdict_put_obj(qmp, "id", mon->mc->id);
  368. mon->mc->id = NULL;
  369. }
  370. monitor_json_emitter(mon, QOBJECT(qmp));
  371. QDECREF(qmp);
  372. }
  373. static void timestamp_put(QDict *qdict)
  374. {
  375. int err;
  376. QObject *obj;
  377. qemu_timeval tv;
  378. err = qemu_gettimeofday(&tv);
  379. if (err < 0)
  380. return;
  381. obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
  382. "'microseconds': %" PRId64 " }",
  383. (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
  384. qdict_put_obj(qdict, "timestamp", obj);
  385. }
  386. static const char *monitor_event_names[] = {
  387. [QEVENT_SHUTDOWN] = "SHUTDOWN",
  388. [QEVENT_RESET] = "RESET",
  389. [QEVENT_POWERDOWN] = "POWERDOWN",
  390. [QEVENT_STOP] = "STOP",
  391. [QEVENT_RESUME] = "RESUME",
  392. [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
  393. [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
  394. [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
  395. [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
  396. [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
  397. [QEVENT_WATCHDOG] = "WATCHDOG",
  398. [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
  399. [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
  400. [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
  401. [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
  402. [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
  403. [QEVENT_BLOCK_JOB_ERROR] = "BLOCK_JOB_ERROR",
  404. [QEVENT_BLOCK_JOB_READY] = "BLOCK_JOB_READY",
  405. [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
  406. [QEVENT_SUSPEND] = "SUSPEND",
  407. [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
  408. [QEVENT_WAKEUP] = "WAKEUP",
  409. [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
  410. [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED",
  411. };
  412. QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
  413. MonitorEventState monitor_event_state[QEVENT_MAX];
  414. QemuMutex monitor_event_state_lock;
  415. /*
  416. * Emits the event to every monitor instance
  417. */
  418. static void
  419. monitor_protocol_event_emit(MonitorEvent event,
  420. QObject *data)
  421. {
  422. Monitor *mon;
  423. trace_monitor_protocol_event_emit(event, data);
  424. QLIST_FOREACH(mon, &mon_list, entry) {
  425. if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
  426. monitor_json_emitter(mon, data);
  427. }
  428. }
  429. }
  430. /*
  431. * Queue a new event for emission to Monitor instances,
  432. * applying any rate limiting if required.
  433. */
  434. static void
  435. monitor_protocol_event_queue(MonitorEvent event,
  436. QObject *data)
  437. {
  438. MonitorEventState *evstate;
  439. int64_t now = qemu_get_clock_ns(rt_clock);
  440. assert(event < QEVENT_MAX);
  441. qemu_mutex_lock(&monitor_event_state_lock);
  442. evstate = &(monitor_event_state[event]);
  443. trace_monitor_protocol_event_queue(event,
  444. data,
  445. evstate->rate,
  446. evstate->last,
  447. now);
  448. /* Rate limit of 0 indicates no throttling */
  449. if (!evstate->rate) {
  450. monitor_protocol_event_emit(event, data);
  451. evstate->last = now;
  452. } else {
  453. int64_t delta = now - evstate->last;
  454. if (evstate->data ||
  455. delta < evstate->rate) {
  456. /* If there's an existing event pending, replace
  457. * it with the new event, otherwise schedule a
  458. * timer for delayed emission
  459. */
  460. if (evstate->data) {
  461. qobject_decref(evstate->data);
  462. } else {
  463. int64_t then = evstate->last + evstate->rate;
  464. qemu_mod_timer_ns(evstate->timer, then);
  465. }
  466. evstate->data = data;
  467. qobject_incref(evstate->data);
  468. } else {
  469. monitor_protocol_event_emit(event, data);
  470. evstate->last = now;
  471. }
  472. }
  473. qemu_mutex_unlock(&monitor_event_state_lock);
  474. }
  475. /*
  476. * The callback invoked by QemuTimer when a delayed
  477. * event is ready to be emitted
  478. */
  479. static void monitor_protocol_event_handler(void *opaque)
  480. {
  481. MonitorEventState *evstate = opaque;
  482. int64_t now = qemu_get_clock_ns(rt_clock);
  483. qemu_mutex_lock(&monitor_event_state_lock);
  484. trace_monitor_protocol_event_handler(evstate->event,
  485. evstate->data,
  486. evstate->last,
  487. now);
  488. if (evstate->data) {
  489. monitor_protocol_event_emit(evstate->event, evstate->data);
  490. qobject_decref(evstate->data);
  491. evstate->data = NULL;
  492. }
  493. evstate->last = now;
  494. qemu_mutex_unlock(&monitor_event_state_lock);
  495. }
  496. /*
  497. * @event: the event ID to be limited
  498. * @rate: the rate limit in milliseconds
  499. *
  500. * Sets a rate limit on a particular event, so no
  501. * more than 1 event will be emitted within @rate
  502. * milliseconds
  503. */
  504. static void
  505. monitor_protocol_event_throttle(MonitorEvent event,
  506. int64_t rate)
  507. {
  508. MonitorEventState *evstate;
  509. assert(event < QEVENT_MAX);
  510. evstate = &(monitor_event_state[event]);
  511. trace_monitor_protocol_event_throttle(event, rate);
  512. evstate->event = event;
  513. evstate->rate = rate * SCALE_MS;
  514. evstate->timer = qemu_new_timer(rt_clock,
  515. SCALE_MS,
  516. monitor_protocol_event_handler,
  517. evstate);
  518. evstate->last = 0;
  519. evstate->data = NULL;
  520. }
  521. /* Global, one-time initializer to configure the rate limiting
  522. * and initialize state */
  523. static void monitor_protocol_event_init(void)
  524. {
  525. qemu_mutex_init(&monitor_event_state_lock);
  526. /* Limit RTC & BALLOON events to 1 per second */
  527. monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
  528. monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
  529. monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
  530. }
  531. /**
  532. * monitor_protocol_event(): Generate a Monitor event
  533. *
  534. * Event-specific data can be emitted through the (optional) 'data' parameter.
  535. */
  536. void monitor_protocol_event(MonitorEvent event, QObject *data)
  537. {
  538. QDict *qmp;
  539. const char *event_name;
  540. assert(event < QEVENT_MAX);
  541. event_name = monitor_event_names[event];
  542. assert(event_name != NULL);
  543. qmp = qdict_new();
  544. timestamp_put(qmp);
  545. qdict_put(qmp, "event", qstring_from_str(event_name));
  546. if (data) {
  547. qobject_incref(data);
  548. qdict_put_obj(qmp, "data", data);
  549. }
  550. trace_monitor_protocol_event(event, event_name, qmp);
  551. monitor_protocol_event_queue(event, QOBJECT(qmp));
  552. QDECREF(qmp);
  553. }
  554. static int do_qmp_capabilities(Monitor *mon, const QDict *params,
  555. QObject **ret_data)
  556. {
  557. /* Will setup QMP capabilities in the future */
  558. if (monitor_ctrl_mode(mon)) {
  559. mon->mc->command_mode = 1;
  560. }
  561. return 0;
  562. }
  563. static void handle_user_command(Monitor *mon, const char *cmdline);
  564. char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
  565. int64_t cpu_index, Error **errp)
  566. {
  567. char *output = NULL;
  568. Monitor *old_mon, hmp;
  569. CharDriverState mchar;
  570. memset(&hmp, 0, sizeof(hmp));
  571. qemu_chr_init_mem(&mchar);
  572. hmp.chr = &mchar;
  573. old_mon = cur_mon;
  574. cur_mon = &hmp;
  575. if (has_cpu_index) {
  576. int ret = monitor_set_cpu(cpu_index);
  577. if (ret < 0) {
  578. cur_mon = old_mon;
  579. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
  580. "a CPU number");
  581. goto out;
  582. }
  583. }
  584. handle_user_command(&hmp, command_line);
  585. cur_mon = old_mon;
  586. if (qemu_chr_mem_osize(hmp.chr) > 0) {
  587. QString *str = qemu_chr_mem_to_qs(hmp.chr);
  588. output = g_strdup(qstring_get_str(str));
  589. QDECREF(str);
  590. } else {
  591. output = g_strdup("");
  592. }
  593. out:
  594. qemu_chr_close_mem(hmp.chr);
  595. return output;
  596. }
  597. static int compare_cmd(const char *name, const char *list)
  598. {
  599. const char *p, *pstart;
  600. int len;
  601. len = strlen(name);
  602. p = list;
  603. for(;;) {
  604. pstart = p;
  605. p = strchr(p, '|');
  606. if (!p)
  607. p = pstart + strlen(pstart);
  608. if ((p - pstart) == len && !memcmp(pstart, name, len))
  609. return 1;
  610. if (*p == '\0')
  611. break;
  612. p++;
  613. }
  614. return 0;
  615. }
  616. static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
  617. const char *prefix, const char *name)
  618. {
  619. const mon_cmd_t *cmd;
  620. for(cmd = cmds; cmd->name != NULL; cmd++) {
  621. if (!name || !strcmp(name, cmd->name))
  622. monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
  623. cmd->params, cmd->help);
  624. }
  625. }
  626. static void help_cmd(Monitor *mon, const char *name)
  627. {
  628. if (name && !strcmp(name, "info")) {
  629. help_cmd_dump(mon, info_cmds, "info ", NULL);
  630. } else {
  631. help_cmd_dump(mon, mon_cmds, "", name);
  632. if (name && !strcmp(name, "log")) {
  633. const CPULogItem *item;
  634. monitor_printf(mon, "Log items (comma separated):\n");
  635. monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
  636. for(item = cpu_log_items; item->mask != 0; item++) {
  637. monitor_printf(mon, "%-10s %s\n", item->name, item->help);
  638. }
  639. }
  640. }
  641. }
  642. static void do_help_cmd(Monitor *mon, const QDict *qdict)
  643. {
  644. help_cmd(mon, qdict_get_try_str(qdict, "name"));
  645. }
  646. static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
  647. {
  648. const char *tp_name = qdict_get_str(qdict, "name");
  649. bool new_state = qdict_get_bool(qdict, "option");
  650. int ret = trace_event_set_state(tp_name, new_state);
  651. if (!ret) {
  652. monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
  653. }
  654. }
  655. #ifdef CONFIG_TRACE_SIMPLE
  656. static void do_trace_file(Monitor *mon, const QDict *qdict)
  657. {
  658. const char *op = qdict_get_try_str(qdict, "op");
  659. const char *arg = qdict_get_try_str(qdict, "arg");
  660. if (!op) {
  661. st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
  662. } else if (!strcmp(op, "on")) {
  663. st_set_trace_file_enabled(true);
  664. } else if (!strcmp(op, "off")) {
  665. st_set_trace_file_enabled(false);
  666. } else if (!strcmp(op, "flush")) {
  667. st_flush_trace_buffer();
  668. } else if (!strcmp(op, "set")) {
  669. if (arg) {
  670. st_set_trace_file(arg);
  671. }
  672. } else {
  673. monitor_printf(mon, "unexpected argument \"%s\"\n", op);
  674. help_cmd(mon, "trace-file");
  675. }
  676. }
  677. #endif
  678. static void user_monitor_complete(void *opaque, QObject *ret_data)
  679. {
  680. MonitorCompletionData *data = (MonitorCompletionData *)opaque;
  681. if (ret_data) {
  682. data->user_print(data->mon, ret_data);
  683. }
  684. monitor_resume(data->mon);
  685. g_free(data);
  686. }
  687. static void qmp_monitor_complete(void *opaque, QObject *ret_data)
  688. {
  689. monitor_protocol_emitter(opaque, ret_data);
  690. }
  691. static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
  692. const QDict *params)
  693. {
  694. return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
  695. }
  696. static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
  697. const QDict *params)
  698. {
  699. int ret;
  700. MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
  701. cb_data->mon = mon;
  702. cb_data->user_print = cmd->user_print;
  703. monitor_suspend(mon);
  704. ret = cmd->mhandler.cmd_async(mon, params,
  705. user_monitor_complete, cb_data);
  706. if (ret < 0) {
  707. monitor_resume(mon);
  708. g_free(cb_data);
  709. }
  710. }
  711. static void do_info_help(Monitor *mon, const QDict *qdict)
  712. {
  713. help_cmd(mon, "info");
  714. }
  715. CommandInfoList *qmp_query_commands(Error **errp)
  716. {
  717. CommandInfoList *info, *cmd_list = NULL;
  718. const mon_cmd_t *cmd;
  719. for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
  720. info = g_malloc0(sizeof(*info));
  721. info->value = g_malloc0(sizeof(*info->value));
  722. info->value->name = g_strdup(cmd->name);
  723. info->next = cmd_list;
  724. cmd_list = info;
  725. }
  726. return cmd_list;
  727. }
  728. EventInfoList *qmp_query_events(Error **errp)
  729. {
  730. EventInfoList *info, *ev_list = NULL;
  731. MonitorEvent e;
  732. for (e = 0 ; e < QEVENT_MAX ; e++) {
  733. const char *event_name = monitor_event_names[e];
  734. assert(event_name != NULL);
  735. info = g_malloc0(sizeof(*info));
  736. info->value = g_malloc0(sizeof(*info->value));
  737. info->value->name = g_strdup(event_name);
  738. info->next = ev_list;
  739. ev_list = info;
  740. }
  741. return ev_list;
  742. }
  743. /* set the current CPU defined by the user */
  744. int monitor_set_cpu(int cpu_index)
  745. {
  746. CPUArchState *env;
  747. CPUState *cpu;
  748. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  749. cpu = ENV_GET_CPU(env);
  750. if (cpu->cpu_index == cpu_index) {
  751. cur_mon->mon_cpu = env;
  752. return 0;
  753. }
  754. }
  755. return -1;
  756. }
  757. static CPUArchState *mon_get_cpu(void)
  758. {
  759. if (!cur_mon->mon_cpu) {
  760. monitor_set_cpu(0);
  761. }
  762. cpu_synchronize_state(cur_mon->mon_cpu);
  763. return cur_mon->mon_cpu;
  764. }
  765. int monitor_get_cpu_index(void)
  766. {
  767. CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
  768. return cpu->cpu_index;
  769. }
  770. static void do_info_registers(Monitor *mon, const QDict *qdict)
  771. {
  772. CPUArchState *env;
  773. env = mon_get_cpu();
  774. cpu_dump_state(env, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
  775. }
  776. static void do_info_jit(Monitor *mon, const QDict *qdict)
  777. {
  778. dump_exec_info((FILE *)mon, monitor_fprintf);
  779. }
  780. static void do_info_history(Monitor *mon, const QDict *qdict)
  781. {
  782. int i;
  783. const char *str;
  784. if (!mon->rs)
  785. return;
  786. i = 0;
  787. for(;;) {
  788. str = readline_get_history(mon->rs, i);
  789. if (!str)
  790. break;
  791. monitor_printf(mon, "%d: '%s'\n", i, str);
  792. i++;
  793. }
  794. }
  795. #if defined(TARGET_PPC)
  796. /* XXX: not implemented in other targets */
  797. static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
  798. {
  799. CPUArchState *env;
  800. env = mon_get_cpu();
  801. cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
  802. }
  803. #endif
  804. static void do_trace_print_events(Monitor *mon, const QDict *qdict)
  805. {
  806. trace_print_events((FILE *)mon, &monitor_fprintf);
  807. }
  808. static int client_migrate_info(Monitor *mon, const QDict *qdict,
  809. MonitorCompletion cb, void *opaque)
  810. {
  811. const char *protocol = qdict_get_str(qdict, "protocol");
  812. const char *hostname = qdict_get_str(qdict, "hostname");
  813. const char *subject = qdict_get_try_str(qdict, "cert-subject");
  814. int port = qdict_get_try_int(qdict, "port", -1);
  815. int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
  816. int ret;
  817. if (strcmp(protocol, "spice") == 0) {
  818. if (!using_spice) {
  819. qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
  820. return -1;
  821. }
  822. if (port == -1 && tls_port == -1) {
  823. qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
  824. return -1;
  825. }
  826. ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
  827. cb, opaque);
  828. if (ret != 0) {
  829. qerror_report(QERR_UNDEFINED_ERROR);
  830. return -1;
  831. }
  832. return 0;
  833. }
  834. qerror_report(QERR_INVALID_PARAMETER, "protocol");
  835. return -1;
  836. }
  837. static void do_logfile(Monitor *mon, const QDict *qdict)
  838. {
  839. cpu_set_log_filename(qdict_get_str(qdict, "filename"));
  840. }
  841. static void do_log(Monitor *mon, const QDict *qdict)
  842. {
  843. int mask;
  844. const char *items = qdict_get_str(qdict, "items");
  845. if (!strcmp(items, "none")) {
  846. mask = 0;
  847. } else {
  848. mask = cpu_str_to_log_mask(items);
  849. if (!mask) {
  850. help_cmd(mon, "log");
  851. return;
  852. }
  853. }
  854. cpu_set_log(mask);
  855. }
  856. static void do_singlestep(Monitor *mon, const QDict *qdict)
  857. {
  858. const char *option = qdict_get_try_str(qdict, "option");
  859. if (!option || !strcmp(option, "on")) {
  860. singlestep = 1;
  861. } else if (!strcmp(option, "off")) {
  862. singlestep = 0;
  863. } else {
  864. monitor_printf(mon, "unexpected option %s\n", option);
  865. }
  866. }
  867. static void do_gdbserver(Monitor *mon, const QDict *qdict)
  868. {
  869. const char *device = qdict_get_try_str(qdict, "device");
  870. if (!device)
  871. device = "tcp::" DEFAULT_GDBSTUB_PORT;
  872. if (gdbserver_start(device) < 0) {
  873. monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
  874. device);
  875. } else if (strcmp(device, "none") == 0) {
  876. monitor_printf(mon, "Disabled gdbserver\n");
  877. } else {
  878. monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
  879. device);
  880. }
  881. }
  882. static void do_watchdog_action(Monitor *mon, const QDict *qdict)
  883. {
  884. const char *action = qdict_get_str(qdict, "action");
  885. if (select_watchdog_action(action) == -1) {
  886. monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
  887. }
  888. }
  889. static void monitor_printc(Monitor *mon, int c)
  890. {
  891. monitor_printf(mon, "'");
  892. switch(c) {
  893. case '\'':
  894. monitor_printf(mon, "\\'");
  895. break;
  896. case '\\':
  897. monitor_printf(mon, "\\\\");
  898. break;
  899. case '\n':
  900. monitor_printf(mon, "\\n");
  901. break;
  902. case '\r':
  903. monitor_printf(mon, "\\r");
  904. break;
  905. default:
  906. if (c >= 32 && c <= 126) {
  907. monitor_printf(mon, "%c", c);
  908. } else {
  909. monitor_printf(mon, "\\x%02x", c);
  910. }
  911. break;
  912. }
  913. monitor_printf(mon, "'");
  914. }
  915. static void memory_dump(Monitor *mon, int count, int format, int wsize,
  916. hwaddr addr, int is_physical)
  917. {
  918. CPUArchState *env;
  919. int l, line_size, i, max_digits, len;
  920. uint8_t buf[16];
  921. uint64_t v;
  922. if (format == 'i') {
  923. int flags;
  924. flags = 0;
  925. env = mon_get_cpu();
  926. #ifdef TARGET_I386
  927. if (wsize == 2) {
  928. flags = 1;
  929. } else if (wsize == 4) {
  930. flags = 0;
  931. } else {
  932. /* as default we use the current CS size */
  933. flags = 0;
  934. if (env) {
  935. #ifdef TARGET_X86_64
  936. if ((env->efer & MSR_EFER_LMA) &&
  937. (env->segs[R_CS].flags & DESC_L_MASK))
  938. flags = 2;
  939. else
  940. #endif
  941. if (!(env->segs[R_CS].flags & DESC_B_MASK))
  942. flags = 1;
  943. }
  944. }
  945. #endif
  946. monitor_disas(mon, env, addr, count, is_physical, flags);
  947. return;
  948. }
  949. len = wsize * count;
  950. if (wsize == 1)
  951. line_size = 8;
  952. else
  953. line_size = 16;
  954. max_digits = 0;
  955. switch(format) {
  956. case 'o':
  957. max_digits = (wsize * 8 + 2) / 3;
  958. break;
  959. default:
  960. case 'x':
  961. max_digits = (wsize * 8) / 4;
  962. break;
  963. case 'u':
  964. case 'd':
  965. max_digits = (wsize * 8 * 10 + 32) / 33;
  966. break;
  967. case 'c':
  968. wsize = 1;
  969. break;
  970. }
  971. while (len > 0) {
  972. if (is_physical)
  973. monitor_printf(mon, TARGET_FMT_plx ":", addr);
  974. else
  975. monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
  976. l = len;
  977. if (l > line_size)
  978. l = line_size;
  979. if (is_physical) {
  980. cpu_physical_memory_read(addr, buf, l);
  981. } else {
  982. env = mon_get_cpu();
  983. if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
  984. monitor_printf(mon, " Cannot access memory\n");
  985. break;
  986. }
  987. }
  988. i = 0;
  989. while (i < l) {
  990. switch(wsize) {
  991. default:
  992. case 1:
  993. v = ldub_raw(buf + i);
  994. break;
  995. case 2:
  996. v = lduw_raw(buf + i);
  997. break;
  998. case 4:
  999. v = (uint32_t)ldl_raw(buf + i);
  1000. break;
  1001. case 8:
  1002. v = ldq_raw(buf + i);
  1003. break;
  1004. }
  1005. monitor_printf(mon, " ");
  1006. switch(format) {
  1007. case 'o':
  1008. monitor_printf(mon, "%#*" PRIo64, max_digits, v);
  1009. break;
  1010. case 'x':
  1011. monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
  1012. break;
  1013. case 'u':
  1014. monitor_printf(mon, "%*" PRIu64, max_digits, v);
  1015. break;
  1016. case 'd':
  1017. monitor_printf(mon, "%*" PRId64, max_digits, v);
  1018. break;
  1019. case 'c':
  1020. monitor_printc(mon, v);
  1021. break;
  1022. }
  1023. i += wsize;
  1024. }
  1025. monitor_printf(mon, "\n");
  1026. addr += l;
  1027. len -= l;
  1028. }
  1029. }
  1030. static void do_memory_dump(Monitor *mon, const QDict *qdict)
  1031. {
  1032. int count = qdict_get_int(qdict, "count");
  1033. int format = qdict_get_int(qdict, "format");
  1034. int size = qdict_get_int(qdict, "size");
  1035. target_long addr = qdict_get_int(qdict, "addr");
  1036. memory_dump(mon, count, format, size, addr, 0);
  1037. }
  1038. static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
  1039. {
  1040. int count = qdict_get_int(qdict, "count");
  1041. int format = qdict_get_int(qdict, "format");
  1042. int size = qdict_get_int(qdict, "size");
  1043. hwaddr addr = qdict_get_int(qdict, "addr");
  1044. memory_dump(mon, count, format, size, addr, 1);
  1045. }
  1046. static void do_print(Monitor *mon, const QDict *qdict)
  1047. {
  1048. int format = qdict_get_int(qdict, "format");
  1049. hwaddr val = qdict_get_int(qdict, "val");
  1050. switch(format) {
  1051. case 'o':
  1052. monitor_printf(mon, "%#" HWADDR_PRIo, val);
  1053. break;
  1054. case 'x':
  1055. monitor_printf(mon, "%#" HWADDR_PRIx, val);
  1056. break;
  1057. case 'u':
  1058. monitor_printf(mon, "%" HWADDR_PRIu, val);
  1059. break;
  1060. default:
  1061. case 'd':
  1062. monitor_printf(mon, "%" HWADDR_PRId, val);
  1063. break;
  1064. case 'c':
  1065. monitor_printc(mon, val);
  1066. break;
  1067. }
  1068. monitor_printf(mon, "\n");
  1069. }
  1070. static void do_sum(Monitor *mon, const QDict *qdict)
  1071. {
  1072. uint32_t addr;
  1073. uint16_t sum;
  1074. uint32_t start = qdict_get_int(qdict, "start");
  1075. uint32_t size = qdict_get_int(qdict, "size");
  1076. sum = 0;
  1077. for(addr = start; addr < (start + size); addr++) {
  1078. uint8_t val = ldub_phys(addr);
  1079. /* BSD sum algorithm ('sum' Unix command) */
  1080. sum = (sum >> 1) | (sum << 15);
  1081. sum += val;
  1082. }
  1083. monitor_printf(mon, "%05d\n", sum);
  1084. }
  1085. static int mouse_button_state;
  1086. static void do_mouse_move(Monitor *mon, const QDict *qdict)
  1087. {
  1088. int dx, dy, dz;
  1089. const char *dx_str = qdict_get_str(qdict, "dx_str");
  1090. const char *dy_str = qdict_get_str(qdict, "dy_str");
  1091. const char *dz_str = qdict_get_try_str(qdict, "dz_str");
  1092. dx = strtol(dx_str, NULL, 0);
  1093. dy = strtol(dy_str, NULL, 0);
  1094. dz = 0;
  1095. if (dz_str)
  1096. dz = strtol(dz_str, NULL, 0);
  1097. kbd_mouse_event(dx, dy, dz, mouse_button_state);
  1098. }
  1099. static void do_mouse_button(Monitor *mon, const QDict *qdict)
  1100. {
  1101. int button_state = qdict_get_int(qdict, "button_state");
  1102. mouse_button_state = button_state;
  1103. kbd_mouse_event(0, 0, 0, mouse_button_state);
  1104. }
  1105. static void do_ioport_read(Monitor *mon, const QDict *qdict)
  1106. {
  1107. int size = qdict_get_int(qdict, "size");
  1108. int addr = qdict_get_int(qdict, "addr");
  1109. int has_index = qdict_haskey(qdict, "index");
  1110. uint32_t val;
  1111. int suffix;
  1112. if (has_index) {
  1113. int index = qdict_get_int(qdict, "index");
  1114. cpu_outb(addr & IOPORTS_MASK, index & 0xff);
  1115. addr++;
  1116. }
  1117. addr &= 0xffff;
  1118. switch(size) {
  1119. default:
  1120. case 1:
  1121. val = cpu_inb(addr);
  1122. suffix = 'b';
  1123. break;
  1124. case 2:
  1125. val = cpu_inw(addr);
  1126. suffix = 'w';
  1127. break;
  1128. case 4:
  1129. val = cpu_inl(addr);
  1130. suffix = 'l';
  1131. break;
  1132. }
  1133. monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
  1134. suffix, addr, size * 2, val);
  1135. }
  1136. static void do_ioport_write(Monitor *mon, const QDict *qdict)
  1137. {
  1138. int size = qdict_get_int(qdict, "size");
  1139. int addr = qdict_get_int(qdict, "addr");
  1140. int val = qdict_get_int(qdict, "val");
  1141. addr &= IOPORTS_MASK;
  1142. switch (size) {
  1143. default:
  1144. case 1:
  1145. cpu_outb(addr, val);
  1146. break;
  1147. case 2:
  1148. cpu_outw(addr, val);
  1149. break;
  1150. case 4:
  1151. cpu_outl(addr, val);
  1152. break;
  1153. }
  1154. }
  1155. static void do_boot_set(Monitor *mon, const QDict *qdict)
  1156. {
  1157. int res;
  1158. const char *bootdevice = qdict_get_str(qdict, "bootdevice");
  1159. res = qemu_boot_set(bootdevice);
  1160. if (res == 0) {
  1161. monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
  1162. } else if (res > 0) {
  1163. monitor_printf(mon, "setting boot device list failed\n");
  1164. } else {
  1165. monitor_printf(mon, "no function defined to set boot device list for "
  1166. "this architecture\n");
  1167. }
  1168. }
  1169. #if defined(TARGET_I386)
  1170. static void print_pte(Monitor *mon, hwaddr addr,
  1171. hwaddr pte,
  1172. hwaddr mask)
  1173. {
  1174. #ifdef TARGET_X86_64
  1175. if (addr & (1ULL << 47)) {
  1176. addr |= -1LL << 48;
  1177. }
  1178. #endif
  1179. monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
  1180. " %c%c%c%c%c%c%c%c%c\n",
  1181. addr,
  1182. pte & mask,
  1183. pte & PG_NX_MASK ? 'X' : '-',
  1184. pte & PG_GLOBAL_MASK ? 'G' : '-',
  1185. pte & PG_PSE_MASK ? 'P' : '-',
  1186. pte & PG_DIRTY_MASK ? 'D' : '-',
  1187. pte & PG_ACCESSED_MASK ? 'A' : '-',
  1188. pte & PG_PCD_MASK ? 'C' : '-',
  1189. pte & PG_PWT_MASK ? 'T' : '-',
  1190. pte & PG_USER_MASK ? 'U' : '-',
  1191. pte & PG_RW_MASK ? 'W' : '-');
  1192. }
  1193. static void tlb_info_32(Monitor *mon, CPUArchState *env)
  1194. {
  1195. unsigned int l1, l2;
  1196. uint32_t pgd, pde, pte;
  1197. pgd = env->cr[3] & ~0xfff;
  1198. for(l1 = 0; l1 < 1024; l1++) {
  1199. cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
  1200. pde = le32_to_cpu(pde);
  1201. if (pde & PG_PRESENT_MASK) {
  1202. if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
  1203. /* 4M pages */
  1204. print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
  1205. } else {
  1206. for(l2 = 0; l2 < 1024; l2++) {
  1207. cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
  1208. pte = le32_to_cpu(pte);
  1209. if (pte & PG_PRESENT_MASK) {
  1210. print_pte(mon, (l1 << 22) + (l2 << 12),
  1211. pte & ~PG_PSE_MASK,
  1212. ~0xfff);
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. }
  1219. static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
  1220. {
  1221. unsigned int l1, l2, l3;
  1222. uint64_t pdpe, pde, pte;
  1223. uint64_t pdp_addr, pd_addr, pt_addr;
  1224. pdp_addr = env->cr[3] & ~0x1f;
  1225. for (l1 = 0; l1 < 4; l1++) {
  1226. cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
  1227. pdpe = le64_to_cpu(pdpe);
  1228. if (pdpe & PG_PRESENT_MASK) {
  1229. pd_addr = pdpe & 0x3fffffffff000ULL;
  1230. for (l2 = 0; l2 < 512; l2++) {
  1231. cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
  1232. pde = le64_to_cpu(pde);
  1233. if (pde & PG_PRESENT_MASK) {
  1234. if (pde & PG_PSE_MASK) {
  1235. /* 2M pages with PAE, CR4.PSE is ignored */
  1236. print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
  1237. ~((hwaddr)(1 << 20) - 1));
  1238. } else {
  1239. pt_addr = pde & 0x3fffffffff000ULL;
  1240. for (l3 = 0; l3 < 512; l3++) {
  1241. cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
  1242. pte = le64_to_cpu(pte);
  1243. if (pte & PG_PRESENT_MASK) {
  1244. print_pte(mon, (l1 << 30 ) + (l2 << 21)
  1245. + (l3 << 12),
  1246. pte & ~PG_PSE_MASK,
  1247. ~(hwaddr)0xfff);
  1248. }
  1249. }
  1250. }
  1251. }
  1252. }
  1253. }
  1254. }
  1255. }
  1256. #ifdef TARGET_X86_64
  1257. static void tlb_info_64(Monitor *mon, CPUArchState *env)
  1258. {
  1259. uint64_t l1, l2, l3, l4;
  1260. uint64_t pml4e, pdpe, pde, pte;
  1261. uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
  1262. pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
  1263. for (l1 = 0; l1 < 512; l1++) {
  1264. cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
  1265. pml4e = le64_to_cpu(pml4e);
  1266. if (pml4e & PG_PRESENT_MASK) {
  1267. pdp_addr = pml4e & 0x3fffffffff000ULL;
  1268. for (l2 = 0; l2 < 512; l2++) {
  1269. cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
  1270. pdpe = le64_to_cpu(pdpe);
  1271. if (pdpe & PG_PRESENT_MASK) {
  1272. if (pdpe & PG_PSE_MASK) {
  1273. /* 1G pages, CR4.PSE is ignored */
  1274. print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
  1275. 0x3ffffc0000000ULL);
  1276. } else {
  1277. pd_addr = pdpe & 0x3fffffffff000ULL;
  1278. for (l3 = 0; l3 < 512; l3++) {
  1279. cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
  1280. pde = le64_to_cpu(pde);
  1281. if (pde & PG_PRESENT_MASK) {
  1282. if (pde & PG_PSE_MASK) {
  1283. /* 2M pages, CR4.PSE is ignored */
  1284. print_pte(mon, (l1 << 39) + (l2 << 30) +
  1285. (l3 << 21), pde,
  1286. 0x3ffffffe00000ULL);
  1287. } else {
  1288. pt_addr = pde & 0x3fffffffff000ULL;
  1289. for (l4 = 0; l4 < 512; l4++) {
  1290. cpu_physical_memory_read(pt_addr
  1291. + l4 * 8,
  1292. &pte, 8);
  1293. pte = le64_to_cpu(pte);
  1294. if (pte & PG_PRESENT_MASK) {
  1295. print_pte(mon, (l1 << 39) +
  1296. (l2 << 30) +
  1297. (l3 << 21) + (l4 << 12),
  1298. pte & ~PG_PSE_MASK,
  1299. 0x3fffffffff000ULL);
  1300. }
  1301. }
  1302. }
  1303. }
  1304. }
  1305. }
  1306. }
  1307. }
  1308. }
  1309. }
  1310. }
  1311. #endif
  1312. static void tlb_info(Monitor *mon, const QDict *qdict)
  1313. {
  1314. CPUArchState *env;
  1315. env = mon_get_cpu();
  1316. if (!(env->cr[0] & CR0_PG_MASK)) {
  1317. monitor_printf(mon, "PG disabled\n");
  1318. return;
  1319. }
  1320. if (env->cr[4] & CR4_PAE_MASK) {
  1321. #ifdef TARGET_X86_64
  1322. if (env->hflags & HF_LMA_MASK) {
  1323. tlb_info_64(mon, env);
  1324. } else
  1325. #endif
  1326. {
  1327. tlb_info_pae32(mon, env);
  1328. }
  1329. } else {
  1330. tlb_info_32(mon, env);
  1331. }
  1332. }
  1333. static void mem_print(Monitor *mon, hwaddr *pstart,
  1334. int *plast_prot,
  1335. hwaddr end, int prot)
  1336. {
  1337. int prot1;
  1338. prot1 = *plast_prot;
  1339. if (prot != prot1) {
  1340. if (*pstart != -1) {
  1341. monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
  1342. TARGET_FMT_plx " %c%c%c\n",
  1343. *pstart, end, end - *pstart,
  1344. prot1 & PG_USER_MASK ? 'u' : '-',
  1345. 'r',
  1346. prot1 & PG_RW_MASK ? 'w' : '-');
  1347. }
  1348. if (prot != 0)
  1349. *pstart = end;
  1350. else
  1351. *pstart = -1;
  1352. *plast_prot = prot;
  1353. }
  1354. }
  1355. static void mem_info_32(Monitor *mon, CPUArchState *env)
  1356. {
  1357. unsigned int l1, l2;
  1358. int prot, last_prot;
  1359. uint32_t pgd, pde, pte;
  1360. hwaddr start, end;
  1361. pgd = env->cr[3] & ~0xfff;
  1362. last_prot = 0;
  1363. start = -1;
  1364. for(l1 = 0; l1 < 1024; l1++) {
  1365. cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
  1366. pde = le32_to_cpu(pde);
  1367. end = l1 << 22;
  1368. if (pde & PG_PRESENT_MASK) {
  1369. if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
  1370. prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
  1371. mem_print(mon, &start, &last_prot, end, prot);
  1372. } else {
  1373. for(l2 = 0; l2 < 1024; l2++) {
  1374. cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
  1375. pte = le32_to_cpu(pte);
  1376. end = (l1 << 22) + (l2 << 12);
  1377. if (pte & PG_PRESENT_MASK) {
  1378. prot = pte & pde &
  1379. (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
  1380. } else {
  1381. prot = 0;
  1382. }
  1383. mem_print(mon, &start, &last_prot, end, prot);
  1384. }
  1385. }
  1386. } else {
  1387. prot = 0;
  1388. mem_print(mon, &start, &last_prot, end, prot);
  1389. }
  1390. }
  1391. /* Flush last range */
  1392. mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
  1393. }
  1394. static void mem_info_pae32(Monitor *mon, CPUArchState *env)
  1395. {
  1396. unsigned int l1, l2, l3;
  1397. int prot, last_prot;
  1398. uint64_t pdpe, pde, pte;
  1399. uint64_t pdp_addr, pd_addr, pt_addr;
  1400. hwaddr start, end;
  1401. pdp_addr = env->cr[3] & ~0x1f;
  1402. last_prot = 0;
  1403. start = -1;
  1404. for (l1 = 0; l1 < 4; l1++) {
  1405. cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
  1406. pdpe = le64_to_cpu(pdpe);
  1407. end = l1 << 30;
  1408. if (pdpe & PG_PRESENT_MASK) {
  1409. pd_addr = pdpe & 0x3fffffffff000ULL;
  1410. for (l2 = 0; l2 < 512; l2++) {
  1411. cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
  1412. pde = le64_to_cpu(pde);
  1413. end = (l1 << 30) + (l2 << 21);
  1414. if (pde & PG_PRESENT_MASK) {
  1415. if (pde & PG_PSE_MASK) {
  1416. prot = pde & (PG_USER_MASK | PG_RW_MASK |
  1417. PG_PRESENT_MASK);
  1418. mem_print(mon, &start, &last_prot, end, prot);
  1419. } else {
  1420. pt_addr = pde & 0x3fffffffff000ULL;
  1421. for (l3 = 0; l3 < 512; l3++) {
  1422. cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
  1423. pte = le64_to_cpu(pte);
  1424. end = (l1 << 30) + (l2 << 21) + (l3 << 12);
  1425. if (pte & PG_PRESENT_MASK) {
  1426. prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
  1427. PG_PRESENT_MASK);
  1428. } else {
  1429. prot = 0;
  1430. }
  1431. mem_print(mon, &start, &last_prot, end, prot);
  1432. }
  1433. }
  1434. } else {
  1435. prot = 0;
  1436. mem_print(mon, &start, &last_prot, end, prot);
  1437. }
  1438. }
  1439. } else {
  1440. prot = 0;
  1441. mem_print(mon, &start, &last_prot, end, prot);
  1442. }
  1443. }
  1444. /* Flush last range */
  1445. mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
  1446. }
  1447. #ifdef TARGET_X86_64
  1448. static void mem_info_64(Monitor *mon, CPUArchState *env)
  1449. {
  1450. int prot, last_prot;
  1451. uint64_t l1, l2, l3, l4;
  1452. uint64_t pml4e, pdpe, pde, pte;
  1453. uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
  1454. pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
  1455. last_prot = 0;
  1456. start = -1;
  1457. for (l1 = 0; l1 < 512; l1++) {
  1458. cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
  1459. pml4e = le64_to_cpu(pml4e);
  1460. end = l1 << 39;
  1461. if (pml4e & PG_PRESENT_MASK) {
  1462. pdp_addr = pml4e & 0x3fffffffff000ULL;
  1463. for (l2 = 0; l2 < 512; l2++) {
  1464. cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
  1465. pdpe = le64_to_cpu(pdpe);
  1466. end = (l1 << 39) + (l2 << 30);
  1467. if (pdpe & PG_PRESENT_MASK) {
  1468. if (pdpe & PG_PSE_MASK) {
  1469. prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
  1470. PG_PRESENT_MASK);
  1471. prot &= pml4e;
  1472. mem_print(mon, &start, &last_prot, end, prot);
  1473. } else {
  1474. pd_addr = pdpe & 0x3fffffffff000ULL;
  1475. for (l3 = 0; l3 < 512; l3++) {
  1476. cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
  1477. pde = le64_to_cpu(pde);
  1478. end = (l1 << 39) + (l2 << 30) + (l3 << 21);
  1479. if (pde & PG_PRESENT_MASK) {
  1480. if (pde & PG_PSE_MASK) {
  1481. prot = pde & (PG_USER_MASK | PG_RW_MASK |
  1482. PG_PRESENT_MASK);
  1483. prot &= pml4e & pdpe;
  1484. mem_print(mon, &start, &last_prot, end, prot);
  1485. } else {
  1486. pt_addr = pde & 0x3fffffffff000ULL;
  1487. for (l4 = 0; l4 < 512; l4++) {
  1488. cpu_physical_memory_read(pt_addr
  1489. + l4 * 8,
  1490. &pte, 8);
  1491. pte = le64_to_cpu(pte);
  1492. end = (l1 << 39) + (l2 << 30) +
  1493. (l3 << 21) + (l4 << 12);
  1494. if (pte & PG_PRESENT_MASK) {
  1495. prot = pte & (PG_USER_MASK | PG_RW_MASK |
  1496. PG_PRESENT_MASK);
  1497. prot &= pml4e & pdpe & pde;
  1498. } else {
  1499. prot = 0;
  1500. }
  1501. mem_print(mon, &start, &last_prot, end, prot);
  1502. }
  1503. }
  1504. } else {
  1505. prot = 0;
  1506. mem_print(mon, &start, &last_prot, end, prot);
  1507. }
  1508. }
  1509. }
  1510. } else {
  1511. prot = 0;
  1512. mem_print(mon, &start, &last_prot, end, prot);
  1513. }
  1514. }
  1515. } else {
  1516. prot = 0;
  1517. mem_print(mon, &start, &last_prot, end, prot);
  1518. }
  1519. }
  1520. /* Flush last range */
  1521. mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
  1522. }
  1523. #endif
  1524. static void mem_info(Monitor *mon, const QDict *qdict)
  1525. {
  1526. CPUArchState *env;
  1527. env = mon_get_cpu();
  1528. if (!(env->cr[0] & CR0_PG_MASK)) {
  1529. monitor_printf(mon, "PG disabled\n");
  1530. return;
  1531. }
  1532. if (env->cr[4] & CR4_PAE_MASK) {
  1533. #ifdef TARGET_X86_64
  1534. if (env->hflags & HF_LMA_MASK) {
  1535. mem_info_64(mon, env);
  1536. } else
  1537. #endif
  1538. {
  1539. mem_info_pae32(mon, env);
  1540. }
  1541. } else {
  1542. mem_info_32(mon, env);
  1543. }
  1544. }
  1545. #endif
  1546. #if defined(TARGET_SH4)
  1547. static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
  1548. {
  1549. monitor_printf(mon, " tlb%i:\t"
  1550. "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
  1551. "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
  1552. "dirty=%hhu writethrough=%hhu\n",
  1553. idx,
  1554. tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
  1555. tlb->v, tlb->sh, tlb->c, tlb->pr,
  1556. tlb->d, tlb->wt);
  1557. }
  1558. static void tlb_info(Monitor *mon, const QDict *qdict)
  1559. {
  1560. CPUArchState *env = mon_get_cpu();
  1561. int i;
  1562. monitor_printf (mon, "ITLB:\n");
  1563. for (i = 0 ; i < ITLB_SIZE ; i++)
  1564. print_tlb (mon, i, &env->itlb[i]);
  1565. monitor_printf (mon, "UTLB:\n");
  1566. for (i = 0 ; i < UTLB_SIZE ; i++)
  1567. print_tlb (mon, i, &env->utlb[i]);
  1568. }
  1569. #endif
  1570. #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
  1571. static void tlb_info(Monitor *mon, const QDict *qdict)
  1572. {
  1573. CPUArchState *env1 = mon_get_cpu();
  1574. dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
  1575. }
  1576. #endif
  1577. static void do_info_mtree(Monitor *mon, const QDict *qdict)
  1578. {
  1579. mtree_info((fprintf_function)monitor_printf, mon);
  1580. }
  1581. static void do_info_numa(Monitor *mon, const QDict *qdict)
  1582. {
  1583. int i;
  1584. CPUArchState *env;
  1585. CPUState *cpu;
  1586. monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
  1587. for (i = 0; i < nb_numa_nodes; i++) {
  1588. monitor_printf(mon, "node %d cpus:", i);
  1589. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1590. cpu = ENV_GET_CPU(env);
  1591. if (cpu->numa_node == i) {
  1592. monitor_printf(mon, " %d", cpu->cpu_index);
  1593. }
  1594. }
  1595. monitor_printf(mon, "\n");
  1596. monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
  1597. node_mem[i] >> 20);
  1598. }
  1599. }
  1600. #ifdef CONFIG_PROFILER
  1601. int64_t qemu_time;
  1602. int64_t dev_time;
  1603. static void do_info_profile(Monitor *mon, const QDict *qdict)
  1604. {
  1605. int64_t total;
  1606. total = qemu_time;
  1607. if (total == 0)
  1608. total = 1;
  1609. monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
  1610. dev_time, dev_time / (double)get_ticks_per_sec());
  1611. monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
  1612. qemu_time, qemu_time / (double)get_ticks_per_sec());
  1613. qemu_time = 0;
  1614. dev_time = 0;
  1615. }
  1616. #else
  1617. static void do_info_profile(Monitor *mon, const QDict *qdict)
  1618. {
  1619. monitor_printf(mon, "Internal profiler not compiled\n");
  1620. }
  1621. #endif
  1622. /* Capture support */
  1623. static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
  1624. static void do_info_capture(Monitor *mon, const QDict *qdict)
  1625. {
  1626. int i;
  1627. CaptureState *s;
  1628. for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
  1629. monitor_printf(mon, "[%d]: ", i);
  1630. s->ops.info (s->opaque);
  1631. }
  1632. }
  1633. #ifdef HAS_AUDIO
  1634. static void do_stop_capture(Monitor *mon, const QDict *qdict)
  1635. {
  1636. int i;
  1637. int n = qdict_get_int(qdict, "n");
  1638. CaptureState *s;
  1639. for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
  1640. if (i == n) {
  1641. s->ops.destroy (s->opaque);
  1642. QLIST_REMOVE (s, entries);
  1643. g_free (s);
  1644. return;
  1645. }
  1646. }
  1647. }
  1648. static void do_wav_capture(Monitor *mon, const QDict *qdict)
  1649. {
  1650. const char *path = qdict_get_str(qdict, "path");
  1651. int has_freq = qdict_haskey(qdict, "freq");
  1652. int freq = qdict_get_try_int(qdict, "freq", -1);
  1653. int has_bits = qdict_haskey(qdict, "bits");
  1654. int bits = qdict_get_try_int(qdict, "bits", -1);
  1655. int has_channels = qdict_haskey(qdict, "nchannels");
  1656. int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
  1657. CaptureState *s;
  1658. s = g_malloc0 (sizeof (*s));
  1659. freq = has_freq ? freq : 44100;
  1660. bits = has_bits ? bits : 16;
  1661. nchannels = has_channels ? nchannels : 2;
  1662. if (wav_start_capture (s, path, freq, bits, nchannels)) {
  1663. monitor_printf(mon, "Failed to add wave capture\n");
  1664. g_free (s);
  1665. return;
  1666. }
  1667. QLIST_INSERT_HEAD (&capture_head, s, entries);
  1668. }
  1669. #endif
  1670. static qemu_acl *find_acl(Monitor *mon, const char *name)
  1671. {
  1672. qemu_acl *acl = qemu_acl_find(name);
  1673. if (!acl) {
  1674. monitor_printf(mon, "acl: unknown list '%s'\n", name);
  1675. }
  1676. return acl;
  1677. }
  1678. static void do_acl_show(Monitor *mon, const QDict *qdict)
  1679. {
  1680. const char *aclname = qdict_get_str(qdict, "aclname");
  1681. qemu_acl *acl = find_acl(mon, aclname);
  1682. qemu_acl_entry *entry;
  1683. int i = 0;
  1684. if (acl) {
  1685. monitor_printf(mon, "policy: %s\n",
  1686. acl->defaultDeny ? "deny" : "allow");
  1687. QTAILQ_FOREACH(entry, &acl->entries, next) {
  1688. i++;
  1689. monitor_printf(mon, "%d: %s %s\n", i,
  1690. entry->deny ? "deny" : "allow", entry->match);
  1691. }
  1692. }
  1693. }
  1694. static void do_acl_reset(Monitor *mon, const QDict *qdict)
  1695. {
  1696. const char *aclname = qdict_get_str(qdict, "aclname");
  1697. qemu_acl *acl = find_acl(mon, aclname);
  1698. if (acl) {
  1699. qemu_acl_reset(acl);
  1700. monitor_printf(mon, "acl: removed all rules\n");
  1701. }
  1702. }
  1703. static void do_acl_policy(Monitor *mon, const QDict *qdict)
  1704. {
  1705. const char *aclname = qdict_get_str(qdict, "aclname");
  1706. const char *policy = qdict_get_str(qdict, "policy");
  1707. qemu_acl *acl = find_acl(mon, aclname);
  1708. if (acl) {
  1709. if (strcmp(policy, "allow") == 0) {
  1710. acl->defaultDeny = 0;
  1711. monitor_printf(mon, "acl: policy set to 'allow'\n");
  1712. } else if (strcmp(policy, "deny") == 0) {
  1713. acl->defaultDeny = 1;
  1714. monitor_printf(mon, "acl: policy set to 'deny'\n");
  1715. } else {
  1716. monitor_printf(mon, "acl: unknown policy '%s', "
  1717. "expected 'deny' or 'allow'\n", policy);
  1718. }
  1719. }
  1720. }
  1721. static void do_acl_add(Monitor *mon, const QDict *qdict)
  1722. {
  1723. const char *aclname = qdict_get_str(qdict, "aclname");
  1724. const char *match = qdict_get_str(qdict, "match");
  1725. const char *policy = qdict_get_str(qdict, "policy");
  1726. int has_index = qdict_haskey(qdict, "index");
  1727. int index = qdict_get_try_int(qdict, "index", -1);
  1728. qemu_acl *acl = find_acl(mon, aclname);
  1729. int deny, ret;
  1730. if (acl) {
  1731. if (strcmp(policy, "allow") == 0) {
  1732. deny = 0;
  1733. } else if (strcmp(policy, "deny") == 0) {
  1734. deny = 1;
  1735. } else {
  1736. monitor_printf(mon, "acl: unknown policy '%s', "
  1737. "expected 'deny' or 'allow'\n", policy);
  1738. return;
  1739. }
  1740. if (has_index)
  1741. ret = qemu_acl_insert(acl, deny, match, index);
  1742. else
  1743. ret = qemu_acl_append(acl, deny, match);
  1744. if (ret < 0)
  1745. monitor_printf(mon, "acl: unable to add acl entry\n");
  1746. else
  1747. monitor_printf(mon, "acl: added rule at position %d\n", ret);
  1748. }
  1749. }
  1750. static void do_acl_remove(Monitor *mon, const QDict *qdict)
  1751. {
  1752. const char *aclname = qdict_get_str(qdict, "aclname");
  1753. const char *match = qdict_get_str(qdict, "match");
  1754. qemu_acl *acl = find_acl(mon, aclname);
  1755. int ret;
  1756. if (acl) {
  1757. ret = qemu_acl_remove(acl, match);
  1758. if (ret < 0)
  1759. monitor_printf(mon, "acl: no matching acl entry\n");
  1760. else
  1761. monitor_printf(mon, "acl: removed rule at position %d\n", ret);
  1762. }
  1763. }
  1764. #if defined(TARGET_I386)
  1765. static void do_inject_mce(Monitor *mon, const QDict *qdict)
  1766. {
  1767. X86CPU *cpu;
  1768. CPUX86State *cenv;
  1769. CPUState *cs;
  1770. int cpu_index = qdict_get_int(qdict, "cpu_index");
  1771. int bank = qdict_get_int(qdict, "bank");
  1772. uint64_t status = qdict_get_int(qdict, "status");
  1773. uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
  1774. uint64_t addr = qdict_get_int(qdict, "addr");
  1775. uint64_t misc = qdict_get_int(qdict, "misc");
  1776. int flags = MCE_INJECT_UNCOND_AO;
  1777. if (qdict_get_try_bool(qdict, "broadcast", 0)) {
  1778. flags |= MCE_INJECT_BROADCAST;
  1779. }
  1780. for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
  1781. cpu = x86_env_get_cpu(cenv);
  1782. cs = CPU(cpu);
  1783. if (cs->cpu_index == cpu_index) {
  1784. cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
  1785. flags);
  1786. break;
  1787. }
  1788. }
  1789. }
  1790. #endif
  1791. void qmp_getfd(const char *fdname, Error **errp)
  1792. {
  1793. mon_fd_t *monfd;
  1794. int fd;
  1795. fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
  1796. if (fd == -1) {
  1797. error_set(errp, QERR_FD_NOT_SUPPLIED);
  1798. return;
  1799. }
  1800. if (qemu_isdigit(fdname[0])) {
  1801. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
  1802. "a name not starting with a digit");
  1803. return;
  1804. }
  1805. QLIST_FOREACH(monfd, &cur_mon->fds, next) {
  1806. if (strcmp(monfd->name, fdname) != 0) {
  1807. continue;
  1808. }
  1809. close(monfd->fd);
  1810. monfd->fd = fd;
  1811. return;
  1812. }
  1813. monfd = g_malloc0(sizeof(mon_fd_t));
  1814. monfd->name = g_strdup(fdname);
  1815. monfd->fd = fd;
  1816. QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
  1817. }
  1818. void qmp_closefd(const char *fdname, Error **errp)
  1819. {
  1820. mon_fd_t *monfd;
  1821. QLIST_FOREACH(monfd, &cur_mon->fds, next) {
  1822. if (strcmp(monfd->name, fdname) != 0) {
  1823. continue;
  1824. }
  1825. QLIST_REMOVE(monfd, next);
  1826. close(monfd->fd);
  1827. g_free(monfd->name);
  1828. g_free(monfd);
  1829. return;
  1830. }
  1831. error_set(errp, QERR_FD_NOT_FOUND, fdname);
  1832. }
  1833. static void do_loadvm(Monitor *mon, const QDict *qdict)
  1834. {
  1835. int saved_vm_running = runstate_is_running();
  1836. const char *name = qdict_get_str(qdict, "name");
  1837. vm_stop(RUN_STATE_RESTORE_VM);
  1838. if (load_vmstate(name) == 0 && saved_vm_running) {
  1839. vm_start();
  1840. }
  1841. }
  1842. int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
  1843. {
  1844. mon_fd_t *monfd;
  1845. QLIST_FOREACH(monfd, &mon->fds, next) {
  1846. int fd;
  1847. if (strcmp(monfd->name, fdname) != 0) {
  1848. continue;
  1849. }
  1850. fd = monfd->fd;
  1851. /* caller takes ownership of fd */
  1852. QLIST_REMOVE(monfd, next);
  1853. g_free(monfd->name);
  1854. g_free(monfd);
  1855. return fd;
  1856. }
  1857. error_setg(errp, "File descriptor named '%s' has not been found", fdname);
  1858. return -1;
  1859. }
  1860. static void monitor_fdset_cleanup(MonFdset *mon_fdset)
  1861. {
  1862. MonFdsetFd *mon_fdset_fd;
  1863. MonFdsetFd *mon_fdset_fd_next;
  1864. QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
  1865. if ((mon_fdset_fd->removed ||
  1866. (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
  1867. runstate_is_running()) {
  1868. close(mon_fdset_fd->fd);
  1869. g_free(mon_fdset_fd->opaque);
  1870. QLIST_REMOVE(mon_fdset_fd, next);
  1871. g_free(mon_fdset_fd);
  1872. }
  1873. }
  1874. if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
  1875. QLIST_REMOVE(mon_fdset, next);
  1876. g_free(mon_fdset);
  1877. }
  1878. }
  1879. static void monitor_fdsets_cleanup(void)
  1880. {
  1881. MonFdset *mon_fdset;
  1882. MonFdset *mon_fdset_next;
  1883. QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
  1884. monitor_fdset_cleanup(mon_fdset);
  1885. }
  1886. }
  1887. AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
  1888. const char *opaque, Error **errp)
  1889. {
  1890. int fd;
  1891. Monitor *mon = cur_mon;
  1892. AddfdInfo *fdinfo;
  1893. fd = qemu_chr_fe_get_msgfd(mon->chr);
  1894. if (fd == -1) {
  1895. error_set(errp, QERR_FD_NOT_SUPPLIED);
  1896. goto error;
  1897. }
  1898. fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
  1899. has_opaque, opaque, errp);
  1900. if (fdinfo) {
  1901. return fdinfo;
  1902. }
  1903. error:
  1904. if (fd != -1) {
  1905. close(fd);
  1906. }
  1907. return NULL;
  1908. }
  1909. void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
  1910. {
  1911. MonFdset *mon_fdset;
  1912. MonFdsetFd *mon_fdset_fd;
  1913. char fd_str[60];
  1914. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  1915. if (mon_fdset->id != fdset_id) {
  1916. continue;
  1917. }
  1918. QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
  1919. if (has_fd) {
  1920. if (mon_fdset_fd->fd != fd) {
  1921. continue;
  1922. }
  1923. mon_fdset_fd->removed = true;
  1924. break;
  1925. } else {
  1926. mon_fdset_fd->removed = true;
  1927. }
  1928. }
  1929. if (has_fd && !mon_fdset_fd) {
  1930. goto error;
  1931. }
  1932. monitor_fdset_cleanup(mon_fdset);
  1933. return;
  1934. }
  1935. error:
  1936. if (has_fd) {
  1937. snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
  1938. fdset_id, fd);
  1939. } else {
  1940. snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
  1941. }
  1942. error_set(errp, QERR_FD_NOT_FOUND, fd_str);
  1943. }
  1944. FdsetInfoList *qmp_query_fdsets(Error **errp)
  1945. {
  1946. MonFdset *mon_fdset;
  1947. MonFdsetFd *mon_fdset_fd;
  1948. FdsetInfoList *fdset_list = NULL;
  1949. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  1950. FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
  1951. FdsetFdInfoList *fdsetfd_list = NULL;
  1952. fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
  1953. fdset_info->value->fdset_id = mon_fdset->id;
  1954. QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
  1955. FdsetFdInfoList *fdsetfd_info;
  1956. fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
  1957. fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
  1958. fdsetfd_info->value->fd = mon_fdset_fd->fd;
  1959. if (mon_fdset_fd->opaque) {
  1960. fdsetfd_info->value->has_opaque = true;
  1961. fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
  1962. } else {
  1963. fdsetfd_info->value->has_opaque = false;
  1964. }
  1965. fdsetfd_info->next = fdsetfd_list;
  1966. fdsetfd_list = fdsetfd_info;
  1967. }
  1968. fdset_info->value->fds = fdsetfd_list;
  1969. fdset_info->next = fdset_list;
  1970. fdset_list = fdset_info;
  1971. }
  1972. return fdset_list;
  1973. }
  1974. AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
  1975. bool has_opaque, const char *opaque,
  1976. Error **errp)
  1977. {
  1978. MonFdset *mon_fdset = NULL;
  1979. MonFdsetFd *mon_fdset_fd;
  1980. AddfdInfo *fdinfo;
  1981. if (has_fdset_id) {
  1982. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  1983. /* Break if match found or match impossible due to ordering by ID */
  1984. if (fdset_id <= mon_fdset->id) {
  1985. if (fdset_id < mon_fdset->id) {
  1986. mon_fdset = NULL;
  1987. }
  1988. break;
  1989. }
  1990. }
  1991. }
  1992. if (mon_fdset == NULL) {
  1993. int64_t fdset_id_prev = -1;
  1994. MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
  1995. if (has_fdset_id) {
  1996. if (fdset_id < 0) {
  1997. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
  1998. "a non-negative value");
  1999. return NULL;
  2000. }
  2001. /* Use specified fdset ID */
  2002. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2003. mon_fdset_cur = mon_fdset;
  2004. if (fdset_id < mon_fdset_cur->id) {
  2005. break;
  2006. }
  2007. }
  2008. } else {
  2009. /* Use first available fdset ID */
  2010. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2011. mon_fdset_cur = mon_fdset;
  2012. if (fdset_id_prev == mon_fdset_cur->id - 1) {
  2013. fdset_id_prev = mon_fdset_cur->id;
  2014. continue;
  2015. }
  2016. break;
  2017. }
  2018. }
  2019. mon_fdset = g_malloc0(sizeof(*mon_fdset));
  2020. if (has_fdset_id) {
  2021. mon_fdset->id = fdset_id;
  2022. } else {
  2023. mon_fdset->id = fdset_id_prev + 1;
  2024. }
  2025. /* The fdset list is ordered by fdset ID */
  2026. if (!mon_fdset_cur) {
  2027. QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
  2028. } else if (mon_fdset->id < mon_fdset_cur->id) {
  2029. QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
  2030. } else {
  2031. QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
  2032. }
  2033. }
  2034. mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
  2035. mon_fdset_fd->fd = fd;
  2036. mon_fdset_fd->removed = false;
  2037. if (has_opaque) {
  2038. mon_fdset_fd->opaque = g_strdup(opaque);
  2039. }
  2040. QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
  2041. fdinfo = g_malloc0(sizeof(*fdinfo));
  2042. fdinfo->fdset_id = mon_fdset->id;
  2043. fdinfo->fd = mon_fdset_fd->fd;
  2044. return fdinfo;
  2045. }
  2046. int monitor_fdset_get_fd(int64_t fdset_id, int flags)
  2047. {
  2048. #ifndef _WIN32
  2049. MonFdset *mon_fdset;
  2050. MonFdsetFd *mon_fdset_fd;
  2051. int mon_fd_flags;
  2052. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2053. if (mon_fdset->id != fdset_id) {
  2054. continue;
  2055. }
  2056. QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
  2057. mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
  2058. if (mon_fd_flags == -1) {
  2059. return -1;
  2060. }
  2061. if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
  2062. return mon_fdset_fd->fd;
  2063. }
  2064. }
  2065. errno = EACCES;
  2066. return -1;
  2067. }
  2068. #endif
  2069. errno = ENOENT;
  2070. return -1;
  2071. }
  2072. int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
  2073. {
  2074. MonFdset *mon_fdset;
  2075. MonFdsetFd *mon_fdset_fd_dup;
  2076. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2077. if (mon_fdset->id != fdset_id) {
  2078. continue;
  2079. }
  2080. QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
  2081. if (mon_fdset_fd_dup->fd == dup_fd) {
  2082. return -1;
  2083. }
  2084. }
  2085. mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
  2086. mon_fdset_fd_dup->fd = dup_fd;
  2087. QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
  2088. return 0;
  2089. }
  2090. return -1;
  2091. }
  2092. static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
  2093. {
  2094. MonFdset *mon_fdset;
  2095. MonFdsetFd *mon_fdset_fd_dup;
  2096. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2097. QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
  2098. if (mon_fdset_fd_dup->fd == dup_fd) {
  2099. if (remove) {
  2100. QLIST_REMOVE(mon_fdset_fd_dup, next);
  2101. if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
  2102. monitor_fdset_cleanup(mon_fdset);
  2103. }
  2104. }
  2105. return mon_fdset->id;
  2106. }
  2107. }
  2108. }
  2109. return -1;
  2110. }
  2111. int monitor_fdset_dup_fd_find(int dup_fd)
  2112. {
  2113. return monitor_fdset_dup_fd_find_remove(dup_fd, false);
  2114. }
  2115. int monitor_fdset_dup_fd_remove(int dup_fd)
  2116. {
  2117. return monitor_fdset_dup_fd_find_remove(dup_fd, true);
  2118. }
  2119. int monitor_handle_fd_param(Monitor *mon, const char *fdname)
  2120. {
  2121. int fd;
  2122. Error *local_err = NULL;
  2123. if (!qemu_isdigit(fdname[0]) && mon) {
  2124. fd = monitor_get_fd(mon, fdname, &local_err);
  2125. if (fd == -1) {
  2126. qerror_report_err(local_err);
  2127. error_free(local_err);
  2128. return -1;
  2129. }
  2130. } else {
  2131. fd = qemu_parse_fd(fdname);
  2132. }
  2133. return fd;
  2134. }
  2135. /* Please update hmp-commands.hx when adding or changing commands */
  2136. static mon_cmd_t info_cmds[] = {
  2137. {
  2138. .name = "version",
  2139. .args_type = "",
  2140. .params = "",
  2141. .help = "show the version of QEMU",
  2142. .mhandler.cmd = hmp_info_version,
  2143. },
  2144. {
  2145. .name = "network",
  2146. .args_type = "",
  2147. .params = "",
  2148. .help = "show the network state",
  2149. .mhandler.cmd = do_info_network,
  2150. },
  2151. {
  2152. .name = "chardev",
  2153. .args_type = "",
  2154. .params = "",
  2155. .help = "show the character devices",
  2156. .mhandler.cmd = hmp_info_chardev,
  2157. },
  2158. {
  2159. .name = "block",
  2160. .args_type = "",
  2161. .params = "",
  2162. .help = "show the block devices",
  2163. .mhandler.cmd = hmp_info_block,
  2164. },
  2165. {
  2166. .name = "blockstats",
  2167. .args_type = "",
  2168. .params = "",
  2169. .help = "show block device statistics",
  2170. .mhandler.cmd = hmp_info_blockstats,
  2171. },
  2172. {
  2173. .name = "block-jobs",
  2174. .args_type = "",
  2175. .params = "",
  2176. .help = "show progress of ongoing block device operations",
  2177. .mhandler.cmd = hmp_info_block_jobs,
  2178. },
  2179. {
  2180. .name = "registers",
  2181. .args_type = "",
  2182. .params = "",
  2183. .help = "show the cpu registers",
  2184. .mhandler.cmd = do_info_registers,
  2185. },
  2186. {
  2187. .name = "cpus",
  2188. .args_type = "",
  2189. .params = "",
  2190. .help = "show infos for each CPU",
  2191. .mhandler.cmd = hmp_info_cpus,
  2192. },
  2193. {
  2194. .name = "history",
  2195. .args_type = "",
  2196. .params = "",
  2197. .help = "show the command line history",
  2198. .mhandler.cmd = do_info_history,
  2199. },
  2200. #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
  2201. defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
  2202. {
  2203. .name = "irq",
  2204. .args_type = "",
  2205. .params = "",
  2206. .help = "show the interrupts statistics (if available)",
  2207. #ifdef TARGET_SPARC
  2208. .mhandler.cmd = sun4m_irq_info,
  2209. #elif defined(TARGET_LM32)
  2210. .mhandler.cmd = lm32_irq_info,
  2211. #else
  2212. .mhandler.cmd = irq_info,
  2213. #endif
  2214. },
  2215. {
  2216. .name = "pic",
  2217. .args_type = "",
  2218. .params = "",
  2219. .help = "show i8259 (PIC) state",
  2220. #ifdef TARGET_SPARC
  2221. .mhandler.cmd = sun4m_pic_info,
  2222. #elif defined(TARGET_LM32)
  2223. .mhandler.cmd = lm32_do_pic_info,
  2224. #else
  2225. .mhandler.cmd = pic_info,
  2226. #endif
  2227. },
  2228. #endif
  2229. {
  2230. .name = "pci",
  2231. .args_type = "",
  2232. .params = "",
  2233. .help = "show PCI info",
  2234. .mhandler.cmd = hmp_info_pci,
  2235. },
  2236. #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
  2237. defined(TARGET_PPC) || defined(TARGET_XTENSA)
  2238. {
  2239. .name = "tlb",
  2240. .args_type = "",
  2241. .params = "",
  2242. .help = "show virtual to physical memory mappings",
  2243. .mhandler.cmd = tlb_info,
  2244. },
  2245. #endif
  2246. #if defined(TARGET_I386)
  2247. {
  2248. .name = "mem",
  2249. .args_type = "",
  2250. .params = "",
  2251. .help = "show the active virtual memory mappings",
  2252. .mhandler.cmd = mem_info,
  2253. },
  2254. #endif
  2255. {
  2256. .name = "mtree",
  2257. .args_type = "",
  2258. .params = "",
  2259. .help = "show memory tree",
  2260. .mhandler.cmd = do_info_mtree,
  2261. },
  2262. {
  2263. .name = "jit",
  2264. .args_type = "",
  2265. .params = "",
  2266. .help = "show dynamic compiler info",
  2267. .mhandler.cmd = do_info_jit,
  2268. },
  2269. {
  2270. .name = "kvm",
  2271. .args_type = "",
  2272. .params = "",
  2273. .help = "show KVM information",
  2274. .mhandler.cmd = hmp_info_kvm,
  2275. },
  2276. {
  2277. .name = "numa",
  2278. .args_type = "",
  2279. .params = "",
  2280. .help = "show NUMA information",
  2281. .mhandler.cmd = do_info_numa,
  2282. },
  2283. {
  2284. .name = "usb",
  2285. .args_type = "",
  2286. .params = "",
  2287. .help = "show guest USB devices",
  2288. .mhandler.cmd = usb_info,
  2289. },
  2290. {
  2291. .name = "usbhost",
  2292. .args_type = "",
  2293. .params = "",
  2294. .help = "show host USB devices",
  2295. .mhandler.cmd = usb_host_info,
  2296. },
  2297. {
  2298. .name = "profile",
  2299. .args_type = "",
  2300. .params = "",
  2301. .help = "show profiling information",
  2302. .mhandler.cmd = do_info_profile,
  2303. },
  2304. {
  2305. .name = "capture",
  2306. .args_type = "",
  2307. .params = "",
  2308. .help = "show capture information",
  2309. .mhandler.cmd = do_info_capture,
  2310. },
  2311. {
  2312. .name = "snapshots",
  2313. .args_type = "",
  2314. .params = "",
  2315. .help = "show the currently saved VM snapshots",
  2316. .mhandler.cmd = do_info_snapshots,
  2317. },
  2318. {
  2319. .name = "status",
  2320. .args_type = "",
  2321. .params = "",
  2322. .help = "show the current VM status (running|paused)",
  2323. .mhandler.cmd = hmp_info_status,
  2324. },
  2325. {
  2326. .name = "pcmcia",
  2327. .args_type = "",
  2328. .params = "",
  2329. .help = "show guest PCMCIA status",
  2330. .mhandler.cmd = pcmcia_info,
  2331. },
  2332. {
  2333. .name = "mice",
  2334. .args_type = "",
  2335. .params = "",
  2336. .help = "show which guest mouse is receiving events",
  2337. .mhandler.cmd = hmp_info_mice,
  2338. },
  2339. {
  2340. .name = "vnc",
  2341. .args_type = "",
  2342. .params = "",
  2343. .help = "show the vnc server status",
  2344. .mhandler.cmd = hmp_info_vnc,
  2345. },
  2346. #if defined(CONFIG_SPICE)
  2347. {
  2348. .name = "spice",
  2349. .args_type = "",
  2350. .params = "",
  2351. .help = "show the spice server status",
  2352. .mhandler.cmd = hmp_info_spice,
  2353. },
  2354. #endif
  2355. {
  2356. .name = "name",
  2357. .args_type = "",
  2358. .params = "",
  2359. .help = "show the current VM name",
  2360. .mhandler.cmd = hmp_info_name,
  2361. },
  2362. {
  2363. .name = "uuid",
  2364. .args_type = "",
  2365. .params = "",
  2366. .help = "show the current VM UUID",
  2367. .mhandler.cmd = hmp_info_uuid,
  2368. },
  2369. #if defined(TARGET_PPC)
  2370. {
  2371. .name = "cpustats",
  2372. .args_type = "",
  2373. .params = "",
  2374. .help = "show CPU statistics",
  2375. .mhandler.cmd = do_info_cpu_stats,
  2376. },
  2377. #endif
  2378. #if defined(CONFIG_SLIRP)
  2379. {
  2380. .name = "usernet",
  2381. .args_type = "",
  2382. .params = "",
  2383. .help = "show user network stack connection states",
  2384. .mhandler.cmd = do_info_usernet,
  2385. },
  2386. #endif
  2387. {
  2388. .name = "migrate",
  2389. .args_type = "",
  2390. .params = "",
  2391. .help = "show migration status",
  2392. .mhandler.cmd = hmp_info_migrate,
  2393. },
  2394. {
  2395. .name = "migrate_capabilities",
  2396. .args_type = "",
  2397. .params = "",
  2398. .help = "show current migration capabilities",
  2399. .mhandler.cmd = hmp_info_migrate_capabilities,
  2400. },
  2401. {
  2402. .name = "migrate_cache_size",
  2403. .args_type = "",
  2404. .params = "",
  2405. .help = "show current migration xbzrle cache size",
  2406. .mhandler.cmd = hmp_info_migrate_cache_size,
  2407. },
  2408. {
  2409. .name = "balloon",
  2410. .args_type = "",
  2411. .params = "",
  2412. .help = "show balloon information",
  2413. .mhandler.cmd = hmp_info_balloon,
  2414. },
  2415. {
  2416. .name = "qtree",
  2417. .args_type = "",
  2418. .params = "",
  2419. .help = "show device tree",
  2420. .mhandler.cmd = do_info_qtree,
  2421. },
  2422. {
  2423. .name = "qdm",
  2424. .args_type = "",
  2425. .params = "",
  2426. .help = "show qdev device model list",
  2427. .mhandler.cmd = do_info_qdm,
  2428. },
  2429. {
  2430. .name = "roms",
  2431. .args_type = "",
  2432. .params = "",
  2433. .help = "show roms",
  2434. .mhandler.cmd = do_info_roms,
  2435. },
  2436. {
  2437. .name = "trace-events",
  2438. .args_type = "",
  2439. .params = "",
  2440. .help = "show available trace-events & their state",
  2441. .mhandler.cmd = do_trace_print_events,
  2442. },
  2443. {
  2444. .name = NULL,
  2445. },
  2446. };
  2447. /* mon_cmds and info_cmds would be sorted at runtime */
  2448. static mon_cmd_t mon_cmds[] = {
  2449. #include "hmp-commands.h"
  2450. { NULL, NULL, },
  2451. };
  2452. static const mon_cmd_t qmp_cmds[] = {
  2453. #include "qmp-commands-old.h"
  2454. { /* NULL */ },
  2455. };
  2456. /*******************************************************************/
  2457. static const char *pch;
  2458. static jmp_buf expr_env;
  2459. #define MD_TLONG 0
  2460. #define MD_I32 1
  2461. typedef struct MonitorDef {
  2462. const char *name;
  2463. int offset;
  2464. target_long (*get_value)(const struct MonitorDef *md, int val);
  2465. int type;
  2466. } MonitorDef;
  2467. #if defined(TARGET_I386)
  2468. static target_long monitor_get_pc (const struct MonitorDef *md, int val)
  2469. {
  2470. CPUArchState *env = mon_get_cpu();
  2471. return env->eip + env->segs[R_CS].base;
  2472. }
  2473. #endif
  2474. #if defined(TARGET_PPC)
  2475. static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
  2476. {
  2477. CPUArchState *env = mon_get_cpu();
  2478. unsigned int u;
  2479. int i;
  2480. u = 0;
  2481. for (i = 0; i < 8; i++)
  2482. u |= env->crf[i] << (32 - (4 * i));
  2483. return u;
  2484. }
  2485. static target_long monitor_get_msr (const struct MonitorDef *md, int val)
  2486. {
  2487. CPUArchState *env = mon_get_cpu();
  2488. return env->msr;
  2489. }
  2490. static target_long monitor_get_xer (const struct MonitorDef *md, int val)
  2491. {
  2492. CPUArchState *env = mon_get_cpu();
  2493. return env->xer;
  2494. }
  2495. static target_long monitor_get_decr (const struct MonitorDef *md, int val)
  2496. {
  2497. CPUArchState *env = mon_get_cpu();
  2498. return cpu_ppc_load_decr(env);
  2499. }
  2500. static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
  2501. {
  2502. CPUArchState *env = mon_get_cpu();
  2503. return cpu_ppc_load_tbu(env);
  2504. }
  2505. static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
  2506. {
  2507. CPUArchState *env = mon_get_cpu();
  2508. return cpu_ppc_load_tbl(env);
  2509. }
  2510. #endif
  2511. #if defined(TARGET_SPARC)
  2512. #ifndef TARGET_SPARC64
  2513. static target_long monitor_get_psr (const struct MonitorDef *md, int val)
  2514. {
  2515. CPUArchState *env = mon_get_cpu();
  2516. return cpu_get_psr(env);
  2517. }
  2518. #endif
  2519. static target_long monitor_get_reg(const struct MonitorDef *md, int val)
  2520. {
  2521. CPUArchState *env = mon_get_cpu();
  2522. return env->regwptr[val];
  2523. }
  2524. #endif
  2525. static const MonitorDef monitor_defs[] = {
  2526. #ifdef TARGET_I386
  2527. #define SEG(name, seg) \
  2528. { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
  2529. { name ".base", offsetof(CPUX86State, segs[seg].base) },\
  2530. { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
  2531. { "eax", offsetof(CPUX86State, regs[0]) },
  2532. { "ecx", offsetof(CPUX86State, regs[1]) },
  2533. { "edx", offsetof(CPUX86State, regs[2]) },
  2534. { "ebx", offsetof(CPUX86State, regs[3]) },
  2535. { "esp|sp", offsetof(CPUX86State, regs[4]) },
  2536. { "ebp|fp", offsetof(CPUX86State, regs[5]) },
  2537. { "esi", offsetof(CPUX86State, regs[6]) },
  2538. { "edi", offsetof(CPUX86State, regs[7]) },
  2539. #ifdef TARGET_X86_64
  2540. { "r8", offsetof(CPUX86State, regs[8]) },
  2541. { "r9", offsetof(CPUX86State, regs[9]) },
  2542. { "r10", offsetof(CPUX86State, regs[10]) },
  2543. { "r11", offsetof(CPUX86State, regs[11]) },
  2544. { "r12", offsetof(CPUX86State, regs[12]) },
  2545. { "r13", offsetof(CPUX86State, regs[13]) },
  2546. { "r14", offsetof(CPUX86State, regs[14]) },
  2547. { "r15", offsetof(CPUX86State, regs[15]) },
  2548. #endif
  2549. { "eflags", offsetof(CPUX86State, eflags) },
  2550. { "eip", offsetof(CPUX86State, eip) },
  2551. SEG("cs", R_CS)
  2552. SEG("ds", R_DS)
  2553. SEG("es", R_ES)
  2554. SEG("ss", R_SS)
  2555. SEG("fs", R_FS)
  2556. SEG("gs", R_GS)
  2557. { "pc", 0, monitor_get_pc, },
  2558. #elif defined(TARGET_PPC)
  2559. /* General purpose registers */
  2560. { "r0", offsetof(CPUPPCState, gpr[0]) },
  2561. { "r1", offsetof(CPUPPCState, gpr[1]) },
  2562. { "r2", offsetof(CPUPPCState, gpr[2]) },
  2563. { "r3", offsetof(CPUPPCState, gpr[3]) },
  2564. { "r4", offsetof(CPUPPCState, gpr[4]) },
  2565. { "r5", offsetof(CPUPPCState, gpr[5]) },
  2566. { "r6", offsetof(CPUPPCState, gpr[6]) },
  2567. { "r7", offsetof(CPUPPCState, gpr[7]) },
  2568. { "r8", offsetof(CPUPPCState, gpr[8]) },
  2569. { "r9", offsetof(CPUPPCState, gpr[9]) },
  2570. { "r10", offsetof(CPUPPCState, gpr[10]) },
  2571. { "r11", offsetof(CPUPPCState, gpr[11]) },
  2572. { "r12", offsetof(CPUPPCState, gpr[12]) },
  2573. { "r13", offsetof(CPUPPCState, gpr[13]) },
  2574. { "r14", offsetof(CPUPPCState, gpr[14]) },
  2575. { "r15", offsetof(CPUPPCState, gpr[15]) },
  2576. { "r16", offsetof(CPUPPCState, gpr[16]) },
  2577. { "r17", offsetof(CPUPPCState, gpr[17]) },
  2578. { "r18", offsetof(CPUPPCState, gpr[18]) },
  2579. { "r19", offsetof(CPUPPCState, gpr[19]) },
  2580. { "r20", offsetof(CPUPPCState, gpr[20]) },
  2581. { "r21", offsetof(CPUPPCState, gpr[21]) },
  2582. { "r22", offsetof(CPUPPCState, gpr[22]) },
  2583. { "r23", offsetof(CPUPPCState, gpr[23]) },
  2584. { "r24", offsetof(CPUPPCState, gpr[24]) },
  2585. { "r25", offsetof(CPUPPCState, gpr[25]) },
  2586. { "r26", offsetof(CPUPPCState, gpr[26]) },
  2587. { "r27", offsetof(CPUPPCState, gpr[27]) },
  2588. { "r28", offsetof(CPUPPCState, gpr[28]) },
  2589. { "r29", offsetof(CPUPPCState, gpr[29]) },
  2590. { "r30", offsetof(CPUPPCState, gpr[30]) },
  2591. { "r31", offsetof(CPUPPCState, gpr[31]) },
  2592. /* Floating point registers */
  2593. { "f0", offsetof(CPUPPCState, fpr[0]) },
  2594. { "f1", offsetof(CPUPPCState, fpr[1]) },
  2595. { "f2", offsetof(CPUPPCState, fpr[2]) },
  2596. { "f3", offsetof(CPUPPCState, fpr[3]) },
  2597. { "f4", offsetof(CPUPPCState, fpr[4]) },
  2598. { "f5", offsetof(CPUPPCState, fpr[5]) },
  2599. { "f6", offsetof(CPUPPCState, fpr[6]) },
  2600. { "f7", offsetof(CPUPPCState, fpr[7]) },
  2601. { "f8", offsetof(CPUPPCState, fpr[8]) },
  2602. { "f9", offsetof(CPUPPCState, fpr[9]) },
  2603. { "f10", offsetof(CPUPPCState, fpr[10]) },
  2604. { "f11", offsetof(CPUPPCState, fpr[11]) },
  2605. { "f12", offsetof(CPUPPCState, fpr[12]) },
  2606. { "f13", offsetof(CPUPPCState, fpr[13]) },
  2607. { "f14", offsetof(CPUPPCState, fpr[14]) },
  2608. { "f15", offsetof(CPUPPCState, fpr[15]) },
  2609. { "f16", offsetof(CPUPPCState, fpr[16]) },
  2610. { "f17", offsetof(CPUPPCState, fpr[17]) },
  2611. { "f18", offsetof(CPUPPCState, fpr[18]) },
  2612. { "f19", offsetof(CPUPPCState, fpr[19]) },
  2613. { "f20", offsetof(CPUPPCState, fpr[20]) },
  2614. { "f21", offsetof(CPUPPCState, fpr[21]) },
  2615. { "f22", offsetof(CPUPPCState, fpr[22]) },
  2616. { "f23", offsetof(CPUPPCState, fpr[23]) },
  2617. { "f24", offsetof(CPUPPCState, fpr[24]) },
  2618. { "f25", offsetof(CPUPPCState, fpr[25]) },
  2619. { "f26", offsetof(CPUPPCState, fpr[26]) },
  2620. { "f27", offsetof(CPUPPCState, fpr[27]) },
  2621. { "f28", offsetof(CPUPPCState, fpr[28]) },
  2622. { "f29", offsetof(CPUPPCState, fpr[29]) },
  2623. { "f30", offsetof(CPUPPCState, fpr[30]) },
  2624. { "f31", offsetof(CPUPPCState, fpr[31]) },
  2625. { "fpscr", offsetof(CPUPPCState, fpscr) },
  2626. /* Next instruction pointer */
  2627. { "nip|pc", offsetof(CPUPPCState, nip) },
  2628. { "lr", offsetof(CPUPPCState, lr) },
  2629. { "ctr", offsetof(CPUPPCState, ctr) },
  2630. { "decr", 0, &monitor_get_decr, },
  2631. { "ccr", 0, &monitor_get_ccr, },
  2632. /* Machine state register */
  2633. { "msr", 0, &monitor_get_msr, },
  2634. { "xer", 0, &monitor_get_xer, },
  2635. { "tbu", 0, &monitor_get_tbu, },
  2636. { "tbl", 0, &monitor_get_tbl, },
  2637. #if defined(TARGET_PPC64)
  2638. /* Address space register */
  2639. { "asr", offsetof(CPUPPCState, asr) },
  2640. #endif
  2641. /* Segment registers */
  2642. { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
  2643. { "sr0", offsetof(CPUPPCState, sr[0]) },
  2644. { "sr1", offsetof(CPUPPCState, sr[1]) },
  2645. { "sr2", offsetof(CPUPPCState, sr[2]) },
  2646. { "sr3", offsetof(CPUPPCState, sr[3]) },
  2647. { "sr4", offsetof(CPUPPCState, sr[4]) },
  2648. { "sr5", offsetof(CPUPPCState, sr[5]) },
  2649. { "sr6", offsetof(CPUPPCState, sr[6]) },
  2650. { "sr7", offsetof(CPUPPCState, sr[7]) },
  2651. { "sr8", offsetof(CPUPPCState, sr[8]) },
  2652. { "sr9", offsetof(CPUPPCState, sr[9]) },
  2653. { "sr10", offsetof(CPUPPCState, sr[10]) },
  2654. { "sr11", offsetof(CPUPPCState, sr[11]) },
  2655. { "sr12", offsetof(CPUPPCState, sr[12]) },
  2656. { "sr13", offsetof(CPUPPCState, sr[13]) },
  2657. { "sr14", offsetof(CPUPPCState, sr[14]) },
  2658. { "sr15", offsetof(CPUPPCState, sr[15]) },
  2659. /* Too lazy to put BATs... */
  2660. { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
  2661. { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
  2662. { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
  2663. { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
  2664. { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
  2665. { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
  2666. { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
  2667. { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
  2668. { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
  2669. { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
  2670. { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
  2671. { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
  2672. { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
  2673. { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
  2674. { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
  2675. { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
  2676. { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
  2677. { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
  2678. { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
  2679. { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
  2680. { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
  2681. { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
  2682. { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
  2683. { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
  2684. { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
  2685. { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
  2686. { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
  2687. { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
  2688. { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
  2689. { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
  2690. { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
  2691. { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
  2692. { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
  2693. { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
  2694. { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
  2695. { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
  2696. { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
  2697. { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
  2698. { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
  2699. { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
  2700. { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
  2701. { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
  2702. { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
  2703. { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
  2704. { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
  2705. { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
  2706. { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
  2707. { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
  2708. { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
  2709. { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
  2710. { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
  2711. { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
  2712. { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
  2713. { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
  2714. { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
  2715. { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
  2716. { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
  2717. { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
  2718. { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
  2719. { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
  2720. { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
  2721. { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
  2722. { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
  2723. { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
  2724. { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
  2725. { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
  2726. { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
  2727. #elif defined(TARGET_SPARC)
  2728. { "g0", offsetof(CPUSPARCState, gregs[0]) },
  2729. { "g1", offsetof(CPUSPARCState, gregs[1]) },
  2730. { "g2", offsetof(CPUSPARCState, gregs[2]) },
  2731. { "g3", offsetof(CPUSPARCState, gregs[3]) },
  2732. { "g4", offsetof(CPUSPARCState, gregs[4]) },
  2733. { "g5", offsetof(CPUSPARCState, gregs[5]) },
  2734. { "g6", offsetof(CPUSPARCState, gregs[6]) },
  2735. { "g7", offsetof(CPUSPARCState, gregs[7]) },
  2736. { "o0", 0, monitor_get_reg },
  2737. { "o1", 1, monitor_get_reg },
  2738. { "o2", 2, monitor_get_reg },
  2739. { "o3", 3, monitor_get_reg },
  2740. { "o4", 4, monitor_get_reg },
  2741. { "o5", 5, monitor_get_reg },
  2742. { "o6", 6, monitor_get_reg },
  2743. { "o7", 7, monitor_get_reg },
  2744. { "l0", 8, monitor_get_reg },
  2745. { "l1", 9, monitor_get_reg },
  2746. { "l2", 10, monitor_get_reg },
  2747. { "l3", 11, monitor_get_reg },
  2748. { "l4", 12, monitor_get_reg },
  2749. { "l5", 13, monitor_get_reg },
  2750. { "l6", 14, monitor_get_reg },
  2751. { "l7", 15, monitor_get_reg },
  2752. { "i0", 16, monitor_get_reg },
  2753. { "i1", 17, monitor_get_reg },
  2754. { "i2", 18, monitor_get_reg },
  2755. { "i3", 19, monitor_get_reg },
  2756. { "i4", 20, monitor_get_reg },
  2757. { "i5", 21, monitor_get_reg },
  2758. { "i6", 22, monitor_get_reg },
  2759. { "i7", 23, monitor_get_reg },
  2760. { "pc", offsetof(CPUSPARCState, pc) },
  2761. { "npc", offsetof(CPUSPARCState, npc) },
  2762. { "y", offsetof(CPUSPARCState, y) },
  2763. #ifndef TARGET_SPARC64
  2764. { "psr", 0, &monitor_get_psr, },
  2765. { "wim", offsetof(CPUSPARCState, wim) },
  2766. #endif
  2767. { "tbr", offsetof(CPUSPARCState, tbr) },
  2768. { "fsr", offsetof(CPUSPARCState, fsr) },
  2769. { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
  2770. { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
  2771. { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
  2772. { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
  2773. { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
  2774. { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
  2775. { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
  2776. { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
  2777. { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
  2778. { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
  2779. { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
  2780. { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
  2781. { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
  2782. { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
  2783. { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
  2784. { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
  2785. { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
  2786. { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
  2787. { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
  2788. { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
  2789. { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
  2790. { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
  2791. { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
  2792. { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
  2793. { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
  2794. { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
  2795. { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
  2796. { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
  2797. { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
  2798. { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
  2799. { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
  2800. { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
  2801. #ifdef TARGET_SPARC64
  2802. { "f32", offsetof(CPUSPARCState, fpr[16]) },
  2803. { "f34", offsetof(CPUSPARCState, fpr[17]) },
  2804. { "f36", offsetof(CPUSPARCState, fpr[18]) },
  2805. { "f38", offsetof(CPUSPARCState, fpr[19]) },
  2806. { "f40", offsetof(CPUSPARCState, fpr[20]) },
  2807. { "f42", offsetof(CPUSPARCState, fpr[21]) },
  2808. { "f44", offsetof(CPUSPARCState, fpr[22]) },
  2809. { "f46", offsetof(CPUSPARCState, fpr[23]) },
  2810. { "f48", offsetof(CPUSPARCState, fpr[24]) },
  2811. { "f50", offsetof(CPUSPARCState, fpr[25]) },
  2812. { "f52", offsetof(CPUSPARCState, fpr[26]) },
  2813. { "f54", offsetof(CPUSPARCState, fpr[27]) },
  2814. { "f56", offsetof(CPUSPARCState, fpr[28]) },
  2815. { "f58", offsetof(CPUSPARCState, fpr[29]) },
  2816. { "f60", offsetof(CPUSPARCState, fpr[30]) },
  2817. { "f62", offsetof(CPUSPARCState, fpr[31]) },
  2818. { "asi", offsetof(CPUSPARCState, asi) },
  2819. { "pstate", offsetof(CPUSPARCState, pstate) },
  2820. { "cansave", offsetof(CPUSPARCState, cansave) },
  2821. { "canrestore", offsetof(CPUSPARCState, canrestore) },
  2822. { "otherwin", offsetof(CPUSPARCState, otherwin) },
  2823. { "wstate", offsetof(CPUSPARCState, wstate) },
  2824. { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
  2825. { "fprs", offsetof(CPUSPARCState, fprs) },
  2826. #endif
  2827. #endif
  2828. { NULL },
  2829. };
  2830. static void expr_error(Monitor *mon, const char *msg)
  2831. {
  2832. monitor_printf(mon, "%s\n", msg);
  2833. longjmp(expr_env, 1);
  2834. }
  2835. /* return 0 if OK, -1 if not found */
  2836. static int get_monitor_def(target_long *pval, const char *name)
  2837. {
  2838. const MonitorDef *md;
  2839. void *ptr;
  2840. for(md = monitor_defs; md->name != NULL; md++) {
  2841. if (compare_cmd(name, md->name)) {
  2842. if (md->get_value) {
  2843. *pval = md->get_value(md, md->offset);
  2844. } else {
  2845. CPUArchState *env = mon_get_cpu();
  2846. ptr = (uint8_t *)env + md->offset;
  2847. switch(md->type) {
  2848. case MD_I32:
  2849. *pval = *(int32_t *)ptr;
  2850. break;
  2851. case MD_TLONG:
  2852. *pval = *(target_long *)ptr;
  2853. break;
  2854. default:
  2855. *pval = 0;
  2856. break;
  2857. }
  2858. }
  2859. return 0;
  2860. }
  2861. }
  2862. return -1;
  2863. }
  2864. static void next(void)
  2865. {
  2866. if (*pch != '\0') {
  2867. pch++;
  2868. while (qemu_isspace(*pch))
  2869. pch++;
  2870. }
  2871. }
  2872. static int64_t expr_sum(Monitor *mon);
  2873. static int64_t expr_unary(Monitor *mon)
  2874. {
  2875. int64_t n;
  2876. char *p;
  2877. int ret;
  2878. switch(*pch) {
  2879. case '+':
  2880. next();
  2881. n = expr_unary(mon);
  2882. break;
  2883. case '-':
  2884. next();
  2885. n = -expr_unary(mon);
  2886. break;
  2887. case '~':
  2888. next();
  2889. n = ~expr_unary(mon);
  2890. break;
  2891. case '(':
  2892. next();
  2893. n = expr_sum(mon);
  2894. if (*pch != ')') {
  2895. expr_error(mon, "')' expected");
  2896. }
  2897. next();
  2898. break;
  2899. case '\'':
  2900. pch++;
  2901. if (*pch == '\0')
  2902. expr_error(mon, "character constant expected");
  2903. n = *pch;
  2904. pch++;
  2905. if (*pch != '\'')
  2906. expr_error(mon, "missing terminating \' character");
  2907. next();
  2908. break;
  2909. case '$':
  2910. {
  2911. char buf[128], *q;
  2912. target_long reg=0;
  2913. pch++;
  2914. q = buf;
  2915. while ((*pch >= 'a' && *pch <= 'z') ||
  2916. (*pch >= 'A' && *pch <= 'Z') ||
  2917. (*pch >= '0' && *pch <= '9') ||
  2918. *pch == '_' || *pch == '.') {
  2919. if ((q - buf) < sizeof(buf) - 1)
  2920. *q++ = *pch;
  2921. pch++;
  2922. }
  2923. while (qemu_isspace(*pch))
  2924. pch++;
  2925. *q = 0;
  2926. ret = get_monitor_def(&reg, buf);
  2927. if (ret < 0)
  2928. expr_error(mon, "unknown register");
  2929. n = reg;
  2930. }
  2931. break;
  2932. case '\0':
  2933. expr_error(mon, "unexpected end of expression");
  2934. n = 0;
  2935. break;
  2936. default:
  2937. errno = 0;
  2938. n = strtoull(pch, &p, 0);
  2939. if (errno == ERANGE) {
  2940. expr_error(mon, "number too large");
  2941. }
  2942. if (pch == p) {
  2943. expr_error(mon, "invalid char in expression");
  2944. }
  2945. pch = p;
  2946. while (qemu_isspace(*pch))
  2947. pch++;
  2948. break;
  2949. }
  2950. return n;
  2951. }
  2952. static int64_t expr_prod(Monitor *mon)
  2953. {
  2954. int64_t val, val2;
  2955. int op;
  2956. val = expr_unary(mon);
  2957. for(;;) {
  2958. op = *pch;
  2959. if (op != '*' && op != '/' && op != '%')
  2960. break;
  2961. next();
  2962. val2 = expr_unary(mon);
  2963. switch(op) {
  2964. default:
  2965. case '*':
  2966. val *= val2;
  2967. break;
  2968. case '/':
  2969. case '%':
  2970. if (val2 == 0)
  2971. expr_error(mon, "division by zero");
  2972. if (op == '/')
  2973. val /= val2;
  2974. else
  2975. val %= val2;
  2976. break;
  2977. }
  2978. }
  2979. return val;
  2980. }
  2981. static int64_t expr_logic(Monitor *mon)
  2982. {
  2983. int64_t val, val2;
  2984. int op;
  2985. val = expr_prod(mon);
  2986. for(;;) {
  2987. op = *pch;
  2988. if (op != '&' && op != '|' && op != '^')
  2989. break;
  2990. next();
  2991. val2 = expr_prod(mon);
  2992. switch(op) {
  2993. default:
  2994. case '&':
  2995. val &= val2;
  2996. break;
  2997. case '|':
  2998. val |= val2;
  2999. break;
  3000. case '^':
  3001. val ^= val2;
  3002. break;
  3003. }
  3004. }
  3005. return val;
  3006. }
  3007. static int64_t expr_sum(Monitor *mon)
  3008. {
  3009. int64_t val, val2;
  3010. int op;
  3011. val = expr_logic(mon);
  3012. for(;;) {
  3013. op = *pch;
  3014. if (op != '+' && op != '-')
  3015. break;
  3016. next();
  3017. val2 = expr_logic(mon);
  3018. if (op == '+')
  3019. val += val2;
  3020. else
  3021. val -= val2;
  3022. }
  3023. return val;
  3024. }
  3025. static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
  3026. {
  3027. pch = *pp;
  3028. if (setjmp(expr_env)) {
  3029. *pp = pch;
  3030. return -1;
  3031. }
  3032. while (qemu_isspace(*pch))
  3033. pch++;
  3034. *pval = expr_sum(mon);
  3035. *pp = pch;
  3036. return 0;
  3037. }
  3038. static int get_double(Monitor *mon, double *pval, const char **pp)
  3039. {
  3040. const char *p = *pp;
  3041. char *tailp;
  3042. double d;
  3043. d = strtod(p, &tailp);
  3044. if (tailp == p) {
  3045. monitor_printf(mon, "Number expected\n");
  3046. return -1;
  3047. }
  3048. if (d != d || d - d != 0) {
  3049. /* NaN or infinity */
  3050. monitor_printf(mon, "Bad number\n");
  3051. return -1;
  3052. }
  3053. *pval = d;
  3054. *pp = tailp;
  3055. return 0;
  3056. }
  3057. static int get_str(char *buf, int buf_size, const char **pp)
  3058. {
  3059. const char *p;
  3060. char *q;
  3061. int c;
  3062. q = buf;
  3063. p = *pp;
  3064. while (qemu_isspace(*p))
  3065. p++;
  3066. if (*p == '\0') {
  3067. fail:
  3068. *q = '\0';
  3069. *pp = p;
  3070. return -1;
  3071. }
  3072. if (*p == '\"') {
  3073. p++;
  3074. while (*p != '\0' && *p != '\"') {
  3075. if (*p == '\\') {
  3076. p++;
  3077. c = *p++;
  3078. switch(c) {
  3079. case 'n':
  3080. c = '\n';
  3081. break;
  3082. case 'r':
  3083. c = '\r';
  3084. break;
  3085. case '\\':
  3086. case '\'':
  3087. case '\"':
  3088. break;
  3089. default:
  3090. qemu_printf("unsupported escape code: '\\%c'\n", c);
  3091. goto fail;
  3092. }
  3093. if ((q - buf) < buf_size - 1) {
  3094. *q++ = c;
  3095. }
  3096. } else {
  3097. if ((q - buf) < buf_size - 1) {
  3098. *q++ = *p;
  3099. }
  3100. p++;
  3101. }
  3102. }
  3103. if (*p != '\"') {
  3104. qemu_printf("unterminated string\n");
  3105. goto fail;
  3106. }
  3107. p++;
  3108. } else {
  3109. while (*p != '\0' && !qemu_isspace(*p)) {
  3110. if ((q - buf) < buf_size - 1) {
  3111. *q++ = *p;
  3112. }
  3113. p++;
  3114. }
  3115. }
  3116. *q = '\0';
  3117. *pp = p;
  3118. return 0;
  3119. }
  3120. /*
  3121. * Store the command-name in cmdname, and return a pointer to
  3122. * the remaining of the command string.
  3123. */
  3124. static const char *get_command_name(const char *cmdline,
  3125. char *cmdname, size_t nlen)
  3126. {
  3127. size_t len;
  3128. const char *p, *pstart;
  3129. p = cmdline;
  3130. while (qemu_isspace(*p))
  3131. p++;
  3132. if (*p == '\0')
  3133. return NULL;
  3134. pstart = p;
  3135. while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
  3136. p++;
  3137. len = p - pstart;
  3138. if (len > nlen - 1)
  3139. len = nlen - 1;
  3140. memcpy(cmdname, pstart, len);
  3141. cmdname[len] = '\0';
  3142. return p;
  3143. }
  3144. /**
  3145. * Read key of 'type' into 'key' and return the current
  3146. * 'type' pointer.
  3147. */
  3148. static char *key_get_info(const char *type, char **key)
  3149. {
  3150. size_t len;
  3151. char *p, *str;
  3152. if (*type == ',')
  3153. type++;
  3154. p = strchr(type, ':');
  3155. if (!p) {
  3156. *key = NULL;
  3157. return NULL;
  3158. }
  3159. len = p - type;
  3160. str = g_malloc(len + 1);
  3161. memcpy(str, type, len);
  3162. str[len] = '\0';
  3163. *key = str;
  3164. return ++p;
  3165. }
  3166. static int default_fmt_format = 'x';
  3167. static int default_fmt_size = 4;
  3168. #define MAX_ARGS 16
  3169. static int is_valid_option(const char *c, const char *typestr)
  3170. {
  3171. char option[3];
  3172. option[0] = '-';
  3173. option[1] = *c;
  3174. option[2] = '\0';
  3175. typestr = strstr(typestr, option);
  3176. return (typestr != NULL);
  3177. }
  3178. static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
  3179. const char *cmdname)
  3180. {
  3181. const mon_cmd_t *cmd;
  3182. for (cmd = disp_table; cmd->name != NULL; cmd++) {
  3183. if (compare_cmd(cmdname, cmd->name)) {
  3184. return cmd;
  3185. }
  3186. }
  3187. return NULL;
  3188. }
  3189. static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
  3190. {
  3191. return search_dispatch_table(qmp_cmds, cmdname);
  3192. }
  3193. /*
  3194. * Parse @cmdline according to command table @table.
  3195. * If @cmdline is blank, return NULL.
  3196. * If it can't be parsed, report to @mon, and return NULL.
  3197. * Else, insert command arguments into @qdict, and return the command.
  3198. * If sub-command table exist, and if @cmdline contains addtional string for
  3199. * sub-command, this function will try search sub-command table. if no
  3200. * addtional string for sub-command exist, this function will return the found
  3201. * one in @table.
  3202. * Do not assume the returned command points into @table! It doesn't
  3203. * when the command is a sub-command.
  3204. */
  3205. static const mon_cmd_t *monitor_parse_command(Monitor *mon,
  3206. const char *cmdline,
  3207. int start,
  3208. mon_cmd_t *table,
  3209. QDict *qdict)
  3210. {
  3211. const char *p, *typestr;
  3212. int c;
  3213. const mon_cmd_t *cmd;
  3214. char cmdname[256];
  3215. char buf[1024];
  3216. char *key;
  3217. #ifdef DEBUG
  3218. monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
  3219. #endif
  3220. /* extract the command name */
  3221. p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
  3222. if (!p)
  3223. return NULL;
  3224. cmd = search_dispatch_table(table, cmdname);
  3225. if (!cmd) {
  3226. monitor_printf(mon, "unknown command: '%.*s'\n",
  3227. (int)(p - cmdline), cmdline);
  3228. return NULL;
  3229. }
  3230. /* filter out following useless space */
  3231. while (qemu_isspace(*p)) {
  3232. p++;
  3233. }
  3234. /* search sub command */
  3235. if (cmd->sub_table != NULL) {
  3236. /* check if user set additional command */
  3237. if (*p == '\0') {
  3238. return cmd;
  3239. }
  3240. return monitor_parse_command(mon, cmdline, p - cmdline,
  3241. cmd->sub_table, qdict);
  3242. }
  3243. /* parse the parameters */
  3244. typestr = cmd->args_type;
  3245. for(;;) {
  3246. typestr = key_get_info(typestr, &key);
  3247. if (!typestr)
  3248. break;
  3249. c = *typestr;
  3250. typestr++;
  3251. switch(c) {
  3252. case 'F':
  3253. case 'B':
  3254. case 's':
  3255. {
  3256. int ret;
  3257. while (qemu_isspace(*p))
  3258. p++;
  3259. if (*typestr == '?') {
  3260. typestr++;
  3261. if (*p == '\0') {
  3262. /* no optional string: NULL argument */
  3263. break;
  3264. }
  3265. }
  3266. ret = get_str(buf, sizeof(buf), &p);
  3267. if (ret < 0) {
  3268. switch(c) {
  3269. case 'F':
  3270. monitor_printf(mon, "%s: filename expected\n",
  3271. cmdname);
  3272. break;
  3273. case 'B':
  3274. monitor_printf(mon, "%s: block device name expected\n",
  3275. cmdname);
  3276. break;
  3277. default:
  3278. monitor_printf(mon, "%s: string expected\n", cmdname);
  3279. break;
  3280. }
  3281. goto fail;
  3282. }
  3283. qdict_put(qdict, key, qstring_from_str(buf));
  3284. }
  3285. break;
  3286. case 'O':
  3287. {
  3288. QemuOptsList *opts_list;
  3289. QemuOpts *opts;
  3290. opts_list = qemu_find_opts(key);
  3291. if (!opts_list || opts_list->desc->name) {
  3292. goto bad_type;
  3293. }
  3294. while (qemu_isspace(*p)) {
  3295. p++;
  3296. }
  3297. if (!*p)
  3298. break;
  3299. if (get_str(buf, sizeof(buf), &p) < 0) {
  3300. goto fail;
  3301. }
  3302. opts = qemu_opts_parse(opts_list, buf, 1);
  3303. if (!opts) {
  3304. goto fail;
  3305. }
  3306. qemu_opts_to_qdict(opts, qdict);
  3307. qemu_opts_del(opts);
  3308. }
  3309. break;
  3310. case '/':
  3311. {
  3312. int count, format, size;
  3313. while (qemu_isspace(*p))
  3314. p++;
  3315. if (*p == '/') {
  3316. /* format found */
  3317. p++;
  3318. count = 1;
  3319. if (qemu_isdigit(*p)) {
  3320. count = 0;
  3321. while (qemu_isdigit(*p)) {
  3322. count = count * 10 + (*p - '0');
  3323. p++;
  3324. }
  3325. }
  3326. size = -1;
  3327. format = -1;
  3328. for(;;) {
  3329. switch(*p) {
  3330. case 'o':
  3331. case 'd':
  3332. case 'u':
  3333. case 'x':
  3334. case 'i':
  3335. case 'c':
  3336. format = *p++;
  3337. break;
  3338. case 'b':
  3339. size = 1;
  3340. p++;
  3341. break;
  3342. case 'h':
  3343. size = 2;
  3344. p++;
  3345. break;
  3346. case 'w':
  3347. size = 4;
  3348. p++;
  3349. break;
  3350. case 'g':
  3351. case 'L':
  3352. size = 8;
  3353. p++;
  3354. break;
  3355. default:
  3356. goto next;
  3357. }
  3358. }
  3359. next:
  3360. if (*p != '\0' && !qemu_isspace(*p)) {
  3361. monitor_printf(mon, "invalid char in format: '%c'\n",
  3362. *p);
  3363. goto fail;
  3364. }
  3365. if (format < 0)
  3366. format = default_fmt_format;
  3367. if (format != 'i') {
  3368. /* for 'i', not specifying a size gives -1 as size */
  3369. if (size < 0)
  3370. size = default_fmt_size;
  3371. default_fmt_size = size;
  3372. }
  3373. default_fmt_format = format;
  3374. } else {
  3375. count = 1;
  3376. format = default_fmt_format;
  3377. if (format != 'i') {
  3378. size = default_fmt_size;
  3379. } else {
  3380. size = -1;
  3381. }
  3382. }
  3383. qdict_put(qdict, "count", qint_from_int(count));
  3384. qdict_put(qdict, "format", qint_from_int(format));
  3385. qdict_put(qdict, "size", qint_from_int(size));
  3386. }
  3387. break;
  3388. case 'i':
  3389. case 'l':
  3390. case 'M':
  3391. {
  3392. int64_t val;
  3393. while (qemu_isspace(*p))
  3394. p++;
  3395. if (*typestr == '?' || *typestr == '.') {
  3396. if (*typestr == '?') {
  3397. if (*p == '\0') {
  3398. typestr++;
  3399. break;
  3400. }
  3401. } else {
  3402. if (*p == '.') {
  3403. p++;
  3404. while (qemu_isspace(*p))
  3405. p++;
  3406. } else {
  3407. typestr++;
  3408. break;
  3409. }
  3410. }
  3411. typestr++;
  3412. }
  3413. if (get_expr(mon, &val, &p))
  3414. goto fail;
  3415. /* Check if 'i' is greater than 32-bit */
  3416. if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
  3417. monitor_printf(mon, "\'%s\' has failed: ", cmdname);
  3418. monitor_printf(mon, "integer is for 32-bit values\n");
  3419. goto fail;
  3420. } else if (c == 'M') {
  3421. if (val < 0) {
  3422. monitor_printf(mon, "enter a positive value\n");
  3423. goto fail;
  3424. }
  3425. val <<= 20;
  3426. }
  3427. qdict_put(qdict, key, qint_from_int(val));
  3428. }
  3429. break;
  3430. case 'o':
  3431. {
  3432. int64_t val;
  3433. char *end;
  3434. while (qemu_isspace(*p)) {
  3435. p++;
  3436. }
  3437. if (*typestr == '?') {
  3438. typestr++;
  3439. if (*p == '\0') {
  3440. break;
  3441. }
  3442. }
  3443. val = strtosz(p, &end);
  3444. if (val < 0) {
  3445. monitor_printf(mon, "invalid size\n");
  3446. goto fail;
  3447. }
  3448. qdict_put(qdict, key, qint_from_int(val));
  3449. p = end;
  3450. }
  3451. break;
  3452. case 'T':
  3453. {
  3454. double val;
  3455. while (qemu_isspace(*p))
  3456. p++;
  3457. if (*typestr == '?') {
  3458. typestr++;
  3459. if (*p == '\0') {
  3460. break;
  3461. }
  3462. }
  3463. if (get_double(mon, &val, &p) < 0) {
  3464. goto fail;
  3465. }
  3466. if (p[0] && p[1] == 's') {
  3467. switch (*p) {
  3468. case 'm':
  3469. val /= 1e3; p += 2; break;
  3470. case 'u':
  3471. val /= 1e6; p += 2; break;
  3472. case 'n':
  3473. val /= 1e9; p += 2; break;
  3474. }
  3475. }
  3476. if (*p && !qemu_isspace(*p)) {
  3477. monitor_printf(mon, "Unknown unit suffix\n");
  3478. goto fail;
  3479. }
  3480. qdict_put(qdict, key, qfloat_from_double(val));
  3481. }
  3482. break;
  3483. case 'b':
  3484. {
  3485. const char *beg;
  3486. int val;
  3487. while (qemu_isspace(*p)) {
  3488. p++;
  3489. }
  3490. beg = p;
  3491. while (qemu_isgraph(*p)) {
  3492. p++;
  3493. }
  3494. if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
  3495. val = 1;
  3496. } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
  3497. val = 0;
  3498. } else {
  3499. monitor_printf(mon, "Expected 'on' or 'off'\n");
  3500. goto fail;
  3501. }
  3502. qdict_put(qdict, key, qbool_from_int(val));
  3503. }
  3504. break;
  3505. case '-':
  3506. {
  3507. const char *tmp = p;
  3508. int skip_key = 0;
  3509. /* option */
  3510. c = *typestr++;
  3511. if (c == '\0')
  3512. goto bad_type;
  3513. while (qemu_isspace(*p))
  3514. p++;
  3515. if (*p == '-') {
  3516. p++;
  3517. if(c != *p) {
  3518. if(!is_valid_option(p, typestr)) {
  3519. monitor_printf(mon, "%s: unsupported option -%c\n",
  3520. cmdname, *p);
  3521. goto fail;
  3522. } else {
  3523. skip_key = 1;
  3524. }
  3525. }
  3526. if(skip_key) {
  3527. p = tmp;
  3528. } else {
  3529. /* has option */
  3530. p++;
  3531. qdict_put(qdict, key, qbool_from_int(1));
  3532. }
  3533. }
  3534. }
  3535. break;
  3536. default:
  3537. bad_type:
  3538. monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
  3539. goto fail;
  3540. }
  3541. g_free(key);
  3542. key = NULL;
  3543. }
  3544. /* check that all arguments were parsed */
  3545. while (qemu_isspace(*p))
  3546. p++;
  3547. if (*p != '\0') {
  3548. monitor_printf(mon, "%s: extraneous characters at the end of line\n",
  3549. cmdname);
  3550. goto fail;
  3551. }
  3552. return cmd;
  3553. fail:
  3554. g_free(key);
  3555. return NULL;
  3556. }
  3557. void monitor_set_error(Monitor *mon, QError *qerror)
  3558. {
  3559. /* report only the first error */
  3560. if (!mon->error) {
  3561. mon->error = qerror;
  3562. } else {
  3563. QDECREF(qerror);
  3564. }
  3565. }
  3566. static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
  3567. {
  3568. if (ret && !monitor_has_error(mon)) {
  3569. /*
  3570. * If it returns failure, it must have passed on error.
  3571. *
  3572. * Action: Report an internal error to the client if in QMP.
  3573. */
  3574. qerror_report(QERR_UNDEFINED_ERROR);
  3575. }
  3576. }
  3577. static void handle_user_command(Monitor *mon, const char *cmdline)
  3578. {
  3579. QDict *qdict;
  3580. const mon_cmd_t *cmd;
  3581. qdict = qdict_new();
  3582. cmd = monitor_parse_command(mon, cmdline, 0, mon_cmds, qdict);
  3583. if (!cmd)
  3584. goto out;
  3585. if (handler_is_async(cmd)) {
  3586. user_async_cmd_handler(mon, cmd, qdict);
  3587. } else if (handler_is_qobject(cmd)) {
  3588. QObject *data = NULL;
  3589. /* XXX: ignores the error code */
  3590. cmd->mhandler.cmd_new(mon, qdict, &data);
  3591. assert(!monitor_has_error(mon));
  3592. if (data) {
  3593. cmd->user_print(mon, data);
  3594. qobject_decref(data);
  3595. }
  3596. } else {
  3597. cmd->mhandler.cmd(mon, qdict);
  3598. }
  3599. out:
  3600. QDECREF(qdict);
  3601. }
  3602. static void cmd_completion(const char *name, const char *list)
  3603. {
  3604. const char *p, *pstart;
  3605. char cmd[128];
  3606. int len;
  3607. p = list;
  3608. for(;;) {
  3609. pstart = p;
  3610. p = strchr(p, '|');
  3611. if (!p)
  3612. p = pstart + strlen(pstart);
  3613. len = p - pstart;
  3614. if (len > sizeof(cmd) - 2)
  3615. len = sizeof(cmd) - 2;
  3616. memcpy(cmd, pstart, len);
  3617. cmd[len] = '\0';
  3618. if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
  3619. readline_add_completion(cur_mon->rs, cmd);
  3620. }
  3621. if (*p == '\0')
  3622. break;
  3623. p++;
  3624. }
  3625. }
  3626. static void file_completion(const char *input)
  3627. {
  3628. DIR *ffs;
  3629. struct dirent *d;
  3630. char path[1024];
  3631. char file[1024], file_prefix[1024];
  3632. int input_path_len;
  3633. const char *p;
  3634. p = strrchr(input, '/');
  3635. if (!p) {
  3636. input_path_len = 0;
  3637. pstrcpy(file_prefix, sizeof(file_prefix), input);
  3638. pstrcpy(path, sizeof(path), ".");
  3639. } else {
  3640. input_path_len = p - input + 1;
  3641. memcpy(path, input, input_path_len);
  3642. if (input_path_len > sizeof(path) - 1)
  3643. input_path_len = sizeof(path) - 1;
  3644. path[input_path_len] = '\0';
  3645. pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
  3646. }
  3647. #ifdef DEBUG_COMPLETION
  3648. monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
  3649. input, path, file_prefix);
  3650. #endif
  3651. ffs = opendir(path);
  3652. if (!ffs)
  3653. return;
  3654. for(;;) {
  3655. struct stat sb;
  3656. d = readdir(ffs);
  3657. if (!d)
  3658. break;
  3659. if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
  3660. continue;
  3661. }
  3662. if (strstart(d->d_name, file_prefix, NULL)) {
  3663. memcpy(file, input, input_path_len);
  3664. if (input_path_len < sizeof(file))
  3665. pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
  3666. d->d_name);
  3667. /* stat the file to find out if it's a directory.
  3668. * In that case add a slash to speed up typing long paths
  3669. */
  3670. if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
  3671. pstrcat(file, sizeof(file), "/");
  3672. }
  3673. readline_add_completion(cur_mon->rs, file);
  3674. }
  3675. }
  3676. closedir(ffs);
  3677. }
  3678. static void block_completion_it(void *opaque, BlockDriverState *bs)
  3679. {
  3680. const char *name = bdrv_get_device_name(bs);
  3681. const char *input = opaque;
  3682. if (input[0] == '\0' ||
  3683. !strncmp(name, (char *)input, strlen(input))) {
  3684. readline_add_completion(cur_mon->rs, name);
  3685. }
  3686. }
  3687. /* NOTE: this parser is an approximate form of the real command parser */
  3688. static void parse_cmdline(const char *cmdline,
  3689. int *pnb_args, char **args)
  3690. {
  3691. const char *p;
  3692. int nb_args, ret;
  3693. char buf[1024];
  3694. p = cmdline;
  3695. nb_args = 0;
  3696. for(;;) {
  3697. while (qemu_isspace(*p))
  3698. p++;
  3699. if (*p == '\0')
  3700. break;
  3701. if (nb_args >= MAX_ARGS)
  3702. break;
  3703. ret = get_str(buf, sizeof(buf), &p);
  3704. args[nb_args] = g_strdup(buf);
  3705. nb_args++;
  3706. if (ret < 0)
  3707. break;
  3708. }
  3709. *pnb_args = nb_args;
  3710. }
  3711. static const char *next_arg_type(const char *typestr)
  3712. {
  3713. const char *p = strchr(typestr, ':');
  3714. return (p != NULL ? ++p : typestr);
  3715. }
  3716. static void monitor_find_completion(const char *cmdline)
  3717. {
  3718. const char *cmdname;
  3719. char *args[MAX_ARGS];
  3720. int nb_args, i, len;
  3721. const char *ptype, *str;
  3722. const mon_cmd_t *cmd;
  3723. parse_cmdline(cmdline, &nb_args, args);
  3724. #ifdef DEBUG_COMPLETION
  3725. for(i = 0; i < nb_args; i++) {
  3726. monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
  3727. }
  3728. #endif
  3729. /* if the line ends with a space, it means we want to complete the
  3730. next arg */
  3731. len = strlen(cmdline);
  3732. if (len > 0 && qemu_isspace(cmdline[len - 1])) {
  3733. if (nb_args >= MAX_ARGS) {
  3734. goto cleanup;
  3735. }
  3736. args[nb_args++] = g_strdup("");
  3737. }
  3738. if (nb_args <= 1) {
  3739. /* command completion */
  3740. if (nb_args == 0)
  3741. cmdname = "";
  3742. else
  3743. cmdname = args[0];
  3744. readline_set_completion_index(cur_mon->rs, strlen(cmdname));
  3745. for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
  3746. cmd_completion(cmdname, cmd->name);
  3747. }
  3748. } else {
  3749. /* find the command */
  3750. for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
  3751. if (compare_cmd(args[0], cmd->name)) {
  3752. break;
  3753. }
  3754. }
  3755. if (!cmd->name) {
  3756. goto cleanup;
  3757. }
  3758. ptype = next_arg_type(cmd->args_type);
  3759. for(i = 0; i < nb_args - 2; i++) {
  3760. if (*ptype != '\0') {
  3761. ptype = next_arg_type(ptype);
  3762. while (*ptype == '?')
  3763. ptype = next_arg_type(ptype);
  3764. }
  3765. }
  3766. str = args[nb_args - 1];
  3767. if (*ptype == '-' && ptype[1] != '\0') {
  3768. ptype = next_arg_type(ptype);
  3769. }
  3770. switch(*ptype) {
  3771. case 'F':
  3772. /* file completion */
  3773. readline_set_completion_index(cur_mon->rs, strlen(str));
  3774. file_completion(str);
  3775. break;
  3776. case 'B':
  3777. /* block device name completion */
  3778. readline_set_completion_index(cur_mon->rs, strlen(str));
  3779. bdrv_iterate(block_completion_it, (void *)str);
  3780. break;
  3781. case 's':
  3782. /* XXX: more generic ? */
  3783. if (!strcmp(cmd->name, "info")) {
  3784. readline_set_completion_index(cur_mon->rs, strlen(str));
  3785. for(cmd = info_cmds; cmd->name != NULL; cmd++) {
  3786. cmd_completion(str, cmd->name);
  3787. }
  3788. } else if (!strcmp(cmd->name, "sendkey")) {
  3789. char *sep = strrchr(str, '-');
  3790. if (sep)
  3791. str = sep + 1;
  3792. readline_set_completion_index(cur_mon->rs, strlen(str));
  3793. for (i = 0; i < Q_KEY_CODE_MAX; i++) {
  3794. cmd_completion(str, QKeyCode_lookup[i]);
  3795. }
  3796. } else if (!strcmp(cmd->name, "help|?")) {
  3797. readline_set_completion_index(cur_mon->rs, strlen(str));
  3798. for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
  3799. cmd_completion(str, cmd->name);
  3800. }
  3801. }
  3802. break;
  3803. default:
  3804. break;
  3805. }
  3806. }
  3807. cleanup:
  3808. for (i = 0; i < nb_args; i++) {
  3809. g_free(args[i]);
  3810. }
  3811. }
  3812. static int monitor_can_read(void *opaque)
  3813. {
  3814. Monitor *mon = opaque;
  3815. return (mon->suspend_cnt == 0) ? 1 : 0;
  3816. }
  3817. static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
  3818. {
  3819. int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
  3820. return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
  3821. }
  3822. /*
  3823. * Argument validation rules:
  3824. *
  3825. * 1. The argument must exist in cmd_args qdict
  3826. * 2. The argument type must be the expected one
  3827. *
  3828. * Special case: If the argument doesn't exist in cmd_args and
  3829. * the QMP_ACCEPT_UNKNOWNS flag is set, then the
  3830. * checking is skipped for it.
  3831. */
  3832. static int check_client_args_type(const QDict *client_args,
  3833. const QDict *cmd_args, int flags)
  3834. {
  3835. const QDictEntry *ent;
  3836. for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
  3837. QObject *obj;
  3838. QString *arg_type;
  3839. const QObject *client_arg = qdict_entry_value(ent);
  3840. const char *client_arg_name = qdict_entry_key(ent);
  3841. obj = qdict_get(cmd_args, client_arg_name);
  3842. if (!obj) {
  3843. if (flags & QMP_ACCEPT_UNKNOWNS) {
  3844. /* handler accepts unknowns */
  3845. continue;
  3846. }
  3847. /* client arg doesn't exist */
  3848. qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
  3849. return -1;
  3850. }
  3851. arg_type = qobject_to_qstring(obj);
  3852. assert(arg_type != NULL);
  3853. /* check if argument's type is correct */
  3854. switch (qstring_get_str(arg_type)[0]) {
  3855. case 'F':
  3856. case 'B':
  3857. case 's':
  3858. if (qobject_type(client_arg) != QTYPE_QSTRING) {
  3859. qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
  3860. "string");
  3861. return -1;
  3862. }
  3863. break;
  3864. case 'i':
  3865. case 'l':
  3866. case 'M':
  3867. case 'o':
  3868. if (qobject_type(client_arg) != QTYPE_QINT) {
  3869. qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
  3870. "int");
  3871. return -1;
  3872. }
  3873. break;
  3874. case 'T':
  3875. if (qobject_type(client_arg) != QTYPE_QINT &&
  3876. qobject_type(client_arg) != QTYPE_QFLOAT) {
  3877. qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
  3878. "number");
  3879. return -1;
  3880. }
  3881. break;
  3882. case 'b':
  3883. case '-':
  3884. if (qobject_type(client_arg) != QTYPE_QBOOL) {
  3885. qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
  3886. "bool");
  3887. return -1;
  3888. }
  3889. break;
  3890. case 'O':
  3891. assert(flags & QMP_ACCEPT_UNKNOWNS);
  3892. break;
  3893. case 'q':
  3894. /* Any QObject can be passed. */
  3895. break;
  3896. case '/':
  3897. case '.':
  3898. /*
  3899. * These types are not supported by QMP and thus are not
  3900. * handled here. Fall through.
  3901. */
  3902. default:
  3903. abort();
  3904. }
  3905. }
  3906. return 0;
  3907. }
  3908. /*
  3909. * - Check if the client has passed all mandatory args
  3910. * - Set special flags for argument validation
  3911. */
  3912. static int check_mandatory_args(const QDict *cmd_args,
  3913. const QDict *client_args, int *flags)
  3914. {
  3915. const QDictEntry *ent;
  3916. for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
  3917. const char *cmd_arg_name = qdict_entry_key(ent);
  3918. QString *type = qobject_to_qstring(qdict_entry_value(ent));
  3919. assert(type != NULL);
  3920. if (qstring_get_str(type)[0] == 'O') {
  3921. assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
  3922. *flags |= QMP_ACCEPT_UNKNOWNS;
  3923. } else if (qstring_get_str(type)[0] != '-' &&
  3924. qstring_get_str(type)[1] != '?' &&
  3925. !qdict_haskey(client_args, cmd_arg_name)) {
  3926. qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
  3927. return -1;
  3928. }
  3929. }
  3930. return 0;
  3931. }
  3932. static QDict *qdict_from_args_type(const char *args_type)
  3933. {
  3934. int i;
  3935. QDict *qdict;
  3936. QString *key, *type, *cur_qs;
  3937. assert(args_type != NULL);
  3938. qdict = qdict_new();
  3939. if (args_type == NULL || args_type[0] == '\0') {
  3940. /* no args, empty qdict */
  3941. goto out;
  3942. }
  3943. key = qstring_new();
  3944. type = qstring_new();
  3945. cur_qs = key;
  3946. for (i = 0;; i++) {
  3947. switch (args_type[i]) {
  3948. case ',':
  3949. case '\0':
  3950. qdict_put(qdict, qstring_get_str(key), type);
  3951. QDECREF(key);
  3952. if (args_type[i] == '\0') {
  3953. goto out;
  3954. }
  3955. type = qstring_new(); /* qdict has ref */
  3956. cur_qs = key = qstring_new();
  3957. break;
  3958. case ':':
  3959. cur_qs = type;
  3960. break;
  3961. default:
  3962. qstring_append_chr(cur_qs, args_type[i]);
  3963. break;
  3964. }
  3965. }
  3966. out:
  3967. return qdict;
  3968. }
  3969. /*
  3970. * Client argument checking rules:
  3971. *
  3972. * 1. Client must provide all mandatory arguments
  3973. * 2. Each argument provided by the client must be expected
  3974. * 3. Each argument provided by the client must have the type expected
  3975. * by the command
  3976. */
  3977. static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
  3978. {
  3979. int flags, err;
  3980. QDict *cmd_args;
  3981. cmd_args = qdict_from_args_type(cmd->args_type);
  3982. flags = 0;
  3983. err = check_mandatory_args(cmd_args, client_args, &flags);
  3984. if (err) {
  3985. goto out;
  3986. }
  3987. err = check_client_args_type(client_args, cmd_args, flags);
  3988. out:
  3989. QDECREF(cmd_args);
  3990. return err;
  3991. }
  3992. /*
  3993. * Input object checking rules
  3994. *
  3995. * 1. Input object must be a dict
  3996. * 2. The "execute" key must exist
  3997. * 3. The "execute" key must be a string
  3998. * 4. If the "arguments" key exists, it must be a dict
  3999. * 5. If the "id" key exists, it can be anything (ie. json-value)
  4000. * 6. Any argument not listed above is considered invalid
  4001. */
  4002. static QDict *qmp_check_input_obj(QObject *input_obj)
  4003. {
  4004. const QDictEntry *ent;
  4005. int has_exec_key = 0;
  4006. QDict *input_dict;
  4007. if (qobject_type(input_obj) != QTYPE_QDICT) {
  4008. qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
  4009. return NULL;
  4010. }
  4011. input_dict = qobject_to_qdict(input_obj);
  4012. for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
  4013. const char *arg_name = qdict_entry_key(ent);
  4014. const QObject *arg_obj = qdict_entry_value(ent);
  4015. if (!strcmp(arg_name, "execute")) {
  4016. if (qobject_type(arg_obj) != QTYPE_QSTRING) {
  4017. qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
  4018. "string");
  4019. return NULL;
  4020. }
  4021. has_exec_key = 1;
  4022. } else if (!strcmp(arg_name, "arguments")) {
  4023. if (qobject_type(arg_obj) != QTYPE_QDICT) {
  4024. qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
  4025. "object");
  4026. return NULL;
  4027. }
  4028. } else if (!strcmp(arg_name, "id")) {
  4029. /* FIXME: check duplicated IDs for async commands */
  4030. } else {
  4031. qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
  4032. return NULL;
  4033. }
  4034. }
  4035. if (!has_exec_key) {
  4036. qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
  4037. return NULL;
  4038. }
  4039. return input_dict;
  4040. }
  4041. static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
  4042. const QDict *params)
  4043. {
  4044. int ret;
  4045. QObject *data = NULL;
  4046. ret = cmd->mhandler.cmd_new(mon, params, &data);
  4047. handler_audit(mon, cmd, ret);
  4048. monitor_protocol_emitter(mon, data);
  4049. qobject_decref(data);
  4050. }
  4051. static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
  4052. {
  4053. int err;
  4054. QObject *obj;
  4055. QDict *input, *args;
  4056. const mon_cmd_t *cmd;
  4057. const char *cmd_name;
  4058. Monitor *mon = cur_mon;
  4059. args = input = NULL;
  4060. obj = json_parser_parse(tokens, NULL);
  4061. if (!obj) {
  4062. // FIXME: should be triggered in json_parser_parse()
  4063. qerror_report(QERR_JSON_PARSING);
  4064. goto err_out;
  4065. }
  4066. input = qmp_check_input_obj(obj);
  4067. if (!input) {
  4068. qobject_decref(obj);
  4069. goto err_out;
  4070. }
  4071. mon->mc->id = qdict_get(input, "id");
  4072. qobject_incref(mon->mc->id);
  4073. cmd_name = qdict_get_str(input, "execute");
  4074. trace_handle_qmp_command(mon, cmd_name);
  4075. if (invalid_qmp_mode(mon, cmd_name)) {
  4076. qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
  4077. goto err_out;
  4078. }
  4079. cmd = qmp_find_cmd(cmd_name);
  4080. if (!cmd) {
  4081. qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
  4082. goto err_out;
  4083. }
  4084. obj = qdict_get(input, "arguments");
  4085. if (!obj) {
  4086. args = qdict_new();
  4087. } else {
  4088. args = qobject_to_qdict(obj);
  4089. QINCREF(args);
  4090. }
  4091. err = qmp_check_client_args(cmd, args);
  4092. if (err < 0) {
  4093. goto err_out;
  4094. }
  4095. if (handler_is_async(cmd)) {
  4096. err = qmp_async_cmd_handler(mon, cmd, args);
  4097. if (err) {
  4098. /* emit the error response */
  4099. goto err_out;
  4100. }
  4101. } else {
  4102. qmp_call_cmd(mon, cmd, args);
  4103. }
  4104. goto out;
  4105. err_out:
  4106. monitor_protocol_emitter(mon, NULL);
  4107. out:
  4108. QDECREF(input);
  4109. QDECREF(args);
  4110. }
  4111. /**
  4112. * monitor_control_read(): Read and handle QMP input
  4113. */
  4114. static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
  4115. {
  4116. Monitor *old_mon = cur_mon;
  4117. cur_mon = opaque;
  4118. json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
  4119. cur_mon = old_mon;
  4120. }
  4121. static void monitor_read(void *opaque, const uint8_t *buf, int size)
  4122. {
  4123. Monitor *old_mon = cur_mon;
  4124. int i;
  4125. cur_mon = opaque;
  4126. if (cur_mon->rs) {
  4127. for (i = 0; i < size; i++)
  4128. readline_handle_byte(cur_mon->rs, buf[i]);
  4129. } else {
  4130. if (size == 0 || buf[size - 1] != 0)
  4131. monitor_printf(cur_mon, "corrupted command\n");
  4132. else
  4133. handle_user_command(cur_mon, (char *)buf);
  4134. }
  4135. cur_mon = old_mon;
  4136. }
  4137. static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
  4138. {
  4139. monitor_suspend(mon);
  4140. handle_user_command(mon, cmdline);
  4141. monitor_resume(mon);
  4142. }
  4143. int monitor_suspend(Monitor *mon)
  4144. {
  4145. if (!mon->rs)
  4146. return -ENOTTY;
  4147. mon->suspend_cnt++;
  4148. return 0;
  4149. }
  4150. void monitor_resume(Monitor *mon)
  4151. {
  4152. if (!mon->rs)
  4153. return;
  4154. if (--mon->suspend_cnt == 0)
  4155. readline_show_prompt(mon->rs);
  4156. }
  4157. static QObject *get_qmp_greeting(void)
  4158. {
  4159. QObject *ver = NULL;
  4160. qmp_marshal_input_query_version(NULL, NULL, &ver);
  4161. return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
  4162. }
  4163. /**
  4164. * monitor_control_event(): Print QMP gretting
  4165. */
  4166. static void monitor_control_event(void *opaque, int event)
  4167. {
  4168. QObject *data;
  4169. Monitor *mon = opaque;
  4170. switch (event) {
  4171. case CHR_EVENT_OPENED:
  4172. mon->mc->command_mode = 0;
  4173. data = get_qmp_greeting();
  4174. monitor_json_emitter(mon, data);
  4175. qobject_decref(data);
  4176. mon_refcount++;
  4177. break;
  4178. case CHR_EVENT_CLOSED:
  4179. json_message_parser_destroy(&mon->mc->parser);
  4180. json_message_parser_init(&mon->mc->parser, handle_qmp_command);
  4181. mon_refcount--;
  4182. monitor_fdsets_cleanup();
  4183. break;
  4184. }
  4185. }
  4186. static void monitor_event(void *opaque, int event)
  4187. {
  4188. Monitor *mon = opaque;
  4189. switch (event) {
  4190. case CHR_EVENT_MUX_IN:
  4191. mon->mux_out = 0;
  4192. if (mon->reset_seen) {
  4193. readline_restart(mon->rs);
  4194. monitor_resume(mon);
  4195. monitor_flush(mon);
  4196. } else {
  4197. mon->suspend_cnt = 0;
  4198. }
  4199. break;
  4200. case CHR_EVENT_MUX_OUT:
  4201. if (mon->reset_seen) {
  4202. if (mon->suspend_cnt == 0) {
  4203. monitor_printf(mon, "\n");
  4204. }
  4205. monitor_flush(mon);
  4206. monitor_suspend(mon);
  4207. } else {
  4208. mon->suspend_cnt++;
  4209. }
  4210. mon->mux_out = 1;
  4211. break;
  4212. case CHR_EVENT_OPENED:
  4213. monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
  4214. "information\n", QEMU_VERSION);
  4215. if (!mon->mux_out) {
  4216. readline_show_prompt(mon->rs);
  4217. }
  4218. mon->reset_seen = 1;
  4219. mon_refcount++;
  4220. break;
  4221. case CHR_EVENT_CLOSED:
  4222. mon_refcount--;
  4223. monitor_fdsets_cleanup();
  4224. break;
  4225. }
  4226. }
  4227. static int
  4228. compare_mon_cmd(const void *a, const void *b)
  4229. {
  4230. return strcmp(((const mon_cmd_t *)a)->name,
  4231. ((const mon_cmd_t *)b)->name);
  4232. }
  4233. static void sortcmdlist(void)
  4234. {
  4235. int array_num;
  4236. int elem_size = sizeof(mon_cmd_t);
  4237. array_num = sizeof(mon_cmds)/elem_size-1;
  4238. qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
  4239. array_num = sizeof(info_cmds)/elem_size-1;
  4240. qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
  4241. }
  4242. /*
  4243. * Local variables:
  4244. * c-indent-level: 4
  4245. * c-basic-offset: 4
  4246. * tab-width: 8
  4247. * End:
  4248. */
  4249. void monitor_init(CharDriverState *chr, int flags)
  4250. {
  4251. static int is_first_init = 1;
  4252. Monitor *mon;
  4253. if (is_first_init) {
  4254. monitor_protocol_event_init();
  4255. is_first_init = 0;
  4256. }
  4257. mon = g_malloc0(sizeof(*mon));
  4258. mon->chr = chr;
  4259. mon->flags = flags;
  4260. if (flags & MONITOR_USE_READLINE) {
  4261. mon->rs = readline_init(mon, monitor_find_completion);
  4262. monitor_read_command(mon, 0);
  4263. }
  4264. if (monitor_ctrl_mode(mon)) {
  4265. mon->mc = g_malloc0(sizeof(MonitorControl));
  4266. /* Control mode requires special handlers */
  4267. qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
  4268. monitor_control_event, mon);
  4269. qemu_chr_fe_set_echo(chr, true);
  4270. json_message_parser_init(&mon->mc->parser, handle_qmp_command);
  4271. } else {
  4272. qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
  4273. monitor_event, mon);
  4274. }
  4275. QLIST_INSERT_HEAD(&mon_list, mon, entry);
  4276. if (!default_mon || (flags & MONITOR_IS_DEFAULT))
  4277. default_mon = mon;
  4278. sortcmdlist();
  4279. }
  4280. static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
  4281. {
  4282. BlockDriverState *bs = opaque;
  4283. int ret = 0;
  4284. if (bdrv_set_key(bs, password) != 0) {
  4285. monitor_printf(mon, "invalid password\n");
  4286. ret = -EPERM;
  4287. }
  4288. if (mon->password_completion_cb)
  4289. mon->password_completion_cb(mon->password_opaque, ret);
  4290. monitor_read_command(mon, 1);
  4291. }
  4292. ReadLineState *monitor_get_rs(Monitor *mon)
  4293. {
  4294. return mon->rs;
  4295. }
  4296. int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
  4297. BlockDriverCompletionFunc *completion_cb,
  4298. void *opaque)
  4299. {
  4300. int err;
  4301. if (!bdrv_key_required(bs)) {
  4302. if (completion_cb)
  4303. completion_cb(opaque, 0);
  4304. return 0;
  4305. }
  4306. if (monitor_ctrl_mode(mon)) {
  4307. qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
  4308. bdrv_get_encrypted_filename(bs));
  4309. return -1;
  4310. }
  4311. monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
  4312. bdrv_get_encrypted_filename(bs));
  4313. mon->password_completion_cb = completion_cb;
  4314. mon->password_opaque = opaque;
  4315. err = monitor_read_password(mon, bdrv_password_cb, bs);
  4316. if (err && completion_cb)
  4317. completion_cb(opaque, err);
  4318. return err;
  4319. }
  4320. int monitor_read_block_device_key(Monitor *mon, const char *device,
  4321. BlockDriverCompletionFunc *completion_cb,
  4322. void *opaque)
  4323. {
  4324. BlockDriverState *bs;
  4325. bs = bdrv_find(device);
  4326. if (!bs) {
  4327. monitor_printf(mon, "Device not found %s\n", device);
  4328. return -1;
  4329. }
  4330. return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
  4331. }
  4332. QemuOptsList qemu_mon_opts = {
  4333. .name = "mon",
  4334. .implied_opt_name = "chardev",
  4335. .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
  4336. .desc = {
  4337. {
  4338. .name = "mode",
  4339. .type = QEMU_OPT_STRING,
  4340. },{
  4341. .name = "chardev",
  4342. .type = QEMU_OPT_STRING,
  4343. },{
  4344. .name = "default",
  4345. .type = QEMU_OPT_BOOL,
  4346. },{
  4347. .name = "pretty",
  4348. .type = QEMU_OPT_BOOL,
  4349. },
  4350. { /* end of list */ }
  4351. },
  4352. };