monitor.c 112 KB

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