@@ -252,45 +252,53 @@ def print_result(parser, resolver, full=True, stack_only=False):
252
252
print_stack (parser .stack , resolver )
253
253
254
254
255
+ def main (toolchain_path , platform , elf_path , exception_input = None , stack_only = False ):
256
+ addr2line = os .path .join (toolchain_path , "bin/xtensa-" + PLATFORMS [platform ] + "-elf-addr2line" )
257
+ if not os .path .exists (addr2line ):
258
+ raise FileNotFoundError (f"addr2line not found at '{ addr2line } '" )
259
+
260
+ if not os .path .exists (elf_path ):
261
+ raise FileNotFoundError (f"ELF file not found at '{ elf_path } '" )
262
+
263
+ if exception_input :
264
+ if not os .path .exists (exception_input ):
265
+ raise FileNotFoundError (f"Exception file not found at '{ exception_input } '" )
266
+ input_handle = open (exception_input , "r" )
267
+ else :
268
+ input_handle = sys .stdin
269
+
270
+ parser = ExceptionDataParser ()
271
+ resolver = AddressResolver (addr2line , elf_path )
272
+
273
+ parser .parse_file (input_handle , stack_only )
274
+ resolver .fill (parser )
275
+
276
+ print_result (parser , resolver , args .full , args .stack_only )
277
+
278
+
255
279
def parse_args ():
256
280
parser = argparse .ArgumentParser (description = "decode ESP Stacktraces." )
257
281
258
- parser .add_argument ("-p" , "--platform" , help = "The platform to decode from " , choices = PLATFORMS .keys (),
282
+ parser .add_argument ("-p" , "--platform" , help = "The platform to decode for " , choices = PLATFORMS .keys (),
259
283
default = "ESP8266" )
260
- parser .add_argument ("-t" , "--tool " , help = "Path to the xtensa toolchain" ,
284
+ parser .add_argument ("-t" , "--toolchain " , help = "Path to the Xtensa toolchain" ,
261
285
default = "~/.platformio/packages/toolchain-xtensa/" )
262
- parser .add_argument ("-e" , "--elf" , help = "path to elf file" , required = True )
286
+ parser .add_argument ("-e" , "--elf" , help = "Path to ELF file" , required = True )
263
287
parser .add_argument ("-f" , "--full" , help = "Print full stack dump" , action = "store_true" )
264
- parser .add_argument ("-s" , "--stack_only " , help = "Decode only a stractrace " , action = "store_true" )
265
- parser .add_argument ("file" , help = "The file to read the exception data from ('-' for STDIN )" , default = "-" )
288
+ parser .add_argument ("-s" , "--stack-only " , help = "Decode only a stacktrace " , action = "store_true" )
289
+ parser .add_argument ("file" , help = "The file to read the exception data from ('-' for stdin )" , default = "-" )
266
290
267
291
return parser .parse_args ()
268
292
269
293
270
294
if __name__ == "__main__" :
271
295
args = parse_args ()
272
296
297
+ toolchain_path = os .path .abspath (os .path .expanduser (args .toolchain ))
298
+ elf_path = os .path .abspath (os .path .expanduser (args .elf ))
273
299
if args .file == "-" :
274
- file = sys . stdin
300
+ exception_input = None
275
301
else :
276
- if not os .path .exists (args .file ):
277
- print ("ERROR: file " + args .file + " not found" )
278
- sys .exit (1 )
279
- file = open (args .file , "r" )
280
-
281
- addr2line = os .path .join (os .path .abspath (os .path .expanduser (args .tool )),
282
- "bin/xtensa-" + PLATFORMS [args .platform ] + "-elf-addr2line" )
283
- if not os .path .exists (addr2line ):
284
- print ("ERROR: addr2line not found (" + addr2line + ")" )
285
-
286
- elf_file = os .path .abspath (os .path .expanduser (args .elf ))
287
- if not os .path .exists (elf_file ):
288
- print ("ERROR: elf file not found (" + elf_file + ")" )
302
+ exception_input = os .path .abspath (os .path .expanduser (args .file ))
289
303
290
- parser = ExceptionDataParser ()
291
- resolver = AddressResolver (addr2line , elf_file )
292
-
293
- parser .parse_file (file , args .stack_only )
294
- resolver .fill (parser )
295
-
296
- print_result (parser , resolver , args .full , args .stack_only )
304
+ main (toolchain_path , args .platform , elf_path , exception_input , args .stack_only )
0 commit comments