1 //===-- RemoteJITUtils.h - Utilities for remote-JITing ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Utilities for TargetProcessControl-based remote JITing with Orc and JITLink.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXAMPLES_ORCV2EXAMPLES_LLJITWITHREMOTEDEBUGGING_REMOTEJITUTILS_H
14 #define LLVM_EXAMPLES_ORCV2EXAMPLES_LLJITWITHREMOTEDEBUGGING_REMOTEJITUTILS_H
15 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/ExecutionEngine/JITSymbol.h"
20 #include "llvm/ExecutionEngine/Orc/Core.h"
21 #include "llvm/ExecutionEngine/Orc/Layer.h"
22 #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
23 #include "llvm/ExecutionEngine/Orc/Shared/FDRawByteChannel.h"
24 #include "llvm/Support/Error.h"
25 
26 #include <memory>
27 #include <string>
28 
29 #if !defined(_MSC_VER) && !defined(__MINGW32__)
30 #include <unistd.h>
31 #else
32 #include <io.h>
33 #endif
34 
35 namespace llvm {
36 namespace orc {
37 
38 class ChildProcessJITLinkExecutor;
39 class RemoteTargetProcessControl;
40 class TCPSocketJITLinkExecutor;
41 
42 class JITLinkExecutor {
43 public:
44   using RPCChannel = shared::FDRawByteChannel;
45 
46   /// Create a JITLinkExecutor for the given exectuable on disk.
47   static Expected<std::unique_ptr<ChildProcessJITLinkExecutor>>
48   CreateLocal(std::string ExecutablePath);
49 
50   /// Find the default exectuable on disk and create a JITLinkExecutor for it.
51   static Expected<std::unique_ptr<ChildProcessJITLinkExecutor>>
52   FindLocal(const char *JITArgv0);
53 
54   /// Create a JITLinkExecutor that connects to the given network address
55   /// through a TCP socket. A valid NetworkAddress provides hostname and port,
56   /// e.g. localhost:20000.
57   static Expected<std::unique_ptr<TCPSocketJITLinkExecutor>>
58   ConnectTCPSocket(StringRef NetworkAddress, ExecutionSession &ES);
59 
60   // Implement ObjectLinkingLayerCreator
61   Expected<std::unique_ptr<ObjectLayer>> operator()(ExecutionSession &,
62                                                     const Triple &);
63 
64   Error addDebugSupport(ObjectLayer &ObjLayer);
65 
66   Expected<std::unique_ptr<DefinitionGenerator>>
67   loadDylib(StringRef RemotePath);
68 
69   Expected<int> runAsMain(JITEvaluatedSymbol MainSym,
70                           ArrayRef<std::string> Args);
71   Error disconnect();
72 
73   virtual ~JITLinkExecutor();
74 
75 protected:
76   std::unique_ptr<RemoteTargetProcessControl> TPC;
77 
78   JITLinkExecutor();
79 };
80 
81 /// JITLinkExecutor that runs in a child process on the local machine.
82 class ChildProcessJITLinkExecutor : public JITLinkExecutor {
83 public:
84   Error launch(ExecutionSession &ES);
85 
86   pid_t getPID() const { return ProcessID; }
87   StringRef getPath() const { return ExecutablePath; }
88 
89 private:
90   std::string ExecutablePath;
91   pid_t ProcessID;
92 
93   ChildProcessJITLinkExecutor(std::string ExecutablePath)
94       : ExecutablePath(std::move(ExecutablePath)) {}
95 
96   static std::string defaultPath(const char *HostArgv0, StringRef ExecutorName);
97   friend class JITLinkExecutor;
98 };
99 
100 /// JITLinkExecutor connected through a TCP socket.
101 class TCPSocketJITLinkExecutor : public JITLinkExecutor {
102 private:
103   TCPSocketJITLinkExecutor(std::unique_ptr<RemoteTargetProcessControl> TPC);
104 
105   friend class JITLinkExecutor;
106 };
107 
108 } // namespace orc
109 } // namespace llvm
110 
111 #endif
112