3 # Low-level QEMU shell on top of QMP.
5 # Copyright (C) 2009, 2010 Red Hat Inc.
8 # Luiz Capitulino <lcapitulino@redhat.com>
10 # This work is licensed under the terms of the GNU GPL, version 2. See
11 # the COPYING file in the top-level directory.
17 # # qemu [...] -qmp unix:./qmp-sock,server
21 # $ qmp-shell ./qmp-sock
23 # Commands have the following format:
25 # < command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
29 # (QEMU) device_add driver=e1000 id=net1
33 # key=value pairs also support Python or JSON object literal subset notations,
34 # without spaces. Dictionaries/objects {} are supported as are arrays [].
36 # example-command arg-name1={'key':'value','obj'={'prop':"value"}}
38 # Both JSON and Python formatting should work, including both styles of
39 # string literal quotes. Both paradigms of literal values should work,
40 # including null/true/false for JSON and None/True/False for Python.
43 # Transactions have the following multi-line format:
46 # action-name1 [ arg-name1=arg1 ] ... [arg-nameN=argN ]
48 # action-nameN [ arg-name1=arg1 ] ... [arg-nameN=argN ]
51 # One line transactions are also supported:
53 # transaction( action-name1 ... )
58 # TRANS> block-dirty-bitmap-add node=drive0 name=bitmap1
59 # TRANS> block-dirty-bitmap-clear node=drive0 name=bitmap0
64 # Use the -v and -p options to activate the verbose and pretty-print options,
65 # which will echo back the properly formatted JSON-compliant QMP that is being
66 # sent to QEMU, which is useful for debugging and documentation generation.
75 class QMPCompleter(list):
76 def complete(self
, text
, state
):
78 if cmd
.startswith(text
):
84 class QMPShellError(Exception):
87 class QMPShellBadPort(QMPShellError
):
90 class FuzzyJSON(ast
.NodeTransformer
):
91 '''This extension of ast.NodeTransformer filters literal "true/false/null"
92 values in an AST and replaces them by proper "True/False/None" values that
93 Python can properly evaluate.'''
94 def visit_Name(self
, node
):
97 if node
.id == 'false':
103 # TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
104 # _execute_cmd()). Let's design a better one.
105 class QMPShell(qmp
.QEMUMonitorProtocol
):
106 def __init__(self
, address
, pp
=None):
107 qmp
.QEMUMonitorProtocol
.__init
__(self
, self
.__get
_address
(address
))
108 self
._greeting
= None
109 self
._completer
= None
111 self
._transmode
= False
112 self
._actions
= list()
114 def __get_address(self
, arg
):
116 Figure out if the argument is in the port:host form, if it's not it's
117 probably a file path.
119 addr
= arg
.split(':')
124 raise QMPShellBadPort
125 return ( addr
[0], port
)
129 def _fill_completion(self
):
130 for cmd
in self
.cmd('query-commands')['return']:
131 self
._completer
.append(cmd
['name'])
133 def __completer_setup(self
):
134 self
._completer
= QMPCompleter()
135 self
._fill
_completion
()
136 readline
.set_completer(self
._completer
.complete
)
137 readline
.parse_and_bind("tab: complete")
138 # XXX: default delimiters conflict with some command names (eg. query-),
139 # clearing everything as it doesn't seem to matter
140 readline
.set_completer_delims('')
142 def __parse_value(self
, val
):
148 if val
.lower() == 'true':
150 if val
.lower() == 'false':
152 if val
.startswith(('{', '[')):
153 # Try first as pure JSON:
155 return json
.loads(val
)
158 # Try once again as FuzzyJSON:
160 st
= ast
.parse(val
, mode
='eval')
161 return ast
.literal_eval(FuzzyJSON().visit(st
))
168 def __cli_expr(self
, tokens
, parent
):
170 (key
, _
, val
) = arg
.partition('=')
172 raise QMPShellError("Expected a key=value pair, got '%s'" % arg
)
174 value
= self
.__parse
_value
(val
)
175 optpath
= key
.split('.')
177 for p
in optpath
[:-1]:
179 d
= parent
.get(p
, {})
180 if type(d
) is not dict:
181 raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath
))
184 if optpath
[-1] in parent
:
185 if type(parent
[optpath
[-1]]) is dict:
186 raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath
))
188 raise QMPShellError('Cannot set "%s" multiple times' % key
)
189 parent
[optpath
[-1]] = value
191 def __build_cmd(self
, cmdline
):
193 Build a QMP input object from a user provided command-line in the
196 < command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
198 cmdargs
= cmdline
.split()
200 # Transactional CLI entry/exit:
201 if cmdargs
[0] == 'transaction(':
202 self
._transmode
= True
204 elif cmdargs
[0] == ')' and self
._transmode
:
205 self
._transmode
= False
207 raise QMPShellError("Unexpected input after close of Transaction sub-shell")
208 qmpcmd
= { 'execute': 'transaction',
209 'arguments': { 'actions': self
._actions
} }
210 self
._actions
= list()
213 # Nothing to process?
217 # Parse and then cache this Transactional Action
220 action
= { 'type': cmdargs
[0], 'data': {} }
221 if cmdargs
[-1] == ')':
224 self
.__cli
_expr
(cmdargs
[1:], action
['data'])
225 self
._actions
.append(action
)
226 return self
.__build
_cmd
(')') if finalize
else None
228 # Standard command: parse and return it to be executed.
229 qmpcmd
= { 'execute': cmdargs
[0], 'arguments': {} }
230 self
.__cli
_expr
(cmdargs
[1:], qmpcmd
['arguments'])
233 def _print(self
, qmp
):
234 jsobj
= json
.dumps(qmp
)
235 if self
._pp
is not None:
236 self
._pp
.pprint(jsobj
)
240 def _execute_cmd(self
, cmdline
):
242 qmpcmd
= self
.__build
_cmd
(cmdline
)
244 print 'Error while parsing command line: %s' % e
245 print 'command format: <command-name> ',
246 print '[arg-name1=arg1] ... [arg-nameN=argN]'
248 # For transaction mode, we may have just cached the action:
253 resp
= self
.cmd_obj(qmpcmd
)
261 self
._greeting
= qmp
.QEMUMonitorProtocol
.connect(self
)
262 self
.__completer
_setup
()
264 def show_banner(self
, msg
='Welcome to the QMP low-level shell!'):
266 version
= self
._greeting
['QMP']['version']['qemu']
267 print 'Connected to QEMU %d.%d.%d\n' % (version
['major'],version
['minor'],version
['micro'])
269 def get_prompt(self
):
274 def read_exec_command(self
, prompt
):
276 Read and execute a command.
278 @return True if execution was ok, return False if disconnected.
281 cmdline
= raw_input(prompt
)
286 for ev
in self
.get_events():
291 return self
._execute
_cmd
(cmdline
)
293 def set_verbosity(self
, verbose
):
294 self
._verbose
= verbose
296 class HMPShell(QMPShell
):
297 def __init__(self
, address
):
298 QMPShell
.__init
__(self
, address
)
301 def __cmd_completion(self
):
302 for cmd
in self
.__cmd
_passthrough
('help')['return'].split('\r\n'):
303 if cmd
and cmd
[0] != '[' and cmd
[0] != '\t':
304 name
= cmd
.split()[0] # drop help text
307 if name
.find('|') != -1:
308 # Command in the form 'foobar|f' or 'f|foobar', take the
310 opt
= name
.split('|')
315 self
._completer
.append(name
)
316 self
._completer
.append('help ' + name
) # help completion
318 def __info_completion(self
):
319 for cmd
in self
.__cmd
_passthrough
('info')['return'].split('\r\n'):
321 self
._completer
.append('info ' + cmd
.split()[1])
323 def __other_completion(self
):
325 self
._completer
.append('help info')
327 def _fill_completion(self
):
328 self
.__cmd
_completion
()
329 self
.__info
_completion
()
330 self
.__other
_completion
()
332 def __cmd_passthrough(self
, cmdline
, cpu_index
= 0):
333 return self
.cmd_obj({ 'execute': 'human-monitor-command', 'arguments':
334 { 'command-line': cmdline
,
335 'cpu-index': cpu_index
} })
337 def _execute_cmd(self
, cmdline
):
338 if cmdline
.split()[0] == "cpu":
339 # trap the cpu command, it requires special setting
341 idx
= int(cmdline
.split()[1])
342 if not 'return' in self
.__cmd
_passthrough
('info version', idx
):
343 print 'bad CPU index'
345 self
.__cpu
_index
= idx
347 print 'cpu command takes an integer argument'
349 resp
= self
.__cmd
_passthrough
(cmdline
, self
.__cpu
_index
)
353 assert 'return' in resp
or 'error' in resp
356 if len(resp
['return']) > 0:
357 print resp
['return'],
360 print '%s: %s' % (resp
['error']['class'], resp
['error']['desc'])
363 def show_banner(self
):
364 QMPShell
.show_banner(self
, msg
='Welcome to the HMP shell!')
367 sys
.stderr
.write('ERROR: %s\n' % msg
)
370 def fail_cmdline(option
=None):
372 sys
.stderr
.write('ERROR: bad command-line option \'%s\'\n' % option
)
373 sys
.stderr
.write('qemu-shell [ -v ] [ -p ] [ -H ] < UNIX socket path> | < TCP address:port >\n')
384 for arg
in sys
.argv
[1:]:
392 pp
= pprint
.PrettyPrinter(indent
=4)
401 qemu
= QMPShell(arg
, pp
)
406 except QMPShellBadPort
:
407 die('bad port number in command-line')
411 except qmp
.QMPConnectError
:
412 die('Didn\'t get QMP greeting message')
413 except qmp
.QMPCapabilitiesError
:
414 die('Could not negotiate capabilities')
416 die('Could not connect to %s' % addr
)
419 qemu
.set_verbosity(verbose
)
420 while qemu
.read_exec_command(qemu
.get_prompt()):
424 if __name__
== '__main__':