How to use hmp method of qemu Package

Best Syzkaller code snippet using qemu.hmp

qmp-shell.go

Source:qmp-shell.go Github

copy

Full Screen

...240 if err != nil {241 return nil, err242 }243 shell.isHMP = true244 shell.prompt = fmt.Sprintf("hmp_shell/%s> ", shell.vmname)245 shell.banner = "Welcome to the HMP low-level shell"246 cmdlist := []string{}247 if s, err := shell.executeCommand("help"); err != nil {248 return nil, fmt.Errorf("cannot build the QMP command list: %s", err)249 } else {250 for _, line := range strings.Split(s, "\r\n") {251 if !(len(line) > 0 && line[0] != '[' && line[0] != '\t') {252 continue253 }254 // Drop arguments and help text255 name := strings.Fields(line)[0]256 if name == "info" {257 continue258 }259 if strings.Index(line, "|") != -1 {260 // Command in the form 'foobar|f' or 'f|foobar',261 // take the full name262 nn := strings.Split(name, "|")263 if len(nn[0]) == 1 {264 name = nn[1]265 } else {266 name = nn[0]267 }268 }269 cmdlist = append(cmdlist, name, "help "+name)270 }271 }272 if s, err := shell.executeCommand("info"); err != nil {273 return nil, fmt.Errorf("cannot build the QMP command list: %s", err)274 } else {275 for _, line := range strings.Split(s, "\r\n") {276 if !(len(line) > 0 && len(strings.Fields(line)) >= 2) {277 continue278 }279 cmdlist = append(cmdlist, "info "+strings.Fields(line)[1])280 }281 }282 sort.Strings(cmdlist)283 shell.line.SetCompleter(func(line string) (c []string) {284 for _, n := range cmdlist {285 if strings.HasPrefix(n, strings.ToLower(line)) {286 c = append(c, n)287 }288 }289 return290 })291 return &HMPShell{*shell}, nil292}293func printUsage() {294 s := fmt.Sprintf("Usage:\n %s [-H] <UNIX socket path>\n\n", filepath.Base(os.Args[0]))295 s += "Options:\n"296 s += " -H run the HMP shell instead QMP\n"297 fmt.Fprintf(os.Stderr, s)298 os.Exit(2)299}300type Shell interface {301 Serve() error302 Execute(string) (string, error)303 LoadHistory(string) error304 SaveHistory(string) error305 Close()306}307func isatty() bool {308 var termios syscall.Termios309 _, _, err := syscall.Syscall6(310 syscall.SYS_IOCTL,311 os.Stdin.Fd(),312 uintptr(syscall.TCGETS),313 uintptr(unsafe.Pointer(&termios)),314 0,315 0,316 0,317 )318 return err == 0319}320func init() {321 flag.Usage = printUsage322}323func main() {324 var hmpMode bool325 flag.BoolVar(&hmpMode, "H", hmpMode, "")326 flag.Parse()327 if flag.NArg() != 1 {328 flag.Usage()329 }330 vmsocket := flag.Arg(0)331 var shell Shell332 var err error333 if hmpMode {334 shell, err = NewHMPShell(vmsocket)335 if err != nil {336 Error.Fatalln(err)337 }338 } else {339 shell, err = NewQMPShell(vmsocket)340 if err != nil {341 Error.Fatalln(err)342 }343 }344 defer shell.Close()345 if !isatty() {346 r := bufio.NewReader(os.Stdin)347 cmdline, err := r.ReadString('\n')348 if err != nil {349 Error.Fatalln("cannot read command from stdin:", err)350 }351 if res, err := shell.Execute(cmdline); err == nil {352 fmt.Println(res)353 } else {354 Error.Fatalln(err)355 }356 os.Exit(0)357 }358 histfile := "/dev/null"359 if homedir, isSet := os.LookupEnv("HOME"); isSet {360 if hmpMode {361 histfile = filepath.Join(homedir, ".hmpshell_history")362 } else {363 histfile = filepath.Join(homedir, ".qmpshell_history")364 }365 }366 // Load history367 if err := shell.LoadHistory(histfile); err != nil {368 Error.Println(err)369 }370 // Main loop371 if err := shell.Serve(); err != nil {372 Error.Println(err)373 }374 // Save history375 if err := shell.SaveHistory(histfile); err != nil {...

Full Screen

Full Screen

qemu.go

Source:qemu.go Github

copy

Full Screen

1// +build !without_qemu2/*3 * This file is part of the libvirt-go project4 *5 * Permission is hereby granted, free of charge, to any person obtaining a copy6 * of this software and associated documentation files (the "Software"), to deal7 * in the Software without restriction, including without limitation the rights8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9 * copies of the Software, and to permit persons to whom the Software is10 * furnished to do so, subject to the following conditions:11 *12 * The above copyright notice and this permission notice shall be included in13 * all copies or substantial portions of the Software.14 *15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN21 * THE SOFTWARE.22 *23 * Copyright (c) 2013 Alex Zorin24 * Copyright (C) 2016 Red Hat, Inc.25 *26 */27package libvirt28/*29#cgo pkg-config: libvirt30// Can't rely on pkg-config for libvirt-qemu since it was not31// installed until 2.6.0 onwards32#cgo LDFLAGS: -lvirt-qemu33#include <stdlib.h>34#include "qemu_wrapper.h"35*/36import "C"37import (38 "unsafe"39)40/*41 * QMP has two different kinds of ways to talk to QEMU. One is legacy (HMP,42 * or 'human' monitor protocol. The default is QMP, which is all-JSON.43 *44 * QMP json commands are of the format:45 * {"execute" : "query-cpus"}46 *47 * whereas the same command in 'HMP' would be:48 * 'info cpus'49 */50type DomainQemuMonitorCommandFlags int51const (52 DOMAIN_QEMU_MONITOR_COMMAND_DEFAULT = DomainQemuMonitorCommandFlags(C.VIR_DOMAIN_QEMU_MONITOR_COMMAND_DEFAULT)53 DOMAIN_QEMU_MONITOR_COMMAND_HMP = DomainQemuMonitorCommandFlags(C.VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP)54)55type DomainQemuAgentCommandTimeout int56const (57 DOMAIN_QEMU_AGENT_COMMAND_MIN = DomainQemuAgentCommandTimeout(C.VIR_DOMAIN_QEMU_AGENT_COMMAND_MIN)58 DOMAIN_QEMU_AGENT_COMMAND_BLOCK = DomainQemuAgentCommandTimeout(C.VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK)59 DOMAIN_QEMU_AGENT_COMMAND_DEFAULT = DomainQemuAgentCommandTimeout(C.VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT)60 DOMAIN_QEMU_AGENT_COMMAND_NOWAIT = DomainQemuAgentCommandTimeout(C.VIR_DOMAIN_QEMU_AGENT_COMMAND_NOWAIT)61 DOMAIN_QEMU_AGENT_COMMAND_SHUTDOWN = DomainQemuAgentCommandTimeout(C.VIR_DOMAIN_QEMU_AGENT_COMMAND_SHUTDOWN)62)63type DomainQemuMonitorEventFlags int64const (65 CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_REGEX = DomainQemuMonitorEventFlags(C.VIR_CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_REGEX)66 CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_NOCASE = DomainQemuMonitorEventFlags(C.VIR_CONNECT_DOMAIN_QEMU_MONITOR_EVENT_REGISTER_NOCASE)67)68func (d *Domain) QemuMonitorCommand(command string, flags DomainQemuMonitorCommandFlags) (string, error) {69 var cResult *C.char70 cCommand := C.CString(command)71 defer C.free(unsafe.Pointer(cCommand))72 var err C.virError73 result := C.virDomainQemuMonitorCommandWrapper(d.ptr, cCommand, &cResult, C.uint(flags), &err)74 if result != 0 {75 return "", makeError(&err)76 }77 rstring := C.GoString(cResult)78 C.free(unsafe.Pointer(cResult))79 return rstring, nil80}81func (d *Domain) QemuAgentCommand(command string, timeout DomainQemuAgentCommandTimeout, flags uint32) (string, error) {82 cCommand := C.CString(command)83 defer C.free(unsafe.Pointer(cCommand))84 var err C.virError85 result := C.virDomainQemuAgentCommandWrapper(d.ptr, cCommand, C.int(timeout), C.uint(flags), &err)86 if result == nil {87 return "", makeError(&err)88 }89 rstring := C.GoString(result)90 C.free(unsafe.Pointer(result))91 return rstring, nil92}93func (c *Connect) DomainQemuAttach(pid uint32, flags uint32) (*Domain, error) {94 var err C.virError95 ptr := C.virDomainQemuAttachWrapper(c.ptr, C.uint(pid), C.uint(flags), &err)96 if ptr == nil {97 return nil, makeError(&err)98 }99 return &Domain{ptr: ptr}, nil100}101type DomainQemuMonitorEvent struct {102 Event string103 Seconds int64104 Micros uint105 Details string106}107type DomainQemuMonitorEventCallback func(c *Connect, d *Domain, event *DomainQemuMonitorEvent)108//export domainQemuMonitorEventCallback109func domainQemuMonitorEventCallback(c C.virConnectPtr, d C.virDomainPtr,110 event *C.char, seconds C.longlong, micros C.uint, details *C.char, goCallbackId int) {111 domain := &Domain{ptr: d}112 connection := &Connect{ptr: c}113 eventDetails := &DomainQemuMonitorEvent{114 Event: C.GoString(event),115 Seconds: int64(seconds),116 Micros: uint(micros),117 Details: C.GoString(details),118 }119 callbackFunc := getCallbackId(goCallbackId)120 callback, ok := callbackFunc.(DomainQemuMonitorEventCallback)121 if !ok {122 panic("Inappropriate callback type called")123 }124 callback(connection, domain, eventDetails)125}126func (c *Connect) DomainQemuMonitorEventRegister(dom *Domain, event string, callback DomainQemuMonitorEventCallback, flags DomainQemuMonitorEventFlags) (int, error) {127 if C.LIBVIR_VERSION_NUMBER < 1002003 {128 return 0, makeNotImplementedError("virConnectDomainQemuMonitorEventRegister")129 }130 cEvent := C.CString(event)131 defer C.free(unsafe.Pointer(cEvent))132 goCallBackId := registerCallbackId(callback)133 var cdom C.virDomainPtr134 if dom != nil {135 cdom = dom.ptr136 }137 var err C.virError138 ret := C.virConnectDomainQemuMonitorEventRegisterWrapper(c.ptr, cdom,139 cEvent,140 C.long(goCallBackId),141 C.uint(flags), &err)142 if ret < 0 {143 freeCallbackId(goCallBackId)144 return 0, makeError(&err)145 }146 return int(ret), nil147}148func (c *Connect) DomainQemuEventDeregister(callbackId int) error {149 if C.LIBVIR_VERSION_NUMBER < 1002003 {150 return makeNotImplementedError("virConnectDomainQemuMonitorEventDeregister")151 }152 // Deregister the callback153 var err C.virError154 ret := int(C.virConnectDomainQemuMonitorEventDeregisterWrapper(c.ptr, C.int(callbackId), &err))155 if ret < 0 {156 return makeError(&err)157 }158 return nil159}...

Full Screen

Full Screen

hmp

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 q, err := qmp.NewSocketMonitor("unix", "/var/run/qemu/vm1.monitor", 2)4 if err != nil {5 panic(err)6 }7 if err := q.Connect(); err != nil {8 panic(err)9 }10 defer q.Disconnect()11 resp, err := q.Run([]byte(`{"execute":"qom-get","arguments":{"path":"machine","property":"max-cpu-count"}}`))12 if err != nil {13 panic(err)14 }15 fmt.Println(string(resp))16}17import (18func main() {19 q, err := qmp.NewSocketMonitor("unix", "/var/run/qemu/vm1.monitor", 2)20 if err != nil {21 panic(err)22 }23 if err := q.Connect(); err != nil {24 panic(err)25 }26 defer q.Disconnect()27 resp, err := q.Run([]byte(`{"execute":"qom-list","arguments":{"path":"machine"}}`))28 if err != nil {29 panic(err)30 }31 fmt.Println(string(resp))32}

Full Screen

Full Screen

hmp

Using AI Code Generation

copy

Full Screen

1func main() {2 cmd := exec.Command("qemu-system-x86_64", "-hmp", "help")3 stdout, err := cmd.StdoutPipe()4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 cmd.Start()9 in := bufio.NewScanner(stdout)10 for in.Scan() {11 }12 cmd.Wait()13}

Full Screen

Full Screen

hmp

Using AI Code Generation

copy

Full Screen

1import (2func main() {3}4import (5func main() {6}

Full Screen

Full Screen

hmp

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 q, err := qemu.NewQemu("qemu-system-x86_64", "test", qemu.Options{4 })5 if err != nil {6 fmt.Println("Error in starting qemu", err)7 }8 fmt.Println("Qemu started")9 err = q.Hmp("info registers")10 if err != nil {11 fmt.Println("Error in hmp method", err)12 }13 fmt.Println("Hmp method executed")14 time.Sleep(5 * time.Second)15 err = q.Close()16 if err != nil {17 fmt.Println("Error in closing qemu", err)18 }19 fmt.Println("Qemu closed")20}

Full Screen

Full Screen

hmp

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 args := []string{"-hda", "/home/rajath/Downloads/ubuntu-16.04.2-server-amd64.iso", "-m", "512", "-smp", "2", "-hmp", "tcp:localhost:4444,server,nowait"}4 cmd := exec.Command(path, args...)5 err := cmd.Run()6 if err != nil {7 fmt.Println(err)8 }9}10import (11func main() {12 conn, _ := net.Dial("tcp", "

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful