From 660b314f9024f77b23f3a14454f6f3a60091d3eb Mon Sep 17 00:00:00 2001 From: Hongjin Li Date: Sat, 20 Aug 2022 17:06:23 +0800 Subject: [PATCH] style: string format in python Signed-off-by: Hongjin Li Change-Id: Ie84a97ca5e0250991aa774b737eb7787e27d80af --- OAT.xml | 1 + tools/scripts/make_rootfs/rootfsdir.sh | 2 +- tools/scripts/parse_exc/parse_excinfo.py | 203 +++++++++---------- tools/scripts/parse_memory/parse_mem_info.sh | 15 +- 4 files changed, 106 insertions(+), 115 deletions(-) diff --git a/OAT.xml b/OAT.xml index edfeff4f..e25ecbec 100644 --- a/OAT.xml +++ b/OAT.xml @@ -23,6 +23,7 @@ + diff --git a/tools/scripts/make_rootfs/rootfsdir.sh b/tools/scripts/make_rootfs/rootfsdir.sh index eea8604d..e00cffdf 100755 --- a/tools/scripts/make_rootfs/rootfsdir.sh +++ b/tools/scripts/make_rootfs/rootfsdir.sh @@ -44,6 +44,6 @@ if [ -d "${BIN_DIR}" ] && [ "$(ls -A "${BIN_DIR}")" != "" ]; then fi cp -f ${LIB_DIR}/* ${ROOTFS_DIR}/lib -if [ -e ${ETC_DIR}/.mkshrc ]; then +if [ -e "${ETC_DIR}"/.mkshrc ]; then cp -f ${ETC_DIR}/.mkshrc ${ROOTFS_DIR}/etc fi diff --git a/tools/scripts/parse_exc/parse_excinfo.py b/tools/scripts/parse_exc/parse_excinfo.py index 91cfb5c2..feafa01c 100644 --- a/tools/scripts/parse_exc/parse_excinfo.py +++ b/tools/scripts/parse_exc/parse_excinfo.py @@ -1,8 +1,8 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -66,47 +66,43 @@ def parse_string_line(excinfo_file, string): def parse_kernel_pc_klr(excinfo_file, ohos_image_file, string, addr2line_cmd, objdump_cmd): #parse pc - f = open(excinfo_file, 'r+') - start = 0 - for lines in f.readlines(): - if 'excFrom: kernel' in lines: - if start == 1: - break - start = 1 - if start and string in lines: - lines = lines[lines.find(string):] - strlist = lines.split() - cmd = objdump_cmd + ohos_image_file + ' | grep ' + strlist[2][2:] + ': -B 10 -A 5 -w' - ret = commands.getoutput(cmd) - print(ret) - cmd = addr2line_cmd + ohos_image_file + ' ' + strlist[2] - ret = commands.getoutput(cmd) - ret = ret.split('\n') - print('<' + string + '>' + ret[0] + ' <' + strlist[2] + '>\n') - f.close() - return 0 - f.close() + with open(excinfo_file, 'r+') as f: + start = 0 + for lines in f.readlines(): + if 'excFrom: kernel' in lines: + if start == 1: + break + start = 1 + if start and string in lines: + lines = lines[lines.find(string):] + strlist = lines.split() + cmd = "%s%s | grep %s: -B 10 -A 5 -w" % (objdump_cmd, ohos_image_file, strlist[2][2:]) + ret = commands.getoutput(cmd) + print(ret) + cmd = "%s%s %s" % (addr2line_cmd, ohos_image_file, strlist[2]) + ret = commands.getoutput(cmd) + ret = ret.split('\n') + print("<%s>%s<%s>\n") % (string, ret[0], strlist[2]) + return 0 return -1 def parse_kernel_lr(excinfo_file, ohos_image_file, addr2line_cmd): - f = open(excinfo_file, 'r+') - start = 0 - index = 1 - for lines in f.readlines(): - if 'excFrom: kernel' in lines: - if start == 1: - break - start = 1 - if start and 'lr =' in lines: - lines = lines[lines.find('lr ='):] - strlist = lines.split() - cmd = addr2line_cmd + ohos_image_file + ' ' + strlist[2] - ret = commands.getoutput(cmd) - ret = ret.split('\n') - print('<%.2d'%index + '>' + ret[0] + ' <' + strlist[2] + '>') - index = index + 1 - - f.close() + with open(excinfo_file, 'r+') as f: + start = 0 + index = 1 + for lines in f.readlines(): + if 'excFrom: kernel' in lines: + if start == 1: + break + start = 1 + if start and 'lr =' in lines: + lines = lines[lines.find('lr ='):] + strlist = lines.split() + cmd = "%s%s %s" % (addr2line_cmd, ohos_image_file, strlist[2]) + ret = commands.getoutput(cmd) + ret = ret.split('\n') + print("<%.2d>%s<%s>" % (index, ret[0], strlist[2])) + index = index + 1 def parse_kernel_exc(excinfo_file, ohos_image_file, addr2line_cmd, objdump_cmd): #parse pc, klr @@ -118,56 +114,50 @@ def parse_kernel_exc(excinfo_file, ohos_image_file, addr2line_cmd, objdump_cmd): def parse_user_pc_ulr(excinfo_file, rootfs_dir, string, addr2line_cmd, objdump_cmd): #parse pc - f = open(excinfo_file, 'r+') - start = 0 - for lines in f.readlines(): - if 'excFrom: User' in lines: - if start == 1: - break - start = 1 - if start and string in lines: - lines = lines[lines.find(string):] - strlist = lines.split() - if len(strlist) < 7: - print('%s is error'%string) - f.close() + with open(excinfo_file, 'r+') as f: + start = 0 + for lines in f.readlines(): + if 'excFrom: User' in lines: + if start == 1: + break + start = 1 + if start and string in lines: + lines = lines[lines.find(string):] + strlist = lines.split() + if len(strlist) < 7: + print('%s is error'%string) + return 0 + cmd = "%s%s%s | grep %s: -B 10 -A 5 -w" % (objdump_cmd, rootfs_dir, strlist[4], strlist[6][2:]) + ret = commands.getoutput(cmd) + print(ret) + cmd = "%s%s%s %s" % (addr2line_cmd, rootfs_dir, strlist[4], strlist[6]) + #print(cmd) + ret = commands.getoutput(cmd) + ret = ret.split('\n') + print("<%s>%s<%s><%s>\n" % (string, ret[0], strlist[6], strlist[4])) return 0 - cmd = objdump_cmd + rootfs_dir + strlist[4] + ' | grep ' + strlist[6][2:] + ': -B 10 -A 5 -w' - ret = commands.getoutput(cmd) - print(ret) - cmd = addr2line_cmd + rootfs_dir + strlist[4] + ' ' + strlist[6] - #print(cmd) - ret = commands.getoutput(cmd) - ret = ret.split('\n') - print('<' + string + '>' + ret[0] + ' <' + strlist[6] + '>' + '<' + strlist[4] + '>\n') - f.close() - return 0 - f.close() return -1 def parse_user_lr(excinfo_file, rootfs_dir, addr2line_cmd): - f = open(excinfo_file, 'r+') - start = 0 - index = 1 - for lines in f.readlines(): - if 'excFrom: User' in lines: - if start == 1: - break - start = 1 - if start and 'lr =' in lines: - lines = lines[lines.find('lr ='):] - strlist = lines.split() - if len(strlist) < 11: - print('%s is error'%strlist) - f.close() - return - cmd = addr2line_cmd + rootfs_dir + strlist[8] + ' ' + strlist[10] - res = commands.getoutput(cmd) - res = res.split('\n') - print('<%.2d>'%index + res[0] + ' <' + strlist[10] + '>' + '<' + strlist[8] + '>') - index = index + 1 - - f.close() + with open(excinfo_file, 'r+') as f: + start = 0 + index = 1 + for lines in f.readlines(): + if 'excFrom: User' in lines: + if start == 1: + break + start = 1 + if start and 'lr =' in lines: + lines = lines[lines.find('lr ='):] + strlist = lines.split() + if len(strlist) < 11: + print('%s is error' % strlist) + return + cmd = "%s%s%s %s" % (addr2line_cmd, rootfs_dir, strlist[8], strlist[10]) + res = commands.getoutput(cmd) + res = res.split('\n') + print("<%.2d>%s<%s><%s>" % (index, res[0], strlist[10], strlist[8])) + index = index + 1 def parse_user_exc(excinfo_file, rootfs_dir, addr2line_cmd, objdump_cmd): #parse pc ulr @@ -178,30 +168,29 @@ def parse_user_exc(excinfo_file, rootfs_dir, addr2line_cmd, objdump_cmd): return ret1 and ret2 def parse_backtrace(backtrace_file, ohos_image_file, addr2line_cmd): - f = open(backtrace_file, 'r+') - find = -1 - start = 0 - index = 1 - for lines in f.readlines(): - if 'backtrace begin' in lines: - if start == 1: - break - start = 1 - if start and 'lr =' in lines: - lines = lines[lines.find('lr ='):] - strlist = lines.split() - cmd = addr2line_cmd + ohos_image_file + ' ' + strlist[2] - ret = commands.getoutput(cmd) - ret = ret.split('\n') - print('\n<%.2d'%index + '>' + ret[0] + ' <' + strlist[2] + '>') - index = index + 1 - find = 0 + with open(backtrace_file, 'r+') as f: + find = -1 + start = 0 + index = 1 + for lines in f.readlines(): + if 'backtrace begin' in lines: + if start == 1: + break + start = 1 + if start and 'lr =' in lines: + lines = lines[lines.find('lr ='):] + strlist = lines.split() + cmd = "%s%s %s" % (addr2line_cmd, ohos_image_file, strlist[2]) + ret = commands.getoutput(cmd) + ret = ret.split('\n') + print("\n<%.2d>%s<%s>" % (index, ret[0], strlist[2])) + index = index + 1 + find = 0 - f.close() return find def parse_excinfo(excinfo_file, ohos_image_file, rootfs_dir, addr2line_cmd, objdump_cmd): - cmd = 'dos2unix ' + excinfo_file + cmd = "dos2unix %s" % (excinfo_file) commands.getoutput(cmd) kernel_exc = is_kernel_exc(excinfo_file) user_exc = is_user_exc(excinfo_file) @@ -225,7 +214,7 @@ def parse_compiler(compiler): addr2line_cmd = '' objdump = '' objdump_cmd = '' - cmd = 'which ' + compiler + cmd = "which %s" % (compiler) ret = commands.getoutput(cmd) if ret == '': print('%s is not exist'%compiler) diff --git a/tools/scripts/parse_memory/parse_mem_info.sh b/tools/scripts/parse_memory/parse_mem_info.sh index 38d56859..eee82c62 100755 --- a/tools/scripts/parse_memory/parse_mem_info.sh +++ b/tools/scripts/parse_memory/parse_mem_info.sh @@ -1,7 +1,7 @@ #!/bin/bash # Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -28,6 +28,7 @@ # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +set -e LOAD_BASE="0x2000000" LLVM_ADDR2LINE=llvm-addr2line @@ -36,12 +37,12 @@ GCC_ADDR2LINE=addr2line get_line() { SYM_ADDR=$(echo $1 | awk '{print $2}') - ELF_OFFSET=`echo ${SYM_ADDR} | cut -d '[' -f2 | cut -d ']' -f1` - FILE_LINE=`${ADDR2LINE} -f -e $2 ${ELF_OFFSET} | awk 'NR==2'` + ELF_OFFSET=$(echo ${SYM_ADDR} | cut -d '[' -f2 | cut -d ']' -f1) + FILE_LINE=$(${ADDR2LINE} -f -e $2 ${ELF_OFFSET} | awk 'NR==2'`) if [[ "${FILE_LINE}" == *"?"* ]]; then typeset ELF_OFFSET=$((ELF_OFFSET+LOAD_BASE)) ELF_OFFSET=$(echo "obase=16;${ELF_OFFSET}" | bc) - FILE_LINE=`${ADDR2LINE} -f -e $2 ${ELF_OFFSET} | awk 'NR==2'` + FILE_LINE=$(${ADDR2LINE} -f -e $2 ${ELF_OFFSET} | awk 'NR==2') fi echo ${FILE_LINE} } @@ -58,12 +59,12 @@ parse_line() echo "Error: no such file: $i" exit 1 fi - FILE_LINE=`get_line "$1" $i` + FILE_LINE=$(get_line "$1" $i) if [[ "${FILE_LINE}" == *"?"* ]] || [ -z "${FILE_LINE}" ]; then echo " * Error: you need ensure whether file: "$i" was compiled with -g or not! *" exit 1 fi - LINE=`echo $1 | tr -d '\r'` + LINE=$(echo $1 | tr -d '\r') LINE=$(echo ${LINE} | awk '{print $1,$2}') echo " "${LINE}" at "${FILE_LINE} FLAG=true @@ -81,7 +82,7 @@ if [ $# -le 1 ]; then fi read -n5 -p "Compiler is [gcc/llvm]: " ANSWER -case ${ANSWER} in +case "${ANSWER}" in (gcc | GCC) which ${GCC_ADDR2LINE} >/dev/null 2>&1 if [ $? -eq 0 ]; then -- GitLab