2
0

monitor.c 147 KB

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