/
decode.go
287 lines (256 loc) · 7.85 KB
/
decode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (C) 2021 Toitware ApS. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.
package commands
import (
"bufio"
"context"
"encoding/base64"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/google/uuid"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
"github.com/toitlang/jaguar/cmd/jag/directory"
"github.com/toitware/ubjson"
)
func DecodeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "decode <message>",
Short: "Decode a stack trace received from a Jaguar device",
Long: "Decode a stack trace received from a Jaguar device. Stack traces are encoded\n" +
"using base64 and are easy to copy from the serial output.",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
pretty, err := cmd.Flags().GetBool("force-pretty")
if err != nil {
return err
}
plain, err := cmd.Flags().GetBool("force-plain")
if err != nil {
return err
}
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")
cmd.Flags().BoolP("force-plain", "l", false, "force output to use plain ASCII text")
cmd.Flags().String("envelope", "", "name or path of the firmware envelope")
return cmd
}
func serialDecode(ctx context.Context, envelope string, message string, forcePretty bool, forcePlain bool) error {
if strings.HasPrefix(message, "jag decode ") {
return jagDecode(ctx, message[11:], forcePretty, forcePlain)
} else if strings.HasPrefix(message, "Backtrace:") {
return crashDecode(ctx, envelope, message)
} else {
return jagDecode(ctx, message, forcePretty, forcePlain)
}
}
func jagDecode(ctx context.Context, base64Message string, forcePretty bool, forcePlain bool) error {
sdk, err := GetSDK(ctx)
if err != nil {
return err
}
equalsIndex := strings.Index(base64Message, "=")
if equalsIndex != -1 && !strings.HasSuffix(base64Message, "=") {
// The = symbols that optionally indicate the end of the base64
// encoding are not at the end. Let's trim the junk off the end.
if base64Message[equalsIndex+1] == '=' {
// There might be two = signs at the end.
equalsIndex++
}
base64Message = base64Message[0 : equalsIndex+1]
} else {
// Try to trim, based on the first index of something that is not
// allowed in base64 encoding.
i := 0
for ; i < len(base64Message); i++ {
c := base64Message[i]
if 'a' <= c && c <= 'z' {
continue
}
if 'A' <= c && c <= 'Z' {
continue
}
if '0' <= c && c <= '9' {
continue
}
if c == '+' || c == '/' || c == '=' {
continue
}
break
}
base64Message = base64Message[0:i]
}
message, err := base64.StdEncoding.DecodeString(base64Message)
if err != nil {
return err
}
var decoded []interface{}
if err = ubjson.Unmarshal(message, &decoded); err != nil {
return fmt.Errorf("failed to parse message as ubjson, reason: %v", err)
}
if len(decoded) != 4 && len(decoded) != 5 {
return fmt.Errorf("message did not have correct format")
}
i := 0
if v, ok := decoded[i].(int64); !ok || rune(v) != 'X' {
return fmt.Errorf("message did not have correct format")
}
i++
_, ok := decoded[i].(string)
if !ok {
return fmt.Errorf("message did not have correct format")
}
i++
if len(decoded) == 5 {
if _, ok := decoded[i].(string); !ok {
return fmt.Errorf("message did not have correct format")
}
i++
}
var programIdBytes []byte
if mapstructure.Decode(decoded[i], &programIdBytes) != nil {
return fmt.Errorf("message did not have correct format")
}
programId, err := uuid.FromBytes(programIdBytes)
if err != nil {
return fmt.Errorf("failed to parse program id: %v", err)
}
snapshotsCache, err := directory.GetSnapshotsCachePath()
if err != nil {
return err
}
snapshot := filepath.Join(snapshotsCache, programId.String()+".snapshot")
pretty := "--no-force-pretty"
if forcePretty {
pretty = "--force-pretty"
}
plain := "--no-force-plain"
if forcePlain {
plain = "--force-plain"
}
var decodeCommand *exec.Cmd
isMissingSnapshot := false
if programId != uuid.Nil {
if _, err := os.Stat(snapshot); errors.Is(err, os.ErrNotExist) {
isMissingSnapshot = true
// Still run the decodeCommand for the exception, even though we won't get the stacktrace
decodeCommand = sdk.SystemMessage(ctx, "--message", base64Message, "--uuid", programId.String(), pretty, plain)
} else {
decodeCommand = sdk.SystemMessage(ctx, "--snapshot", snapshot, "--message", base64Message, "--uuid", programId.String(), pretty, plain)
}
} else {
decodeCommand = sdk.SystemMessage(ctx, "--message", base64Message, pretty, plain)
}
decodeCommand.Stderr = os.Stderr
decodeCommand.Stdout = os.Stdout
err = decodeCommand.Run()
if err == nil && isMissingSnapshot {
// Inform the user that they could get better output if they had the snapshot.
fmt.Fprintf(os.Stderr, "No such file: %s\n", snapshot)
return fmt.Errorf("cannot decode stacktrace without snapshot for program: %s", programId.String())
}
return err
}
func crashDecode(ctx context.Context, envelope string, backtrace string) error {
sdk, err := GetSDK(ctx)
if err != nil {
return err
}
if envelope == "" {
envelope = "esp32"
}
// If the given envelope is a path, just use it.
var envelopePath string
if _, err := os.Stat(envelope); err == nil {
envelopePath = envelope
} else {
envelopePath, err = GetCachedFirmwareEnvelopePath(ctx, sdk.Version, envelope)
if err != nil {
return err
}
}
firmwareElf, err := ExtractFirmware(ctx, sdk, envelopePath, "elf")
if err != nil {
return err
}
defer firmwareElf.Close()
objdump, err := exec.LookPath("xtensa-esp32-elf-objdump")
if err != nil {
objdump, err = exec.LookPath("objdump")
}
if err != nil {
return err
}
stacktraceCommand := sdk.Stacktrace(ctx, "--objdump", objdump, "--backtrace", backtrace, firmwareElf.Name())
stacktraceCommand.Stderr = os.Stderr
stacktraceCommand.Stdout = os.Stdout
fmt.Println("Crash in native code:")
fmt.Println(backtrace)
return stacktraceCommand.Run()
}
type Decoder struct {
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) {
POSTPONED_LINES := map[string]bool{
"----": true,
"Received a Toit system message. Executing the command below will": true,
"make it human readable:": true,
}
Version := ""
postponed := []string{}
for d.scanner.Scan() {
// Get next line from device (or simulator) console.
line := d.scanner.Text()
versionPrefix := "[toit] INFO: starting <v"
if strings.HasPrefix(line, versionPrefix) && strings.HasSuffix(line, ">") {
Version = line[len(versionPrefix) : len(line)-1]
}
if _, contains := POSTPONED_LINES[line]; contains {
postponed = append(postponed, line)
} else {
separator := strings.Repeat("*", 78)
if strings.HasPrefix(line, "jag decode ") || strings.HasPrefix(line, "Backtrace:") {
fmt.Printf("\n" + separator + "\n")
if Version != "" {
fmt.Printf("Decoding by `jag`, device has version <%s>\n", Version)
fmt.Printf(separator + "\n")
}
if err := serialDecode(d.context, d.envelope, line, forcePretty, forcePlain); err != nil {
if len(postponed) != 0 {
fmt.Println(strings.Join(postponed, "\n"))
postponed = []string{}
}
fmt.Println(line)
fmt.Println("jag: Failed to decode line.")
} else {
postponed = []string{}
}
fmt.Printf(separator + "\n\n")
} else {
if len(postponed) != 0 {
fmt.Println(strings.Join(postponed, "\n"))
postponed = []string{}
}
fmt.Println(line)
}
}
}
}