monitor.c 131 KB

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