main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Emulator.h"
  7. #include <AK/FileStream.h>
  8. #include <AK/Format.h>
  9. #include <AK/LexicalPath.h>
  10. #include <AK/StringBuilder.h>
  11. #include <LibCore/ArgsParser.h>
  12. #include <LibCore/DirIterator.h>
  13. #include <LibCore/File.h>
  14. #include <fcntl.h>
  15. #include <pthread.h>
  16. #include <serenity.h>
  17. #include <string.h>
  18. bool g_report_to_debug = false;
  19. int main(int argc, char** argv, char** env)
  20. {
  21. Vector<StringView> arguments;
  22. bool pause_on_startup { false };
  23. String profile_dump_path;
  24. FILE* profile_output_file { nullptr };
  25. bool enable_roi_mode { false };
  26. bool dump_profile { false };
  27. unsigned profile_instruction_interval { 0 };
  28. Core::ArgsParser parser;
  29. parser.set_stop_on_first_non_option(true);
  30. parser.add_option(g_report_to_debug, "Write reports to the debug log", "report-to-debug", 0);
  31. parser.add_option(pause_on_startup, "Pause on startup", "pause", 'p');
  32. parser.add_option(dump_profile, "Generate a ProfileViewer-compatible profile", "profile", 0);
  33. parser.add_option(profile_instruction_interval, "Set the profile instruction capture interval, 128 by default", "profile-interval", 'i', "num_instructions");
  34. parser.add_option(profile_dump_path, "File path for profile dump", "profile-file", 0, "path");
  35. parser.add_option(enable_roi_mode, "Enable Region-of-Interest mode for profiling", "roi", 0);
  36. parser.add_positional_argument(arguments, "Command to emulate", "command");
  37. parser.parse(argc, argv);
  38. if (dump_profile && profile_instruction_interval == 0)
  39. profile_instruction_interval = 128;
  40. String executable_path;
  41. if (arguments[0].contains("/"sv))
  42. executable_path = Core::File::real_path_for(arguments[0]);
  43. else
  44. executable_path = Core::find_executable_in_path(arguments[0]);
  45. if (executable_path.is_empty()) {
  46. reportln("Cannot find executable for '{}'.", arguments[0]);
  47. return 1;
  48. }
  49. if (dump_profile && profile_dump_path.is_empty())
  50. profile_dump_path = String::formatted("{}.{}.profile", LexicalPath(executable_path).basename(), getpid());
  51. OwnPtr<OutputFileStream> profile_stream;
  52. OwnPtr<NonnullOwnPtrVector<String>> profile_strings;
  53. OwnPtr<Vector<int>> profile_string_id_map;
  54. if (dump_profile) {
  55. profile_output_file = fopen(profile_dump_path.characters(), "w+");
  56. if (profile_output_file == nullptr) {
  57. char const* error_string = strerror(errno);
  58. warnln("Failed to open '{}' for writing: {}", profile_dump_path, error_string);
  59. return 1;
  60. }
  61. profile_stream = make<OutputFileStream>(profile_output_file);
  62. profile_strings = make<NonnullOwnPtrVector<String>>();
  63. profile_string_id_map = make<Vector<int>>();
  64. profile_stream->write_or_error(R"({"events":[)"sv.bytes());
  65. timeval tv {};
  66. gettimeofday(&tv, nullptr);
  67. profile_stream->write_or_error(
  68. String::formatted(
  69. R"~({{"type": "process_create", "parent_pid": 1, "executable": "{}", "pid": {}, "tid": {}, "timestamp": {}, "lost_samples": 0, "stack": []}})~",
  70. executable_path, getpid(), gettid(), tv.tv_sec * 1000 + tv.tv_usec / 1000)
  71. .bytes());
  72. }
  73. Vector<String> environment;
  74. for (int i = 0; env[i]; ++i) {
  75. environment.append(env[i]);
  76. }
  77. // FIXME: It might be nice to tear down the emulator properly.
  78. auto& emulator = *new UserspaceEmulator::Emulator(executable_path, arguments, environment);
  79. emulator.set_profiling_details(dump_profile, profile_instruction_interval, profile_stream, profile_strings, profile_string_id_map);
  80. emulator.set_in_region_of_interest(!enable_roi_mode);
  81. if (!emulator.load_elf())
  82. return 1;
  83. StringBuilder builder;
  84. builder.append("(UE) ");
  85. builder.append(LexicalPath::basename(arguments[0]));
  86. if (set_process_name(builder.string_view().characters_without_null_termination(), builder.string_view().length()) < 0) {
  87. perror("set_process_name");
  88. return 1;
  89. }
  90. int rc = pthread_setname_np(pthread_self(), builder.to_string().characters());
  91. if (rc != 0) {
  92. reportln("pthread_setname_np: {}", strerror(rc));
  93. return 1;
  94. }
  95. if (pause_on_startup)
  96. emulator.pause();
  97. rc = emulator.exec();
  98. if (dump_profile) {
  99. emulator.profile_stream().write_or_error("], \"strings\": ["sv.bytes());
  100. if (emulator.profiler_strings().size()) {
  101. for (size_t i = 0; i < emulator.profiler_strings().size() - 1; ++i)
  102. emulator.profile_stream().write_or_error(String::formatted("\"{}\", ", emulator.profiler_strings().at(i)).bytes());
  103. emulator.profile_stream().write_or_error(String::formatted("\"{}\"", emulator.profiler_strings().last()).bytes());
  104. }
  105. emulator.profile_stream().write_or_error("]}"sv.bytes());
  106. }
  107. return rc;
  108. }