monitor.c 141 KB

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