monitor.c 154 KB

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