1 //===-- TargetList.h --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_TargetList_h_
11 #define liblldb_TargetList_h_
12 
13 // C Includes
14 // C++ Includes
15 // Other libraries and framework includes
16 // Project includes
17 #include "lldb/Core/Broadcaster.h"
18 #include "lldb/Host/Mutex.h"
19 #include "lldb/Target/Target.h"
20 
21 namespace lldb_private {
22 
23 class TargetList : public Broadcaster
24 {
25 private:
26     friend class Debugger;
27 
28     //------------------------------------------------------------------
29     /// Constructor
30     ///
31     /// The constructor for the target list is private. Clients can
32     /// get ahold of of the one and only target list through the
33     /// lldb_private::Debugger::GetSharedInstance().GetTargetList().
34     ///
35     /// @see static TargetList& lldb_private::Debugger::GetTargetList().
36     //------------------------------------------------------------------
37     TargetList(Debugger &debugger);
38 
39 public:
40 
41     //------------------------------------------------------------------
42     /// Broadcaster event bits definitions.
43     //------------------------------------------------------------------
44     enum
45     {
46         eBroadcastBitInterrupt = (1 << 0)
47     };
48 
49 
50     // These two functions fill out the Broadcaster interface:
51 
52     static ConstString &GetStaticBroadcasterClass ();
53 
GetBroadcasterClass()54     virtual ConstString &GetBroadcasterClass() const
55     {
56         return GetStaticBroadcasterClass();
57     }
58 
59     virtual ~TargetList();
60 
61     //------------------------------------------------------------------
62     /// Create a new Target.
63     ///
64     /// Clients must use this function to create a Target. This allows
65     /// a global list of targets to be maintained in a central location
66     /// so signal handlers and other global functions can use it to
67     /// locate an appropriate target to deliver asynchronous information
68     /// to.
69     ///
70     /// @param[in] debugger
71     ///     The debugger to associate this target with
72     ///
73     /// @param[in] file_spec
74     ///     The main executable file for a debug target. This value
75     ///     can be NULL and the file can be set later using:
76     ///     Target::SetExecutableModule (ModuleSP&)
77     ///
78     /// @param[in] triple_cstr
79     ///     A target triple string to be used for the target. This can
80     ///     be NULL if the triple is not known or when attaching to a
81     ///     process.
82     ///
83     /// @param[in] get_dependent_modules
84     ///     Track down the dependent modules for an executable and
85     ///     load those into the module list.
86     ///
87     /// @param[in] platform_options
88     ///     A pointer to the platform options to use when creating this
89     ///     target. If this value is NULL, then the currently selected
90     ///     platform will be used.
91     ///
92     /// @param[out] target_sp
93     ///     A shared pointer to a target that will be filled in if
94     ///     this call is successful.
95     ///
96     /// @return
97     ///     An error object that indicates success or failure
98     //------------------------------------------------------------------
99     Error
100     CreateTarget (Debugger &debugger,
101                   const char *user_exe_path,
102                   const char *triple_cstr,
103                   bool get_dependent_modules,
104                   const OptionGroupPlatform *platform_options,
105                   lldb::TargetSP &target_sp);
106 
107     //------------------------------------------------------------------
108     /// Create a new Target.
109     ///
110     /// Same as the function above, but used when you already know the
111     /// platform you will be using
112     //------------------------------------------------------------------
113     Error
114     CreateTarget (Debugger &debugger,
115                   const char *user_exe_path,
116                   const ArchSpec& arch,
117                   bool get_dependent_modules,
118                   lldb::PlatformSP &platform_sp,
119                   lldb::TargetSP &target_sp);
120 
121     //------------------------------------------------------------------
122     /// Delete a Target object from the list.
123     ///
124     /// When clients are done with the Target objets, this function
125     /// should be called to release the memory associated with a target
126     /// object.
127     ///
128     /// @param[in] target_sp
129     ///     The shared pointer to a target.
130     ///
131     /// @return
132     ///     Returns \b true if the target was successfully removed from
133     ///     from this target list, \b false otherwise. The client will
134     ///     be left with the last remaining shared pointer to the target
135     ///     in \a target_sp which can then be properly released.
136     //------------------------------------------------------------------
137     bool
138     DeleteTarget (lldb::TargetSP &target_sp);
139 
140     int
141     GetNumTargets () const;
142 
143     lldb::TargetSP
144     GetTargetAtIndex (uint32_t index) const;
145 
146     uint32_t
147     GetIndexOfTarget (lldb::TargetSP target_sp) const;
148 
149     //------------------------------------------------------------------
150     /// Find the target that contains has an executable whose path
151     /// matches \a exe_file_spec, and whose architecture matches
152     /// \a arch_ptr if arch_ptr is not NULL.
153     ///
154     /// @param[in] exe_file_spec
155     ///     A file spec containing a basename, or a full path (directory
156     ///     and basename). If \a exe_file_spec contains only a filename
157     ///     (empty GetDirectory() value) then matching will be done
158     ///     solely based on the filenames and directories won't be
159     ///     compared. If \a exe_file_spec contains a filename and a
160     ///     directory, then both must match.
161     ///
162     /// @param[in] exe_arch_ptr
163     ///     If not NULL then the architecture also needs to match, else
164     ///     the architectures will be compared.
165     ///
166     /// @return
167     ///     A shared pointer to a target object. The returned shared
168     ///     pointer will contain NULL if no target objects have a
169     ///     executable whose full or partial path matches
170     ///     with a matching process ID.
171     //------------------------------------------------------------------
172     lldb::TargetSP
173     FindTargetWithExecutableAndArchitecture (const FileSpec &exe_file_spec,
174                                              const ArchSpec *exe_arch_ptr = NULL) const;
175 
176     //------------------------------------------------------------------
177     /// Find the target that contains a process with process ID \a
178     /// pid.
179     ///
180     /// @param[in] pid
181     ///     The process ID to search our target list for.
182     ///
183     /// @return
184     ///     A shared pointer to a target object. The returned shared
185     ///     pointer will contain NULL if no target objects own a process
186     ///     with a matching process ID.
187     //------------------------------------------------------------------
188     lldb::TargetSP
189     FindTargetWithProcessID (lldb::pid_t pid) const;
190 
191     lldb::TargetSP
192     FindTargetWithProcess (lldb_private::Process *process) const;
193 
194     lldb::TargetSP
195     GetTargetSP (Target *target) const;
196 
197     //------------------------------------------------------------------
198     /// Send an async interrupt to one or all processes.
199     ///
200     /// Find the target that contains the process with process ID \a
201     /// pid and send a LLDB_EVENT_ASYNC_INTERRUPT event to the process's
202     /// event queue.
203     ///
204     /// @param[in] pid
205     ///     The process ID to search our target list for, if \a pid is
206     ///     LLDB_INVALID_PROCESS_ID, then the interrupt will be sent to
207     ///     all processes.
208     ///
209     /// @return
210     ///     The number of async interrupts sent.
211     //------------------------------------------------------------------
212     uint32_t
213     SendAsyncInterrupt (lldb::pid_t pid = LLDB_INVALID_PROCESS_ID);
214 
215     uint32_t
216     SignalIfRunning (lldb::pid_t pid, int signo);
217 
218     uint32_t
219     SetSelectedTarget (Target *target);
220 
221     lldb::TargetSP
222     GetSelectedTarget ();
223 
224 
225 protected:
226     typedef std::vector<lldb::TargetSP> collection;
227     //------------------------------------------------------------------
228     // Member variables.
229     //------------------------------------------------------------------
230     collection m_target_list;
231     mutable Mutex m_target_list_mutex;
232     uint32_t m_selected_target_idx;
233 private:
234     DISALLOW_COPY_AND_ASSIGN (TargetList);
235 };
236 
237 } // namespace lldb_private
238 
239 #endif  // liblldb_TargetList_h_
240