monitor.c 113 KB

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