Skip to content

Commit

Permalink
Fix monitor crash decoding. (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Jan 30, 2024
1 parent 15d7cb4 commit f626a70
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
37 changes: 20 additions & 17 deletions cmd/jag/commands/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package commands

import (
"bufio"
"context"
"encoding/base64"
"errors"
"fmt"
Expand Down Expand Up @@ -38,7 +39,11 @@ func DecodeCmd() *cobra.Command {
if err != nil {
return err
}
return serialDecode(cmd, args[0], pretty, plain)
envelope, err := cmd.Flags().GetString("envelope")
if err != nil {
return err
}
return serialDecode(cmd.Context(), envelope, args[0], pretty, plain)
},
}
cmd.Flags().BoolP("force-pretty", "r", false, "force output to use terminal graphics")
Expand All @@ -47,18 +52,17 @@ func DecodeCmd() *cobra.Command {
return cmd
}

func serialDecode(cmd *cobra.Command, message string, forcePretty bool, forcePlain bool) error {
func serialDecode(ctx context.Context, envelope string, message string, forcePretty bool, forcePlain bool) error {
if strings.HasPrefix(message, "jag decode ") {
return jagDecode(cmd, message[11:], forcePretty, forcePlain)
return jagDecode(ctx, message[11:], forcePretty, forcePlain)
} else if strings.HasPrefix(message, "Backtrace:") {
return crashDecode(cmd, message)
return crashDecode(ctx, envelope, message)
} else {
return jagDecode(cmd, message, forcePretty, forcePlain)
return jagDecode(ctx, message, forcePretty, forcePlain)
}
}

func jagDecode(cmd *cobra.Command, base64Message string, forcePretty bool, forcePlain bool) error {
ctx := cmd.Context()
func jagDecode(ctx context.Context, base64Message string, forcePretty bool, forcePlain bool) error {
sdk, err := GetSDK(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -180,18 +184,12 @@ func jagDecode(cmd *cobra.Command, base64Message string, forcePretty bool, force
return err
}

func crashDecode(cmd *cobra.Command, backtrace string) error {
ctx := cmd.Context()
func crashDecode(ctx context.Context, envelope string, backtrace string) error {
sdk, err := GetSDK(ctx)
if err != nil {
return err
}

envelope, err := cmd.Flags().GetString("envelope")
if err != nil {
return err
}

if envelope == "" {
envelope = "esp32"
}
Expand Down Expand Up @@ -229,8 +227,13 @@ func crashDecode(cmd *cobra.Command, backtrace string) error {
}

type Decoder struct {
scanner *bufio.Scanner
cmd *cobra.Command
scanner *bufio.Scanner
context context.Context
envelope string
}

func NewDecoder(scanner *bufio.Scanner, ctx context.Context, envelope string) *Decoder {
return &Decoder{scanner, ctx, envelope}
}

func (d *Decoder) decode(forcePretty bool, forcePlain bool) {
Expand Down Expand Up @@ -261,7 +264,7 @@ func (d *Decoder) decode(forcePretty bool, forcePlain bool) {
fmt.Printf("Decoding by `jag`, device has version <%s>\n", Version)
fmt.Printf(separator + "\n")
}
if err := serialDecode(d.cmd, line, forcePretty, forcePlain); err != nil {
if err := serialDecode(d.context, d.envelope, line, forcePretty, forcePlain); err != nil {
if len(postponed) != 0 {
fmt.Println(strings.Join(postponed, "\n"))
postponed = []string{}
Expand Down
8 changes: 7 additions & 1 deletion cmd/jag/commands/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ func MonitorCmd() *cobra.Command {

scanner := bufio.NewScanner(logReader)

decoder := Decoder{scanner, cmd}
envelope, err := cmd.Flags().GetString("envelope")
if err != nil {
return err
}

decoder := NewDecoder(scanner, cmd.Context(), envelope)

decoder.decode(pretty, plain)

Expand All @@ -93,6 +98,7 @@ func MonitorCmd() *cobra.Command {
cmd.Flags().BoolP("force-plain", "l", false, "force output to use plain ASCII text")
cmd.Flags().Uint("baud", 115200, "the baud rate for serial monitoring")
cmd.Flags().Bool("proxy", false, "proxy the connected device to the local network")
cmd.Flags().String("envelope", "", "name or path of the firmware envelope")
return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/jag/commands/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func SimulateCmd() *cobra.Command {
go func() {
scanner := bufio.NewScanner(outReader)

decoder := Decoder{scanner, cmd}
decoder := NewDecoder(scanner, cmd.Context(), "")

decoder.decode(pretty, plain)
}()
Expand Down

0 comments on commit f626a70

Please sign in to comment.