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