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