main.cpp 1.8 KB

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