-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for consuming QMP callbacks
- Loading branch information
Andrew Fasano
committed
Dec 6, 2023
1 parent
274d6c5
commit 54d08db
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from pandare import Panda | ||
from time import sleep | ||
import json | ||
import tempfile | ||
|
||
path = tempfile.mktemp(".sock") | ||
panda = Panda(generic="x86_64", extra_args=["-qmp", f"unix:{path},server,nowait"]) | ||
|
||
print() | ||
print("QMP example running! To send a QMP command to this VM, run:") | ||
print("""echo '{ "execute": "mycmd", "arguments": { "arg_key" : "arg_val" } }' | socat - UNIX-CONNECT:""" + path + """ | cat""") | ||
print() | ||
|
||
@panda.cb_qmp | ||
def on_qmp_command(cmd, arg_json, result_json): | ||
args = {} | ||
cmd = panda.ffi.string(cmd).decode() if cmd != panda.ffi.NULL else None | ||
if cmd != 'mycmd': | ||
return False | ||
if arg_json != panda.ffi.NULL: | ||
args = json.loads(panda.ffi.string(arg_json).decode()) | ||
|
||
print(f"PyPANDA handling QMP command {cmd} with args {args}") | ||
|
||
data = { | ||
"hello": "world", | ||
"key": "value", | ||
"0": 1, | ||
} | ||
|
||
# Dump our result to json and copy it into the result_json buffer | ||
encoded_data = json.dumps(data).encode() | ||
result_buffer = panda.ffi.new("char[]", len(encoded_data) + 1) # Null term | ||
panda.ffi.memmove(result_buffer, encoded_data, len(encoded_data)) | ||
result_json[0] = result_buffer | ||
return True | ||
|
||
@panda.queue_blocking | ||
def driver(): | ||
panda.revert_sync("root") | ||
panda.run_serial_cmd("whoami") | ||
sleep(300) | ||
panda.end_analysis() | ||
|
||
panda.run() |