-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathEspExceptionDecoder.java
627 lines (573 loc) · 21.9 KB
/
EspExceptionDecoder.java
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
Copyright (c) 2015 Hristo Gochkov (ficeto at ficeto dot com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.ficeto.esp;
import cc.arduino.files.DeleteFilesOnShutdown;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.apache.commons.codec.digest.DigestUtils;
import processing.app.Base;
import processing.app.BaseNoGui;
import processing.app.Editor;
import processing.app.Platform;
import processing.app.PreferencesData;
import processing.app.Sketch;
//import processing.app.SketchData;
import processing.app.debug.TargetPlatform;
import processing.app.helpers.FileUtils;
import processing.app.helpers.OSUtils;
import processing.app.helpers.ProcessUtils;
import processing.app.tools.Tool;
public class EspExceptionDecoder implements Tool, DocumentListener {
Editor editor;
JTextPane outputArea;
String outputText;
JTextArea inputArea;
JFrame frame;
File tool;
File elf;
private static String[] exceptions = {
"Illegal instruction",
"SYSCALL instruction",
"InstructionFetchError: Processor internal physical address or data error during instruction fetch",
"LoadStoreError: Processor internal physical address or data error during load or store",
"Level1Interrupt: Level-1 interrupt as indicated by set level-1 bits in the INTERRUPT register",
"Alloca: MOVSP instruction, if caller's registers are not in the register file",
"IntegerDivideByZero: QUOS, QUOU, REMS, or REMU divisor operand is zero",
"reserved",
"Privileged: Attempt to execute a privileged operation when CRING ? 0",
"LoadStoreAlignmentCause: Load or store to an unaligned address",
"reserved",
"reserved",
"InstrPIFDataError: PIF data error during instruction fetch",
"LoadStorePIFDataError: Synchronous PIF data error during LoadStore access",
"InstrPIFAddrError: PIF address error during instruction fetch",
"LoadStorePIFAddrError: Synchronous PIF address error during LoadStore access",
"InstTLBMiss: Error during Instruction TLB refill",
"InstTLBMultiHit: Multiple instruction TLB entries matched",
"InstFetchPrivilege: An instruction fetch referenced a virtual address at a ring level less than CRING",
"reserved",
"InstFetchProhibited: An instruction fetch referenced a page mapped with an attribute that does not permit instruction fetch",
"reserved",
"reserved",
"reserved",
"LoadStoreTLBMiss: Error during TLB refill for a load or store",
"LoadStoreTLBMultiHit: Multiple TLB entries matched for a load or store",
"LoadStorePrivilege: A load or store referenced a virtual address at a ring level less than CRING",
"reserved",
"LoadProhibited: A load referenced a page mapped with an attribute that does not permit loads",
"StoreProhibited: A store referenced a page mapped with an attribute that does not permit stores"
};
public void init(Editor editor) {
this.editor = editor;
}
public String getMenuTitle() {
return "ESP Exception Decoder";
}
private void printLine(String line){
String s = prettyPrintGDBLine(line);
if (s != null)
outputText += s +"\n";
}
private void printLogLine(String line, String color){
outputText += "<b><font color="+color+">"+line+"</font></b>\n";
}
private void printError(String line){
printLogLine(line, "red");
}
// Original code from processing.app.helpers.ProcessUtils.exec()
// Need custom version to redirect STDERR to STDOUT for GDB processing
public static Process execRedirected(String[] command) throws IOException {
ProcessBuilder pb;
// No problems on linux and mac
if (!OSUtils.isWindows()) {
pb = new ProcessBuilder(command);
} else {
// Brutal hack to workaround windows command line parsing.
// http://stackoverflow.com/questions/5969724/java-runtime-exec-fails-to-escape-characters-properly
// http://msdn.microsoft.com/en-us/library/a1y7w461.aspx
// http://bugs.sun.com/view_bug.do?bug_id=6468220
// http://bugs.sun.com/view_bug.do?bug_id=6518827
String[] cmdLine = new String[command.length];
for (int i = 0; i < command.length; i++)
cmdLine[i] = command[i].replace("\"", "\\\"");
pb = new ProcessBuilder(cmdLine);
Map<String, String> env = pb.environment();
env.put("CYGWIN", "nodosfilewarning");
}
pb.redirectErrorStream(true);
return pb.start();
}
private int listenOnProcess(String[] arguments){
try {
final Process p = execRedirected(arguments);
Thread thread = new Thread() {
public void run() {
try {
InputStreamReader reader = new InputStreamReader(p.getInputStream());
int c;
String line = "";
while ((c = reader.read()) != -1){
if((char)c == '\r')
continue;
if((char)c == '\n'){
printLine(line);
line = "";
} else {
line += (char)c;
}
}
printLine(line);
reader.close();
reader = new InputStreamReader(p.getErrorStream());
while ((c = reader.read()) != -1)
System.err.print((char) c);
reader.close();
} catch (Exception e){
printError("Run Exception: "+e.getMessage());
}
}
};
thread.start();
int res = p.waitFor();
thread.join();
if(res != 0){
printError("Decode Failed");
}
return res;
} catch (Exception e){
printError("Decode Exception: "+e.getMessage());
}
return -1;
}
private void sysExec(final String[] arguments){
Thread thread = new Thread() {
public void run() {
try {
if(listenOnProcess(arguments) != 0){
editor.statusError("Decode Failed");
} else {
editor.statusNotice("Decode Success");
}
} catch (Exception e){
editor.statusError("Decode Exception");
printError("Decode Exception: "+e.getMessage());
}
outputArea.setText(outputText);
}
};
thread.start();
}
private String getBuildFolderPath(Sketch s) {
// first of all try the getBuildPath() function introduced with IDE 1.6.12
// see commit arduino/Arduino#fd1541eb47d589f9b9ea7e558018a8cf49bb6d03
try {
String buildpath = s.getBuildPath().getAbsolutePath();
return buildpath;
}
catch (IOException er) {
editor.statusError(er);
}
catch (Exception er) {
try {
File buildFolder = FileUtils.createTempFolder("build", DigestUtils.md5Hex(s.getMainFilePath()) + ".tmp");
//DeleteFilesOnShutdown.add(buildFolder);
return buildFolder.getAbsolutePath();
}
catch (IOException e) {
editor.statusError(e);
}
catch (Exception e) {
// Arduino 1.6.5 doesn't have FileUtils.createTempFolder
// String buildPath = BaseNoGui.getBuildFolder().getAbsolutePath();
java.lang.reflect.Method method;
try {
method = BaseNoGui.class.getMethod("getBuildFolder");
File f = (File) method.invoke(null);
return f.getAbsolutePath();
} catch (SecurityException ex) {
editor.statusError(ex);
} catch (IllegalAccessException ex) {
editor.statusError(ex);
} catch (InvocationTargetException ex) {
editor.statusError(ex);
} catch (NoSuchMethodException ex) {
editor.statusError(ex);
}
}
}
return "";
}
private long getIntPref(String name){
String data = BaseNoGui.getBoardPreferences().get(name);
if(data == null || data.contentEquals("")) return 0;
if(data.startsWith("0x")) return Long.parseLong(data.substring(2), 16);
else return Integer.parseInt(data);
}
class ElfFilter extends FileFilter {
public String getExtension(File f) {
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
return ext;
}
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String extension = getExtension(f);
if (extension != null) {
return extension.equals("elf");
}
return false;
}
public String getDescription() {
return "*.elf files";
}
}
private void createAndUpload(){
if(!PreferencesData.get("target_platform").contentEquals("esp8266") && !PreferencesData.get("target_platform").contentEquals("esp32") && !PreferencesData.get("target_platform").contentEquals("ESP31B")){
System.err.println();
editor.statusError("Not Supported on "+PreferencesData.get("target_platform"));
return;
}
String core = PreferencesData.get("target_platform");
String tarch = "xtensa";
String target = "lx106";
if(core.contentEquals("esp32")){
try {
tarch = BaseNoGui.getBoardPreferences().get("build.tarch");
if(tarch == null || tarch.contentEquals("")){
editor.statusError("Arch Not Found for '"+BaseNoGui.getBoardPreferences().get("name")+"'. Defaulting to 'xtensa'");
tarch = "xtensa";
}
target = BaseNoGui.getBoardPreferences().get("build.target");
if(target == null || target.contentEquals("")){
editor.statusError("Target Not Found for '"+BaseNoGui.getBoardPreferences().get("name")+"'. Defaulting to 'esp32'");
target = "esp32";
}
} catch(Exception e){
editor.statusError(e);
return;
}
}
TargetPlatform platform = BaseNoGui.getTargetPlatform();
String tc = tarch+"-"+target+"-elf";
String tcgdb = tarch+"-esp-elf-gdb";
String tools = platform.getFolder() + "/tools/";
String gdb;
if(PreferencesData.get("runtime.os").contentEquals("windows"))
gdb = tc+"-gdb.exe";
else
gdb = tc+"-gdb";
// Search for GDB
// First check if the GDB package is installed locally (v2.0.8+)
tool = new File(tools + tcgdb + "/bin", gdb);
if (!tool.exists() || !tool.isFile()){
//System.err.println("tools/"+tcgdb+"/bin/"+gdb+" not found");
// Then check if the toolchain is installed locally
tool = new File(tools + tc + "/bin", gdb);
if (!tool.exists() || !tool.isFile()){
//System.err.println("tools/"+tc+"/bin/"+gdb+" not found");
// Then check if the GDB package is installed (v2.0.8+)
String gdbPath = PreferencesData.get("runtime.tools."+tcgdb+".path");
if(gdbPath == null || gdbPath.contentEquals("")){
//System.err.println("runtime.tools."+tcgdb+".path not found");
// Then check if the toolchain is installed
gdbPath = PreferencesData.get("runtime.tools."+tc+"-gcc.path");
}
if(gdbPath == null || gdbPath.contentEquals("")){
//System.err.println("runtime.tools."+tc+"-gcc.path not found");
// If still fails, offer the local toolchain folder
gdbPath = tools+tc;
}
System.err.println("gdbPath: "+gdbPath+"/bin/"+gdb);
tool = new File(gdbPath + "/bin", gdb);
} else {
System.err.println("gdbPath: "+tools+tc+"/bin/"+gdb);
}
} else {
System.err.println("gdbPath: "+tools+tcgdb+"/bin/"+gdb);
}
System.err.println();
if (!tool.exists() || !tool.isFile()) {
System.err.println();
editor.statusError("ERROR: "+gdb+" not found!");
return;
}
elf = new File(getBuildFolderPath(editor.getSketch()), editor.getSketch().getName() + ".ino.elf");
if (!elf.exists() || !elf.isFile()) {
elf = new File(getBuildFolderPath(editor.getSketch()), editor.getSketch().getName() + ".cpp.elf");
if (!elf.exists() || !elf.isFile()){
//lets give the user a chance to select the elf
final JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new ElfFilter());
fc.setAcceptAllFileFilterUsed(false);
int returnVal = fc.showDialog(editor, "Select ELF");
if (returnVal == JFileChooser.APPROVE_OPTION) {
elf = fc.getSelectedFile();
} else {
editor.statusError("ERROR: elf was not found!");
System.err.println("Open command cancelled by user.");
return;
}
}
}
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("Exception Decoder");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
inputArea = new JTextArea("Paste your stack trace here", 16, 60);
inputArea.setLineWrap(true);
inputArea.setWrapStyleWord(true);
inputArea.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "commit");
inputArea.getActionMap().put("commit", new CommitAction());
inputArea.getDocument().addDocumentListener(this);
frame.getContentPane().add(new JScrollPane(inputArea), BorderLayout.PAGE_START);
outputText = "";
outputArea = new JTextPane();
outputArea.setContentType("text/html");
outputArea.setEditable(false);
outputArea.setBackground(null);
outputArea.setBorder(null);
outputArea.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
outputArea.setText(outputText);
JScrollPane outputScrollPane = new JScrollPane(outputArea);
outputScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
outputScrollPane.setPreferredSize(new Dimension(640, 200));
outputScrollPane.setMinimumSize(new Dimension(10, 10));
frame.getContentPane().add(outputScrollPane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private String prettyPrintGDBLine(String line) {
String address = "", method = "", file = "", fileline = "", html = "";
if (!line.startsWith("0x")) {
return null;
}
address = line.substring(0, line.indexOf(' '));
line = line.substring(line.indexOf(' ') + 1);
int atIndex = line.indexOf("is in ");
if(atIndex == -1) {
return null;
}
try {
method = line.substring(atIndex + 6, line.lastIndexOf('(') - 1);
fileline = line.substring(line.lastIndexOf('(') + 1, line.lastIndexOf(')'));
file = fileline.substring(0, fileline.lastIndexOf(':'));
line = fileline.substring(fileline.lastIndexOf(':') + 1);
if(file.length() > 0){
int lastfs = file.lastIndexOf('/');
int lastbs = file.lastIndexOf('\\');
int slash = (lastfs > lastbs)?lastfs:lastbs;
if(slash != -1){
String filename = file.substring(slash+1);
file = file.substring(0,slash+1) + "<b>" + filename + "</b>";
}
}
html = "<font color=green>" + address + ": </font>" +
"<b><font color=blue>" + method + "</font></b> at " +
file + " line <b>" + line + "</b>";
} catch (Exception e) {
// Something weird in the GDB output format, report what we can
html = "<font color=green>" + address + ": </font> " + line;
}
return html;
}
public void run() {
createAndUpload();
}
private void parseException(){
String content = inputArea.getText();
Pattern p = Pattern.compile("Exception \\(([0-9]*)\\):");
Matcher m = p.matcher(content);
if(m.find()){
int exception = Integer.parseInt(m.group(1));
if(exception < 0 || exception > 29){
return;
}
outputText += "<b><font color=red>Exception "+exception+": "+exceptions[exception]+"</font></b>\n";
}
}
// Strip out just the STACK lines or BACKTRACE line, and generate the reference log
private void parseStackOrBacktrace(String regexp, boolean multiLine, String stripAfter) {
String content = inputArea.getText();
Pattern strip;
if (multiLine) strip = Pattern.compile(regexp, Pattern.DOTALL);
else strip = Pattern.compile(regexp);
Matcher stripMatch = strip.matcher(content);
if (!stripMatch.find()) {
return; // Didn't find it in the text box.
}
// Strip out just the interesting bits to make RexExp sane
content = content.substring(stripMatch.start(), stripMatch.end());
if (stripAfter != null) {
Pattern after = Pattern.compile(stripAfter);
Matcher afterMatch = after.matcher(content);
if (afterMatch.find()) {
content = content.substring(0, afterMatch.start());
}
}
// Anything looking like an instruction address, dump!
Pattern p = Pattern.compile("4[0-3](\\d|[a-f]|[A-F]){6}\\b");
int count = 0;
Matcher m = p.matcher(content);
while(m.find()) {
count ++;
}
if(count == 0){
return;
}
String command[] = new String[7 + count*2];
int i = 0;
command[i++] = tool.getAbsolutePath();
command[i++] = "--batch";
command[i++] = elf.getAbsolutePath();
command[i++] = "-ex";
command[i++] = "set listsize 1";
m = p.matcher(content);
while(m.find()) {
command[i++] = "-ex";
command[i++] = "l *0x"+content.substring(m.start(), m.end());
}
command[i++] = "-ex";
command[i++] = "q";
System.out.println("\""+String.join("\" \"", command)+"\"");
outputText += "\n<i>Decoding stack results</i>\n";
sysExec(command);
}
// Heavyweight call GDB, run list on address, and return result if it succeeded
private String decodeFunctionAtAddress( String addr ) {
String command[] = new String[9];
command[0] = tool.getAbsolutePath();
command[1] = "--batch";
command[2] = elf.getAbsolutePath();
command[3] = "-ex";
command[4] = "set listsize 1";
command[5] = "-ex";
command[6] = "l *0x" + addr;
command[7] = "-ex";
command[8] = "q";
System.out.println("\""+String.join("\" \"", command)+"\"");
try {
final Process proc = execRedirected(command);
InputStreamReader reader = new InputStreamReader(proc.getInputStream());
int c;
String line = "";
while ((c = reader.read()) != -1){
if((char)c == '\r')
continue;
if((char)c == '\n' && line != ""){
reader.close();
return prettyPrintGDBLine(line);
} else {
line += (char)c;
}
}
reader.close();
} catch (Exception e) {
// Something went wrong
System.err.println("Function Decode Exception: "+e.getMessage());
}
return null;
}
// Scan and report the last failed memory allocation attempt, if present on the ESP8266
private void parseAlloc() {
String content = inputArea.getText();
Pattern p = Pattern.compile("last failed alloc call: 4[0-3](\\d|[a-f]|[A-F]){6}\\((\\d)+\\)");
Matcher m = p.matcher(content);
if (m.find()) {
String fs = content.substring(m.start(), m.end());
Pattern p2 = Pattern.compile("4[0-3](\\d|[a-f]|[A-F]){6}\\b");
Matcher m2 = p2.matcher(fs);
if (m2.find()) {
String addr = fs.substring(m2.start(), m2.end());
Pattern p3 = Pattern.compile("\\((\\d)+\\)");
Matcher m3 = p3.matcher(fs);
if (m3.find()) {
String size = fs.substring(m3.start()+1, m3.end()-1);
String line = decodeFunctionAtAddress(addr);
if (line != null) {
outputText += "Memory allocation of " + size + " bytes failed at " + line + "\n";
}
}
}
}
}
// Filter out a register output given a regex (ESP8266/ESP32 differ in format)
private void parseRegister(String regName, String prettyName) {
String content = inputArea.getText();
Pattern p = Pattern.compile(regName + "(\\d|[a-f]|[A-F]){8}\\b");
Matcher m = p.matcher(content);
if (m.find()) {
String fs = content.substring(m.start(), m.end());
Pattern p2 = Pattern.compile("(\\d|[a-f]|[A-F]){8}\\b");
Matcher m2 = p2.matcher(fs);
if (m2.find()) {
String addr = fs.substring(m2.start(), m2.end());
String line = decodeFunctionAtAddress(addr);
if (line != null) {
outputText += prettyName + ": " + line + "\n";
} else {
outputText += prettyName + ": <font color=\"green\">0x" + addr + "</font>\n";
}
}
}
}
private void runParser(){
outputText = "<html><pre>\n";
// Main error cause
parseException();
// ESP8266 register format
parseRegister("epc1=0x", "<font color=\"red\">PC</font>");
parseRegister("excvaddr=0x", "<font color=\"red\">EXCVADDR</font>");
// ESP32 register format
parseRegister("PC\\s*:\\s*(0x)?", "<font color=\"red\">PC</font>");
parseRegister("EXCVADDR\\s*:\\s*(0x)?", "<font color=\"red\">EXCVADDR</font>");
// Last memory allocation failure
parseAlloc();
// The stack on ESP8266, multiline
parseStackOrBacktrace(">>>stack>>>(.)*", true, "<<<stack<<<");
// The backtrace on ESP32, one-line only
parseStackOrBacktrace("Backtrace:(.)*", false, null);
}
private class CommitAction extends AbstractAction {
public void actionPerformed(ActionEvent ev) {
runParser();
}
}
public void changedUpdate(DocumentEvent ev) {
}
public void removeUpdate(DocumentEvent ev) {
}
public void insertUpdate(DocumentEvent ev) {
runParser();
}
}