main.cpp 1.9 KB

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