๐Ÿ“ฆ apache / nuttx

๐Ÿ“„ btdecode.sh ยท 167 lines
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#!/usr/bin/env bash
############################################################################
# tools/btdecode.sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################

# This script can be used to decode the backtrace that's dumped on assertions.
#
# On assertions we can find the raw backtrace dump similar to:
# ...
# sched_dumpstack: backtrace| 0: 0x400e1a2a 0x40082912
# sched_dumpstack: backtrace| 1: 0x400e39ac 0x400ef7c3 0x400ef7fc 0x400e8116 0x400e7910 0x400e7be8 0x400e6c5c 0x400e6ad6
# sched_dumpstack: backtrace| 1: 0x400e6a99 0x400e4005 0x400e2754
# sched_dumpstack: backtrace| 2: 0x400f13ee 0x400e4005 0x400e2754
# ...
#
# Copy that to a file and call this script as:
#    ./tools/btdecode.sh esp32 backtrace_file
#
# The result should be similar to the following:
#    0x400e1a2a: function_name at file.c:line
#    0x40082912: function_name at file.c:line

USAGE="USAGE: ${0} chip|toolchain-addr2line backtrace_file [elf_file]
If the first argument contains 'addr2line', it will be used as the toolchain's addr2line tool.
Otherwise, the script will try to identify the toolchain based on the chip name."

GREP=${GREP:-grep}

VALID_CHIPS=(
  "esp32"
  "esp32s2"
  "esp32s3"
  "esp32c3"
  "esp32c6"
  "esp32h2"
)

# Make sure we have the required argument(s)

if [ -z "$2" ]; then
  echo "No backtrace supplied!"
  echo "$USAGE"
  exit 1
fi

elf_file="nuttx"

if [ -n "$3" ]; then
  elf_file=$3
fi

# Check if the first argument is an addr2line tool or a chip

chip_or_tool=$1
if [[ $chip_or_tool == *addr2line* ]]; then
  ADDR2LINE_TOOL=$chip_or_tool
else
  chip=$chip_or_tool
  if [[ ! " ${VALID_CHIPS[@]} " =~ " ${chip} " ]]; then
    echo "Invalid chip specified! Valid options are: ${VALID_CHIPS[*]}"
    echo "$USAGE"
    exit 4
  fi

  # Set the appropriate addr2line tool based on the chip
  case $chip in
    esp32)
      ADDR2LINE_TOOL="xtensa-esp32-elf-addr2line"
      ;;
    esp32s2)
      ADDR2LINE_TOOL="xtensa-esp32s2-elf-addr2line"
      ;;
    esp32s3)
      ADDR2LINE_TOOL="xtensa-esp32s3-elf-addr2line"
      ;;
    esp32c3)
      ADDR2LINE_TOOL="riscv-none-elf-addr2line"
      ;;
    esp32c6)
      ADDR2LINE_TOOL="riscv-none-elf-addr2line"
      ;;
    esp32h2)
      ADDR2LINE_TOOL="riscv-none-elf-addr2line"
      ;;
  esac
fi

# Make sure the elf file is accessible

if [ ! -f ${elf_file} ]; then
  echo "NuttX binaries not found!"
  exit 2
fi

# Check that the toolchain is in the PATH

if [ ! -x "$(command -v $ADDR2LINE_TOOL)" ]; then
  echo "Toolchain for $chip_or_tool not found!"
  exit 3
fi

# Decode backtrace

declare -A backtraces_before
declare -A backtraces_after
in_dump_tasks_section=false

while read -r line; do
  if [[ $line =~ (\[CPU[0-9]+\]\ )?dump_tasks: ]]; then
    in_dump_tasks_section=true
  fi

  if [[ $line =~ (\[CPU[0-9]+\]\ )?sched_dumpstack: ]]; then
    task_id=$(echo $line | ${GREP} -oP 'backtrace\|\s*\K\d+')
    addresses=$(echo $line | ${GREP} -oP '0x[0-9a-fA-F]+')
    if $in_dump_tasks_section; then
      if [[ -n "${backtraces_after[$task_id]}" ]]; then
        backtraces_after[$task_id]="${backtraces_after[$task_id]} $addresses"
      else
        backtraces_after[$task_id]="$addresses"
      fi
    else
      if [[ -n "${backtraces_before[$task_id]}" ]]; then
        backtraces_before[$task_id]="${backtraces_before[$task_id]} $addresses"
      else
        backtraces_before[$task_id]="$addresses"
      fi
    fi
  fi
done < "$2"

for task_id in "${!backtraces_before[@]}"; do
  echo "Backtrace for task $task_id:"
  for bt in ${backtraces_before[$task_id]}; do
    $ADDR2LINE_TOOL -pfiaCs -e ${elf_file} $bt
  done
  echo ""
done

if $in_dump_tasks_section; then
  echo "Backtrace dump for all tasks:"
  echo ""
  for task_id in "${!backtraces_after[@]}"; do
    echo "Backtrace for task $task_id:"
    for bt in ${backtraces_after[$task_id]}; do
      $ADDR2LINE_TOOL -pfiaCs -e ${elf_file} $bt
    done
    echo ""
  done
fi