2020-07-07 16:13:04 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-07 16:13:04 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Emulator.h"
|
2020-10-13 13:34:27 -03:00
|
|
|
#include <AK/Format.h>
|
2020-07-27 13:56:19 -03:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
|
#include <AK/StringBuilder.h>
|
2020-09-24 15:07:07 -03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-09-16 14:19:30 -03:00
|
|
|
#include <LibCore/DirIterator.h>
|
2020-07-27 13:56:19 -03:00
|
|
|
#include <pthread.h>
|
2021-03-12 13:29:37 -03:00
|
|
|
#include <serenity.h>
|
2020-07-27 13:56:19 -03:00
|
|
|
#include <string.h>
|
2020-07-07 16:13:04 -03:00
|
|
|
|
2020-10-24 04:45:51 -03:00
|
|
|
bool g_report_to_debug = false;
|
|
|
|
|
|
2020-07-27 10:57:12 -03:00
|
|
|
int main(int argc, char** argv, char** env)
|
2020-07-07 16:13:04 -03:00
|
|
|
{
|
2020-09-24 15:07:07 -03:00
|
|
|
Vector<const char*> command;
|
|
|
|
|
|
|
|
|
|
Core::ArgsParser parser;
|
2020-10-24 04:45:51 -03:00
|
|
|
parser.add_option(g_report_to_debug, "Write reports to the debug log", "report-to-debug", 0);
|
2020-09-24 15:07:07 -03:00
|
|
|
parser.add_positional_argument(command, "Command to emulate", "command");
|
|
|
|
|
parser.parse(argc, argv);
|
2020-07-07 16:13:04 -03:00
|
|
|
|
2020-09-24 15:07:07 -03:00
|
|
|
auto executable_path = Core::find_executable_in_path(command[0]);
|
2021-01-23 11:12:24 -03:00
|
|
|
if (executable_path.is_empty()) {
|
|
|
|
|
reportln("Cannot find executable for '{}'.", command[0]);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-07-07 16:13:04 -03:00
|
|
|
|
2020-07-13 06:22:50 -03:00
|
|
|
Vector<String> arguments;
|
2020-09-18 04:49:51 -03:00
|
|
|
for (auto arg : command) {
|
2020-09-24 15:07:07 -03:00
|
|
|
arguments.append(arg);
|
2020-07-13 06:22:50 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-27 10:57:12 -03:00
|
|
|
Vector<String> environment;
|
|
|
|
|
for (int i = 0; env[i]; ++i) {
|
|
|
|
|
environment.append(env[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 19:39:44 -03:00
|
|
|
// FIXME: It might be nice to tear down the emulator properly.
|
2020-11-23 15:00:35 -03:00
|
|
|
auto& emulator = *new UserspaceEmulator::Emulator(executable_path, arguments, environment);
|
2020-07-11 12:18:07 -03:00
|
|
|
if (!emulator.load_elf())
|
2020-07-07 16:13:04 -03:00
|
|
|
return 1;
|
|
|
|
|
|
2020-07-27 13:56:19 -03:00
|
|
|
StringBuilder builder;
|
|
|
|
|
builder.append("(UE) ");
|
|
|
|
|
builder.append(LexicalPath(arguments[0]).basename());
|
|
|
|
|
if (set_process_name(builder.string_view().characters_without_null_termination(), builder.string_view().length()) < 0) {
|
|
|
|
|
perror("set_process_name");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
int rc = pthread_setname_np(pthread_self(), builder.to_string().characters());
|
|
|
|
|
if (rc != 0) {
|
2020-10-24 04:45:51 -03:00
|
|
|
reportln("pthread_setname_np: {}", strerror(rc));
|
2020-07-27 13:56:19 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
2020-07-11 11:45:48 -03:00
|
|
|
return emulator.exec();
|
2020-07-07 16:13:04 -03:00
|
|
|
}
|