objdump.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2023 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <filesystem>
  15. #include <iostream>
  16. #include "extract_source.h"
  17. #include "source/opt/log.h"
  18. #include "tools/io.h"
  19. #include "tools/util/cli_consumer.h"
  20. #include "tools/util/flags.h"
  21. namespace {
  22. constexpr auto kHelpTextFmt =
  23. R"(%s - Dumps information from a SPIR-V binary.
  24. Usage: %s [options] <filename>
  25. one of the following switches must be given:
  26. --source Extract source files obtained from debug symbols, output to stdout.
  27. --entrypoint Extracts the entrypoint name of the module, output to stdout.
  28. --compiler-cmd Extracts the command line used to compile this module, output to stdout.
  29. General options:
  30. -h, --help Print this help.
  31. --version Display assembler version information.
  32. -f,--force Allow output file overwrite.
  33. Source dump options:
  34. --list Do not extract source code, only print filenames to stdout.
  35. --outdir Where shall the exrtacted HLSL/HLSL files be written to?
  36. File written to stdout if '-' is given. Default is `-`.
  37. )";
  38. // Removes trailing '/' from `input`.
  39. // A behavior difference has been observed between libc++ implementations.
  40. // Fixing path to prevent this edge case to be reached.
  41. // (https://github.com/llvm/llvm-project/issues/60634)
  42. std::string fixPathForLLVM(std::string input) {
  43. while (!input.empty() && input.back() == '/') input.resize(input.size() - 1);
  44. return input;
  45. }
  46. // Write each HLSL file described in `sources` in a file in `outdirPath`.
  47. // Doesn't ovewrite existing files, unless `overwrite` is set to true. The
  48. // created HLSL file's filename is the path's filename obtained from `sources`.
  49. // Returns true if all files could be written. False otherwise.
  50. bool OutputSourceFiles(
  51. const std::unordered_map<std::string, std::string>& sources,
  52. const std::string& outdirPath, bool overwrite) {
  53. std::filesystem::path outdir(fixPathForLLVM(outdirPath));
  54. if (!std::filesystem::is_directory(outdir)) {
  55. if (!std::filesystem::create_directories(outdir)) {
  56. std::cerr << "error: could not create output directory " << outdir
  57. << std::endl;
  58. return false;
  59. }
  60. }
  61. for (const auto & [ filepath, code ] : sources) {
  62. if (code.empty()) {
  63. std::cout << "Ignoring source for " << filepath
  64. << ": no code source in debug infos." << std::endl;
  65. continue;
  66. }
  67. std::filesystem::path old_path(filepath);
  68. std::filesystem::path new_path = outdir / old_path.filename();
  69. if (!overwrite && std::filesystem::exists(new_path)) {
  70. std::cerr << "file " << filepath
  71. << " already exists, aborting (use --overwrite to allow it)."
  72. << std::endl;
  73. return false;
  74. }
  75. std::cout << "Exporting " << new_path << std::endl;
  76. if (!WriteFile<char>(new_path.string().c_str(), "w", code.c_str(),
  77. code.size())) {
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. } // namespace
  84. // clang-format off
  85. FLAG_SHORT_bool( h, /* default_value= */ false, /* required= */ false);
  86. FLAG_LONG_bool( help, /* default_value= */ false, /* required= */ false);
  87. FLAG_LONG_bool( version, /* default_value= */ false, /* required= */ false);
  88. FLAG_LONG_bool( source, /* default_value= */ false, /* required= */ false);
  89. FLAG_LONG_bool( entrypoint, /* default_value= */ false, /* required= */ false);
  90. FLAG_LONG_bool( compiler_cmd, /* default_value= */ false, /* required= */ false);
  91. FLAG_SHORT_bool( f, /* default_value= */ false, /* required= */ false);
  92. FLAG_LONG_bool( force, /* default_value= */ false, /* required= */ false);
  93. FLAG_LONG_string( outdir, /* default_value= */ "-", /* required= */ false);
  94. FLAG_LONG_bool( list, /* default_value= */ false, /* required= */ false);
  95. // clang-format on
  96. int main(int, const char** argv) {
  97. if (!flags::Parse(argv)) {
  98. return 1;
  99. }
  100. if (flags::h.value() || flags::help.value()) {
  101. printf(kHelpTextFmt, argv[0], argv[0]);
  102. return 0;
  103. }
  104. if (flags::version.value()) {
  105. printf("%s\n", spvSoftwareVersionDetailsString());
  106. return 0;
  107. }
  108. if (flags::positional_arguments.size() != 1) {
  109. std::cerr << "Expected exactly one input file." << std::endl;
  110. return 1;
  111. }
  112. if (flags::entrypoint.value() || flags::compiler_cmd.value()) {
  113. std::cerr << "Unimplemented flags." << std::endl;
  114. return 1;
  115. }
  116. std::vector<uint32_t> binary;
  117. if (!ReadBinaryFile(flags::positional_arguments[0].c_str(), &binary)) {
  118. return 1;
  119. }
  120. if (flags::source.value()) {
  121. std::unordered_map<std::string, std::string> sourceCode;
  122. if (!ExtractSourceFromModule(binary, &sourceCode)) {
  123. return 1;
  124. }
  125. if (flags::list.value()) {
  126. for (const auto & [ filename, source ] : sourceCode) {
  127. printf("%s\n", filename.c_str());
  128. }
  129. return 0;
  130. }
  131. const bool outputToConsole = flags::outdir.value() == "-";
  132. if (outputToConsole) {
  133. for (const auto & [ filename, source ] : sourceCode) {
  134. std::cout << filename << ":" << std::endl
  135. << source << std::endl
  136. << std::endl;
  137. }
  138. return 0;
  139. }
  140. const std::filesystem::path outdirPath(flags::outdir.value());
  141. if (!OutputSourceFiles(sourceCode, outdirPath.string(),
  142. flags::force.value())) {
  143. return 1;
  144. }
  145. }
  146. // FIXME: implement logic.
  147. return 0;
  148. }