1#!/usr/bin/env bash
2
3# Copyright 2017 The Fuchsia Authors
4#
5# Use of this source code is governed by a MIT-style
6# license that can be found in the LICENSE file or at
7# https://opensource.org/licenses/MIT
8
9# Dump information about this host that may help with development efforts.
10
11set -u -e
12
13LSPCI_FILE_PREFIX="lspci"
14LSUSB_FILE_PREFIX="lsusb"
15CPUID_FILE_PREFIX="cpuid"
16CPUINFO_FILE="cpuinfo.txt"
17ACPI_FILE="acpidump"
18MSR_FILE="msr.txt"
19
20# If this is non-zero, unload the msr kmod when exiting
21UNLOAD_MSR_MOD=0
22
23function print_usage() {
24  echo "Usage: $0 <machine_name>"
25  echo "machine_name: A string identifying the hardware"
26  echo
27  echo "This will output a file named 'system_info.<machine_name>.tar.bz2'"
28}
29
30function check_utility() {
31  if ! which "$1" &> /dev/null; then
32    echo "Please install '$1' (Ubuntu package '$2')"
33    exit
34  fi
35}
36
37function dump_pci() {
38  echo "Dumping PCI..."
39  sudo lspci -v > "$DIR/${LSPCI_FILE_PREFIX}-v.txt"
40  sudo lspci -vv > "$DIR/${LSPCI_FILE_PREFIX}-vv.txt"
41  sudo lspci -n > "$DIR/${LSPCI_FILE_PREFIX}-n.txt"
42}
43
44function dump_usb() {
45  echo "Dumping USB..."
46  sudo lsusb -t > "$DIR/${LSUSB_FILE_PREFIX}-t.txt"
47  sudo lsusb -v > "$DIR/${LSUSB_FILE_PREFIX}-v.txt"
48}
49
50function dump_acpi() {
51  echo "Dumping ACPI..."
52  sudo acpidump -o "$DIR/${ACPI_FILE}"
53}
54
55function dump_cpuinfo() {
56  echo "Dumping CPU info..."
57  cat /proc/cpuinfo > "$DIR/${CPUINFO_FILE}"
58}
59
60function dump_cpuid() {
61  echo "Dumping CPUID tables..."
62  cpuid > "$DIR/${CPUID_FILE_PREFIX}.txt"
63  cpuid -r > "$DIR/${CPUID_FILE_PREFIX}.raw"
64}
65
66function dump_msrs() {
67  if ! lsmod | grep "^msr " &> /dev/null; then
68    UNLOAD_MSR_MOD=1
69    echo "Loading MSR kmod..."
70    sudo modprobe msr
71  fi
72  echo "Dumping useful MSRs..."
73  for msr in 480 481 482 483 484 485 486 487 488 489 \
74             48a 48b 48c 48d 48e 48f 490 491; do
75    (echo -n "$msr: "; sudo rdmsr -0 "0x$msr" 2>/dev/null || echo unavailable) >> "$DIR/$MSR_FILE"
76  done
77}
78
79if [ $# -lt 1 ]; then
80  print_usage
81  exit 1
82fi
83
84MACHINE_NAME="$1"
85BUNDLE_DIR="system_info.${MACHINE_NAME}"
86BUNDLE_TAR="${BUNDLE_DIR}.tar.bz2"
87
88if [ -e "$BUNDLE_TAR" ]; then
89  echo "$BUNDLE_TAR already exists.  Please delete before running."
90  exit 1
91fi
92
93check_utility lspci pciutils
94check_utility lsusb usbutils
95check_utility acpidump acpica-tools
96check_utility cpuid cpuid
97check_utility rdmsr msr-tools
98
99TMPDIR="$(mktemp -d)"
100function on_exit() {
101  rm -rf "$TMPDIR"
102  if [ "$UNLOAD_MSR_MOD" -ne 0 ]; then
103    echo "Unloading MSR kmod..."
104    sudo modprobe -r msr || echo "Failed to unload msr kmod!"
105  fi
106}
107trap on_exit EXIT
108
109DIR="$TMPDIR/${BUNDLE_DIR}"
110mkdir "$DIR"
111
112dump_pci
113dump_usb
114dump_acpi
115dump_cpuinfo
116dump_cpuid
117dump_msrs
118tar -C "$TMPDIR" -cjf "$BUNDLE_TAR" "$BUNDLE_DIR"
119echo "Finished building $BUNDLE_TAR"
120