monitor.c 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735
  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 "qemu/osdep.h"
  25. #include <dirent.h>
  26. #include "cpu.h"
  27. #include "hw/hw.h"
  28. #include "monitor/qdev.h"
  29. #include "hw/usb.h"
  30. #include "hw/pci/pci.h"
  31. #include "sysemu/watchdog.h"
  32. #include "hw/loader.h"
  33. #include "exec/gdbstub.h"
  34. #include "net/net.h"
  35. #include "net/slirp.h"
  36. #include "chardev/char-fe.h"
  37. #include "chardev/char-io.h"
  38. #include "chardev/char-mux.h"
  39. #include "ui/qemu-spice.h"
  40. #include "sysemu/numa.h"
  41. #include "monitor/monitor.h"
  42. #include "qemu/config-file.h"
  43. #include "qemu/readline.h"
  44. #include "ui/console.h"
  45. #include "ui/input.h"
  46. #include "sysemu/blockdev.h"
  47. #include "sysemu/block-backend.h"
  48. #include "audio/audio.h"
  49. #include "disas/disas.h"
  50. #include "sysemu/balloon.h"
  51. #include "qemu/timer.h"
  52. #include "sysemu/hw_accel.h"
  53. #include "qemu/acl.h"
  54. #include "sysemu/tpm.h"
  55. #include "qapi/qmp/qdict.h"
  56. #include "qapi/qmp/qerror.h"
  57. #include "qapi/qmp/qnum.h"
  58. #include "qapi/qmp/qstring.h"
  59. #include "qapi/qmp/qjson.h"
  60. #include "qapi/qmp/json-streamer.h"
  61. #include "qapi/qmp/json-parser.h"
  62. #include "qapi/qmp/qlist.h"
  63. #include "qom/object_interfaces.h"
  64. #include "trace-root.h"
  65. #include "trace/control.h"
  66. #include "monitor/hmp-target.h"
  67. #ifdef CONFIG_TRACE_SIMPLE
  68. #include "trace/simple.h"
  69. #endif
  70. #include "exec/memory.h"
  71. #include "exec/exec-all.h"
  72. #include "qemu/log.h"
  73. #include "qemu/option.h"
  74. #include "hmp.h"
  75. #include "qemu/thread.h"
  76. #include "block/qapi.h"
  77. #include "qapi/qapi-commands.h"
  78. #include "qapi/qapi-events.h"
  79. #include "qapi/error.h"
  80. #include "qapi/qmp-event.h"
  81. #include "qapi/qapi-introspect.h"
  82. #include "sysemu/qtest.h"
  83. #include "sysemu/cpus.h"
  84. #include "sysemu/iothread.h"
  85. #include "qemu/cutils.h"
  86. #if defined(TARGET_S390X)
  87. #include "hw/s390x/storage-keys.h"
  88. #include "hw/s390x/storage-attributes.h"
  89. #endif
  90. /*
  91. * Supported types:
  92. *
  93. * 'F' filename
  94. * 'B' block device name
  95. * 's' string (accept optional quote)
  96. * 'S' it just appends the rest of the string (accept optional quote)
  97. * 'O' option string of the form NAME=VALUE,...
  98. * parsed according to QemuOptsList given by its name
  99. * Example: 'device:O' uses qemu_device_opts.
  100. * Restriction: only lists with empty desc are supported
  101. * TODO lift the restriction
  102. * 'i' 32 bit integer
  103. * 'l' target long (32 or 64 bit)
  104. * 'M' Non-negative target long (32 or 64 bit), in user mode the
  105. * value is multiplied by 2^20 (think Mebibyte)
  106. * 'o' octets (aka bytes)
  107. * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
  108. * K, k suffix, which multiplies the value by 2^60 for suffixes E
  109. * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
  110. * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
  111. * 'T' double
  112. * user mode accepts an optional ms, us, ns suffix,
  113. * which divides the value by 1e3, 1e6, 1e9, respectively
  114. * '/' optional gdb-like print format (like "/10x")
  115. *
  116. * '?' optional type (for all types, except '/')
  117. * '.' other form of optional type (for 'i' and 'l')
  118. * 'b' boolean
  119. * user mode accepts "on" or "off"
  120. * '-' optional parameter (eg. '-f')
  121. *
  122. */
  123. typedef struct mon_cmd_t {
  124. const char *name;
  125. const char *args_type;
  126. const char *params;
  127. const char *help;
  128. void (*cmd)(Monitor *mon, const QDict *qdict);
  129. /* @sub_table is a list of 2nd level of commands. If it does not exist,
  130. * cmd should be used. If it exists, sub_table[?].cmd should be
  131. * used, and cmd of 1st level plays the role of help function.
  132. */
  133. struct mon_cmd_t *sub_table;
  134. void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
  135. } mon_cmd_t;
  136. /* file descriptors passed via SCM_RIGHTS */
  137. typedef struct mon_fd_t mon_fd_t;
  138. struct mon_fd_t {
  139. char *name;
  140. int fd;
  141. QLIST_ENTRY(mon_fd_t) next;
  142. };
  143. /* file descriptor associated with a file descriptor set */
  144. typedef struct MonFdsetFd MonFdsetFd;
  145. struct MonFdsetFd {
  146. int fd;
  147. bool removed;
  148. char *opaque;
  149. QLIST_ENTRY(MonFdsetFd) next;
  150. };
  151. /* file descriptor set containing fds passed via SCM_RIGHTS */
  152. typedef struct MonFdset MonFdset;
  153. struct MonFdset {
  154. int64_t id;
  155. QLIST_HEAD(, MonFdsetFd) fds;
  156. QLIST_HEAD(, MonFdsetFd) dup_fds;
  157. QLIST_ENTRY(MonFdset) next;
  158. };
  159. typedef struct {
  160. JSONMessageParser parser;
  161. /*
  162. * When a client connects, we're in capabilities negotiation mode.
  163. * When command qmp_capabilities succeeds, we go into command
  164. * mode.
  165. */
  166. QmpCommandList *commands;
  167. bool qmp_caps[QMP_CAPABILITY__MAX];
  168. /*
  169. * Protects qmp request/response queue. Please take monitor_lock
  170. * first when used together.
  171. */
  172. QemuMutex qmp_queue_lock;
  173. /* Input queue that holds all the parsed QMP requests */
  174. GQueue *qmp_requests;
  175. /* Output queue contains all the QMP responses in order */
  176. GQueue *qmp_responses;
  177. } MonitorQMP;
  178. /*
  179. * To prevent flooding clients, events can be throttled. The
  180. * throttling is calculated globally, rather than per-Monitor
  181. * instance.
  182. */
  183. typedef struct MonitorQAPIEventState {
  184. QAPIEvent event; /* Throttling state for this event type and... */
  185. QDict *data; /* ... data, see qapi_event_throttle_equal() */
  186. QEMUTimer *timer; /* Timer for handling delayed events */
  187. QDict *qdict; /* Delayed event (if any) */
  188. } MonitorQAPIEventState;
  189. typedef struct {
  190. int64_t rate; /* Minimum time (in ns) between two events */
  191. } MonitorQAPIEventConf;
  192. struct Monitor {
  193. CharBackend chr;
  194. int reset_seen;
  195. int flags;
  196. int suspend_cnt; /* Needs to be accessed atomically */
  197. bool skip_flush;
  198. bool use_io_thr;
  199. /* We can't access guest memory when holding the lock */
  200. QemuMutex out_lock;
  201. QString *outbuf;
  202. guint out_watch;
  203. /* Read under either BQL or out_lock, written with BQL+out_lock. */
  204. int mux_out;
  205. ReadLineState *rs;
  206. MonitorQMP qmp;
  207. gchar *mon_cpu_path;
  208. BlockCompletionFunc *password_completion_cb;
  209. void *password_opaque;
  210. mon_cmd_t *cmd_table;
  211. QLIST_HEAD(,mon_fd_t) fds;
  212. QTAILQ_ENTRY(Monitor) entry;
  213. };
  214. /* Let's add monitor global variables to this struct. */
  215. static struct {
  216. IOThread *mon_iothread;
  217. /* Bottom half to dispatch the requests received from IO thread */
  218. QEMUBH *qmp_dispatcher_bh;
  219. /* Bottom half to deliver the responses back to clients */
  220. QEMUBH *qmp_respond_bh;
  221. } mon_global;
  222. struct QMPRequest {
  223. /* Owner of the request */
  224. Monitor *mon;
  225. /* "id" field of the request */
  226. QObject *id;
  227. /* Request object to be handled */
  228. QObject *req;
  229. /*
  230. * Whether we need to resume the monitor afterward. This flag is
  231. * used to emulate the old QMP server behavior that the current
  232. * command must be completed before execution of the next one.
  233. */
  234. bool need_resume;
  235. };
  236. typedef struct QMPRequest QMPRequest;
  237. /* QMP checker flags */
  238. #define QMP_ACCEPT_UNKNOWNS 1
  239. /* Protects mon_list, monitor_event_state. */
  240. static QemuMutex monitor_lock;
  241. static QTAILQ_HEAD(mon_list, Monitor) mon_list;
  242. static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
  243. static int mon_refcount;
  244. static mon_cmd_t mon_cmds[];
  245. static mon_cmd_t info_cmds[];
  246. QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
  247. Monitor *cur_mon;
  248. static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
  249. static void monitor_command_cb(void *opaque, const char *cmdline,
  250. void *readline_opaque);
  251. /**
  252. * Is @mon a QMP monitor?
  253. */
  254. static inline bool monitor_is_qmp(const Monitor *mon)
  255. {
  256. return (mon->flags & MONITOR_USE_CONTROL);
  257. }
  258. /**
  259. * Whether @mon is using readline? Note: not all HMP monitors use
  260. * readline, e.g., gdbserver has a non-interactive HMP monitor, so
  261. * readline is not used there.
  262. */
  263. static inline bool monitor_uses_readline(const Monitor *mon)
  264. {
  265. return mon->flags & MONITOR_USE_READLINE;
  266. }
  267. static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
  268. {
  269. return !monitor_is_qmp(mon) && !monitor_uses_readline(mon);
  270. }
  271. /**
  272. * Is the current monitor, if any, a QMP monitor?
  273. */
  274. bool monitor_cur_is_qmp(void)
  275. {
  276. return cur_mon && monitor_is_qmp(cur_mon);
  277. }
  278. void monitor_read_command(Monitor *mon, int show_prompt)
  279. {
  280. if (!mon->rs)
  281. return;
  282. readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
  283. if (show_prompt)
  284. readline_show_prompt(mon->rs);
  285. }
  286. int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
  287. void *opaque)
  288. {
  289. if (mon->rs) {
  290. readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
  291. /* prompt is printed on return from the command handler */
  292. return 0;
  293. } else {
  294. monitor_printf(mon, "terminal does not support password prompting\n");
  295. return -ENOTTY;
  296. }
  297. }
  298. static void qmp_request_free(QMPRequest *req)
  299. {
  300. qobject_decref(req->id);
  301. qobject_decref(req->req);
  302. g_free(req);
  303. }
  304. /* Must with the mon->qmp.qmp_queue_lock held */
  305. static void monitor_qmp_cleanup_req_queue_locked(Monitor *mon)
  306. {
  307. while (!g_queue_is_empty(mon->qmp.qmp_requests)) {
  308. qmp_request_free(g_queue_pop_head(mon->qmp.qmp_requests));
  309. }
  310. }
  311. /* Must with the mon->qmp.qmp_queue_lock held */
  312. static void monitor_qmp_cleanup_resp_queue_locked(Monitor *mon)
  313. {
  314. while (!g_queue_is_empty(mon->qmp.qmp_responses)) {
  315. qobject_decref(g_queue_pop_head(mon->qmp.qmp_responses));
  316. }
  317. }
  318. static void monitor_qmp_cleanup_queues(Monitor *mon)
  319. {
  320. qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
  321. monitor_qmp_cleanup_req_queue_locked(mon);
  322. monitor_qmp_cleanup_resp_queue_locked(mon);
  323. qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
  324. }
  325. static void monitor_flush_locked(Monitor *mon);
  326. static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
  327. void *opaque)
  328. {
  329. Monitor *mon = opaque;
  330. qemu_mutex_lock(&mon->out_lock);
  331. mon->out_watch = 0;
  332. monitor_flush_locked(mon);
  333. qemu_mutex_unlock(&mon->out_lock);
  334. return FALSE;
  335. }
  336. /* Called with mon->out_lock held. */
  337. static void monitor_flush_locked(Monitor *mon)
  338. {
  339. int rc;
  340. size_t len;
  341. const char *buf;
  342. if (mon->skip_flush) {
  343. return;
  344. }
  345. buf = qstring_get_str(mon->outbuf);
  346. len = qstring_get_length(mon->outbuf);
  347. if (len && !mon->mux_out) {
  348. rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
  349. if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
  350. /* all flushed or error */
  351. QDECREF(mon->outbuf);
  352. mon->outbuf = qstring_new();
  353. return;
  354. }
  355. if (rc > 0) {
  356. /* partial write */
  357. QString *tmp = qstring_from_str(buf + rc);
  358. QDECREF(mon->outbuf);
  359. mon->outbuf = tmp;
  360. }
  361. if (mon->out_watch == 0) {
  362. mon->out_watch =
  363. qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
  364. monitor_unblocked, mon);
  365. }
  366. }
  367. }
  368. void monitor_flush(Monitor *mon)
  369. {
  370. qemu_mutex_lock(&mon->out_lock);
  371. monitor_flush_locked(mon);
  372. qemu_mutex_unlock(&mon->out_lock);
  373. }
  374. /* flush at every end of line */
  375. static void monitor_puts(Monitor *mon, const char *str)
  376. {
  377. char c;
  378. qemu_mutex_lock(&mon->out_lock);
  379. for(;;) {
  380. c = *str++;
  381. if (c == '\0')
  382. break;
  383. if (c == '\n') {
  384. qstring_append_chr(mon->outbuf, '\r');
  385. }
  386. qstring_append_chr(mon->outbuf, c);
  387. if (c == '\n') {
  388. monitor_flush_locked(mon);
  389. }
  390. }
  391. qemu_mutex_unlock(&mon->out_lock);
  392. }
  393. void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
  394. {
  395. char *buf;
  396. if (!mon)
  397. return;
  398. if (monitor_is_qmp(mon)) {
  399. return;
  400. }
  401. buf = g_strdup_vprintf(fmt, ap);
  402. monitor_puts(mon, buf);
  403. g_free(buf);
  404. }
  405. void monitor_printf(Monitor *mon, const char *fmt, ...)
  406. {
  407. va_list ap;
  408. va_start(ap, fmt);
  409. monitor_vprintf(mon, fmt, ap);
  410. va_end(ap);
  411. }
  412. int monitor_fprintf(FILE *stream, const char *fmt, ...)
  413. {
  414. va_list ap;
  415. va_start(ap, fmt);
  416. monitor_vprintf((Monitor *)stream, fmt, ap);
  417. va_end(ap);
  418. return 0;
  419. }
  420. static void monitor_json_emitter_raw(Monitor *mon,
  421. QObject *data)
  422. {
  423. QString *json;
  424. json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
  425. qobject_to_json(data);
  426. assert(json != NULL);
  427. qstring_append_chr(json, '\n');
  428. monitor_puts(mon, qstring_get_str(json));
  429. QDECREF(json);
  430. }
  431. static void monitor_json_emitter(Monitor *mon, QObject *data)
  432. {
  433. if (mon->use_io_thr) {
  434. /*
  435. * If using IO thread, we need to queue the item so that IO
  436. * thread will do the rest for us. Take refcount so that
  437. * caller won't free the data (which will be finally freed in
  438. * responder thread).
  439. */
  440. qobject_incref(data);
  441. qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
  442. g_queue_push_tail(mon->qmp.qmp_responses, (void *)data);
  443. qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
  444. qemu_bh_schedule(mon_global.qmp_respond_bh);
  445. } else {
  446. /*
  447. * If not using monitor IO thread, then we are in main thread.
  448. * Do the emission right away.
  449. */
  450. monitor_json_emitter_raw(mon, data);
  451. }
  452. }
  453. struct QMPResponse {
  454. Monitor *mon;
  455. QObject *data;
  456. };
  457. typedef struct QMPResponse QMPResponse;
  458. /*
  459. * Return one QMPResponse. The response is only valid if
  460. * response.data is not NULL.
  461. */
  462. static QMPResponse monitor_qmp_response_pop_one(void)
  463. {
  464. Monitor *mon;
  465. QObject *data = NULL;
  466. qemu_mutex_lock(&monitor_lock);
  467. QTAILQ_FOREACH(mon, &mon_list, entry) {
  468. qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
  469. data = g_queue_pop_head(mon->qmp.qmp_responses);
  470. qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
  471. if (data) {
  472. break;
  473. }
  474. }
  475. qemu_mutex_unlock(&monitor_lock);
  476. return (QMPResponse) { .mon = mon, .data = data };
  477. }
  478. static void monitor_qmp_bh_responder(void *opaque)
  479. {
  480. QMPResponse response;
  481. while (true) {
  482. response = monitor_qmp_response_pop_one();
  483. if (!response.data) {
  484. break;
  485. }
  486. monitor_json_emitter_raw(response.mon, response.data);
  487. qobject_decref(response.data);
  488. }
  489. }
  490. static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
  491. /* Limit guest-triggerable events to 1 per second */
  492. [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
  493. [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
  494. [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
  495. [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
  496. [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
  497. [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
  498. };
  499. GHashTable *monitor_qapi_event_state;
  500. /*
  501. * Emits the event to every monitor instance, @event is only used for trace
  502. * Called with monitor_lock held.
  503. */
  504. static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
  505. {
  506. Monitor *mon;
  507. trace_monitor_protocol_event_emit(event, qdict);
  508. QTAILQ_FOREACH(mon, &mon_list, entry) {
  509. if (monitor_is_qmp(mon)
  510. && mon->qmp.commands != &qmp_cap_negotiation_commands) {
  511. monitor_json_emitter(mon, QOBJECT(qdict));
  512. }
  513. }
  514. }
  515. static void monitor_qapi_event_handler(void *opaque);
  516. /*
  517. * Queue a new event for emission to Monitor instances,
  518. * applying any rate limiting if required.
  519. */
  520. static void
  521. monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
  522. {
  523. MonitorQAPIEventConf *evconf;
  524. MonitorQAPIEventState *evstate;
  525. assert(event < QAPI_EVENT__MAX);
  526. evconf = &monitor_qapi_event_conf[event];
  527. trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
  528. qemu_mutex_lock(&monitor_lock);
  529. if (!evconf->rate) {
  530. /* Unthrottled event */
  531. monitor_qapi_event_emit(event, qdict);
  532. } else {
  533. QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
  534. MonitorQAPIEventState key = { .event = event, .data = data };
  535. evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
  536. assert(!evstate || timer_pending(evstate->timer));
  537. if (evstate) {
  538. /*
  539. * Timer is pending for (at least) evconf->rate ns after
  540. * last send. Store event for sending when timer fires,
  541. * replacing a prior stored event if any.
  542. */
  543. QDECREF(evstate->qdict);
  544. evstate->qdict = qdict;
  545. QINCREF(evstate->qdict);
  546. } else {
  547. /*
  548. * Last send was (at least) evconf->rate ns ago.
  549. * Send immediately, and arm the timer to call
  550. * monitor_qapi_event_handler() in evconf->rate ns. Any
  551. * events arriving before then will be delayed until then.
  552. */
  553. int64_t now = qemu_clock_get_ns(event_clock_type);
  554. monitor_qapi_event_emit(event, qdict);
  555. evstate = g_new(MonitorQAPIEventState, 1);
  556. evstate->event = event;
  557. evstate->data = data;
  558. QINCREF(evstate->data);
  559. evstate->qdict = NULL;
  560. evstate->timer = timer_new_ns(event_clock_type,
  561. monitor_qapi_event_handler,
  562. evstate);
  563. g_hash_table_add(monitor_qapi_event_state, evstate);
  564. timer_mod_ns(evstate->timer, now + evconf->rate);
  565. }
  566. }
  567. qemu_mutex_unlock(&monitor_lock);
  568. }
  569. /*
  570. * This function runs evconf->rate ns after sending a throttled
  571. * event.
  572. * If another event has since been stored, send it.
  573. */
  574. static void monitor_qapi_event_handler(void *opaque)
  575. {
  576. MonitorQAPIEventState *evstate = opaque;
  577. MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
  578. trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
  579. qemu_mutex_lock(&monitor_lock);
  580. if (evstate->qdict) {
  581. int64_t now = qemu_clock_get_ns(event_clock_type);
  582. monitor_qapi_event_emit(evstate->event, evstate->qdict);
  583. QDECREF(evstate->qdict);
  584. evstate->qdict = NULL;
  585. timer_mod_ns(evstate->timer, now + evconf->rate);
  586. } else {
  587. g_hash_table_remove(monitor_qapi_event_state, evstate);
  588. QDECREF(evstate->data);
  589. timer_free(evstate->timer);
  590. g_free(evstate);
  591. }
  592. qemu_mutex_unlock(&monitor_lock);
  593. }
  594. static unsigned int qapi_event_throttle_hash(const void *key)
  595. {
  596. const MonitorQAPIEventState *evstate = key;
  597. unsigned int hash = evstate->event * 255;
  598. if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
  599. hash += g_str_hash(qdict_get_str(evstate->data, "id"));
  600. }
  601. if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
  602. hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
  603. }
  604. return hash;
  605. }
  606. static gboolean qapi_event_throttle_equal(const void *a, const void *b)
  607. {
  608. const MonitorQAPIEventState *eva = a;
  609. const MonitorQAPIEventState *evb = b;
  610. if (eva->event != evb->event) {
  611. return FALSE;
  612. }
  613. if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
  614. return !strcmp(qdict_get_str(eva->data, "id"),
  615. qdict_get_str(evb->data, "id"));
  616. }
  617. if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
  618. return !strcmp(qdict_get_str(eva->data, "node-name"),
  619. qdict_get_str(evb->data, "node-name"));
  620. }
  621. return TRUE;
  622. }
  623. static void monitor_qapi_event_init(void)
  624. {
  625. if (qtest_enabled()) {
  626. event_clock_type = QEMU_CLOCK_VIRTUAL;
  627. }
  628. monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
  629. qapi_event_throttle_equal);
  630. qmp_event_set_func_emit(monitor_qapi_event_queue);
  631. }
  632. static void handle_hmp_command(Monitor *mon, const char *cmdline);
  633. static void monitor_data_init(Monitor *mon, bool skip_flush,
  634. bool use_io_thr)
  635. {
  636. memset(mon, 0, sizeof(Monitor));
  637. qemu_mutex_init(&mon->out_lock);
  638. qemu_mutex_init(&mon->qmp.qmp_queue_lock);
  639. mon->outbuf = qstring_new();
  640. /* Use *mon_cmds by default. */
  641. mon->cmd_table = mon_cmds;
  642. mon->skip_flush = skip_flush;
  643. mon->use_io_thr = use_io_thr;
  644. mon->qmp.qmp_requests = g_queue_new();
  645. mon->qmp.qmp_responses = g_queue_new();
  646. }
  647. static void monitor_data_destroy(Monitor *mon)
  648. {
  649. g_free(mon->mon_cpu_path);
  650. qemu_chr_fe_deinit(&mon->chr, false);
  651. if (monitor_is_qmp(mon)) {
  652. json_message_parser_destroy(&mon->qmp.parser);
  653. }
  654. readline_free(mon->rs);
  655. QDECREF(mon->outbuf);
  656. qemu_mutex_destroy(&mon->out_lock);
  657. qemu_mutex_destroy(&mon->qmp.qmp_queue_lock);
  658. monitor_qmp_cleanup_req_queue_locked(mon);
  659. monitor_qmp_cleanup_resp_queue_locked(mon);
  660. g_queue_free(mon->qmp.qmp_requests);
  661. g_queue_free(mon->qmp.qmp_responses);
  662. }
  663. char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
  664. int64_t cpu_index, Error **errp)
  665. {
  666. char *output = NULL;
  667. Monitor *old_mon, hmp;
  668. monitor_data_init(&hmp, true, false);
  669. old_mon = cur_mon;
  670. cur_mon = &hmp;
  671. if (has_cpu_index) {
  672. int ret = monitor_set_cpu(cpu_index);
  673. if (ret < 0) {
  674. cur_mon = old_mon;
  675. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
  676. "a CPU number");
  677. goto out;
  678. }
  679. }
  680. handle_hmp_command(&hmp, command_line);
  681. cur_mon = old_mon;
  682. qemu_mutex_lock(&hmp.out_lock);
  683. if (qstring_get_length(hmp.outbuf) > 0) {
  684. output = g_strdup(qstring_get_str(hmp.outbuf));
  685. } else {
  686. output = g_strdup("");
  687. }
  688. qemu_mutex_unlock(&hmp.out_lock);
  689. out:
  690. monitor_data_destroy(&hmp);
  691. return output;
  692. }
  693. static int compare_cmd(const char *name, const char *list)
  694. {
  695. const char *p, *pstart;
  696. int len;
  697. len = strlen(name);
  698. p = list;
  699. for(;;) {
  700. pstart = p;
  701. p = strchr(p, '|');
  702. if (!p)
  703. p = pstart + strlen(pstart);
  704. if ((p - pstart) == len && !memcmp(pstart, name, len))
  705. return 1;
  706. if (*p == '\0')
  707. break;
  708. p++;
  709. }
  710. return 0;
  711. }
  712. static int get_str(char *buf, int buf_size, const char **pp)
  713. {
  714. const char *p;
  715. char *q;
  716. int c;
  717. q = buf;
  718. p = *pp;
  719. while (qemu_isspace(*p)) {
  720. p++;
  721. }
  722. if (*p == '\0') {
  723. fail:
  724. *q = '\0';
  725. *pp = p;
  726. return -1;
  727. }
  728. if (*p == '\"') {
  729. p++;
  730. while (*p != '\0' && *p != '\"') {
  731. if (*p == '\\') {
  732. p++;
  733. c = *p++;
  734. switch (c) {
  735. case 'n':
  736. c = '\n';
  737. break;
  738. case 'r':
  739. c = '\r';
  740. break;
  741. case '\\':
  742. case '\'':
  743. case '\"':
  744. break;
  745. default:
  746. printf("unsupported escape code: '\\%c'\n", c);
  747. goto fail;
  748. }
  749. if ((q - buf) < buf_size - 1) {
  750. *q++ = c;
  751. }
  752. } else {
  753. if ((q - buf) < buf_size - 1) {
  754. *q++ = *p;
  755. }
  756. p++;
  757. }
  758. }
  759. if (*p != '\"') {
  760. printf("unterminated string\n");
  761. goto fail;
  762. }
  763. p++;
  764. } else {
  765. while (*p != '\0' && !qemu_isspace(*p)) {
  766. if ((q - buf) < buf_size - 1) {
  767. *q++ = *p;
  768. }
  769. p++;
  770. }
  771. }
  772. *q = '\0';
  773. *pp = p;
  774. return 0;
  775. }
  776. #define MAX_ARGS 16
  777. static void free_cmdline_args(char **args, int nb_args)
  778. {
  779. int i;
  780. assert(nb_args <= MAX_ARGS);
  781. for (i = 0; i < nb_args; i++) {
  782. g_free(args[i]);
  783. }
  784. }
  785. /*
  786. * Parse the command line to get valid args.
  787. * @cmdline: command line to be parsed.
  788. * @pnb_args: location to store the number of args, must NOT be NULL.
  789. * @args: location to store the args, which should be freed by caller, must
  790. * NOT be NULL.
  791. *
  792. * Returns 0 on success, negative on failure.
  793. *
  794. * NOTE: this parser is an approximate form of the real command parser. Number
  795. * of args have a limit of MAX_ARGS. If cmdline contains more, it will
  796. * return with failure.
  797. */
  798. static int parse_cmdline(const char *cmdline,
  799. int *pnb_args, char **args)
  800. {
  801. const char *p;
  802. int nb_args, ret;
  803. char buf[1024];
  804. p = cmdline;
  805. nb_args = 0;
  806. for (;;) {
  807. while (qemu_isspace(*p)) {
  808. p++;
  809. }
  810. if (*p == '\0') {
  811. break;
  812. }
  813. if (nb_args >= MAX_ARGS) {
  814. goto fail;
  815. }
  816. ret = get_str(buf, sizeof(buf), &p);
  817. if (ret < 0) {
  818. goto fail;
  819. }
  820. args[nb_args] = g_strdup(buf);
  821. nb_args++;
  822. }
  823. *pnb_args = nb_args;
  824. return 0;
  825. fail:
  826. free_cmdline_args(args, nb_args);
  827. return -1;
  828. }
  829. static void help_cmd_dump_one(Monitor *mon,
  830. const mon_cmd_t *cmd,
  831. char **prefix_args,
  832. int prefix_args_nb)
  833. {
  834. int i;
  835. for (i = 0; i < prefix_args_nb; i++) {
  836. monitor_printf(mon, "%s ", prefix_args[i]);
  837. }
  838. monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
  839. }
  840. /* @args[@arg_index] is the valid command need to find in @cmds */
  841. static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
  842. char **args, int nb_args, int arg_index)
  843. {
  844. const mon_cmd_t *cmd;
  845. /* No valid arg need to compare with, dump all in *cmds */
  846. if (arg_index >= nb_args) {
  847. for (cmd = cmds; cmd->name != NULL; cmd++) {
  848. help_cmd_dump_one(mon, cmd, args, arg_index);
  849. }
  850. return;
  851. }
  852. /* Find one entry to dump */
  853. for (cmd = cmds; cmd->name != NULL; cmd++) {
  854. if (compare_cmd(args[arg_index], cmd->name)) {
  855. if (cmd->sub_table) {
  856. /* continue with next arg */
  857. help_cmd_dump(mon, cmd->sub_table,
  858. args, nb_args, arg_index + 1);
  859. } else {
  860. help_cmd_dump_one(mon, cmd, args, arg_index);
  861. }
  862. break;
  863. }
  864. }
  865. }
  866. static void help_cmd(Monitor *mon, const char *name)
  867. {
  868. char *args[MAX_ARGS];
  869. int nb_args = 0;
  870. /* 1. parse user input */
  871. if (name) {
  872. /* special case for log, directly dump and return */
  873. if (!strcmp(name, "log")) {
  874. const QEMULogItem *item;
  875. monitor_printf(mon, "Log items (comma separated):\n");
  876. monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
  877. for (item = qemu_log_items; item->mask != 0; item++) {
  878. monitor_printf(mon, "%-10s %s\n", item->name, item->help);
  879. }
  880. return;
  881. }
  882. if (parse_cmdline(name, &nb_args, args) < 0) {
  883. return;
  884. }
  885. }
  886. /* 2. dump the contents according to parsed args */
  887. help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
  888. free_cmdline_args(args, nb_args);
  889. }
  890. static void do_help_cmd(Monitor *mon, const QDict *qdict)
  891. {
  892. help_cmd(mon, qdict_get_try_str(qdict, "name"));
  893. }
  894. static void hmp_trace_event(Monitor *mon, const QDict *qdict)
  895. {
  896. const char *tp_name = qdict_get_str(qdict, "name");
  897. bool new_state = qdict_get_bool(qdict, "option");
  898. bool has_vcpu = qdict_haskey(qdict, "vcpu");
  899. int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
  900. Error *local_err = NULL;
  901. if (vcpu < 0) {
  902. monitor_printf(mon, "argument vcpu must be positive");
  903. return;
  904. }
  905. qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
  906. if (local_err) {
  907. error_report_err(local_err);
  908. }
  909. }
  910. #ifdef CONFIG_TRACE_SIMPLE
  911. static void hmp_trace_file(Monitor *mon, const QDict *qdict)
  912. {
  913. const char *op = qdict_get_try_str(qdict, "op");
  914. const char *arg = qdict_get_try_str(qdict, "arg");
  915. if (!op) {
  916. st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
  917. } else if (!strcmp(op, "on")) {
  918. st_set_trace_file_enabled(true);
  919. } else if (!strcmp(op, "off")) {
  920. st_set_trace_file_enabled(false);
  921. } else if (!strcmp(op, "flush")) {
  922. st_flush_trace_buffer();
  923. } else if (!strcmp(op, "set")) {
  924. if (arg) {
  925. st_set_trace_file(arg);
  926. }
  927. } else {
  928. monitor_printf(mon, "unexpected argument \"%s\"\n", op);
  929. help_cmd(mon, "trace-file");
  930. }
  931. }
  932. #endif
  933. static void hmp_info_help(Monitor *mon, const QDict *qdict)
  934. {
  935. help_cmd(mon, "info");
  936. }
  937. static void query_commands_cb(QmpCommand *cmd, void *opaque)
  938. {
  939. CommandInfoList *info, **list = opaque;
  940. if (!cmd->enabled) {
  941. return;
  942. }
  943. info = g_malloc0(sizeof(*info));
  944. info->value = g_malloc0(sizeof(*info->value));
  945. info->value->name = g_strdup(cmd->name);
  946. info->next = *list;
  947. *list = info;
  948. }
  949. CommandInfoList *qmp_query_commands(Error **errp)
  950. {
  951. CommandInfoList *list = NULL;
  952. qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
  953. return list;
  954. }
  955. EventInfoList *qmp_query_events(Error **errp)
  956. {
  957. EventInfoList *info, *ev_list = NULL;
  958. QAPIEvent e;
  959. for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
  960. const char *event_name = QAPIEvent_str(e);
  961. assert(event_name != NULL);
  962. info = g_malloc0(sizeof(*info));
  963. info->value = g_malloc0(sizeof(*info->value));
  964. info->value->name = g_strdup(event_name);
  965. info->next = ev_list;
  966. ev_list = info;
  967. }
  968. return ev_list;
  969. }
  970. /*
  971. * Minor hack: generated marshalling suppressed for this command
  972. * ('gen': false in the schema) so we can parse the JSON string
  973. * directly into QObject instead of first parsing it with
  974. * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
  975. * to QObject with generated output marshallers, every time. Instead,
  976. * we do it in test-qobject-input-visitor.c, just to make sure
  977. * qapi-gen.py's output actually conforms to the schema.
  978. */
  979. static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  980. Error **errp)
  981. {
  982. *ret_data = qobject_from_qlit(&qmp_schema_qlit);
  983. }
  984. /*
  985. * We used to define commands in qmp-commands.hx in addition to the
  986. * QAPI schema. This permitted defining some of them only in certain
  987. * configurations. query-commands has always reflected that (good,
  988. * because it lets QMP clients figure out what's actually available),
  989. * while query-qmp-schema never did (not so good). This function is a
  990. * hack to keep the configuration-specific commands defined exactly as
  991. * before, even though qmp-commands.hx is gone.
  992. *
  993. * FIXME Educate the QAPI schema on configuration-specific commands,
  994. * and drop this hack.
  995. */
  996. static void qmp_unregister_commands_hack(void)
  997. {
  998. #ifndef CONFIG_SPICE
  999. qmp_unregister_command(&qmp_commands, "query-spice");
  1000. #endif
  1001. #ifndef CONFIG_REPLICATION
  1002. qmp_unregister_command(&qmp_commands, "xen-set-replication");
  1003. qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
  1004. qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
  1005. #endif
  1006. #ifndef TARGET_I386
  1007. qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
  1008. qmp_unregister_command(&qmp_commands, "query-sev");
  1009. qmp_unregister_command(&qmp_commands, "query-sev-launch-measure");
  1010. qmp_unregister_command(&qmp_commands, "query-sev-capabilities");
  1011. #endif
  1012. #ifndef TARGET_S390X
  1013. qmp_unregister_command(&qmp_commands, "dump-skeys");
  1014. #endif
  1015. #ifndef TARGET_ARM
  1016. qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
  1017. #endif
  1018. #if !defined(TARGET_S390X) && !defined(TARGET_I386)
  1019. qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
  1020. #endif
  1021. #if !defined(TARGET_S390X)
  1022. qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
  1023. qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
  1024. #endif
  1025. #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
  1026. && !defined(TARGET_S390X)
  1027. qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
  1028. #endif
  1029. }
  1030. static void monitor_init_qmp_commands(void)
  1031. {
  1032. /*
  1033. * Two command lists:
  1034. * - qmp_commands contains all QMP commands
  1035. * - qmp_cap_negotiation_commands contains just
  1036. * "qmp_capabilities", to enforce capability negotiation
  1037. */
  1038. qmp_init_marshal(&qmp_commands);
  1039. qmp_register_command(&qmp_commands, "query-qmp-schema",
  1040. qmp_query_qmp_schema,
  1041. QCO_NO_OPTIONS);
  1042. qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
  1043. QCO_NO_OPTIONS);
  1044. qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
  1045. QCO_NO_OPTIONS);
  1046. qmp_unregister_commands_hack();
  1047. QTAILQ_INIT(&qmp_cap_negotiation_commands);
  1048. qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
  1049. qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
  1050. }
  1051. static bool qmp_cap_enabled(Monitor *mon, QMPCapability cap)
  1052. {
  1053. return mon->qmp.qmp_caps[cap];
  1054. }
  1055. static bool qmp_oob_enabled(Monitor *mon)
  1056. {
  1057. return qmp_cap_enabled(mon, QMP_CAPABILITY_OOB);
  1058. }
  1059. static void qmp_caps_check(Monitor *mon, QMPCapabilityList *list,
  1060. Error **errp)
  1061. {
  1062. for (; list; list = list->next) {
  1063. assert(list->value < QMP_CAPABILITY__MAX);
  1064. switch (list->value) {
  1065. case QMP_CAPABILITY_OOB:
  1066. if (!mon->use_io_thr) {
  1067. /*
  1068. * Out-Of-Band only works with monitors that are
  1069. * running on dedicated IOThread.
  1070. */
  1071. error_setg(errp, "This monitor does not support "
  1072. "Out-Of-Band (OOB)");
  1073. return;
  1074. }
  1075. break;
  1076. default:
  1077. break;
  1078. }
  1079. }
  1080. }
  1081. /* This function should only be called after capabilities are checked. */
  1082. static void qmp_caps_apply(Monitor *mon, QMPCapabilityList *list)
  1083. {
  1084. for (; list; list = list->next) {
  1085. mon->qmp.qmp_caps[list->value] = true;
  1086. }
  1087. }
  1088. /*
  1089. * Return true if check successful, or false otherwise. When false is
  1090. * returned, detailed error will be in errp if provided.
  1091. */
  1092. static bool qmp_cmd_oob_check(Monitor *mon, QDict *req, Error **errp)
  1093. {
  1094. const char *command;
  1095. QmpCommand *cmd;
  1096. command = qdict_get_try_str(req, "execute");
  1097. if (!command) {
  1098. error_setg(errp, "Command field 'execute' missing");
  1099. return false;
  1100. }
  1101. cmd = qmp_find_command(mon->qmp.commands, command);
  1102. if (!cmd) {
  1103. if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
  1104. error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
  1105. "Expecting capabilities negotiation "
  1106. "with 'qmp_capabilities'");
  1107. } else {
  1108. error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
  1109. "The command %s has not been found", command);
  1110. }
  1111. return false;
  1112. }
  1113. if (qmp_is_oob(req)) {
  1114. if (!qmp_oob_enabled(mon)) {
  1115. error_setg(errp, "Please enable Out-Of-Band first "
  1116. "for the session during capabilities negotiation");
  1117. return false;
  1118. }
  1119. if (!(cmd->options & QCO_ALLOW_OOB)) {
  1120. error_setg(errp, "The command %s does not support OOB",
  1121. command);
  1122. return false;
  1123. }
  1124. }
  1125. return true;
  1126. }
  1127. void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
  1128. Error **errp)
  1129. {
  1130. Error *local_err = NULL;
  1131. if (cur_mon->qmp.commands == &qmp_commands) {
  1132. error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
  1133. "Capabilities negotiation is already complete, command "
  1134. "ignored");
  1135. return;
  1136. }
  1137. /* Enable QMP capabilities provided by the client if applicable. */
  1138. if (has_enable) {
  1139. qmp_caps_check(cur_mon, enable, &local_err);
  1140. if (local_err) {
  1141. /*
  1142. * Failed check on any of the capabilities will fail the
  1143. * entire command (and thus not apply any of the other
  1144. * capabilities that were also requested).
  1145. */
  1146. error_propagate(errp, local_err);
  1147. return;
  1148. }
  1149. qmp_caps_apply(cur_mon, enable);
  1150. }
  1151. cur_mon->qmp.commands = &qmp_commands;
  1152. }
  1153. /* set the current CPU defined by the user */
  1154. int monitor_set_cpu(int cpu_index)
  1155. {
  1156. CPUState *cpu;
  1157. cpu = qemu_get_cpu(cpu_index);
  1158. if (cpu == NULL) {
  1159. return -1;
  1160. }
  1161. g_free(cur_mon->mon_cpu_path);
  1162. cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
  1163. return 0;
  1164. }
  1165. static CPUState *mon_get_cpu_sync(bool synchronize)
  1166. {
  1167. CPUState *cpu;
  1168. if (cur_mon->mon_cpu_path) {
  1169. cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
  1170. TYPE_CPU, NULL);
  1171. if (!cpu) {
  1172. g_free(cur_mon->mon_cpu_path);
  1173. cur_mon->mon_cpu_path = NULL;
  1174. }
  1175. }
  1176. if (!cur_mon->mon_cpu_path) {
  1177. if (!first_cpu) {
  1178. return NULL;
  1179. }
  1180. monitor_set_cpu(first_cpu->cpu_index);
  1181. cpu = first_cpu;
  1182. }
  1183. if (synchronize) {
  1184. cpu_synchronize_state(cpu);
  1185. }
  1186. return cpu;
  1187. }
  1188. CPUState *mon_get_cpu(void)
  1189. {
  1190. return mon_get_cpu_sync(true);
  1191. }
  1192. CPUArchState *mon_get_cpu_env(void)
  1193. {
  1194. CPUState *cs = mon_get_cpu();
  1195. return cs ? cs->env_ptr : NULL;
  1196. }
  1197. int monitor_get_cpu_index(void)
  1198. {
  1199. CPUState *cs = mon_get_cpu_sync(false);
  1200. return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
  1201. }
  1202. static void hmp_info_registers(Monitor *mon, const QDict *qdict)
  1203. {
  1204. bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
  1205. CPUState *cs;
  1206. if (all_cpus) {
  1207. CPU_FOREACH(cs) {
  1208. monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
  1209. cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
  1210. }
  1211. } else {
  1212. cs = mon_get_cpu();
  1213. if (!cs) {
  1214. monitor_printf(mon, "No CPU available\n");
  1215. return;
  1216. }
  1217. cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
  1218. }
  1219. }
  1220. #ifdef CONFIG_TCG
  1221. static void hmp_info_jit(Monitor *mon, const QDict *qdict)
  1222. {
  1223. if (!tcg_enabled()) {
  1224. error_report("JIT information is only available with accel=tcg");
  1225. return;
  1226. }
  1227. dump_exec_info((FILE *)mon, monitor_fprintf);
  1228. dump_drift_info((FILE *)mon, monitor_fprintf);
  1229. }
  1230. static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
  1231. {
  1232. dump_opcount_info((FILE *)mon, monitor_fprintf);
  1233. }
  1234. #endif
  1235. static void hmp_info_history(Monitor *mon, const QDict *qdict)
  1236. {
  1237. int i;
  1238. const char *str;
  1239. if (!mon->rs)
  1240. return;
  1241. i = 0;
  1242. for(;;) {
  1243. str = readline_get_history(mon->rs, i);
  1244. if (!str)
  1245. break;
  1246. monitor_printf(mon, "%d: '%s'\n", i, str);
  1247. i++;
  1248. }
  1249. }
  1250. static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
  1251. {
  1252. CPUState *cs = mon_get_cpu();
  1253. if (!cs) {
  1254. monitor_printf(mon, "No CPU available\n");
  1255. return;
  1256. }
  1257. cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
  1258. }
  1259. static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
  1260. {
  1261. const char *name = qdict_get_try_str(qdict, "name");
  1262. bool has_vcpu = qdict_haskey(qdict, "vcpu");
  1263. int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
  1264. TraceEventInfoList *events;
  1265. TraceEventInfoList *elem;
  1266. Error *local_err = NULL;
  1267. if (name == NULL) {
  1268. name = "*";
  1269. }
  1270. if (vcpu < 0) {
  1271. monitor_printf(mon, "argument vcpu must be positive");
  1272. return;
  1273. }
  1274. events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
  1275. if (local_err) {
  1276. error_report_err(local_err);
  1277. return;
  1278. }
  1279. for (elem = events; elem != NULL; elem = elem->next) {
  1280. monitor_printf(mon, "%s : state %u\n",
  1281. elem->value->name,
  1282. elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
  1283. }
  1284. qapi_free_TraceEventInfoList(events);
  1285. }
  1286. void qmp_client_migrate_info(const char *protocol, const char *hostname,
  1287. bool has_port, int64_t port,
  1288. bool has_tls_port, int64_t tls_port,
  1289. bool has_cert_subject, const char *cert_subject,
  1290. Error **errp)
  1291. {
  1292. if (strcmp(protocol, "spice") == 0) {
  1293. if (!qemu_using_spice(errp)) {
  1294. return;
  1295. }
  1296. if (!has_port && !has_tls_port) {
  1297. error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
  1298. return;
  1299. }
  1300. if (qemu_spice_migrate_info(hostname,
  1301. has_port ? port : -1,
  1302. has_tls_port ? tls_port : -1,
  1303. cert_subject)) {
  1304. error_setg(errp, QERR_UNDEFINED_ERROR);
  1305. return;
  1306. }
  1307. return;
  1308. }
  1309. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
  1310. }
  1311. static void hmp_logfile(Monitor *mon, const QDict *qdict)
  1312. {
  1313. Error *err = NULL;
  1314. qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
  1315. if (err) {
  1316. error_report_err(err);
  1317. }
  1318. }
  1319. static void hmp_log(Monitor *mon, const QDict *qdict)
  1320. {
  1321. int mask;
  1322. const char *items = qdict_get_str(qdict, "items");
  1323. if (!strcmp(items, "none")) {
  1324. mask = 0;
  1325. } else {
  1326. mask = qemu_str_to_log_mask(items);
  1327. if (!mask) {
  1328. help_cmd(mon, "log");
  1329. return;
  1330. }
  1331. }
  1332. qemu_set_log(mask);
  1333. }
  1334. static void hmp_singlestep(Monitor *mon, const QDict *qdict)
  1335. {
  1336. const char *option = qdict_get_try_str(qdict, "option");
  1337. if (!option || !strcmp(option, "on")) {
  1338. singlestep = 1;
  1339. } else if (!strcmp(option, "off")) {
  1340. singlestep = 0;
  1341. } else {
  1342. monitor_printf(mon, "unexpected option %s\n", option);
  1343. }
  1344. }
  1345. static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
  1346. {
  1347. const char *device = qdict_get_try_str(qdict, "device");
  1348. if (!device)
  1349. device = "tcp::" DEFAULT_GDBSTUB_PORT;
  1350. if (gdbserver_start(device) < 0) {
  1351. monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
  1352. device);
  1353. } else if (strcmp(device, "none") == 0) {
  1354. monitor_printf(mon, "Disabled gdbserver\n");
  1355. } else {
  1356. monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
  1357. device);
  1358. }
  1359. }
  1360. static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
  1361. {
  1362. const char *action = qdict_get_str(qdict, "action");
  1363. if (select_watchdog_action(action) == -1) {
  1364. monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
  1365. }
  1366. }
  1367. static void monitor_printc(Monitor *mon, int c)
  1368. {
  1369. monitor_printf(mon, "'");
  1370. switch(c) {
  1371. case '\'':
  1372. monitor_printf(mon, "\\'");
  1373. break;
  1374. case '\\':
  1375. monitor_printf(mon, "\\\\");
  1376. break;
  1377. case '\n':
  1378. monitor_printf(mon, "\\n");
  1379. break;
  1380. case '\r':
  1381. monitor_printf(mon, "\\r");
  1382. break;
  1383. default:
  1384. if (c >= 32 && c <= 126) {
  1385. monitor_printf(mon, "%c", c);
  1386. } else {
  1387. monitor_printf(mon, "\\x%02x", c);
  1388. }
  1389. break;
  1390. }
  1391. monitor_printf(mon, "'");
  1392. }
  1393. static void memory_dump(Monitor *mon, int count, int format, int wsize,
  1394. hwaddr addr, int is_physical)
  1395. {
  1396. int l, line_size, i, max_digits, len;
  1397. uint8_t buf[16];
  1398. uint64_t v;
  1399. CPUState *cs = mon_get_cpu();
  1400. if (!cs && (format == 'i' || !is_physical)) {
  1401. monitor_printf(mon, "Can not dump without CPU\n");
  1402. return;
  1403. }
  1404. if (format == 'i') {
  1405. monitor_disas(mon, cs, addr, count, is_physical);
  1406. return;
  1407. }
  1408. len = wsize * count;
  1409. if (wsize == 1)
  1410. line_size = 8;
  1411. else
  1412. line_size = 16;
  1413. max_digits = 0;
  1414. switch(format) {
  1415. case 'o':
  1416. max_digits = DIV_ROUND_UP(wsize * 8, 3);
  1417. break;
  1418. default:
  1419. case 'x':
  1420. max_digits = (wsize * 8) / 4;
  1421. break;
  1422. case 'u':
  1423. case 'd':
  1424. max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
  1425. break;
  1426. case 'c':
  1427. wsize = 1;
  1428. break;
  1429. }
  1430. while (len > 0) {
  1431. if (is_physical)
  1432. monitor_printf(mon, TARGET_FMT_plx ":", addr);
  1433. else
  1434. monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
  1435. l = len;
  1436. if (l > line_size)
  1437. l = line_size;
  1438. if (is_physical) {
  1439. cpu_physical_memory_read(addr, buf, l);
  1440. } else {
  1441. if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
  1442. monitor_printf(mon, " Cannot access memory\n");
  1443. break;
  1444. }
  1445. }
  1446. i = 0;
  1447. while (i < l) {
  1448. switch(wsize) {
  1449. default:
  1450. case 1:
  1451. v = ldub_p(buf + i);
  1452. break;
  1453. case 2:
  1454. v = lduw_p(buf + i);
  1455. break;
  1456. case 4:
  1457. v = (uint32_t)ldl_p(buf + i);
  1458. break;
  1459. case 8:
  1460. v = ldq_p(buf + i);
  1461. break;
  1462. }
  1463. monitor_printf(mon, " ");
  1464. switch(format) {
  1465. case 'o':
  1466. monitor_printf(mon, "%#*" PRIo64, max_digits, v);
  1467. break;
  1468. case 'x':
  1469. monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
  1470. break;
  1471. case 'u':
  1472. monitor_printf(mon, "%*" PRIu64, max_digits, v);
  1473. break;
  1474. case 'd':
  1475. monitor_printf(mon, "%*" PRId64, max_digits, v);
  1476. break;
  1477. case 'c':
  1478. monitor_printc(mon, v);
  1479. break;
  1480. }
  1481. i += wsize;
  1482. }
  1483. monitor_printf(mon, "\n");
  1484. addr += l;
  1485. len -= l;
  1486. }
  1487. }
  1488. static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
  1489. {
  1490. int count = qdict_get_int(qdict, "count");
  1491. int format = qdict_get_int(qdict, "format");
  1492. int size = qdict_get_int(qdict, "size");
  1493. target_long addr = qdict_get_int(qdict, "addr");
  1494. memory_dump(mon, count, format, size, addr, 0);
  1495. }
  1496. static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
  1497. {
  1498. int count = qdict_get_int(qdict, "count");
  1499. int format = qdict_get_int(qdict, "format");
  1500. int size = qdict_get_int(qdict, "size");
  1501. hwaddr addr = qdict_get_int(qdict, "addr");
  1502. memory_dump(mon, count, format, size, addr, 1);
  1503. }
  1504. static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
  1505. {
  1506. MemoryRegionSection mrs = memory_region_find(get_system_memory(),
  1507. addr, 1);
  1508. if (!mrs.mr) {
  1509. error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
  1510. return NULL;
  1511. }
  1512. if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
  1513. error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
  1514. memory_region_unref(mrs.mr);
  1515. return NULL;
  1516. }
  1517. *p_mr = mrs.mr;
  1518. return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
  1519. }
  1520. static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
  1521. {
  1522. hwaddr addr = qdict_get_int(qdict, "addr");
  1523. Error *local_err = NULL;
  1524. MemoryRegion *mr = NULL;
  1525. void *ptr;
  1526. ptr = gpa2hva(&mr, addr, &local_err);
  1527. if (local_err) {
  1528. error_report_err(local_err);
  1529. return;
  1530. }
  1531. monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
  1532. " (%s) is %p\n",
  1533. addr, mr->name, ptr);
  1534. memory_region_unref(mr);
  1535. }
  1536. #ifdef CONFIG_LINUX
  1537. static uint64_t vtop(void *ptr, Error **errp)
  1538. {
  1539. uint64_t pinfo;
  1540. uint64_t ret = -1;
  1541. uintptr_t addr = (uintptr_t) ptr;
  1542. uintptr_t pagesize = getpagesize();
  1543. off_t offset = addr / pagesize * sizeof(pinfo);
  1544. int fd;
  1545. fd = open("/proc/self/pagemap", O_RDONLY);
  1546. if (fd == -1) {
  1547. error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
  1548. return -1;
  1549. }
  1550. /* Force copy-on-write if necessary. */
  1551. atomic_add((uint8_t *)ptr, 0);
  1552. if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
  1553. error_setg_errno(errp, errno, "Cannot read pagemap");
  1554. goto out;
  1555. }
  1556. if ((pinfo & (1ull << 63)) == 0) {
  1557. error_setg(errp, "Page not present");
  1558. goto out;
  1559. }
  1560. ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
  1561. out:
  1562. close(fd);
  1563. return ret;
  1564. }
  1565. static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
  1566. {
  1567. hwaddr addr = qdict_get_int(qdict, "addr");
  1568. Error *local_err = NULL;
  1569. MemoryRegion *mr = NULL;
  1570. void *ptr;
  1571. uint64_t physaddr;
  1572. ptr = gpa2hva(&mr, addr, &local_err);
  1573. if (local_err) {
  1574. error_report_err(local_err);
  1575. return;
  1576. }
  1577. physaddr = vtop(ptr, &local_err);
  1578. if (local_err) {
  1579. error_report_err(local_err);
  1580. } else {
  1581. monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
  1582. " (%s) is 0x%" PRIx64 "\n",
  1583. addr, mr->name, (uint64_t) physaddr);
  1584. }
  1585. memory_region_unref(mr);
  1586. }
  1587. #endif
  1588. static void do_print(Monitor *mon, const QDict *qdict)
  1589. {
  1590. int format = qdict_get_int(qdict, "format");
  1591. hwaddr val = qdict_get_int(qdict, "val");
  1592. switch(format) {
  1593. case 'o':
  1594. monitor_printf(mon, "%#" HWADDR_PRIo, val);
  1595. break;
  1596. case 'x':
  1597. monitor_printf(mon, "%#" HWADDR_PRIx, val);
  1598. break;
  1599. case 'u':
  1600. monitor_printf(mon, "%" HWADDR_PRIu, val);
  1601. break;
  1602. default:
  1603. case 'd':
  1604. monitor_printf(mon, "%" HWADDR_PRId, val);
  1605. break;
  1606. case 'c':
  1607. monitor_printc(mon, val);
  1608. break;
  1609. }
  1610. monitor_printf(mon, "\n");
  1611. }
  1612. static void hmp_sum(Monitor *mon, const QDict *qdict)
  1613. {
  1614. uint32_t addr;
  1615. uint16_t sum;
  1616. uint32_t start = qdict_get_int(qdict, "start");
  1617. uint32_t size = qdict_get_int(qdict, "size");
  1618. sum = 0;
  1619. for(addr = start; addr < (start + size); addr++) {
  1620. uint8_t val = address_space_ldub(&address_space_memory, addr,
  1621. MEMTXATTRS_UNSPECIFIED, NULL);
  1622. /* BSD sum algorithm ('sum' Unix command) */
  1623. sum = (sum >> 1) | (sum << 15);
  1624. sum += val;
  1625. }
  1626. monitor_printf(mon, "%05d\n", sum);
  1627. }
  1628. static int mouse_button_state;
  1629. static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
  1630. {
  1631. int dx, dy, dz, button;
  1632. const char *dx_str = qdict_get_str(qdict, "dx_str");
  1633. const char *dy_str = qdict_get_str(qdict, "dy_str");
  1634. const char *dz_str = qdict_get_try_str(qdict, "dz_str");
  1635. dx = strtol(dx_str, NULL, 0);
  1636. dy = strtol(dy_str, NULL, 0);
  1637. qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
  1638. qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
  1639. if (dz_str) {
  1640. dz = strtol(dz_str, NULL, 0);
  1641. if (dz != 0) {
  1642. button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
  1643. qemu_input_queue_btn(NULL, button, true);
  1644. qemu_input_event_sync();
  1645. qemu_input_queue_btn(NULL, button, false);
  1646. }
  1647. }
  1648. qemu_input_event_sync();
  1649. }
  1650. static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
  1651. {
  1652. static uint32_t bmap[INPUT_BUTTON__MAX] = {
  1653. [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
  1654. [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
  1655. [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
  1656. };
  1657. int button_state = qdict_get_int(qdict, "button_state");
  1658. if (mouse_button_state == button_state) {
  1659. return;
  1660. }
  1661. qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
  1662. qemu_input_event_sync();
  1663. mouse_button_state = button_state;
  1664. }
  1665. static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
  1666. {
  1667. int size = qdict_get_int(qdict, "size");
  1668. int addr = qdict_get_int(qdict, "addr");
  1669. int has_index = qdict_haskey(qdict, "index");
  1670. uint32_t val;
  1671. int suffix;
  1672. if (has_index) {
  1673. int index = qdict_get_int(qdict, "index");
  1674. cpu_outb(addr & IOPORTS_MASK, index & 0xff);
  1675. addr++;
  1676. }
  1677. addr &= 0xffff;
  1678. switch(size) {
  1679. default:
  1680. case 1:
  1681. val = cpu_inb(addr);
  1682. suffix = 'b';
  1683. break;
  1684. case 2:
  1685. val = cpu_inw(addr);
  1686. suffix = 'w';
  1687. break;
  1688. case 4:
  1689. val = cpu_inl(addr);
  1690. suffix = 'l';
  1691. break;
  1692. }
  1693. monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
  1694. suffix, addr, size * 2, val);
  1695. }
  1696. static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
  1697. {
  1698. int size = qdict_get_int(qdict, "size");
  1699. int addr = qdict_get_int(qdict, "addr");
  1700. int val = qdict_get_int(qdict, "val");
  1701. addr &= IOPORTS_MASK;
  1702. switch (size) {
  1703. default:
  1704. case 1:
  1705. cpu_outb(addr, val);
  1706. break;
  1707. case 2:
  1708. cpu_outw(addr, val);
  1709. break;
  1710. case 4:
  1711. cpu_outl(addr, val);
  1712. break;
  1713. }
  1714. }
  1715. static void hmp_boot_set(Monitor *mon, const QDict *qdict)
  1716. {
  1717. Error *local_err = NULL;
  1718. const char *bootdevice = qdict_get_str(qdict, "bootdevice");
  1719. qemu_boot_set(bootdevice, &local_err);
  1720. if (local_err) {
  1721. error_report_err(local_err);
  1722. } else {
  1723. monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
  1724. }
  1725. }
  1726. static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
  1727. {
  1728. bool flatview = qdict_get_try_bool(qdict, "flatview", false);
  1729. bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
  1730. mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree);
  1731. }
  1732. static void hmp_info_numa(Monitor *mon, const QDict *qdict)
  1733. {
  1734. int i;
  1735. NumaNodeMem *node_mem;
  1736. CpuInfoList *cpu_list, *cpu;
  1737. cpu_list = qmp_query_cpus(&error_abort);
  1738. node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
  1739. query_numa_node_mem(node_mem);
  1740. monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
  1741. for (i = 0; i < nb_numa_nodes; i++) {
  1742. monitor_printf(mon, "node %d cpus:", i);
  1743. for (cpu = cpu_list; cpu; cpu = cpu->next) {
  1744. if (cpu->value->has_props && cpu->value->props->has_node_id &&
  1745. cpu->value->props->node_id == i) {
  1746. monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
  1747. }
  1748. }
  1749. monitor_printf(mon, "\n");
  1750. monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
  1751. node_mem[i].node_mem >> 20);
  1752. monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
  1753. node_mem[i].node_plugged_mem >> 20);
  1754. }
  1755. qapi_free_CpuInfoList(cpu_list);
  1756. g_free(node_mem);
  1757. }
  1758. #ifdef CONFIG_PROFILER
  1759. int64_t tcg_time;
  1760. int64_t dev_time;
  1761. static void hmp_info_profile(Monitor *mon, const QDict *qdict)
  1762. {
  1763. monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
  1764. dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
  1765. monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
  1766. tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
  1767. tcg_time = 0;
  1768. dev_time = 0;
  1769. }
  1770. #else
  1771. static void hmp_info_profile(Monitor *mon, const QDict *qdict)
  1772. {
  1773. monitor_printf(mon, "Internal profiler not compiled\n");
  1774. }
  1775. #endif
  1776. /* Capture support */
  1777. static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
  1778. static void hmp_info_capture(Monitor *mon, const QDict *qdict)
  1779. {
  1780. int i;
  1781. CaptureState *s;
  1782. for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
  1783. monitor_printf(mon, "[%d]: ", i);
  1784. s->ops.info (s->opaque);
  1785. }
  1786. }
  1787. static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
  1788. {
  1789. int i;
  1790. int n = qdict_get_int(qdict, "n");
  1791. CaptureState *s;
  1792. for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
  1793. if (i == n) {
  1794. s->ops.destroy (s->opaque);
  1795. QLIST_REMOVE (s, entries);
  1796. g_free (s);
  1797. return;
  1798. }
  1799. }
  1800. }
  1801. static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
  1802. {
  1803. const char *path = qdict_get_str(qdict, "path");
  1804. int has_freq = qdict_haskey(qdict, "freq");
  1805. int freq = qdict_get_try_int(qdict, "freq", -1);
  1806. int has_bits = qdict_haskey(qdict, "bits");
  1807. int bits = qdict_get_try_int(qdict, "bits", -1);
  1808. int has_channels = qdict_haskey(qdict, "nchannels");
  1809. int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
  1810. CaptureState *s;
  1811. s = g_malloc0 (sizeof (*s));
  1812. freq = has_freq ? freq : 44100;
  1813. bits = has_bits ? bits : 16;
  1814. nchannels = has_channels ? nchannels : 2;
  1815. if (wav_start_capture (s, path, freq, bits, nchannels)) {
  1816. monitor_printf(mon, "Failed to add wave capture\n");
  1817. g_free (s);
  1818. return;
  1819. }
  1820. QLIST_INSERT_HEAD (&capture_head, s, entries);
  1821. }
  1822. static qemu_acl *find_acl(Monitor *mon, const char *name)
  1823. {
  1824. qemu_acl *acl = qemu_acl_find(name);
  1825. if (!acl) {
  1826. monitor_printf(mon, "acl: unknown list '%s'\n", name);
  1827. }
  1828. return acl;
  1829. }
  1830. static void hmp_acl_show(Monitor *mon, const QDict *qdict)
  1831. {
  1832. const char *aclname = qdict_get_str(qdict, "aclname");
  1833. qemu_acl *acl = find_acl(mon, aclname);
  1834. qemu_acl_entry *entry;
  1835. int i = 0;
  1836. if (acl) {
  1837. monitor_printf(mon, "policy: %s\n",
  1838. acl->defaultDeny ? "deny" : "allow");
  1839. QTAILQ_FOREACH(entry, &acl->entries, next) {
  1840. i++;
  1841. monitor_printf(mon, "%d: %s %s\n", i,
  1842. entry->deny ? "deny" : "allow", entry->match);
  1843. }
  1844. }
  1845. }
  1846. static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
  1847. {
  1848. const char *aclname = qdict_get_str(qdict, "aclname");
  1849. qemu_acl *acl = find_acl(mon, aclname);
  1850. if (acl) {
  1851. qemu_acl_reset(acl);
  1852. monitor_printf(mon, "acl: removed all rules\n");
  1853. }
  1854. }
  1855. static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
  1856. {
  1857. const char *aclname = qdict_get_str(qdict, "aclname");
  1858. const char *policy = qdict_get_str(qdict, "policy");
  1859. qemu_acl *acl = find_acl(mon, aclname);
  1860. if (acl) {
  1861. if (strcmp(policy, "allow") == 0) {
  1862. acl->defaultDeny = 0;
  1863. monitor_printf(mon, "acl: policy set to 'allow'\n");
  1864. } else if (strcmp(policy, "deny") == 0) {
  1865. acl->defaultDeny = 1;
  1866. monitor_printf(mon, "acl: policy set to 'deny'\n");
  1867. } else {
  1868. monitor_printf(mon, "acl: unknown policy '%s', "
  1869. "expected 'deny' or 'allow'\n", policy);
  1870. }
  1871. }
  1872. }
  1873. static void hmp_acl_add(Monitor *mon, const QDict *qdict)
  1874. {
  1875. const char *aclname = qdict_get_str(qdict, "aclname");
  1876. const char *match = qdict_get_str(qdict, "match");
  1877. const char *policy = qdict_get_str(qdict, "policy");
  1878. int has_index = qdict_haskey(qdict, "index");
  1879. int index = qdict_get_try_int(qdict, "index", -1);
  1880. qemu_acl *acl = find_acl(mon, aclname);
  1881. int deny, ret;
  1882. if (acl) {
  1883. if (strcmp(policy, "allow") == 0) {
  1884. deny = 0;
  1885. } else if (strcmp(policy, "deny") == 0) {
  1886. deny = 1;
  1887. } else {
  1888. monitor_printf(mon, "acl: unknown policy '%s', "
  1889. "expected 'deny' or 'allow'\n", policy);
  1890. return;
  1891. }
  1892. if (has_index)
  1893. ret = qemu_acl_insert(acl, deny, match, index);
  1894. else
  1895. ret = qemu_acl_append(acl, deny, match);
  1896. if (ret < 0)
  1897. monitor_printf(mon, "acl: unable to add acl entry\n");
  1898. else
  1899. monitor_printf(mon, "acl: added rule at position %d\n", ret);
  1900. }
  1901. }
  1902. static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
  1903. {
  1904. const char *aclname = qdict_get_str(qdict, "aclname");
  1905. const char *match = qdict_get_str(qdict, "match");
  1906. qemu_acl *acl = find_acl(mon, aclname);
  1907. int ret;
  1908. if (acl) {
  1909. ret = qemu_acl_remove(acl, match);
  1910. if (ret < 0)
  1911. monitor_printf(mon, "acl: no matching acl entry\n");
  1912. else
  1913. monitor_printf(mon, "acl: removed rule at position %d\n", ret);
  1914. }
  1915. }
  1916. void qmp_getfd(const char *fdname, Error **errp)
  1917. {
  1918. mon_fd_t *monfd;
  1919. int fd;
  1920. fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
  1921. if (fd == -1) {
  1922. error_setg(errp, QERR_FD_NOT_SUPPLIED);
  1923. return;
  1924. }
  1925. if (qemu_isdigit(fdname[0])) {
  1926. close(fd);
  1927. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
  1928. "a name not starting with a digit");
  1929. return;
  1930. }
  1931. QLIST_FOREACH(monfd, &cur_mon->fds, next) {
  1932. if (strcmp(monfd->name, fdname) != 0) {
  1933. continue;
  1934. }
  1935. close(monfd->fd);
  1936. monfd->fd = fd;
  1937. return;
  1938. }
  1939. monfd = g_malloc0(sizeof(mon_fd_t));
  1940. monfd->name = g_strdup(fdname);
  1941. monfd->fd = fd;
  1942. QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
  1943. }
  1944. void qmp_closefd(const char *fdname, Error **errp)
  1945. {
  1946. mon_fd_t *monfd;
  1947. QLIST_FOREACH(monfd, &cur_mon->fds, next) {
  1948. if (strcmp(monfd->name, fdname) != 0) {
  1949. continue;
  1950. }
  1951. QLIST_REMOVE(monfd, next);
  1952. close(monfd->fd);
  1953. g_free(monfd->name);
  1954. g_free(monfd);
  1955. return;
  1956. }
  1957. error_setg(errp, QERR_FD_NOT_FOUND, fdname);
  1958. }
  1959. int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
  1960. {
  1961. mon_fd_t *monfd;
  1962. QLIST_FOREACH(monfd, &mon->fds, next) {
  1963. int fd;
  1964. if (strcmp(monfd->name, fdname) != 0) {
  1965. continue;
  1966. }
  1967. fd = monfd->fd;
  1968. /* caller takes ownership of fd */
  1969. QLIST_REMOVE(monfd, next);
  1970. g_free(monfd->name);
  1971. g_free(monfd);
  1972. return fd;
  1973. }
  1974. error_setg(errp, "File descriptor named '%s' has not been found", fdname);
  1975. return -1;
  1976. }
  1977. static void monitor_fdset_cleanup(MonFdset *mon_fdset)
  1978. {
  1979. MonFdsetFd *mon_fdset_fd;
  1980. MonFdsetFd *mon_fdset_fd_next;
  1981. QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
  1982. if ((mon_fdset_fd->removed ||
  1983. (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
  1984. runstate_is_running()) {
  1985. close(mon_fdset_fd->fd);
  1986. g_free(mon_fdset_fd->opaque);
  1987. QLIST_REMOVE(mon_fdset_fd, next);
  1988. g_free(mon_fdset_fd);
  1989. }
  1990. }
  1991. if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
  1992. QLIST_REMOVE(mon_fdset, next);
  1993. g_free(mon_fdset);
  1994. }
  1995. }
  1996. static void monitor_fdsets_cleanup(void)
  1997. {
  1998. MonFdset *mon_fdset;
  1999. MonFdset *mon_fdset_next;
  2000. QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
  2001. monitor_fdset_cleanup(mon_fdset);
  2002. }
  2003. }
  2004. AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
  2005. const char *opaque, Error **errp)
  2006. {
  2007. int fd;
  2008. Monitor *mon = cur_mon;
  2009. AddfdInfo *fdinfo;
  2010. fd = qemu_chr_fe_get_msgfd(&mon->chr);
  2011. if (fd == -1) {
  2012. error_setg(errp, QERR_FD_NOT_SUPPLIED);
  2013. goto error;
  2014. }
  2015. fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
  2016. has_opaque, opaque, errp);
  2017. if (fdinfo) {
  2018. return fdinfo;
  2019. }
  2020. error:
  2021. if (fd != -1) {
  2022. close(fd);
  2023. }
  2024. return NULL;
  2025. }
  2026. void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
  2027. {
  2028. MonFdset *mon_fdset;
  2029. MonFdsetFd *mon_fdset_fd;
  2030. char fd_str[60];
  2031. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2032. if (mon_fdset->id != fdset_id) {
  2033. continue;
  2034. }
  2035. QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
  2036. if (has_fd) {
  2037. if (mon_fdset_fd->fd != fd) {
  2038. continue;
  2039. }
  2040. mon_fdset_fd->removed = true;
  2041. break;
  2042. } else {
  2043. mon_fdset_fd->removed = true;
  2044. }
  2045. }
  2046. if (has_fd && !mon_fdset_fd) {
  2047. goto error;
  2048. }
  2049. monitor_fdset_cleanup(mon_fdset);
  2050. return;
  2051. }
  2052. error:
  2053. if (has_fd) {
  2054. snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
  2055. fdset_id, fd);
  2056. } else {
  2057. snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
  2058. }
  2059. error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
  2060. }
  2061. FdsetInfoList *qmp_query_fdsets(Error **errp)
  2062. {
  2063. MonFdset *mon_fdset;
  2064. MonFdsetFd *mon_fdset_fd;
  2065. FdsetInfoList *fdset_list = NULL;
  2066. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2067. FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
  2068. FdsetFdInfoList *fdsetfd_list = NULL;
  2069. fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
  2070. fdset_info->value->fdset_id = mon_fdset->id;
  2071. QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
  2072. FdsetFdInfoList *fdsetfd_info;
  2073. fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
  2074. fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
  2075. fdsetfd_info->value->fd = mon_fdset_fd->fd;
  2076. if (mon_fdset_fd->opaque) {
  2077. fdsetfd_info->value->has_opaque = true;
  2078. fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
  2079. } else {
  2080. fdsetfd_info->value->has_opaque = false;
  2081. }
  2082. fdsetfd_info->next = fdsetfd_list;
  2083. fdsetfd_list = fdsetfd_info;
  2084. }
  2085. fdset_info->value->fds = fdsetfd_list;
  2086. fdset_info->next = fdset_list;
  2087. fdset_list = fdset_info;
  2088. }
  2089. return fdset_list;
  2090. }
  2091. AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
  2092. bool has_opaque, const char *opaque,
  2093. Error **errp)
  2094. {
  2095. MonFdset *mon_fdset = NULL;
  2096. MonFdsetFd *mon_fdset_fd;
  2097. AddfdInfo *fdinfo;
  2098. if (has_fdset_id) {
  2099. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2100. /* Break if match found or match impossible due to ordering by ID */
  2101. if (fdset_id <= mon_fdset->id) {
  2102. if (fdset_id < mon_fdset->id) {
  2103. mon_fdset = NULL;
  2104. }
  2105. break;
  2106. }
  2107. }
  2108. }
  2109. if (mon_fdset == NULL) {
  2110. int64_t fdset_id_prev = -1;
  2111. MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
  2112. if (has_fdset_id) {
  2113. if (fdset_id < 0) {
  2114. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
  2115. "a non-negative value");
  2116. return NULL;
  2117. }
  2118. /* Use specified fdset ID */
  2119. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2120. mon_fdset_cur = mon_fdset;
  2121. if (fdset_id < mon_fdset_cur->id) {
  2122. break;
  2123. }
  2124. }
  2125. } else {
  2126. /* Use first available fdset ID */
  2127. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2128. mon_fdset_cur = mon_fdset;
  2129. if (fdset_id_prev == mon_fdset_cur->id - 1) {
  2130. fdset_id_prev = mon_fdset_cur->id;
  2131. continue;
  2132. }
  2133. break;
  2134. }
  2135. }
  2136. mon_fdset = g_malloc0(sizeof(*mon_fdset));
  2137. if (has_fdset_id) {
  2138. mon_fdset->id = fdset_id;
  2139. } else {
  2140. mon_fdset->id = fdset_id_prev + 1;
  2141. }
  2142. /* The fdset list is ordered by fdset ID */
  2143. if (!mon_fdset_cur) {
  2144. QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
  2145. } else if (mon_fdset->id < mon_fdset_cur->id) {
  2146. QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
  2147. } else {
  2148. QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
  2149. }
  2150. }
  2151. mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
  2152. mon_fdset_fd->fd = fd;
  2153. mon_fdset_fd->removed = false;
  2154. if (has_opaque) {
  2155. mon_fdset_fd->opaque = g_strdup(opaque);
  2156. }
  2157. QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
  2158. fdinfo = g_malloc0(sizeof(*fdinfo));
  2159. fdinfo->fdset_id = mon_fdset->id;
  2160. fdinfo->fd = mon_fdset_fd->fd;
  2161. return fdinfo;
  2162. }
  2163. int monitor_fdset_get_fd(int64_t fdset_id, int flags)
  2164. {
  2165. #ifndef _WIN32
  2166. MonFdset *mon_fdset;
  2167. MonFdsetFd *mon_fdset_fd;
  2168. int mon_fd_flags;
  2169. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2170. if (mon_fdset->id != fdset_id) {
  2171. continue;
  2172. }
  2173. QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
  2174. mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
  2175. if (mon_fd_flags == -1) {
  2176. return -1;
  2177. }
  2178. if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
  2179. return mon_fdset_fd->fd;
  2180. }
  2181. }
  2182. errno = EACCES;
  2183. return -1;
  2184. }
  2185. #endif
  2186. errno = ENOENT;
  2187. return -1;
  2188. }
  2189. int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
  2190. {
  2191. MonFdset *mon_fdset;
  2192. MonFdsetFd *mon_fdset_fd_dup;
  2193. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2194. if (mon_fdset->id != fdset_id) {
  2195. continue;
  2196. }
  2197. QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
  2198. if (mon_fdset_fd_dup->fd == dup_fd) {
  2199. return -1;
  2200. }
  2201. }
  2202. mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
  2203. mon_fdset_fd_dup->fd = dup_fd;
  2204. QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
  2205. return 0;
  2206. }
  2207. return -1;
  2208. }
  2209. static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
  2210. {
  2211. MonFdset *mon_fdset;
  2212. MonFdsetFd *mon_fdset_fd_dup;
  2213. QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
  2214. QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
  2215. if (mon_fdset_fd_dup->fd == dup_fd) {
  2216. if (remove) {
  2217. QLIST_REMOVE(mon_fdset_fd_dup, next);
  2218. if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
  2219. monitor_fdset_cleanup(mon_fdset);
  2220. }
  2221. return -1;
  2222. } else {
  2223. return mon_fdset->id;
  2224. }
  2225. }
  2226. }
  2227. }
  2228. return -1;
  2229. }
  2230. int monitor_fdset_dup_fd_find(int dup_fd)
  2231. {
  2232. return monitor_fdset_dup_fd_find_remove(dup_fd, false);
  2233. }
  2234. void monitor_fdset_dup_fd_remove(int dup_fd)
  2235. {
  2236. monitor_fdset_dup_fd_find_remove(dup_fd, true);
  2237. }
  2238. int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
  2239. {
  2240. int fd;
  2241. Error *local_err = NULL;
  2242. if (!qemu_isdigit(fdname[0]) && mon) {
  2243. fd = monitor_get_fd(mon, fdname, &local_err);
  2244. } else {
  2245. fd = qemu_parse_fd(fdname);
  2246. if (fd == -1) {
  2247. error_setg(&local_err, "Invalid file descriptor number '%s'",
  2248. fdname);
  2249. }
  2250. }
  2251. if (local_err) {
  2252. error_propagate(errp, local_err);
  2253. assert(fd == -1);
  2254. } else {
  2255. assert(fd != -1);
  2256. }
  2257. return fd;
  2258. }
  2259. /* Please update hmp-commands.hx when adding or changing commands */
  2260. static mon_cmd_t info_cmds[] = {
  2261. #include "hmp-commands-info.h"
  2262. { NULL, NULL, },
  2263. };
  2264. /* mon_cmds and info_cmds would be sorted at runtime */
  2265. static mon_cmd_t mon_cmds[] = {
  2266. #include "hmp-commands.h"
  2267. { NULL, NULL, },
  2268. };
  2269. /*******************************************************************/
  2270. static const char *pch;
  2271. static sigjmp_buf expr_env;
  2272. static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
  2273. expr_error(Monitor *mon, const char *fmt, ...)
  2274. {
  2275. va_list ap;
  2276. va_start(ap, fmt);
  2277. monitor_vprintf(mon, fmt, ap);
  2278. monitor_printf(mon, "\n");
  2279. va_end(ap);
  2280. siglongjmp(expr_env, 1);
  2281. }
  2282. /* return 0 if OK, -1 if not found */
  2283. static int get_monitor_def(target_long *pval, const char *name)
  2284. {
  2285. const MonitorDef *md = target_monitor_defs();
  2286. CPUState *cs = mon_get_cpu();
  2287. void *ptr;
  2288. uint64_t tmp = 0;
  2289. int ret;
  2290. if (cs == NULL || md == NULL) {
  2291. return -1;
  2292. }
  2293. for(; md->name != NULL; md++) {
  2294. if (compare_cmd(name, md->name)) {
  2295. if (md->get_value) {
  2296. *pval = md->get_value(md, md->offset);
  2297. } else {
  2298. CPUArchState *env = mon_get_cpu_env();
  2299. ptr = (uint8_t *)env + md->offset;
  2300. switch(md->type) {
  2301. case MD_I32:
  2302. *pval = *(int32_t *)ptr;
  2303. break;
  2304. case MD_TLONG:
  2305. *pval = *(target_long *)ptr;
  2306. break;
  2307. default:
  2308. *pval = 0;
  2309. break;
  2310. }
  2311. }
  2312. return 0;
  2313. }
  2314. }
  2315. ret = target_get_monitor_def(cs, name, &tmp);
  2316. if (!ret) {
  2317. *pval = (target_long) tmp;
  2318. }
  2319. return ret;
  2320. }
  2321. static void next(void)
  2322. {
  2323. if (*pch != '\0') {
  2324. pch++;
  2325. while (qemu_isspace(*pch))
  2326. pch++;
  2327. }
  2328. }
  2329. static int64_t expr_sum(Monitor *mon);
  2330. static int64_t expr_unary(Monitor *mon)
  2331. {
  2332. int64_t n;
  2333. char *p;
  2334. int ret;
  2335. switch(*pch) {
  2336. case '+':
  2337. next();
  2338. n = expr_unary(mon);
  2339. break;
  2340. case '-':
  2341. next();
  2342. n = -expr_unary(mon);
  2343. break;
  2344. case '~':
  2345. next();
  2346. n = ~expr_unary(mon);
  2347. break;
  2348. case '(':
  2349. next();
  2350. n = expr_sum(mon);
  2351. if (*pch != ')') {
  2352. expr_error(mon, "')' expected");
  2353. }
  2354. next();
  2355. break;
  2356. case '\'':
  2357. pch++;
  2358. if (*pch == '\0')
  2359. expr_error(mon, "character constant expected");
  2360. n = *pch;
  2361. pch++;
  2362. if (*pch != '\'')
  2363. expr_error(mon, "missing terminating \' character");
  2364. next();
  2365. break;
  2366. case '$':
  2367. {
  2368. char buf[128], *q;
  2369. target_long reg=0;
  2370. pch++;
  2371. q = buf;
  2372. while ((*pch >= 'a' && *pch <= 'z') ||
  2373. (*pch >= 'A' && *pch <= 'Z') ||
  2374. (*pch >= '0' && *pch <= '9') ||
  2375. *pch == '_' || *pch == '.') {
  2376. if ((q - buf) < sizeof(buf) - 1)
  2377. *q++ = *pch;
  2378. pch++;
  2379. }
  2380. while (qemu_isspace(*pch))
  2381. pch++;
  2382. *q = 0;
  2383. ret = get_monitor_def(&reg, buf);
  2384. if (ret < 0)
  2385. expr_error(mon, "unknown register");
  2386. n = reg;
  2387. }
  2388. break;
  2389. case '\0':
  2390. expr_error(mon, "unexpected end of expression");
  2391. n = 0;
  2392. break;
  2393. default:
  2394. errno = 0;
  2395. n = strtoull(pch, &p, 0);
  2396. if (errno == ERANGE) {
  2397. expr_error(mon, "number too large");
  2398. }
  2399. if (pch == p) {
  2400. expr_error(mon, "invalid char '%c' in expression", *p);
  2401. }
  2402. pch = p;
  2403. while (qemu_isspace(*pch))
  2404. pch++;
  2405. break;
  2406. }
  2407. return n;
  2408. }
  2409. static int64_t expr_prod(Monitor *mon)
  2410. {
  2411. int64_t val, val2;
  2412. int op;
  2413. val = expr_unary(mon);
  2414. for(;;) {
  2415. op = *pch;
  2416. if (op != '*' && op != '/' && op != '%')
  2417. break;
  2418. next();
  2419. val2 = expr_unary(mon);
  2420. switch(op) {
  2421. default:
  2422. case '*':
  2423. val *= val2;
  2424. break;
  2425. case '/':
  2426. case '%':
  2427. if (val2 == 0)
  2428. expr_error(mon, "division by zero");
  2429. if (op == '/')
  2430. val /= val2;
  2431. else
  2432. val %= val2;
  2433. break;
  2434. }
  2435. }
  2436. return val;
  2437. }
  2438. static int64_t expr_logic(Monitor *mon)
  2439. {
  2440. int64_t val, val2;
  2441. int op;
  2442. val = expr_prod(mon);
  2443. for(;;) {
  2444. op = *pch;
  2445. if (op != '&' && op != '|' && op != '^')
  2446. break;
  2447. next();
  2448. val2 = expr_prod(mon);
  2449. switch(op) {
  2450. default:
  2451. case '&':
  2452. val &= val2;
  2453. break;
  2454. case '|':
  2455. val |= val2;
  2456. break;
  2457. case '^':
  2458. val ^= val2;
  2459. break;
  2460. }
  2461. }
  2462. return val;
  2463. }
  2464. static int64_t expr_sum(Monitor *mon)
  2465. {
  2466. int64_t val, val2;
  2467. int op;
  2468. val = expr_logic(mon);
  2469. for(;;) {
  2470. op = *pch;
  2471. if (op != '+' && op != '-')
  2472. break;
  2473. next();
  2474. val2 = expr_logic(mon);
  2475. if (op == '+')
  2476. val += val2;
  2477. else
  2478. val -= val2;
  2479. }
  2480. return val;
  2481. }
  2482. static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
  2483. {
  2484. pch = *pp;
  2485. if (sigsetjmp(expr_env, 0)) {
  2486. *pp = pch;
  2487. return -1;
  2488. }
  2489. while (qemu_isspace(*pch))
  2490. pch++;
  2491. *pval = expr_sum(mon);
  2492. *pp = pch;
  2493. return 0;
  2494. }
  2495. static int get_double(Monitor *mon, double *pval, const char **pp)
  2496. {
  2497. const char *p = *pp;
  2498. char *tailp;
  2499. double d;
  2500. d = strtod(p, &tailp);
  2501. if (tailp == p) {
  2502. monitor_printf(mon, "Number expected\n");
  2503. return -1;
  2504. }
  2505. if (d != d || d - d != 0) {
  2506. /* NaN or infinity */
  2507. monitor_printf(mon, "Bad number\n");
  2508. return -1;
  2509. }
  2510. *pval = d;
  2511. *pp = tailp;
  2512. return 0;
  2513. }
  2514. /*
  2515. * Store the command-name in cmdname, and return a pointer to
  2516. * the remaining of the command string.
  2517. */
  2518. static const char *get_command_name(const char *cmdline,
  2519. char *cmdname, size_t nlen)
  2520. {
  2521. size_t len;
  2522. const char *p, *pstart;
  2523. p = cmdline;
  2524. while (qemu_isspace(*p))
  2525. p++;
  2526. if (*p == '\0')
  2527. return NULL;
  2528. pstart = p;
  2529. while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
  2530. p++;
  2531. len = p - pstart;
  2532. if (len > nlen - 1)
  2533. len = nlen - 1;
  2534. memcpy(cmdname, pstart, len);
  2535. cmdname[len] = '\0';
  2536. return p;
  2537. }
  2538. /**
  2539. * Read key of 'type' into 'key' and return the current
  2540. * 'type' pointer.
  2541. */
  2542. static char *key_get_info(const char *type, char **key)
  2543. {
  2544. size_t len;
  2545. char *p, *str;
  2546. if (*type == ',')
  2547. type++;
  2548. p = strchr(type, ':');
  2549. if (!p) {
  2550. *key = NULL;
  2551. return NULL;
  2552. }
  2553. len = p - type;
  2554. str = g_malloc(len + 1);
  2555. memcpy(str, type, len);
  2556. str[len] = '\0';
  2557. *key = str;
  2558. return ++p;
  2559. }
  2560. static int default_fmt_format = 'x';
  2561. static int default_fmt_size = 4;
  2562. static int is_valid_option(const char *c, const char *typestr)
  2563. {
  2564. char option[3];
  2565. option[0] = '-';
  2566. option[1] = *c;
  2567. option[2] = '\0';
  2568. typestr = strstr(typestr, option);
  2569. return (typestr != NULL);
  2570. }
  2571. static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
  2572. const char *cmdname)
  2573. {
  2574. const mon_cmd_t *cmd;
  2575. for (cmd = disp_table; cmd->name != NULL; cmd++) {
  2576. if (compare_cmd(cmdname, cmd->name)) {
  2577. return cmd;
  2578. }
  2579. }
  2580. return NULL;
  2581. }
  2582. /*
  2583. * Parse command name from @cmdp according to command table @table.
  2584. * If blank, return NULL.
  2585. * Else, if no valid command can be found, report to @mon, and return
  2586. * NULL.
  2587. * Else, change @cmdp to point right behind the name, and return its
  2588. * command table entry.
  2589. * Do not assume the return value points into @table! It doesn't when
  2590. * the command is found in a sub-command table.
  2591. */
  2592. static const mon_cmd_t *monitor_parse_command(Monitor *mon,
  2593. const char *cmdp_start,
  2594. const char **cmdp,
  2595. mon_cmd_t *table)
  2596. {
  2597. const char *p;
  2598. const mon_cmd_t *cmd;
  2599. char cmdname[256];
  2600. /* extract the command name */
  2601. p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
  2602. if (!p)
  2603. return NULL;
  2604. cmd = search_dispatch_table(table, cmdname);
  2605. if (!cmd) {
  2606. monitor_printf(mon, "unknown command: '%.*s'\n",
  2607. (int)(p - cmdp_start), cmdp_start);
  2608. return NULL;
  2609. }
  2610. /* filter out following useless space */
  2611. while (qemu_isspace(*p)) {
  2612. p++;
  2613. }
  2614. *cmdp = p;
  2615. /* search sub command */
  2616. if (cmd->sub_table != NULL && *p != '\0') {
  2617. return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
  2618. }
  2619. return cmd;
  2620. }
  2621. /*
  2622. * Parse arguments for @cmd.
  2623. * If it can't be parsed, report to @mon, and return NULL.
  2624. * Else, insert command arguments into a QDict, and return it.
  2625. * Note: On success, caller has to free the QDict structure.
  2626. */
  2627. static QDict *monitor_parse_arguments(Monitor *mon,
  2628. const char **endp,
  2629. const mon_cmd_t *cmd)
  2630. {
  2631. const char *typestr;
  2632. char *key;
  2633. int c;
  2634. const char *p = *endp;
  2635. char buf[1024];
  2636. QDict *qdict = qdict_new();
  2637. /* parse the parameters */
  2638. typestr = cmd->args_type;
  2639. for(;;) {
  2640. typestr = key_get_info(typestr, &key);
  2641. if (!typestr)
  2642. break;
  2643. c = *typestr;
  2644. typestr++;
  2645. switch(c) {
  2646. case 'F':
  2647. case 'B':
  2648. case 's':
  2649. {
  2650. int ret;
  2651. while (qemu_isspace(*p))
  2652. p++;
  2653. if (*typestr == '?') {
  2654. typestr++;
  2655. if (*p == '\0') {
  2656. /* no optional string: NULL argument */
  2657. break;
  2658. }
  2659. }
  2660. ret = get_str(buf, sizeof(buf), &p);
  2661. if (ret < 0) {
  2662. switch(c) {
  2663. case 'F':
  2664. monitor_printf(mon, "%s: filename expected\n",
  2665. cmd->name);
  2666. break;
  2667. case 'B':
  2668. monitor_printf(mon, "%s: block device name expected\n",
  2669. cmd->name);
  2670. break;
  2671. default:
  2672. monitor_printf(mon, "%s: string expected\n", cmd->name);
  2673. break;
  2674. }
  2675. goto fail;
  2676. }
  2677. qdict_put_str(qdict, key, buf);
  2678. }
  2679. break;
  2680. case 'O':
  2681. {
  2682. QemuOptsList *opts_list;
  2683. QemuOpts *opts;
  2684. opts_list = qemu_find_opts(key);
  2685. if (!opts_list || opts_list->desc->name) {
  2686. goto bad_type;
  2687. }
  2688. while (qemu_isspace(*p)) {
  2689. p++;
  2690. }
  2691. if (!*p)
  2692. break;
  2693. if (get_str(buf, sizeof(buf), &p) < 0) {
  2694. goto fail;
  2695. }
  2696. opts = qemu_opts_parse_noisily(opts_list, buf, true);
  2697. if (!opts) {
  2698. goto fail;
  2699. }
  2700. qemu_opts_to_qdict(opts, qdict);
  2701. qemu_opts_del(opts);
  2702. }
  2703. break;
  2704. case '/':
  2705. {
  2706. int count, format, size;
  2707. while (qemu_isspace(*p))
  2708. p++;
  2709. if (*p == '/') {
  2710. /* format found */
  2711. p++;
  2712. count = 1;
  2713. if (qemu_isdigit(*p)) {
  2714. count = 0;
  2715. while (qemu_isdigit(*p)) {
  2716. count = count * 10 + (*p - '0');
  2717. p++;
  2718. }
  2719. }
  2720. size = -1;
  2721. format = -1;
  2722. for(;;) {
  2723. switch(*p) {
  2724. case 'o':
  2725. case 'd':
  2726. case 'u':
  2727. case 'x':
  2728. case 'i':
  2729. case 'c':
  2730. format = *p++;
  2731. break;
  2732. case 'b':
  2733. size = 1;
  2734. p++;
  2735. break;
  2736. case 'h':
  2737. size = 2;
  2738. p++;
  2739. break;
  2740. case 'w':
  2741. size = 4;
  2742. p++;
  2743. break;
  2744. case 'g':
  2745. case 'L':
  2746. size = 8;
  2747. p++;
  2748. break;
  2749. default:
  2750. goto next;
  2751. }
  2752. }
  2753. next:
  2754. if (*p != '\0' && !qemu_isspace(*p)) {
  2755. monitor_printf(mon, "invalid char in format: '%c'\n",
  2756. *p);
  2757. goto fail;
  2758. }
  2759. if (format < 0)
  2760. format = default_fmt_format;
  2761. if (format != 'i') {
  2762. /* for 'i', not specifying a size gives -1 as size */
  2763. if (size < 0)
  2764. size = default_fmt_size;
  2765. default_fmt_size = size;
  2766. }
  2767. default_fmt_format = format;
  2768. } else {
  2769. count = 1;
  2770. format = default_fmt_format;
  2771. if (format != 'i') {
  2772. size = default_fmt_size;
  2773. } else {
  2774. size = -1;
  2775. }
  2776. }
  2777. qdict_put_int(qdict, "count", count);
  2778. qdict_put_int(qdict, "format", format);
  2779. qdict_put_int(qdict, "size", size);
  2780. }
  2781. break;
  2782. case 'i':
  2783. case 'l':
  2784. case 'M':
  2785. {
  2786. int64_t val;
  2787. while (qemu_isspace(*p))
  2788. p++;
  2789. if (*typestr == '?' || *typestr == '.') {
  2790. if (*typestr == '?') {
  2791. if (*p == '\0') {
  2792. typestr++;
  2793. break;
  2794. }
  2795. } else {
  2796. if (*p == '.') {
  2797. p++;
  2798. while (qemu_isspace(*p))
  2799. p++;
  2800. } else {
  2801. typestr++;
  2802. break;
  2803. }
  2804. }
  2805. typestr++;
  2806. }
  2807. if (get_expr(mon, &val, &p))
  2808. goto fail;
  2809. /* Check if 'i' is greater than 32-bit */
  2810. if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
  2811. monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
  2812. monitor_printf(mon, "integer is for 32-bit values\n");
  2813. goto fail;
  2814. } else if (c == 'M') {
  2815. if (val < 0) {
  2816. monitor_printf(mon, "enter a positive value\n");
  2817. goto fail;
  2818. }
  2819. val <<= 20;
  2820. }
  2821. qdict_put_int(qdict, key, val);
  2822. }
  2823. break;
  2824. case 'o':
  2825. {
  2826. int ret;
  2827. uint64_t val;
  2828. char *end;
  2829. while (qemu_isspace(*p)) {
  2830. p++;
  2831. }
  2832. if (*typestr == '?') {
  2833. typestr++;
  2834. if (*p == '\0') {
  2835. break;
  2836. }
  2837. }
  2838. ret = qemu_strtosz_MiB(p, &end, &val);
  2839. if (ret < 0 || val > INT64_MAX) {
  2840. monitor_printf(mon, "invalid size\n");
  2841. goto fail;
  2842. }
  2843. qdict_put_int(qdict, key, val);
  2844. p = end;
  2845. }
  2846. break;
  2847. case 'T':
  2848. {
  2849. double val;
  2850. while (qemu_isspace(*p))
  2851. p++;
  2852. if (*typestr == '?') {
  2853. typestr++;
  2854. if (*p == '\0') {
  2855. break;
  2856. }
  2857. }
  2858. if (get_double(mon, &val, &p) < 0) {
  2859. goto fail;
  2860. }
  2861. if (p[0] && p[1] == 's') {
  2862. switch (*p) {
  2863. case 'm':
  2864. val /= 1e3; p += 2; break;
  2865. case 'u':
  2866. val /= 1e6; p += 2; break;
  2867. case 'n':
  2868. val /= 1e9; p += 2; break;
  2869. }
  2870. }
  2871. if (*p && !qemu_isspace(*p)) {
  2872. monitor_printf(mon, "Unknown unit suffix\n");
  2873. goto fail;
  2874. }
  2875. qdict_put(qdict, key, qnum_from_double(val));
  2876. }
  2877. break;
  2878. case 'b':
  2879. {
  2880. const char *beg;
  2881. bool val;
  2882. while (qemu_isspace(*p)) {
  2883. p++;
  2884. }
  2885. beg = p;
  2886. while (qemu_isgraph(*p)) {
  2887. p++;
  2888. }
  2889. if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
  2890. val = true;
  2891. } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
  2892. val = false;
  2893. } else {
  2894. monitor_printf(mon, "Expected 'on' or 'off'\n");
  2895. goto fail;
  2896. }
  2897. qdict_put_bool(qdict, key, val);
  2898. }
  2899. break;
  2900. case '-':
  2901. {
  2902. const char *tmp = p;
  2903. int skip_key = 0;
  2904. /* option */
  2905. c = *typestr++;
  2906. if (c == '\0')
  2907. goto bad_type;
  2908. while (qemu_isspace(*p))
  2909. p++;
  2910. if (*p == '-') {
  2911. p++;
  2912. if(c != *p) {
  2913. if(!is_valid_option(p, typestr)) {
  2914. monitor_printf(mon, "%s: unsupported option -%c\n",
  2915. cmd->name, *p);
  2916. goto fail;
  2917. } else {
  2918. skip_key = 1;
  2919. }
  2920. }
  2921. if(skip_key) {
  2922. p = tmp;
  2923. } else {
  2924. /* has option */
  2925. p++;
  2926. qdict_put_bool(qdict, key, true);
  2927. }
  2928. }
  2929. }
  2930. break;
  2931. case 'S':
  2932. {
  2933. /* package all remaining string */
  2934. int len;
  2935. while (qemu_isspace(*p)) {
  2936. p++;
  2937. }
  2938. if (*typestr == '?') {
  2939. typestr++;
  2940. if (*p == '\0') {
  2941. /* no remaining string: NULL argument */
  2942. break;
  2943. }
  2944. }
  2945. len = strlen(p);
  2946. if (len <= 0) {
  2947. monitor_printf(mon, "%s: string expected\n",
  2948. cmd->name);
  2949. goto fail;
  2950. }
  2951. qdict_put_str(qdict, key, p);
  2952. p += len;
  2953. }
  2954. break;
  2955. default:
  2956. bad_type:
  2957. monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
  2958. goto fail;
  2959. }
  2960. g_free(key);
  2961. key = NULL;
  2962. }
  2963. /* check that all arguments were parsed */
  2964. while (qemu_isspace(*p))
  2965. p++;
  2966. if (*p != '\0') {
  2967. monitor_printf(mon, "%s: extraneous characters at the end of line\n",
  2968. cmd->name);
  2969. goto fail;
  2970. }
  2971. return qdict;
  2972. fail:
  2973. QDECREF(qdict);
  2974. g_free(key);
  2975. return NULL;
  2976. }
  2977. static void handle_hmp_command(Monitor *mon, const char *cmdline)
  2978. {
  2979. QDict *qdict;
  2980. const mon_cmd_t *cmd;
  2981. trace_handle_hmp_command(mon, cmdline);
  2982. cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
  2983. if (!cmd) {
  2984. return;
  2985. }
  2986. qdict = monitor_parse_arguments(mon, &cmdline, cmd);
  2987. if (!qdict) {
  2988. monitor_printf(mon, "Try \"help %s\" for more information\n",
  2989. cmd->name);
  2990. return;
  2991. }
  2992. cmd->cmd(mon, qdict);
  2993. QDECREF(qdict);
  2994. }
  2995. static void cmd_completion(Monitor *mon, const char *name, const char *list)
  2996. {
  2997. const char *p, *pstart;
  2998. char cmd[128];
  2999. int len;
  3000. p = list;
  3001. for(;;) {
  3002. pstart = p;
  3003. p = strchr(p, '|');
  3004. if (!p)
  3005. p = pstart + strlen(pstart);
  3006. len = p - pstart;
  3007. if (len > sizeof(cmd) - 2)
  3008. len = sizeof(cmd) - 2;
  3009. memcpy(cmd, pstart, len);
  3010. cmd[len] = '\0';
  3011. if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
  3012. readline_add_completion(mon->rs, cmd);
  3013. }
  3014. if (*p == '\0')
  3015. break;
  3016. p++;
  3017. }
  3018. }
  3019. static void file_completion(Monitor *mon, const char *input)
  3020. {
  3021. DIR *ffs;
  3022. struct dirent *d;
  3023. char path[1024];
  3024. char file[1024], file_prefix[1024];
  3025. int input_path_len;
  3026. const char *p;
  3027. p = strrchr(input, '/');
  3028. if (!p) {
  3029. input_path_len = 0;
  3030. pstrcpy(file_prefix, sizeof(file_prefix), input);
  3031. pstrcpy(path, sizeof(path), ".");
  3032. } else {
  3033. input_path_len = p - input + 1;
  3034. memcpy(path, input, input_path_len);
  3035. if (input_path_len > sizeof(path) - 1)
  3036. input_path_len = sizeof(path) - 1;
  3037. path[input_path_len] = '\0';
  3038. pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
  3039. }
  3040. ffs = opendir(path);
  3041. if (!ffs)
  3042. return;
  3043. for(;;) {
  3044. struct stat sb;
  3045. d = readdir(ffs);
  3046. if (!d)
  3047. break;
  3048. if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
  3049. continue;
  3050. }
  3051. if (strstart(d->d_name, file_prefix, NULL)) {
  3052. memcpy(file, input, input_path_len);
  3053. if (input_path_len < sizeof(file))
  3054. pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
  3055. d->d_name);
  3056. /* stat the file to find out if it's a directory.
  3057. * In that case add a slash to speed up typing long paths
  3058. */
  3059. if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
  3060. pstrcat(file, sizeof(file), "/");
  3061. }
  3062. readline_add_completion(mon->rs, file);
  3063. }
  3064. }
  3065. closedir(ffs);
  3066. }
  3067. static const char *next_arg_type(const char *typestr)
  3068. {
  3069. const char *p = strchr(typestr, ':');
  3070. return (p != NULL ? ++p : typestr);
  3071. }
  3072. static void add_completion_option(ReadLineState *rs, const char *str,
  3073. const char *option)
  3074. {
  3075. if (!str || !option) {
  3076. return;
  3077. }
  3078. if (!strncmp(option, str, strlen(str))) {
  3079. readline_add_completion(rs, option);
  3080. }
  3081. }
  3082. void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
  3083. {
  3084. size_t len;
  3085. ChardevBackendInfoList *list, *start;
  3086. if (nb_args != 2) {
  3087. return;
  3088. }
  3089. len = strlen(str);
  3090. readline_set_completion_index(rs, len);
  3091. start = list = qmp_query_chardev_backends(NULL);
  3092. while (list) {
  3093. const char *chr_name = list->value->name;
  3094. if (!strncmp(chr_name, str, len)) {
  3095. readline_add_completion(rs, chr_name);
  3096. }
  3097. list = list->next;
  3098. }
  3099. qapi_free_ChardevBackendInfoList(start);
  3100. }
  3101. void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
  3102. {
  3103. size_t len;
  3104. int i;
  3105. if (nb_args != 2) {
  3106. return;
  3107. }
  3108. len = strlen(str);
  3109. readline_set_completion_index(rs, len);
  3110. for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
  3111. add_completion_option(rs, str, NetClientDriver_str(i));
  3112. }
  3113. }
  3114. void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
  3115. {
  3116. GSList *list, *elt;
  3117. size_t len;
  3118. if (nb_args != 2) {
  3119. return;
  3120. }
  3121. len = strlen(str);
  3122. readline_set_completion_index(rs, len);
  3123. list = elt = object_class_get_list(TYPE_DEVICE, false);
  3124. while (elt) {
  3125. const char *name;
  3126. DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
  3127. TYPE_DEVICE);
  3128. name = object_class_get_name(OBJECT_CLASS(dc));
  3129. if (dc->user_creatable
  3130. && !strncmp(name, str, len)) {
  3131. readline_add_completion(rs, name);
  3132. }
  3133. elt = elt->next;
  3134. }
  3135. g_slist_free(list);
  3136. }
  3137. void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
  3138. {
  3139. GSList *list, *elt;
  3140. size_t len;
  3141. if (nb_args != 2) {
  3142. return;
  3143. }
  3144. len = strlen(str);
  3145. readline_set_completion_index(rs, len);
  3146. list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
  3147. while (elt) {
  3148. const char *name;
  3149. name = object_class_get_name(OBJECT_CLASS(elt->data));
  3150. if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
  3151. readline_add_completion(rs, name);
  3152. }
  3153. elt = elt->next;
  3154. }
  3155. g_slist_free(list);
  3156. }
  3157. static void peripheral_device_del_completion(ReadLineState *rs,
  3158. const char *str, size_t len)
  3159. {
  3160. Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
  3161. GSList *list, *item;
  3162. list = qdev_build_hotpluggable_device_list(peripheral);
  3163. if (!list) {
  3164. return;
  3165. }
  3166. for (item = list; item; item = g_slist_next(item)) {
  3167. DeviceState *dev = item->data;
  3168. if (dev->id && !strncmp(str, dev->id, len)) {
  3169. readline_add_completion(rs, dev->id);
  3170. }
  3171. }
  3172. g_slist_free(list);
  3173. }
  3174. void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
  3175. {
  3176. size_t len;
  3177. ChardevInfoList *list, *start;
  3178. if (nb_args != 2) {
  3179. return;
  3180. }
  3181. len = strlen(str);
  3182. readline_set_completion_index(rs, len);
  3183. start = list = qmp_query_chardev(NULL);
  3184. while (list) {
  3185. ChardevInfo *chr = list->value;
  3186. if (!strncmp(chr->label, str, len)) {
  3187. readline_add_completion(rs, chr->label);
  3188. }
  3189. list = list->next;
  3190. }
  3191. qapi_free_ChardevInfoList(start);
  3192. }
  3193. static void ringbuf_completion(ReadLineState *rs, const char *str)
  3194. {
  3195. size_t len;
  3196. ChardevInfoList *list, *start;
  3197. len = strlen(str);
  3198. readline_set_completion_index(rs, len);
  3199. start = list = qmp_query_chardev(NULL);
  3200. while (list) {
  3201. ChardevInfo *chr_info = list->value;
  3202. if (!strncmp(chr_info->label, str, len)) {
  3203. Chardev *chr = qemu_chr_find(chr_info->label);
  3204. if (chr && CHARDEV_IS_RINGBUF(chr)) {
  3205. readline_add_completion(rs, chr_info->label);
  3206. }
  3207. }
  3208. list = list->next;
  3209. }
  3210. qapi_free_ChardevInfoList(start);
  3211. }
  3212. void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
  3213. {
  3214. if (nb_args != 2) {
  3215. return;
  3216. }
  3217. ringbuf_completion(rs, str);
  3218. }
  3219. void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
  3220. {
  3221. size_t len;
  3222. if (nb_args != 2) {
  3223. return;
  3224. }
  3225. len = strlen(str);
  3226. readline_set_completion_index(rs, len);
  3227. peripheral_device_del_completion(rs, str, len);
  3228. }
  3229. void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
  3230. {
  3231. ObjectPropertyInfoList *list, *start;
  3232. size_t len;
  3233. if (nb_args != 2) {
  3234. return;
  3235. }
  3236. len = strlen(str);
  3237. readline_set_completion_index(rs, len);
  3238. start = list = qmp_qom_list("/objects", NULL);
  3239. while (list) {
  3240. ObjectPropertyInfo *info = list->value;
  3241. if (!strncmp(info->type, "child<", 5)
  3242. && !strncmp(info->name, str, len)) {
  3243. readline_add_completion(rs, info->name);
  3244. }
  3245. list = list->next;
  3246. }
  3247. qapi_free_ObjectPropertyInfoList(start);
  3248. }
  3249. void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
  3250. {
  3251. int i;
  3252. char *sep;
  3253. size_t len;
  3254. if (nb_args != 2) {
  3255. return;
  3256. }
  3257. sep = strrchr(str, '-');
  3258. if (sep) {
  3259. str = sep + 1;
  3260. }
  3261. len = strlen(str);
  3262. readline_set_completion_index(rs, len);
  3263. for (i = 0; i < Q_KEY_CODE__MAX; i++) {
  3264. if (!strncmp(str, QKeyCode_str(i), len)) {
  3265. readline_add_completion(rs, QKeyCode_str(i));
  3266. }
  3267. }
  3268. }
  3269. void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
  3270. {
  3271. size_t len;
  3272. len = strlen(str);
  3273. readline_set_completion_index(rs, len);
  3274. if (nb_args == 2) {
  3275. NetClientState *ncs[MAX_QUEUE_NUM];
  3276. int count, i;
  3277. count = qemu_find_net_clients_except(NULL, ncs,
  3278. NET_CLIENT_DRIVER_NONE,
  3279. MAX_QUEUE_NUM);
  3280. for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
  3281. const char *name = ncs[i]->name;
  3282. if (!strncmp(str, name, len)) {
  3283. readline_add_completion(rs, name);
  3284. }
  3285. }
  3286. } else if (nb_args == 3) {
  3287. add_completion_option(rs, str, "on");
  3288. add_completion_option(rs, str, "off");
  3289. }
  3290. }
  3291. void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
  3292. {
  3293. int len, count, i;
  3294. NetClientState *ncs[MAX_QUEUE_NUM];
  3295. if (nb_args != 2) {
  3296. return;
  3297. }
  3298. len = strlen(str);
  3299. readline_set_completion_index(rs, len);
  3300. count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
  3301. MAX_QUEUE_NUM);
  3302. for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
  3303. QemuOpts *opts;
  3304. const char *name = ncs[i]->name;
  3305. if (strncmp(str, name, len)) {
  3306. continue;
  3307. }
  3308. opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
  3309. if (opts) {
  3310. readline_add_completion(rs, name);
  3311. }
  3312. }
  3313. }
  3314. void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
  3315. {
  3316. size_t len;
  3317. len = strlen(str);
  3318. readline_set_completion_index(rs, len);
  3319. if (nb_args == 2) {
  3320. TraceEventIter iter;
  3321. TraceEvent *ev;
  3322. char *pattern = g_strdup_printf("%s*", str);
  3323. trace_event_iter_init(&iter, pattern);
  3324. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  3325. readline_add_completion(rs, trace_event_get_name(ev));
  3326. }
  3327. g_free(pattern);
  3328. }
  3329. }
  3330. void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
  3331. {
  3332. size_t len;
  3333. len = strlen(str);
  3334. readline_set_completion_index(rs, len);
  3335. if (nb_args == 2) {
  3336. TraceEventIter iter;
  3337. TraceEvent *ev;
  3338. char *pattern = g_strdup_printf("%s*", str);
  3339. trace_event_iter_init(&iter, pattern);
  3340. while ((ev = trace_event_iter_next(&iter)) != NULL) {
  3341. readline_add_completion(rs, trace_event_get_name(ev));
  3342. }
  3343. g_free(pattern);
  3344. } else if (nb_args == 3) {
  3345. add_completion_option(rs, str, "on");
  3346. add_completion_option(rs, str, "off");
  3347. }
  3348. }
  3349. void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
  3350. {
  3351. int i;
  3352. if (nb_args != 2) {
  3353. return;
  3354. }
  3355. readline_set_completion_index(rs, strlen(str));
  3356. for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
  3357. add_completion_option(rs, str, WatchdogAction_str(i));
  3358. }
  3359. }
  3360. void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
  3361. const char *str)
  3362. {
  3363. size_t len;
  3364. len = strlen(str);
  3365. readline_set_completion_index(rs, len);
  3366. if (nb_args == 2) {
  3367. int i;
  3368. for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
  3369. const char *name = MigrationCapability_str(i);
  3370. if (!strncmp(str, name, len)) {
  3371. readline_add_completion(rs, name);
  3372. }
  3373. }
  3374. } else if (nb_args == 3) {
  3375. add_completion_option(rs, str, "on");
  3376. add_completion_option(rs, str, "off");
  3377. }
  3378. }
  3379. void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
  3380. const char *str)
  3381. {
  3382. size_t len;
  3383. len = strlen(str);
  3384. readline_set_completion_index(rs, len);
  3385. if (nb_args == 2) {
  3386. int i;
  3387. for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
  3388. const char *name = MigrationParameter_str(i);
  3389. if (!strncmp(str, name, len)) {
  3390. readline_add_completion(rs, name);
  3391. }
  3392. }
  3393. }
  3394. }
  3395. static void vm_completion(ReadLineState *rs, const char *str)
  3396. {
  3397. size_t len;
  3398. BlockDriverState *bs;
  3399. BdrvNextIterator it;
  3400. len = strlen(str);
  3401. readline_set_completion_index(rs, len);
  3402. for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
  3403. SnapshotInfoList *snapshots, *snapshot;
  3404. AioContext *ctx = bdrv_get_aio_context(bs);
  3405. bool ok = false;
  3406. aio_context_acquire(ctx);
  3407. if (bdrv_can_snapshot(bs)) {
  3408. ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
  3409. }
  3410. aio_context_release(ctx);
  3411. if (!ok) {
  3412. continue;
  3413. }
  3414. snapshot = snapshots;
  3415. while (snapshot) {
  3416. char *completion = snapshot->value->name;
  3417. if (!strncmp(str, completion, len)) {
  3418. readline_add_completion(rs, completion);
  3419. }
  3420. completion = snapshot->value->id;
  3421. if (!strncmp(str, completion, len)) {
  3422. readline_add_completion(rs, completion);
  3423. }
  3424. snapshot = snapshot->next;
  3425. }
  3426. qapi_free_SnapshotInfoList(snapshots);
  3427. }
  3428. }
  3429. void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
  3430. {
  3431. if (nb_args == 2) {
  3432. vm_completion(rs, str);
  3433. }
  3434. }
  3435. void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
  3436. {
  3437. if (nb_args == 2) {
  3438. vm_completion(rs, str);
  3439. }
  3440. }
  3441. static void monitor_find_completion_by_table(Monitor *mon,
  3442. const mon_cmd_t *cmd_table,
  3443. char **args,
  3444. int nb_args)
  3445. {
  3446. const char *cmdname;
  3447. int i;
  3448. const char *ptype, *old_ptype, *str, *name;
  3449. const mon_cmd_t *cmd;
  3450. BlockBackend *blk = NULL;
  3451. if (nb_args <= 1) {
  3452. /* command completion */
  3453. if (nb_args == 0)
  3454. cmdname = "";
  3455. else
  3456. cmdname = args[0];
  3457. readline_set_completion_index(mon->rs, strlen(cmdname));
  3458. for (cmd = cmd_table; cmd->name != NULL; cmd++) {
  3459. cmd_completion(mon, cmdname, cmd->name);
  3460. }
  3461. } else {
  3462. /* find the command */
  3463. for (cmd = cmd_table; cmd->name != NULL; cmd++) {
  3464. if (compare_cmd(args[0], cmd->name)) {
  3465. break;
  3466. }
  3467. }
  3468. if (!cmd->name) {
  3469. return;
  3470. }
  3471. if (cmd->sub_table) {
  3472. /* do the job again */
  3473. monitor_find_completion_by_table(mon, cmd->sub_table,
  3474. &args[1], nb_args - 1);
  3475. return;
  3476. }
  3477. if (cmd->command_completion) {
  3478. cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
  3479. return;
  3480. }
  3481. ptype = next_arg_type(cmd->args_type);
  3482. for(i = 0; i < nb_args - 2; i++) {
  3483. if (*ptype != '\0') {
  3484. ptype = next_arg_type(ptype);
  3485. while (*ptype == '?')
  3486. ptype = next_arg_type(ptype);
  3487. }
  3488. }
  3489. str = args[nb_args - 1];
  3490. old_ptype = NULL;
  3491. while (*ptype == '-' && old_ptype != ptype) {
  3492. old_ptype = ptype;
  3493. ptype = next_arg_type(ptype);
  3494. }
  3495. switch(*ptype) {
  3496. case 'F':
  3497. /* file completion */
  3498. readline_set_completion_index(mon->rs, strlen(str));
  3499. file_completion(mon, str);
  3500. break;
  3501. case 'B':
  3502. /* block device name completion */
  3503. readline_set_completion_index(mon->rs, strlen(str));
  3504. while ((blk = blk_next(blk)) != NULL) {
  3505. name = blk_name(blk);
  3506. if (str[0] == '\0' ||
  3507. !strncmp(name, str, strlen(str))) {
  3508. readline_add_completion(mon->rs, name);
  3509. }
  3510. }
  3511. break;
  3512. case 's':
  3513. case 'S':
  3514. if (!strcmp(cmd->name, "help|?")) {
  3515. monitor_find_completion_by_table(mon, cmd_table,
  3516. &args[1], nb_args - 1);
  3517. }
  3518. break;
  3519. default:
  3520. break;
  3521. }
  3522. }
  3523. }
  3524. static void monitor_find_completion(void *opaque,
  3525. const char *cmdline)
  3526. {
  3527. Monitor *mon = opaque;
  3528. char *args[MAX_ARGS];
  3529. int nb_args, len;
  3530. /* 1. parse the cmdline */
  3531. if (parse_cmdline(cmdline, &nb_args, args) < 0) {
  3532. return;
  3533. }
  3534. /* if the line ends with a space, it means we want to complete the
  3535. next arg */
  3536. len = strlen(cmdline);
  3537. if (len > 0 && qemu_isspace(cmdline[len - 1])) {
  3538. if (nb_args >= MAX_ARGS) {
  3539. goto cleanup;
  3540. }
  3541. args[nb_args++] = g_strdup("");
  3542. }
  3543. /* 2. auto complete according to args */
  3544. monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
  3545. cleanup:
  3546. free_cmdline_args(args, nb_args);
  3547. }
  3548. static int monitor_can_read(void *opaque)
  3549. {
  3550. Monitor *mon = opaque;
  3551. return !atomic_mb_read(&mon->suspend_cnt);
  3552. }
  3553. /*
  3554. * 1. This function takes ownership of rsp, err, and id.
  3555. * 2. rsp, err, and id may be NULL.
  3556. * 3. If err != NULL then rsp must be NULL.
  3557. */
  3558. static void monitor_qmp_respond(Monitor *mon, QObject *rsp,
  3559. Error *err, QObject *id)
  3560. {
  3561. if (err) {
  3562. assert(!rsp);
  3563. rsp = QOBJECT(qmp_error_response(err));
  3564. }
  3565. if (rsp) {
  3566. if (id) {
  3567. /* This is for the qdict below. */
  3568. qobject_incref(id);
  3569. qdict_put_obj(qobject_to(QDict, rsp), "id", id);
  3570. }
  3571. monitor_json_emitter(mon, rsp);
  3572. }
  3573. qobject_decref(id);
  3574. qobject_decref(rsp);
  3575. }
  3576. /*
  3577. * Dispatch one single QMP request. The function will free the req_obj
  3578. * and objects inside it before return.
  3579. */
  3580. static void monitor_qmp_dispatch_one(QMPRequest *req_obj)
  3581. {
  3582. Monitor *mon, *old_mon;
  3583. QObject *req, *rsp = NULL, *id;
  3584. bool need_resume;
  3585. req = req_obj->req;
  3586. mon = req_obj->mon;
  3587. id = req_obj->id;
  3588. need_resume = req_obj->need_resume;
  3589. g_free(req_obj);
  3590. if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
  3591. QString *req_json = qobject_to_json(req);
  3592. trace_handle_qmp_command(mon, qstring_get_str(req_json));
  3593. QDECREF(req_json);
  3594. }
  3595. old_mon = cur_mon;
  3596. cur_mon = mon;
  3597. rsp = qmp_dispatch(mon->qmp.commands, req);
  3598. cur_mon = old_mon;
  3599. /* Respond if necessary */
  3600. monitor_qmp_respond(mon, rsp, NULL, id);
  3601. /* This pairs with the monitor_suspend() in handle_qmp_command(). */
  3602. if (need_resume) {
  3603. monitor_resume(mon);
  3604. }
  3605. qobject_decref(req);
  3606. }
  3607. /*
  3608. * Pop one QMP request from monitor queues, return NULL if not found.
  3609. * We are using round-robin fashion to pop the request, to avoid
  3610. * processing commands only on a very busy monitor. To achieve that,
  3611. * when we process one request on a specific monitor, we put that
  3612. * monitor to the end of mon_list queue.
  3613. */
  3614. static QMPRequest *monitor_qmp_requests_pop_one(void)
  3615. {
  3616. QMPRequest *req_obj = NULL;
  3617. Monitor *mon;
  3618. qemu_mutex_lock(&monitor_lock);
  3619. QTAILQ_FOREACH(mon, &mon_list, entry) {
  3620. qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
  3621. req_obj = g_queue_pop_head(mon->qmp.qmp_requests);
  3622. qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
  3623. if (req_obj) {
  3624. break;
  3625. }
  3626. }
  3627. if (req_obj) {
  3628. /*
  3629. * We found one request on the monitor. Degrade this monitor's
  3630. * priority to lowest by re-inserting it to end of queue.
  3631. */
  3632. QTAILQ_REMOVE(&mon_list, mon, entry);
  3633. QTAILQ_INSERT_TAIL(&mon_list, mon, entry);
  3634. }
  3635. qemu_mutex_unlock(&monitor_lock);
  3636. return req_obj;
  3637. }
  3638. static void monitor_qmp_bh_dispatcher(void *data)
  3639. {
  3640. QMPRequest *req_obj = monitor_qmp_requests_pop_one();
  3641. if (req_obj) {
  3642. trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
  3643. monitor_qmp_dispatch_one(req_obj);
  3644. /* Reschedule instead of looping so the main loop stays responsive */
  3645. qemu_bh_schedule(mon_global.qmp_dispatcher_bh);
  3646. }
  3647. }
  3648. #define QMP_REQ_QUEUE_LEN_MAX (8)
  3649. static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
  3650. {
  3651. QObject *req, *id = NULL;
  3652. QDict *qdict = NULL;
  3653. MonitorQMP *mon_qmp = container_of(parser, MonitorQMP, parser);
  3654. Monitor *mon = container_of(mon_qmp, Monitor, qmp);
  3655. Error *err = NULL;
  3656. QMPRequest *req_obj;
  3657. req = json_parser_parse_err(tokens, NULL, &err);
  3658. if (!req && !err) {
  3659. /* json_parser_parse_err() sucks: can fail without setting @err */
  3660. error_setg(&err, QERR_JSON_PARSING);
  3661. }
  3662. if (err) {
  3663. goto err;
  3664. }
  3665. /* Check against the request in general layout */
  3666. qdict = qmp_dispatch_check_obj(req, &err);
  3667. if (!qdict) {
  3668. goto err;
  3669. }
  3670. /* Check against OOB specific */
  3671. if (!qmp_cmd_oob_check(mon, qdict, &err)) {
  3672. goto err;
  3673. }
  3674. id = qdict_get(qdict, "id");
  3675. /* When OOB is enabled, the "id" field is mandatory. */
  3676. if (qmp_oob_enabled(mon) && !id) {
  3677. error_setg(&err, "Out-Of-Band capability requires that "
  3678. "every command contains an 'id' field");
  3679. goto err;
  3680. }
  3681. qobject_incref(id);
  3682. qdict_del(qdict, "id");
  3683. req_obj = g_new0(QMPRequest, 1);
  3684. req_obj->mon = mon;
  3685. req_obj->id = id;
  3686. req_obj->req = req;
  3687. req_obj->need_resume = false;
  3688. if (qmp_is_oob(qdict)) {
  3689. /* Out-Of-Band (OOB) requests are executed directly in parser. */
  3690. trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(req_obj->id)
  3691. ?: "");
  3692. monitor_qmp_dispatch_one(req_obj);
  3693. return;
  3694. }
  3695. /* Protect qmp_requests and fetching its length. */
  3696. qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
  3697. /*
  3698. * If OOB is not enabled on the current monitor, we'll emulate the
  3699. * old behavior that we won't process the current monitor any more
  3700. * until it has responded. This helps make sure that as long as
  3701. * OOB is not enabled, the server will never drop any command.
  3702. */
  3703. if (!qmp_oob_enabled(mon)) {
  3704. monitor_suspend(mon);
  3705. req_obj->need_resume = true;
  3706. } else {
  3707. /* Drop the request if queue is full. */
  3708. if (mon->qmp.qmp_requests->length >= QMP_REQ_QUEUE_LEN_MAX) {
  3709. qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
  3710. qapi_event_send_command_dropped(id,
  3711. COMMAND_DROP_REASON_QUEUE_FULL,
  3712. &error_abort);
  3713. qmp_request_free(req_obj);
  3714. return;
  3715. }
  3716. }
  3717. /*
  3718. * Put the request to the end of queue so that requests will be
  3719. * handled in time order. Ownership for req_obj, req, id,
  3720. * etc. will be delivered to the handler side.
  3721. */
  3722. g_queue_push_tail(mon->qmp.qmp_requests, req_obj);
  3723. qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
  3724. /* Kick the dispatcher routine */
  3725. qemu_bh_schedule(mon_global.qmp_dispatcher_bh);
  3726. return;
  3727. err:
  3728. monitor_qmp_respond(mon, NULL, err, NULL);
  3729. qobject_decref(req);
  3730. }
  3731. static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
  3732. {
  3733. Monitor *mon = opaque;
  3734. json_message_parser_feed(&mon->qmp.parser, (const char *) buf, size);
  3735. }
  3736. static void monitor_read(void *opaque, const uint8_t *buf, int size)
  3737. {
  3738. Monitor *old_mon = cur_mon;
  3739. int i;
  3740. cur_mon = opaque;
  3741. if (cur_mon->rs) {
  3742. for (i = 0; i < size; i++)
  3743. readline_handle_byte(cur_mon->rs, buf[i]);
  3744. } else {
  3745. if (size == 0 || buf[size - 1] != 0)
  3746. monitor_printf(cur_mon, "corrupted command\n");
  3747. else
  3748. handle_hmp_command(cur_mon, (char *)buf);
  3749. }
  3750. cur_mon = old_mon;
  3751. }
  3752. static void monitor_command_cb(void *opaque, const char *cmdline,
  3753. void *readline_opaque)
  3754. {
  3755. Monitor *mon = opaque;
  3756. monitor_suspend(mon);
  3757. handle_hmp_command(mon, cmdline);
  3758. monitor_resume(mon);
  3759. }
  3760. int monitor_suspend(Monitor *mon)
  3761. {
  3762. if (monitor_is_hmp_non_interactive(mon)) {
  3763. return -ENOTTY;
  3764. }
  3765. atomic_inc(&mon->suspend_cnt);
  3766. if (monitor_is_qmp(mon)) {
  3767. /*
  3768. * Kick iothread to make sure this takes effect. It'll be
  3769. * evaluated again in prepare() of the watch object.
  3770. */
  3771. aio_notify(iothread_get_aio_context(mon_global.mon_iothread));
  3772. }
  3773. trace_monitor_suspend(mon, 1);
  3774. return 0;
  3775. }
  3776. void monitor_resume(Monitor *mon)
  3777. {
  3778. if (monitor_is_hmp_non_interactive(mon)) {
  3779. return;
  3780. }
  3781. if (atomic_dec_fetch(&mon->suspend_cnt) == 0) {
  3782. if (monitor_is_qmp(mon)) {
  3783. /*
  3784. * For QMP monitors that are running in IOThread, let's
  3785. * kick the thread in case it's sleeping.
  3786. */
  3787. if (mon->use_io_thr) {
  3788. aio_notify(iothread_get_aio_context(mon_global.mon_iothread));
  3789. }
  3790. } else {
  3791. assert(mon->rs);
  3792. readline_show_prompt(mon->rs);
  3793. }
  3794. }
  3795. trace_monitor_suspend(mon, -1);
  3796. }
  3797. static QObject *get_qmp_greeting(Monitor *mon)
  3798. {
  3799. QList *cap_list = qlist_new();
  3800. QObject *ver = NULL;
  3801. QMPCapability cap;
  3802. qmp_marshal_query_version(NULL, &ver, NULL);
  3803. for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) {
  3804. if (!mon->use_io_thr && cap == QMP_CAPABILITY_OOB) {
  3805. /* Monitors that are not using IOThread won't support OOB */
  3806. continue;
  3807. }
  3808. qlist_append_str(cap_list, QMPCapability_str(cap));
  3809. }
  3810. return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': %p}}",
  3811. ver, cap_list);
  3812. }
  3813. static void monitor_qmp_caps_reset(Monitor *mon)
  3814. {
  3815. memset(mon->qmp.qmp_caps, 0, sizeof(mon->qmp.qmp_caps));
  3816. }
  3817. static void monitor_qmp_event(void *opaque, int event)
  3818. {
  3819. QObject *data;
  3820. Monitor *mon = opaque;
  3821. switch (event) {
  3822. case CHR_EVENT_OPENED:
  3823. mon->qmp.commands = &qmp_cap_negotiation_commands;
  3824. monitor_qmp_caps_reset(mon);
  3825. data = get_qmp_greeting(mon);
  3826. monitor_json_emitter(mon, data);
  3827. qobject_decref(data);
  3828. mon_refcount++;
  3829. break;
  3830. case CHR_EVENT_CLOSED:
  3831. monitor_qmp_cleanup_queues(mon);
  3832. json_message_parser_destroy(&mon->qmp.parser);
  3833. json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
  3834. mon_refcount--;
  3835. monitor_fdsets_cleanup();
  3836. break;
  3837. }
  3838. }
  3839. static void monitor_event(void *opaque, int event)
  3840. {
  3841. Monitor *mon = opaque;
  3842. switch (event) {
  3843. case CHR_EVENT_MUX_IN:
  3844. qemu_mutex_lock(&mon->out_lock);
  3845. mon->mux_out = 0;
  3846. qemu_mutex_unlock(&mon->out_lock);
  3847. if (mon->reset_seen) {
  3848. readline_restart(mon->rs);
  3849. monitor_resume(mon);
  3850. monitor_flush(mon);
  3851. } else {
  3852. atomic_mb_set(&mon->suspend_cnt, 0);
  3853. }
  3854. break;
  3855. case CHR_EVENT_MUX_OUT:
  3856. if (mon->reset_seen) {
  3857. if (atomic_mb_read(&mon->suspend_cnt) == 0) {
  3858. monitor_printf(mon, "\n");
  3859. }
  3860. monitor_flush(mon);
  3861. monitor_suspend(mon);
  3862. } else {
  3863. atomic_inc(&mon->suspend_cnt);
  3864. }
  3865. qemu_mutex_lock(&mon->out_lock);
  3866. mon->mux_out = 1;
  3867. qemu_mutex_unlock(&mon->out_lock);
  3868. break;
  3869. case CHR_EVENT_OPENED:
  3870. monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
  3871. "information\n", QEMU_VERSION);
  3872. if (!mon->mux_out) {
  3873. readline_restart(mon->rs);
  3874. readline_show_prompt(mon->rs);
  3875. }
  3876. mon->reset_seen = 1;
  3877. mon_refcount++;
  3878. break;
  3879. case CHR_EVENT_CLOSED:
  3880. mon_refcount--;
  3881. monitor_fdsets_cleanup();
  3882. break;
  3883. }
  3884. }
  3885. static int
  3886. compare_mon_cmd(const void *a, const void *b)
  3887. {
  3888. return strcmp(((const mon_cmd_t *)a)->name,
  3889. ((const mon_cmd_t *)b)->name);
  3890. }
  3891. static void sortcmdlist(void)
  3892. {
  3893. int array_num;
  3894. int elem_size = sizeof(mon_cmd_t);
  3895. array_num = sizeof(mon_cmds)/elem_size-1;
  3896. qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
  3897. array_num = sizeof(info_cmds)/elem_size-1;
  3898. qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
  3899. }
  3900. static GMainContext *monitor_get_io_context(void)
  3901. {
  3902. return iothread_get_g_main_context(mon_global.mon_iothread);
  3903. }
  3904. static AioContext *monitor_get_aio_context(void)
  3905. {
  3906. return iothread_get_aio_context(mon_global.mon_iothread);
  3907. }
  3908. static void monitor_iothread_init(void)
  3909. {
  3910. mon_global.mon_iothread = iothread_create("mon_iothread",
  3911. &error_abort);
  3912. /*
  3913. * This MUST be on main loop thread since we have commands that
  3914. * have assumption to be run on main loop thread. It would be
  3915. * nice that one day we can remove this assumption in the future.
  3916. */
  3917. mon_global.qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
  3918. monitor_qmp_bh_dispatcher,
  3919. NULL);
  3920. /*
  3921. * Unlike the dispatcher BH, this must be run on the monitor IO
  3922. * thread, so that monitors that are using IO thread will make
  3923. * sure read/write operations are all done on the IO thread.
  3924. */
  3925. mon_global.qmp_respond_bh = aio_bh_new(monitor_get_aio_context(),
  3926. monitor_qmp_bh_responder,
  3927. NULL);
  3928. }
  3929. void monitor_init_globals(void)
  3930. {
  3931. monitor_init_qmp_commands();
  3932. monitor_qapi_event_init();
  3933. sortcmdlist();
  3934. qemu_mutex_init(&monitor_lock);
  3935. monitor_iothread_init();
  3936. }
  3937. /* These functions just adapt the readline interface in a typesafe way. We
  3938. * could cast function pointers but that discards compiler checks.
  3939. */
  3940. static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
  3941. const char *fmt, ...)
  3942. {
  3943. va_list ap;
  3944. va_start(ap, fmt);
  3945. monitor_vprintf(opaque, fmt, ap);
  3946. va_end(ap);
  3947. }
  3948. static void monitor_readline_flush(void *opaque)
  3949. {
  3950. monitor_flush(opaque);
  3951. }
  3952. /*
  3953. * Print to current monitor if we have one, else to stderr.
  3954. * TODO should return int, so callers can calculate width, but that
  3955. * requires surgery to monitor_vprintf(). Left for another day.
  3956. */
  3957. void error_vprintf(const char *fmt, va_list ap)
  3958. {
  3959. if (cur_mon && !monitor_cur_is_qmp()) {
  3960. monitor_vprintf(cur_mon, fmt, ap);
  3961. } else {
  3962. vfprintf(stderr, fmt, ap);
  3963. }
  3964. }
  3965. void error_vprintf_unless_qmp(const char *fmt, va_list ap)
  3966. {
  3967. if (cur_mon && !monitor_cur_is_qmp()) {
  3968. monitor_vprintf(cur_mon, fmt, ap);
  3969. } else if (!cur_mon) {
  3970. vfprintf(stderr, fmt, ap);
  3971. }
  3972. }
  3973. static void monitor_list_append(Monitor *mon)
  3974. {
  3975. qemu_mutex_lock(&monitor_lock);
  3976. QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
  3977. qemu_mutex_unlock(&monitor_lock);
  3978. }
  3979. static void monitor_qmp_setup_handlers_bh(void *opaque)
  3980. {
  3981. Monitor *mon = opaque;
  3982. GMainContext *context;
  3983. if (mon->use_io_thr) {
  3984. /*
  3985. * When use_io_thr is set, we use the global shared dedicated
  3986. * IO thread for this monitor to handle input/output.
  3987. */
  3988. context = monitor_get_io_context();
  3989. /* We should have inited globals before reaching here. */
  3990. assert(context);
  3991. } else {
  3992. /* The default main loop, which is the main thread */
  3993. context = NULL;
  3994. }
  3995. qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
  3996. monitor_qmp_event, NULL, mon, context, true);
  3997. monitor_list_append(mon);
  3998. }
  3999. void monitor_init(Chardev *chr, int flags)
  4000. {
  4001. Monitor *mon = g_malloc(sizeof(*mon));
  4002. bool use_readline = flags & MONITOR_USE_READLINE;
  4003. bool use_oob = flags & MONITOR_USE_OOB;
  4004. if (use_oob) {
  4005. if (CHARDEV_IS_MUX(chr)) {
  4006. error_report("Monitor Out-Of-Band is not supported with "
  4007. "MUX typed chardev backend");
  4008. exit(1);
  4009. }
  4010. if (use_readline) {
  4011. error_report("Monitor Out-Of-band is only supported by QMP");
  4012. exit(1);
  4013. }
  4014. }
  4015. monitor_data_init(mon, false, use_oob);
  4016. qemu_chr_fe_init(&mon->chr, chr, &error_abort);
  4017. mon->flags = flags;
  4018. if (use_readline) {
  4019. mon->rs = readline_init(monitor_readline_printf,
  4020. monitor_readline_flush,
  4021. mon,
  4022. monitor_find_completion);
  4023. monitor_read_command(mon, 0);
  4024. }
  4025. if (monitor_is_qmp(mon)) {
  4026. qemu_chr_fe_set_echo(&mon->chr, true);
  4027. json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
  4028. if (mon->use_io_thr) {
  4029. /*
  4030. * Make sure the old iowatch is gone. It's possible when
  4031. * e.g. the chardev is in client mode, with wait=on.
  4032. */
  4033. remove_fd_in_watch(chr);
  4034. /*
  4035. * We can't call qemu_chr_fe_set_handlers() directly here
  4036. * since during the procedure the chardev will be active
  4037. * and running in monitor iothread, while we'll still do
  4038. * something before returning from it, which is a possible
  4039. * race too. To avoid that, we just create a BH to setup
  4040. * the handlers.
  4041. */
  4042. aio_bh_schedule_oneshot(monitor_get_aio_context(),
  4043. monitor_qmp_setup_handlers_bh, mon);
  4044. /* We'll add this to mon_list in the BH when setup done */
  4045. return;
  4046. } else {
  4047. qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read,
  4048. monitor_qmp_read, monitor_qmp_event,
  4049. NULL, mon, NULL, true);
  4050. }
  4051. } else {
  4052. qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
  4053. monitor_event, NULL, mon, NULL, true);
  4054. }
  4055. monitor_list_append(mon);
  4056. }
  4057. void monitor_cleanup(void)
  4058. {
  4059. Monitor *mon, *next;
  4060. /*
  4061. * We need to explicitly stop the iothread (but not destroy it),
  4062. * cleanup the monitor resources, then destroy the iothread since
  4063. * we need to unregister from chardev below in
  4064. * monitor_data_destroy(), and chardev is not thread-safe yet
  4065. */
  4066. iothread_stop(mon_global.mon_iothread);
  4067. /*
  4068. * After we have IOThread to send responses, it's possible that
  4069. * when we stop the IOThread there are still replies queued in the
  4070. * responder queue. Flush all of them. Note that even after this
  4071. * flush it's still possible that out buffer is not flushed.
  4072. * It'll be done in below monitor_flush() as the last resort.
  4073. */
  4074. monitor_qmp_bh_responder(NULL);
  4075. qemu_mutex_lock(&monitor_lock);
  4076. QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) {
  4077. QTAILQ_REMOVE(&mon_list, mon, entry);
  4078. monitor_flush(mon);
  4079. monitor_data_destroy(mon);
  4080. g_free(mon);
  4081. }
  4082. qemu_mutex_unlock(&monitor_lock);
  4083. /* QEMUBHs needs to be deleted before destroying the IOThread. */
  4084. qemu_bh_delete(mon_global.qmp_dispatcher_bh);
  4085. mon_global.qmp_dispatcher_bh = NULL;
  4086. qemu_bh_delete(mon_global.qmp_respond_bh);
  4087. mon_global.qmp_respond_bh = NULL;
  4088. iothread_destroy(mon_global.mon_iothread);
  4089. mon_global.mon_iothread = NULL;
  4090. }
  4091. QemuOptsList qemu_mon_opts = {
  4092. .name = "mon",
  4093. .implied_opt_name = "chardev",
  4094. .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
  4095. .desc = {
  4096. {
  4097. .name = "mode",
  4098. .type = QEMU_OPT_STRING,
  4099. },{
  4100. .name = "chardev",
  4101. .type = QEMU_OPT_STRING,
  4102. },{
  4103. .name = "pretty",
  4104. .type = QEMU_OPT_BOOL,
  4105. },{
  4106. .name = "x-oob",
  4107. .type = QEMU_OPT_BOOL,
  4108. },
  4109. { /* end of list */ }
  4110. },
  4111. };
  4112. #ifndef TARGET_I386
  4113. void qmp_rtc_reset_reinjection(Error **errp)
  4114. {
  4115. error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
  4116. }
  4117. SevInfo *qmp_query_sev(Error **errp)
  4118. {
  4119. error_setg(errp, QERR_FEATURE_DISABLED, "query-sev");
  4120. return NULL;
  4121. }
  4122. SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp)
  4123. {
  4124. error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure");
  4125. return NULL;
  4126. }
  4127. SevCapability *qmp_query_sev_capabilities(Error **errp)
  4128. {
  4129. error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities");
  4130. return NULL;
  4131. }
  4132. #endif
  4133. #ifndef TARGET_S390X
  4134. void qmp_dump_skeys(const char *filename, Error **errp)
  4135. {
  4136. error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
  4137. }
  4138. #endif
  4139. #ifndef TARGET_ARM
  4140. GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
  4141. {
  4142. error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
  4143. return NULL;
  4144. }
  4145. #endif
  4146. HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
  4147. {
  4148. MachineState *ms = MACHINE(qdev_get_machine());
  4149. MachineClass *mc = MACHINE_GET_CLASS(ms);
  4150. if (!mc->has_hotpluggable_cpus) {
  4151. error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
  4152. return NULL;
  4153. }
  4154. return machine_query_hotpluggable_cpus(ms);
  4155. }