2020-07-07 22:13:04 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Emulator.h"
|
|
|
|
#include "SoftCPU.h"
|
2020-10-13 19:34:27 +03:00
|
|
|
#include <AK/Format.h>
|
2020-07-27 19:56:19 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-07-07 22:13:04 +03:00
|
|
|
#include <AK/MappedFile.h>
|
2020-07-27 19:56:19 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-09-24 21:07:07 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-09-16 20:19:30 +03:00
|
|
|
#include <LibCore/DirIterator.h>
|
2020-12-25 04:14:56 +03:00
|
|
|
#include <LibELF/Image.h>
|
2020-07-13 12:22:50 +03:00
|
|
|
#include <getopt.h>
|
2020-07-27 19:56:19 +03:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <string.h>
|
2020-07-07 22:13:04 +03:00
|
|
|
|
2020-10-24 10:45:51 +03:00
|
|
|
bool g_report_to_debug = false;
|
|
|
|
|
2020-07-27 16:57:12 +03:00
|
|
|
int main(int argc, char** argv, char** env)
|
2020-07-07 22:13:04 +03:00
|
|
|
{
|
2020-09-24 21:07:07 +03:00
|
|
|
Vector<const char*> command;
|
|
|
|
|
|
|
|
Core::ArgsParser parser;
|
2020-10-24 10:45:51 +03:00
|
|
|
parser.add_option(g_report_to_debug, "Write reports to the debug log", "report-to-debug", 0);
|
2020-09-24 21:07:07 +03:00
|
|
|
parser.add_positional_argument(command, "Command to emulate", "command");
|
|
|
|
parser.parse(argc, argv);
|
2020-07-07 22:13:04 +03:00
|
|
|
|
2020-09-24 21:07:07 +03:00
|
|
|
auto executable_path = Core::find_executable_in_path(command[0]);
|
2021-01-23 17:12:24 +03:00
|
|
|
if (executable_path.is_empty()) {
|
|
|
|
reportln("Cannot find executable for '{}'.", command[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-07 22:13:04 +03:00
|
|
|
|
2020-07-13 12:22:50 +03:00
|
|
|
Vector<String> arguments;
|
2020-09-18 10:49:51 +03:00
|
|
|
for (auto arg : command) {
|
2020-09-24 21:07:07 +03:00
|
|
|
arguments.append(arg);
|
2020-07-13 12:22:50 +03:00
|
|
|
}
|
|
|
|
|
2020-07-27 16:57:12 +03:00
|
|
|
Vector<String> environment;
|
|
|
|
for (int i = 0; env[i]; ++i) {
|
|
|
|
environment.append(env[i]);
|
|
|
|
}
|
|
|
|
|
2020-11-15 01:39:44 +03:00
|
|
|
// FIXME: It might be nice to tear down the emulator properly.
|
2020-11-23 21:00:35 +03:00
|
|
|
auto& emulator = *new UserspaceEmulator::Emulator(executable_path, arguments, environment);
|
2020-07-11 18:18:07 +03:00
|
|
|
if (!emulator.load_elf())
|
2020-07-07 22:13:04 +03:00
|
|
|
return 1;
|
|
|
|
|
2020-07-27 19: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 10:45:51 +03:00
|
|
|
reportln("pthread_setname_np: {}", strerror(rc));
|
2020-07-27 19:56:19 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2020-07-11 17:45:48 +03:00
|
|
|
return emulator.exec();
|
2020-07-07 22:13:04 +03:00
|
|
|
}
|