Skip to content

Commit f3eec51

Browse files
committed
Move main implementation to main() function
1 parent 53a3f35 commit f3eec51

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

decoder.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -252,45 +252,53 @@ def print_result(parser, resolver, full=True, stack_only=False):
252252
print_stack(parser.stack, resolver)
253253

254254

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+
255279
def parse_args():
256280
parser = argparse.ArgumentParser(description="decode ESP Stacktraces.")
257281

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(),
259283
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",
261285
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)
263287
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="-")
266290

267291
return parser.parse_args()
268292

269293

270294
if __name__ == "__main__":
271295
args = parse_args()
272296

297+
toolchain_path = os.path.abspath(os.path.expanduser(args.toolchain))
298+
elf_path = os.path.abspath(os.path.expanduser(args.elf))
273299
if args.file == "-":
274-
file = sys.stdin
300+
exception_input = None
275301
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))
289303

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

Comments
 (0)