monitor.c 112 KB

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