|  | #!/bin/bash | 
|  | # Copyright 2018 The Fuchsia Authors. All rights reserved. | 
|  | # Use of this source code is governed by a BSD-style license that can be | 
|  | # found in the LICENSE file. | 
|  |  | 
|  | #### CATEGORY=Run, inspect and debug | 
|  | ### symbolize backtraces and program locations provided as input on stdin | 
|  |  | 
|  | ## usage: fx symbolize [--experimental] [--legacy] [-i IDS_TXT_FILE] | 
|  | ##                     [-r | --remote-symbols] | 
|  | ## | 
|  | ##    --experimental          use the experimental C++ symbolizer | 
|  | ## | 
|  | ##    --legacy                use the legacy Go symbolizer | 
|  | ## | 
|  | ##    -i IDS_TXT_FILE         specify an ids.txt file to be used | 
|  | ## | 
|  | ##    --remote-symbols|-r     attemp to resolve symbols using predefined remote | 
|  | ##                            symbol servers for symbols that cannot be resolved | 
|  | ##                            locally. | 
|  | ## | 
|  | ##    --auth                  (C++ symbolizer) start the authentication process. | 
|  | ## | 
|  | ##   Converts sybmolizer markup found in stdin to human readable using | 
|  | ##   local symbols (and remote if the --remote-symbols option is used). | 
|  | ##   This command is usually used on log output from either zx_log or syslog. | 
|  | ##   Anything that is not valid symbolizer markup is left alone. This is similar | 
|  | ##   to how c++filt works for demangling C++ symbols. | 
|  | ## | 
|  | ##   By default, `fx log` and `fx klog` automatically pipe their results into | 
|  | ##   this command. | 
|  | ## | 
|  | ##   --experimental / --legacy controls which implementation to use. The default | 
|  | ##   is to use the experimental symbolizer. | 
|  | ## | 
|  | ## Authentication | 
|  | ## | 
|  | ##   If the `--remote-symbols` option is specified, authentication for the symbol | 
|  | ##   server is needed. | 
|  | ## | 
|  | ##   When using the Go symbolizer, `fx symbolize` will fail if the current user | 
|  | ##   is not authenticated or doesn't have enough privileges on the symbol | 
|  | ##   servers. To ensure that the current user is properly authenticated, run: | 
|  | ##      `gcloud auth application-default login` | 
|  | ## | 
|  | ##   When using the C++ implementation, failed or missing authentication will be | 
|  | ##   silent. To authenticate, run: | 
|  | ##      `fx symbolize --experimental --auth` | 
|  |  | 
|  | source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $? | 
|  |  | 
|  | function go_symbolizer { | 
|  | fx-config-read | 
|  | source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/contrib/lib/symbol-index.sh || exit $? | 
|  | ensure-symbol-index-registered || fx-warn "Failed to register ${FUCHSIA_DIR} in symbol-index!" | 
|  | local symbolize="${HOST_OUT_DIR}/symbolize" | 
|  | if [[ ! -x "${symbolize}" ]]; then | 
|  | fx-error "Cannot execute '${symbolize}'. Try \`fx build\` first." | 
|  | exit 1 | 
|  | fi | 
|  | local llvm_symbolizer="${PREBUILT_CLANG_DIR}/bin/llvm-symbolizer" | 
|  | local extra_args=() | 
|  | for i in "${ids_txt[@]}"; do | 
|  | extra_args+=(-ids-txt "${i}") | 
|  | done | 
|  | if [[ "${is_remote}" -eq 1 ]]; then | 
|  | extra_args+=( | 
|  | "-symbol-cache" "$HOME/.fuchsia-symbol-cache" | 
|  | "-symbol-server" "gs://fuchsia-artifacts/debug" | 
|  | "-symbol-server" "gs://fuchsia-artifacts-internal/debug" | 
|  | "-symbol-server" "gs://fuchsia-artifacts-release/debug" | 
|  | ) | 
|  | trap '_code=$?; set +x; exit-with-warning ${_code}' ERR | 
|  | fi | 
|  | "${symbolize}" -llvm-symbolizer "${llvm_symbolizer}" -ids-rel "${extra_args[@]}" | 
|  | } | 
|  |  | 
|  | function cpp_symbolizer { | 
|  | fx-config-read | 
|  | source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/contrib/lib/symbol-index.sh || exit $? | 
|  | ensure-symbol-index-registered || fx-warn "Failed to register ${FUCHSIA_DIR} in symbol-index!" | 
|  | local symbolizer="${HOST_OUT_DIR}/symbolizer" | 
|  | if [[ ! -x "${symbolizer}" ]]; then | 
|  | fx-error "Cannot execute '${symbolizer}'. Try \`fx build\` first." | 
|  | exit 1 | 
|  | fi | 
|  | local extra_args=() | 
|  | for i in "${ids_txt[@]}"; do | 
|  | extra_args+=(--ids-txt "${i}") | 
|  | done | 
|  | if [[ "${is_remote}" -eq 1 ]]; then | 
|  | if [[ ! -f ~/.fuchsia/debug/googleapi_auth && ! -f ~/.config/gcloud/application_default_credentials.json ]]; then | 
|  | fx-error "Please authenticate first using \`fx symbolize --auth\`." | 
|  | exit 1 | 
|  | fi | 
|  | extra_args+=( | 
|  | "--symbol-server" "gs://fuchsia-artifacts/debug" | 
|  | "--symbol-server" "gs://fuchsia-artifacts-internal/debug" | 
|  | "--symbol-server" "gs://fuchsia-artifacts-release/debug" | 
|  | ) | 
|  | fi | 
|  | if [[ "${auth_mode}" -eq 1 ]]; then | 
|  | extra_args+=(--auth) | 
|  | fi | 
|  | "${symbolizer}" "${extra_args[@]}" | 
|  | } | 
|  |  | 
|  | function exit-with-warning { | 
|  | local error_code="$1" | 
|  | if [[ "${error_code}" -ne 0 ]]; then | 
|  | fx-error "If you are having trouble with authentication, make sure you are" \ | 
|  | "authenticated in gcloud and you have permission to access" \ | 
|  | "the remote symbols servers.\nSee \`fx help symbolize\` for more information" | 
|  | fi | 
|  | exit ${error_code} | 
|  | } | 
|  |  | 
|  | declare ids_txt=() | 
|  | declare is_remote=0 | 
|  | declare auth_mode=0 | 
|  | declare use_experimental=1 | 
|  |  | 
|  | while [[ $# -ne 0 ]]; do | 
|  | case "$1" in | 
|  | -h|--help) | 
|  | fx-command-help | 
|  | exit 0 | 
|  | ;; | 
|  | --experimental) | 
|  | use_experimental=1 | 
|  | ;; | 
|  | --legacy) | 
|  | use_experimental=0 | 
|  | ;; | 
|  | --auth) | 
|  | auth_mode=1 | 
|  | ;; | 
|  | -i) | 
|  | if [[ $# -lt 2 ]]; then | 
|  | fx-error Invalid syntax | 
|  | fx-command-help | 
|  | return 1 | 
|  | fi | 
|  | ids_txt+=("$2") | 
|  | shift | 
|  | ;; | 
|  | --remote-symbols|-r) | 
|  | is_remote=1 | 
|  | ;; | 
|  | *) | 
|  | fx-error Invalid syntax | 
|  | fx-command-help | 
|  | exit 1 | 
|  | esac | 
|  | shift | 
|  | done | 
|  |  | 
|  | if [[ "${use_experimental}" -eq 1 ]]; then | 
|  | cpp_symbolizer | 
|  | else | 
|  | go_symbolizer | 
|  | fi |