main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/Process.h>
  13. #include <LibFileSystem/FileSystem.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. auto executable_path_or_error = arguments[0].contains('/')
  40. ? FileSystem::real_path(arguments[0])
  41. : Core::System::resolve_executable_from_environment(arguments[0]);
  42. if (executable_path_or_error.is_error()) {
  43. reportln("Cannot find executable for '{}'."sv, arguments[0]);
  44. return 1;
  45. }
  46. auto executable_path = executable_path_or_error.release_value().to_deprecated_string();
  47. if (dump_profile && profile_dump_path.is_empty())
  48. profile_dump_path = DeprecatedString::formatted("{}.{}.profile", LexicalPath(executable_path).basename(), getpid());
  49. OwnPtr<Stream> profile_stream;
  50. OwnPtr<Vector<NonnullOwnPtr<DeprecatedString>>> profile_strings;
  51. OwnPtr<Vector<int>> profile_string_id_map;
  52. if (dump_profile) {
  53. auto profile_stream_or_error = Core::File::open(profile_dump_path, Core::File::OpenMode::Write);
  54. if (profile_stream_or_error.is_error()) {
  55. warnln("Failed to open '{}' for writing: {}", profile_dump_path, profile_stream_or_error.error());
  56. return 1;
  57. }
  58. profile_stream = profile_stream_or_error.release_value();
  59. profile_strings = make<Vector<NonnullOwnPtr<DeprecatedString>>>();
  60. profile_string_id_map = make<Vector<int>>();
  61. profile_stream->write_until_depleted(R"({"events":[)"sv.bytes()).release_value_but_fixme_should_propagate_errors();
  62. timeval tv {};
  63. gettimeofday(&tv, nullptr);
  64. profile_stream->write_until_depleted(
  65. DeprecatedString::formatted(
  66. R"~({{"type": "process_create", "parent_pid": 1, "executable": "{}", "pid": {}, "tid": {}, "timestamp": {}, "lost_samples": 0, "stack": []}})~",
  67. executable_path, getpid(), gettid(), tv.tv_sec * 1000 + tv.tv_usec / 1000)
  68. .bytes())
  69. .release_value_but_fixme_should_propagate_errors();
  70. }
  71. Vector<DeprecatedString> environment;
  72. for (int i = 0; env[i]; ++i) {
  73. environment.append(env[i]);
  74. }
  75. // FIXME: It might be nice to tear down the emulator properly.
  76. auto& emulator = *new UserspaceEmulator::Emulator(executable_path, arguments, environment);
  77. emulator.set_profiling_details(dump_profile, profile_instruction_interval, profile_stream, profile_strings, profile_string_id_map);
  78. emulator.set_in_region_of_interest(!enable_roi_mode);
  79. if (!emulator.load_elf())
  80. return 1;
  81. StringBuilder builder;
  82. builder.append("(UE) "sv);
  83. builder.append(LexicalPath::basename(arguments[0]));
  84. if (auto result = Core::Process::set_name(builder.string_view(), Core::Process::SetThreadName::Yes); result.is_error()) {
  85. reportln("Core::Process::set_name: {}"sv, result.error());
  86. return 1;
  87. }
  88. if (pause_on_startup)
  89. emulator.pause();
  90. int rc = emulator.exec();
  91. if (dump_profile) {
  92. emulator.profile_stream().write_until_depleted("], \"strings\": ["sv.bytes()).release_value_but_fixme_should_propagate_errors();
  93. if (emulator.profiler_strings().size()) {
  94. for (size_t i = 0; i < emulator.profiler_strings().size() - 1; ++i)
  95. emulator.profile_stream().write_until_depleted(DeprecatedString::formatted("\"{}\", ", emulator.profiler_strings().at(i)).bytes()).release_value_but_fixme_should_propagate_errors();
  96. emulator.profile_stream().write_until_depleted(DeprecatedString::formatted("\"{}\"", emulator.profiler_strings().last()).bytes()).release_value_but_fixme_should_propagate_errors();
  97. }
  98. emulator.profile_stream().write_until_depleted("]}"sv.bytes()).release_value_but_fixme_should_propagate_errors();
  99. }
  100. return rc;
  101. }