2
0

monitor.c 136 KB

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