monitor.c 147 KB

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