Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to decode native stack traces in jag monitor #180

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions cmd/jag/commands/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/google/uuid"
"github.com/mitchellh/mapstructure"
Expand All @@ -27,12 +29,22 @@ func DecodeCmd() *cobra.Command {
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return jagDecode(cmd, args[0])
return serialDecode(cmd, args[0])
},
}
return cmd
}

func serialDecode(cmd *cobra.Command, message string) error {
if strings.HasPrefix(message, "jag decode ") {
return jagDecode(cmd, message[11:])
} else if strings.HasPrefix(message, "Backtrace:") {
return crashDecode(cmd, message)
} else {
return fmt.Errorf("Could not decode %s", message)
}
}

func jagDecode(cmd *cobra.Command, base64Message string) error {
ctx := cmd.Context()
sdk, err := GetSDK(ctx)
Expand Down Expand Up @@ -94,8 +106,36 @@ func jagDecode(cmd *cobra.Command, base64Message string) error {
return fmt.Errorf("cannot find snapshot for program: %s", programId.String())
}

decodeCmd := sdk.ToitRun(ctx, sdk.SystemMessageSnapshotPath(), snapshot, "-b", base64Message)
decodeCmd.Stderr = os.Stderr
decodeCmd.Stdout = os.Stdout
return decodeCmd.Run()
decodeCommand := sdk.ToitRun(ctx, sdk.SystemMessageSnapshotPath(), snapshot, "-b", base64Message)
decodeCommand.Stderr = os.Stderr
decodeCommand.Stdout = os.Stdout
return decodeCommand.Run()
}

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

elf, err := directory.GetESP32ImagePath()
if err != nil {
return err
}
elf = filepath.Join(elf, "toit.elf")

objdump, err := exec.LookPath("xtensa-esp32-elf-objdump")
if err != nil {
objdump, err = exec.LookPath("objdump")
}
if err != nil {
return err
}
stacktraceCommand := sdk.ToitRun(ctx, sdk.StacktracePath(), "--objdump", objdump, "--backtrace", backtrace, elf)
stacktraceCommand.Stderr = os.Stderr
stacktraceCommand.Stdout = os.Stdout
fmt.Println("Crash in native code:")
fmt.Printf("Backtrace:%s", backtrace)
return stacktraceCommand.Run()
}
6 changes: 3 additions & 3 deletions cmd/jag/commands/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ func MonitorCmd() *cobra.Command {
if _, contains := POSTPONED_LINES[line]; contains {
postponed = append(postponed, line)
} else {
if strings.HasPrefix(line, "jag decode ") {
if err := jagDecode(cmd, line[11:]); err != nil {
if strings.HasPrefix(line, "jag decode ") || strings.HasPrefix(line, "Backtrace:") {
if err := serialDecode(cmd, line); err != nil {
if len(postponed) != 0 {
fmt.Println(strings.Join(postponed, "\n"))
postponed = []string{}
}
fmt.Println(line)
fmt.Println("jag monitor: failed to decode line - serial line corruption?")
fmt.Println("jag monitor: Failed to decode line.")
} else {
postponed = []string{}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/jag/commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func (s *SDK) InjectConfigPath() string {
return filepath.Join(s.Path, "snapshots", "inject_config.snapshot")
}

func (s *SDK) StacktracePath() string {
return filepath.Join(s.Path, "snapshots", "stacktrace.snapshot")
}

func (s *SDK) validate(info Info, skipSDKVersionCheck bool) error {
if !skipSDKVersionCheck {
if s.Version == "" {
Expand Down