blob: 8ef0631818519cb7876e4f83db7982e49862ccbf [file] [log] [blame]
Pi-Hsun Shihd3121bb2020-07-09 07:41:121#!/bin/bash
2# Copyright 2020 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Loads script libraries.
7CONTRIB_DIR=$(dirname "$(readlink -f "$0")")
8. "${CONTRIB_DIR}/common.sh" || exit 1
9
10FLAGS_HELP="
11Decode kernel stack trace from kernel logs.
12
13Read kernel logs from stdin, and print the decoded stack trace to stdout.
14
15Usage:
16 kernel_decode_stack -b <board>
17"
18
19# Flags
20DEFINE_string board "${DEFAULT_BOARD}" "Which board to decode stack trace" b
21
22# Parse command line.
23FLAGS "$@" || exit 1
24eval set -- "${FLAGS_ARGV}"
25set -e
26
27# Script must be run inside the chroot.
28assert_inside_chroot
29
30decode_stacktrace() {
31 local board="$1"
32 shift
33
34 local CHOST
35 CHOST=$("portageq-${board}" envvar CHOST)
36
37 local ARCH
38 ARCH=$("portageq-${board}" envvar ARCH)
39
40 # cros-kernel2.eclass falls back to tc-arch-kernel when CHROMEOS_KERNEL_ARCH
41 # is not set, but since tc-arch-kernel parse the arch from CHOST, we can just
42 # ignnore kernel_arch and use the original CHOST as CROSS_COMPILE in this
43 # case.
44 local kernel_arch
45 kernel_arch=$("portageq-${board}" envvar CHROMEOS_KERNEL_ARCH || true)
46
47 # This part is from cros-kernel2.eclass
48 # Support 64bit kernels w/32bit userlands.
49 local cross=${CHOST}
50 if [[ "${ARCH}:${kernel_arch}" == "arm:arm64" ]]; then
51 cross="aarch64-cros-linux-gnu"
52 fi
53
54 # Note: This would point to some temporary path if the kernel is not worked
55 # on, but it's ok since the decode_stacktrace.sh only use this path to strip
56 # the output.
57 #
58 # This is also why we always use the decode_stacktrace.sh from
59 # kernel/upstream instead of using the one in kernel_source_path.
60 local kernel_source_path
61 kernel_source_path=$(
62 objdump -WL "/build/${board}/usr/lib/debug/boot/vmlinux" |
63 grep 'include/linux/compiler.h:$' |
64 head -1 |
65 sed 's|\(.*\)/include/linux/compiler.h:$|\1|')
66
67 CROSS_COMPILE="${cross}-" \
68 "${SRC_ROOT}/third_party/kernel/upstream/scripts/decode_stacktrace.sh" \
69 "/build/${board}/usr/lib/debug/boot/vmlinux" \
70 "${kernel_source_path}" \
71 "/build/${board}/usr/lib/debug/lib/modules/"*/
72}
73
74main() {
75 if [[ -z "${FLAGS_board}" ]]; then
76 die "-b or --board required."
77 fi
78
79 decode_stacktrace "${FLAGS_board}" "$@"
80}
81
82main "$@"