main.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <pthread.h>
  14. #include <serenity.h>
  15. #include <string.h>
  16. bool g_report_to_debug = false;
  17. int main(int argc, char** argv, char** env)
  18. {
  19. Vector<const char*> command;
  20. Core::ArgsParser parser;
  21. parser.add_option(g_report_to_debug, "Write reports to the debug log", "report-to-debug", 0);
  22. parser.add_positional_argument(command, "Command to emulate", "command");
  23. parser.parse(argc, argv);
  24. auto executable_path = Core::find_executable_in_path(command[0]);
  25. if (executable_path.is_empty()) {
  26. executable_path = Core::File::real_path_for(command[0]);
  27. if (executable_path.is_empty()) {
  28. reportln("Cannot find executable for '{}'.", executable_path);
  29. return 1;
  30. }
  31. }
  32. Vector<String> arguments;
  33. for (auto arg : command) {
  34. arguments.append(arg);
  35. }
  36. Vector<String> environment;
  37. for (int i = 0; env[i]; ++i) {
  38. environment.append(env[i]);
  39. }
  40. // FIXME: It might be nice to tear down the emulator properly.
  41. auto& emulator = *new UserspaceEmulator::Emulator(executable_path, arguments, environment);
  42. if (!emulator.load_elf())
  43. return 1;
  44. StringBuilder builder;
  45. builder.append("(UE) ");
  46. builder.append(LexicalPath(arguments[0]).basename());
  47. if (set_process_name(builder.string_view().characters_without_null_termination(), builder.string_view().length()) < 0) {
  48. perror("set_process_name");
  49. return 1;
  50. }
  51. int rc = pthread_setname_np(pthread_self(), builder.to_string().characters());
  52. if (rc != 0) {
  53. reportln("pthread_setname_np: {}", strerror(rc));
  54. return 1;
  55. }
  56. return emulator.exec();
  57. }