@@ -659,15 +659,15 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
659659 // connect to monitor
660660 conn , err := net .Dial ("unix" , d .monitorPath ())
661661 if err != nil {
662- return nil , err
662+ return nil , errors . Wrap ( err , "connect" )
663663 }
664664 defer conn .Close ()
665665
666666 // initial QMP response
667667 var buf [1024 ]byte
668668 nr , err := conn .Read (buf [:])
669669 if err != nil {
670- return nil , err
670+ return nil , errors . Wrap ( err , "read initial resp" )
671671 }
672672 type qmpInitialResponse struct {
673673 QMP struct {
@@ -684,9 +684,8 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
684684 }
685685
686686 var initialResponse qmpInitialResponse
687- err = json .Unmarshal (buf [:nr ], & initialResponse )
688- if err != nil {
689- return nil , err
687+ if err := json .Unmarshal (buf [:nr ], & initialResponse ); err != nil {
688+ return nil , errors .Wrap (err , "unmarshal initial resp" )
690689 }
691690
692691 // run 'qmp_capabilities' to switch to command mode
@@ -696,22 +695,21 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
696695 }
697696 jsonCommand , err := json .Marshal (qmpCommand {Command : "qmp_capabilities" })
698697 if err != nil {
699- return nil , err
698+ return nil , errors . Wrap ( err , "marshal qmp_capabilities" )
700699 }
701700 if _ , err := conn .Write (jsonCommand ); err != nil {
702- return nil , err
701+ return nil , errors . Wrap ( err , "write qmp_capabilities" )
703702 }
704703 nr , err = conn .Read (buf [:])
705704 if err != nil {
706- return nil , err
705+ return nil , errors . Wrap ( err , "read qmp_capabilities resp" )
707706 }
708707 type qmpResponse struct {
709708 Return map [string ]interface {} `json:"return"`
710709 }
711710 var response qmpResponse
712- err = json .Unmarshal (buf [:nr ], & response )
713- if err != nil {
714- return nil , err
711+ if err := json .Unmarshal (buf [:nr ], & response ); err != nil {
712+ return nil , errors .Wrap (err , "unmarshal qmp_capabilities resp" )
715713 }
716714 // expecting empty response
717715 if len (response .Return ) != 0 {
@@ -721,18 +719,21 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
721719 // { "execute": command }
722720 jsonCommand , err = json .Marshal (qmpCommand {Command : command })
723721 if err != nil {
724- return nil , err
722+ return nil , errors . Wrap ( err , "marshal command" )
725723 }
726724 if _ , err := conn .Write (jsonCommand ); err != nil {
727- return nil , err
725+ return nil , errors . Wrap ( err , "write command" )
728726 }
729727 nr , err = conn .Read (buf [:])
730728 if err != nil {
731- return nil , err
729+ return nil , errors . Wrap ( err , "read command resp" )
732730 }
733- err = json .Unmarshal (buf [:nr ], & response )
734- if err != nil {
735- return nil , err
731+
732+ // Sometimes QEMU returns two JSON objects with the first object being the command response
733+ // and the second object being an event log (unimportant)
734+ firstRespObj := strings .Split (string (buf [:nr ]), "\n " )[0 ]
735+ if err := json .Unmarshal ([]byte (firstRespObj ), & response ); err != nil {
736+ return nil , errors .Wrap (err , "unmarshal command resp" )
736737 }
737738 if strings .HasPrefix (command , "query-" ) {
738739 return response .Return , nil
0 commit comments