main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/Format.h>
  8. #include <AK/LexicalPath.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/DirIterator.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/Process.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. DeprecatedString profile_dump_path;
  24. bool enable_roi_mode { false };
  25. bool dump_profile { false };
  26. unsigned profile_instruction_interval { 0 };
  27. Core::ArgsParser parser;
  28. parser.set_stop_on_first_non_option(true);
  29. parser.add_option(g_report_to_debug, "Write reports to the debug log", "report-to-debug", 0);
  30. parser.add_option(pause_on_startup, "Pause on startup", "pause", 'p');
  31. parser.add_option(dump_profile, "Generate a ProfileViewer-compatible profile", "profile", 0);
  32. parser.add_option(profile_instruction_interval, "Set the profile instruction capture interval, 128 by default", "profile-interval", 'i', "num_instructions");
  33. parser.add_option(profile_dump_path, "File path for profile dump", "profile-file", 0, "path");
  34. parser.add_option(enable_roi_mode, "Enable Region-of-Interest mode for profiling", "roi", 0);
  35. parser.add_positional_argument(arguments, "Command to emulate", "command");
  36. parser.parse(argc, argv);
  37. if (dump_profile && profile_instruction_interval == 0)
  38. profile_instruction_interval = 128;
  39. DeprecatedString executable_path;
  40. if (arguments[0].contains("/"sv))
  41. executable_path = Core::File::real_path_for(arguments[0]);
  42. else
  43. executable_path = Core::File::resolve_executable_from_environment(arguments[0]).value_or({});
  44. if (executable_path.is_empty()) {
  45. reportln("Cannot find executable for '{}'."sv, arguments[0]);
  46. return 1;
  47. }
  48. if (dump_profile && profile_dump_path.is_empty())
  49. profile_dump_path = DeprecatedString::formatted("{}.{}.profile", LexicalPath(executable_path).basename(), getpid());
  50. OwnPtr<Core::Stream::Stream> profile_stream;
  51. OwnPtr<NonnullOwnPtrVector<DeprecatedString>> profile_strings;
  52. OwnPtr<Vector<int>> profile_string_id_map;
  53. if (dump_profile) {
  54. auto profile_stream_or_error = Core::Stream::File::open(profile_dump_path, Core::Stream::OpenMode::Write);
  55. if (profile_stream_or_error.is_error()) {
  56. warnln("Failed to open '{}' for writing: {}", profile_dump_path, profile_stream_or_error.error());
  57. return 1;
  58. }
  59. profile_stream = profile_stream_or_error.release_value();
  60. profile_strings = make<NonnullOwnPtrVector<DeprecatedString>>();
  61. profile_string_id_map = make<Vector<int>>();
  62. profile_stream->write_entire_buffer(R"({"events":[)"sv.bytes()).release_value_but_fixme_should_propagate_errors();
  63. timeval tv {};
  64. gettimeofday(&tv, nullptr);
  65. profile_stream->write_entire_buffer(
  66. DeprecatedString::formatted(
  67. R"~({{"type": "process_create", "parent_pid": 1, "executable": "{}", "pid": {}, "tid": {}, "timestamp": {}, "lost_samples": 0, "stack": []}})~",
  68. executable_path, getpid(), gettid(), tv.tv_sec * 1000 + tv.tv_usec / 1000)
  69. .bytes())
  70. .release_value_but_fixme_should_propagate_errors();
  71. }
  72. Vector<DeprecatedString> environment;
  73. for (int i = 0; env[i]; ++i) {
  74. environment.append(env[i]);
  75. }
  76. // FIXME: It might be nice to tear down the emulator properly.
  77. auto& emulator = *new UserspaceEmulator::Emulator(executable_path, arguments, environment);
  78. emulator.set_profiling_details(dump_profile, profile_instruction_interval, profile_stream, profile_strings, profile_string_id_map);
  79. emulator.set_in_region_of_interest(!enable_roi_mode);
  80. if (!emulator.load_elf())
  81. return 1;
  82. StringBuilder builder;
  83. builder.append("(UE) "sv);
  84. builder.append(LexicalPath::basename(arguments[0]));
  85. if (auto result = Core::Process::set_name(builder.string_view(), Core::Process::SetThreadName::Yes); result.is_error()) {
  86. reportln("Core::Process::set_name: {}"sv, result.error());
  87. return 1;
  88. }
  89. if (pause_on_startup)
  90. emulator.pause();
  91. int rc = emulator.exec();
  92. if (dump_profile) {
  93. emulator.profile_stream().write_entire_buffer("], \"strings\": ["sv.bytes()).release_value_but_fixme_should_propagate_errors();
  94. if (emulator.profiler_strings().size()) {
  95. for (size_t i = 0; i < emulator.profiler_strings().size() - 1; ++i)
  96. emulator.profile_stream().write_entire_buffer(DeprecatedString::formatted("\"{}\", ", emulator.profiler_strings().at(i)).bytes()).release_value_but_fixme_should_propagate_errors();
  97. emulator.profile_stream().write_entire_buffer(DeprecatedString::formatted("\"{}\"", emulator.profiler_strings().last()).bytes()).release_value_but_fixme_should_propagate_errors();
  98. }
  99. emulator.profile_stream().write_entire_buffer("]}"sv.bytes()).release_value_but_fixme_should_propagate_errors();
  100. }
  101. return rc;
  102. }