|
| 1 | +/* |
| 2 | + * Copyright 2018- The Pixie Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | + |
| 19 | +#include "src/common/base/base.h" |
| 20 | +#include "src/common/base/env.h" |
| 21 | +#include "src/common/json/json.h" |
| 22 | +#include "src/stirling/obj_tools/dwarf_reader.h" |
| 23 | +#include "src/stirling/source_connectors/socket_tracer/uprobe_symaddrs.h" |
| 24 | + |
| 25 | +using px::StatusOr; |
| 26 | +using px::stirling::obj_tools::ArgInfo; |
| 27 | +using px::stirling::obj_tools::DwarfReader; |
| 28 | +using px::stirling::obj_tools::ElfReader; |
| 29 | +using px::utils::ToJSONString; |
| 30 | + |
| 31 | +//----------------------------------------------------------------------------- |
| 32 | +// This utility is designed to isolate parsing the debug symbols of a Go binary. This |
| 33 | +// verifies that the go version detection code is functioning as well. This is useful |
| 34 | +// for debugging when the Go elf/DWARF parsing is not working correctly and has been the |
| 35 | +// source of a few PEM crashes (gh#1300, gh#1646). This makes it easy for asking end users to run |
| 36 | +// against their binaries when they are sensitive (proprietary) and we can't debug them ourselves. |
| 37 | +//----------------------------------------------------------------------------- |
| 38 | + |
| 39 | +DEFINE_string(binary, "", "The binary to parse. Required argument"); |
| 40 | +DEFINE_string( |
| 41 | + func_names, "", |
| 42 | + "Comma separated list of function args to parse. Required argument. Example: foo,bar"); |
| 43 | +DEFINE_bool(json_output, true, "Whether to use JSON output when printing to stdout"); |
| 44 | + |
| 45 | +void PrintArgsAsJSON(const std::map<std::string, ArgInfo>& args) { |
| 46 | + px::utils::JSONObjectBuilder builder; |
| 47 | + for (const auto& [name, arg] : args) { |
| 48 | + builder.WriteKVRecursive(name, arg); |
| 49 | + } |
| 50 | + std::cout << builder.GetString() << "\n"; |
| 51 | +} |
| 52 | + |
| 53 | +int main(int argc, char** argv) { |
| 54 | + px::EnvironmentGuard env_guard(&argc, argv); |
| 55 | + |
| 56 | + if (FLAGS_binary.empty() || FLAGS_func_names.empty()) { |
| 57 | + LOG(FATAL) << absl::Substitute( |
| 58 | + "Expected --binary and --func_names arguments to be provided. Instead received $0", *argv); |
| 59 | + } |
| 60 | + |
| 61 | + StatusOr<std::unique_ptr<ElfReader>> elf_reader_status = ElfReader::Create(FLAGS_binary); |
| 62 | + if (!elf_reader_status.ok()) { |
| 63 | + LOG(WARNING) << absl::Substitute( |
| 64 | + "Failed to parse elf binary $0 with" |
| 65 | + "Message = $1", |
| 66 | + FLAGS_binary, elf_reader_status.msg()); |
| 67 | + } |
| 68 | + std::unique_ptr<ElfReader> elf_reader = elf_reader_status.ConsumeValueOrDie(); |
| 69 | + |
| 70 | + StatusOr<std::unique_ptr<DwarfReader>> dwarf_reader_status = |
| 71 | + DwarfReader::CreateIndexingAll(FLAGS_binary); |
| 72 | + if (!dwarf_reader_status.ok()) { |
| 73 | + VLOG(1) << absl::Substitute( |
| 74 | + "Failed to get binary $0 debug symbols. " |
| 75 | + "Message = $1", |
| 76 | + FLAGS_binary, dwarf_reader_status.msg()); |
| 77 | + } |
| 78 | + std::unique_ptr<DwarfReader> dwarf_reader = dwarf_reader_status.ConsumeValueOrDie(); |
| 79 | + |
| 80 | + for (const auto& func_name : absl::StrSplit(FLAGS_func_names, ',')) { |
| 81 | + if (func_name.empty()) { |
| 82 | + LOG(FATAL) << absl::Substitute("Empty function name provided in --func_names: $0", |
| 83 | + FLAGS_func_names); |
| 84 | + } |
| 85 | + auto args_or_status = dwarf_reader->GetFunctionArgInfo(func_name); |
| 86 | + |
| 87 | + if (!args_or_status.ok()) { |
| 88 | + LOG(ERROR) << absl::Substitute("debug symbol parsing failed with: $0", args_or_status.msg()); |
| 89 | + } |
| 90 | + auto args = args_or_status.ConsumeValueOrDie(); |
| 91 | + if (!FLAGS_json_output) { |
| 92 | + for (const auto& [name, arg] : args) { |
| 93 | + LOG(INFO) << absl::Substitute("Arg $0: $1", name, arg.ToString()); |
| 94 | + } |
| 95 | + } else { |
| 96 | + PrintArgsAsJSON(args); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments