Skip to content

Commit

Permalink
vm/qemu: better handle qmp errors
Browse files Browse the repository at this point in the history
Sometimes qemu just returns an "Error: ..." string in reply
instead of returning an error. Handle these cases.
Also log all qmp commands in debug mode.
  • Loading branch information
dvyukov committed Jul 8, 2024
1 parent 023352d commit 845ac47
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions vm/qemu/qmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package qemu

import (
"encoding/json"
"errors"
"fmt"
"net"
"strings"

"github.com/google/syzkaller/pkg/log"
)
Expand Down Expand Up @@ -109,15 +111,22 @@ func (inst *instance) qmp(cmd *qmpCommand) (interface{}, error) {
return nil, err
}
if resp.Error.Desc != "" {
return nil, fmt.Errorf("error %v", resp.Error)
return nil, fmt.Errorf("%v", resp.Error)
}
if resp.Return == nil {
return nil, fmt.Errorf(`no "return" nor "error" in [%v]`, resp)
}
if output, _ := resp.Return.(string); strings.HasPrefix(output, "Error:") ||
strings.HasPrefix(output, "unknown command:") {
return nil, errors.New(output)
}
return resp.Return, nil
}

func (inst *instance) hmp(cmd string, cpu int) (string, error) {
if inst.debug {
log.Logf(0, "qemu: running hmp command: %v", cmd)
}
req := &qmpCommand{
Execute: "human-monitor-command",
Arguments: &hmpCommand{
Expand All @@ -126,8 +135,11 @@ func (inst *instance) hmp(cmd string, cpu int) (string, error) {
},
}
resp, err := inst.qmp(req)
if inst.debug {
log.Logf(0, "qemu: reply: %v\n%v", err, resp)
}
if err != nil {
return "", err
return "", fmt.Errorf("qemu hmp command '%s': %w", cmd, err)
}
return resp.(string), nil
}

0 comments on commit 845ac47

Please sign in to comment.