2022-07-06 09:03:11 -04:00
|
|
|
#!/usr/bin/env python
|
2016-12-21 18:56:23 -05:00
|
|
|
#
|
2023-11-01 05:27:36 -04:00
|
|
|
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
2022-02-17 02:27:34 -05:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
2016-12-21 18:56:23 -05:00
|
|
|
|
2022-04-26 09:24:01 -04:00
|
|
|
import json
|
2018-11-12 16:18:17 -05:00
|
|
|
import logging
|
2022-02-17 02:27:34 -05:00
|
|
|
import os.path
|
2023-11-01 05:27:36 -04:00
|
|
|
import re
|
|
|
|
from typing import Any, Optional
|
2017-01-26 10:01:55 -05:00
|
|
|
|
2016-12-21 18:56:23 -05:00
|
|
|
try:
|
2022-02-17 02:27:34 -05:00
|
|
|
from esp_coredump import CoreDump
|
2016-12-21 18:56:23 -05:00
|
|
|
except ImportError:
|
2022-02-17 02:27:34 -05:00
|
|
|
raise ModuleNotFoundError('No module named "esp_coredump" please install esp_coredump by running '
|
|
|
|
'"python -m pip install esp-coredump"')
|
2021-04-08 23:39:37 -04:00
|
|
|
|
2022-02-17 02:27:34 -05:00
|
|
|
from esp_coredump.cli_ext import parser
|
2021-04-08 23:39:37 -04:00
|
|
|
|
2016-12-21 18:56:23 -05:00
|
|
|
|
2023-11-01 05:27:36 -04:00
|
|
|
def get_sdkconfig_value(sdkconfig_file, key): # type: (str, str) -> Optional[str]
|
|
|
|
"""
|
|
|
|
Return the value of given key from sdkconfig_file.
|
|
|
|
If sdkconfig_file does not exist or the option is not present, returns None.
|
|
|
|
"""
|
|
|
|
assert key.startswith('CONFIG_')
|
|
|
|
if not os.path.exists(sdkconfig_file):
|
|
|
|
return None
|
|
|
|
# keep track of the last seen value for the given key
|
|
|
|
value = None
|
|
|
|
# if the value is quoted, this excludes the quotes from the value
|
|
|
|
pattern = re.compile(r"^{}=\"?([^\"]*)\"?$".format(key))
|
|
|
|
with open(sdkconfig_file, 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
match = re.match(pattern, line)
|
|
|
|
if match:
|
|
|
|
value = match.group(1)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def get_project_desc(prog_path): # type: (str) -> Any
|
2022-04-26 09:24:01 -04:00
|
|
|
build_dir = os.path.abspath(os.path.dirname(prog_path))
|
|
|
|
desc_path = os.path.abspath(os.path.join(build_dir, 'project_description.json'))
|
|
|
|
if not os.path.isfile(desc_path):
|
2022-05-25 00:00:13 -04:00
|
|
|
logging.warning('%s does not exist. Please build the app with "idf.py build"', desc_path)
|
|
|
|
return ''
|
2022-04-26 09:24:01 -04:00
|
|
|
|
|
|
|
with open(desc_path, 'r') as f:
|
|
|
|
project_desc = json.load(f)
|
|
|
|
|
2023-11-01 05:27:36 -04:00
|
|
|
return project_desc
|
2022-04-26 09:24:01 -04:00
|
|
|
|
|
|
|
|
2022-02-17 02:27:34 -05:00
|
|
|
def main(): # type: () -> None
|
2016-12-21 18:56:23 -05:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-11-12 16:18:17 -05:00
|
|
|
if args.debug == 0:
|
|
|
|
log_level = logging.CRITICAL
|
|
|
|
elif args.debug == 1:
|
|
|
|
log_level = logging.ERROR
|
|
|
|
elif args.debug == 2:
|
|
|
|
log_level = logging.WARNING
|
|
|
|
elif args.debug == 3:
|
|
|
|
log_level = logging.INFO
|
|
|
|
else:
|
|
|
|
log_level = logging.DEBUG
|
2019-11-22 00:25:43 -05:00
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)
|
2018-11-12 16:18:17 -05:00
|
|
|
|
2022-02-17 02:27:34 -05:00
|
|
|
kwargs = {k: v for k, v in vars(args).items() if v is not None}
|
2023-11-01 05:27:36 -04:00
|
|
|
project_desc = get_project_desc(kwargs['prog'])
|
2022-04-26 09:24:01 -04:00
|
|
|
# pass the extra_gdbinit_file if the build is reproducible
|
2023-11-08 06:53:00 -05:00
|
|
|
if project_desc:
|
|
|
|
kwargs['extra_gdbinit_file'] = project_desc.get('debug_prefix_map_gdbinit')
|
|
|
|
kwargs['parttable_off'] = get_sdkconfig_value(project_desc['config_file'], 'CONFIG_PARTITION_TABLE_OFFSET')
|
2022-02-17 02:27:34 -05:00
|
|
|
|
2022-08-10 03:01:57 -04:00
|
|
|
del kwargs['debug']
|
|
|
|
del kwargs['operation']
|
2022-02-17 02:27:34 -05:00
|
|
|
|
2023-11-01 05:27:36 -04:00
|
|
|
try:
|
|
|
|
espcoredump = CoreDump(**kwargs)
|
|
|
|
except TypeError as e:
|
|
|
|
# 'parttable_off' was added in esp-coredump 1.5.2
|
|
|
|
# remove argument and retry without it
|
|
|
|
if 'parttable_off' in str(e):
|
|
|
|
kwargs.pop('parttable_off')
|
|
|
|
espcoredump = CoreDump(**kwargs)
|
|
|
|
else:
|
|
|
|
raise
|
2021-04-08 23:39:37 -04:00
|
|
|
temp_core_files = None
|
2022-02-17 02:27:34 -05:00
|
|
|
|
2021-04-08 23:39:37 -04:00
|
|
|
try:
|
|
|
|
if args.operation == 'info_corefile':
|
2022-02-17 02:27:34 -05:00
|
|
|
temp_core_files = espcoredump.info_corefile()
|
2021-04-08 23:39:37 -04:00
|
|
|
elif args.operation == 'dbg_corefile':
|
2022-02-17 02:27:34 -05:00
|
|
|
temp_core_files = espcoredump.dbg_corefile()
|
2021-04-08 23:39:37 -04:00
|
|
|
else:
|
|
|
|
raise ValueError('Please specify action, should be info_corefile or dbg_corefile')
|
|
|
|
finally:
|
|
|
|
if temp_core_files:
|
|
|
|
for f in temp_core_files:
|
|
|
|
try:
|
|
|
|
os.remove(f)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2022-02-17 02:27:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|