2
0

monitor.c 136 KB

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