monitor.c 154 KB

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