Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Target.cpp ----------------------------------------------*- 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 | #include "lldb/Target/Target.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Breakpoint/BreakpointResolver.h" |
| 17 | #include "lldb/Breakpoint/BreakpointResolverAddress.h" |
| 18 | #include "lldb/Breakpoint/BreakpointResolverFileLine.h" |
| 19 | #include "lldb/Breakpoint/BreakpointResolverName.h" |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Event.h" |
| 22 | #include "lldb/Core/Log.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Core/StreamString.h" |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 24 | #include "lldb/Core/Timer.h" |
| 25 | #include "lldb/Core/ValueObject.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | #include "lldb/Host/Host.h" |
Jim Ingham | d60d94a | 2011-03-11 03:53:59 +0000 | [diff] [blame^] | 27 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 28 | #include "lldb/Interpreter/CommandReturnObject.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/lldb-private-log.h" |
| 30 | #include "lldb/Symbol/ObjectFile.h" |
| 31 | #include "lldb/Target/Process.h" |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 32 | #include "lldb/Target/StackFrame.h" |
Jim Ingham | d60d94a | 2011-03-11 03:53:59 +0000 | [diff] [blame^] | 33 | #include "lldb/Target/Thread.h" |
| 34 | #include "lldb/Target/ThreadSpec.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace lldb; |
| 37 | using namespace lldb_private; |
| 38 | |
| 39 | //---------------------------------------------------------------------- |
| 40 | // Target constructor |
| 41 | //---------------------------------------------------------------------- |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 42 | Target::Target(Debugger &debugger) : |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 43 | Broadcaster("lldb.target"), |
Greg Clayton | c0c1b0c | 2010-11-19 03:46:01 +0000 | [diff] [blame] | 44 | TargetInstanceSettings (*GetSettingsController()), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 45 | m_debugger (debugger), |
Greg Clayton | bdcda46 | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 46 | m_mutex (Mutex::eMutexTypeRecursive), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | m_images(), |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 48 | m_section_load_list (), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | m_breakpoint_list (false), |
| 50 | m_internal_breakpoint_list (true), |
| 51 | m_process_sp(), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | m_search_filter_sp(), |
| 53 | m_image_search_paths (ImageSearchPathsChanged, this), |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 54 | m_scratch_ast_context_ap (NULL), |
Jim Ingham | d60d94a | 2011-03-11 03:53:59 +0000 | [diff] [blame^] | 55 | m_persistent_variables (), |
| 56 | m_stop_hook_next_id(0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | { |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 58 | SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed"); |
| 59 | SetEventName (eBroadcastBitModulesLoaded, "modules-loaded"); |
| 60 | SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded"); |
| 61 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 62 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | if (log) |
| 64 | log->Printf ("%p Target::Target()", this); |
| 65 | } |
| 66 | |
| 67 | //---------------------------------------------------------------------- |
| 68 | // Destructor |
| 69 | //---------------------------------------------------------------------- |
| 70 | Target::~Target() |
| 71 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 72 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | if (log) |
| 74 | log->Printf ("%p Target::~Target()", this); |
| 75 | DeleteCurrentProcess (); |
| 76 | } |
| 77 | |
| 78 | void |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 79 | Target::Dump (Stream *s, lldb::DescriptionLevel description_level) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | { |
Greg Clayton | 3fed8b9 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 81 | // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 82 | if (description_level != lldb::eDescriptionLevelBrief) |
| 83 | { |
| 84 | s->Indent(); |
| 85 | s->PutCString("Target\n"); |
| 86 | s->IndentMore(); |
Greg Clayton | 3f5ee7f | 2010-10-29 04:59:35 +0000 | [diff] [blame] | 87 | m_images.Dump(s); |
| 88 | m_breakpoint_list.Dump(s); |
| 89 | m_internal_breakpoint_list.Dump(s); |
| 90 | s->IndentLess(); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 91 | } |
| 92 | else |
| 93 | { |
Greg Clayton | a66ba46 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 94 | s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString()); |
Caroline Tice | 7826c88 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 95 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | Target::DeleteCurrentProcess () |
| 100 | { |
| 101 | if (m_process_sp.get()) |
| 102 | { |
Greg Clayton | 49480b1 | 2010-09-14 23:52:43 +0000 | [diff] [blame] | 103 | m_section_load_list.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | if (m_process_sp->IsAlive()) |
| 105 | m_process_sp->Destroy(); |
Jim Ingham | 88fa7bd | 2011-02-16 17:54:55 +0000 | [diff] [blame] | 106 | |
| 107 | m_process_sp->Finalize(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | |
| 109 | // Do any cleanup of the target we need to do between process instances. |
| 110 | // NB It is better to do this before destroying the process in case the |
| 111 | // clean up needs some help from the process. |
| 112 | m_breakpoint_list.ClearAllBreakpointSites(); |
| 113 | m_internal_breakpoint_list.ClearAllBreakpointSites(); |
| 114 | m_process_sp.reset(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | const lldb::ProcessSP & |
| 119 | Target::CreateProcess (Listener &listener, const char *plugin_name) |
| 120 | { |
| 121 | DeleteCurrentProcess (); |
| 122 | m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener)); |
| 123 | return m_process_sp; |
| 124 | } |
| 125 | |
| 126 | const lldb::ProcessSP & |
| 127 | Target::GetProcessSP () const |
| 128 | { |
| 129 | return m_process_sp; |
| 130 | } |
| 131 | |
| 132 | lldb::TargetSP |
| 133 | Target::GetSP() |
| 134 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 135 | return m_debugger.GetTargetList().GetTargetSP(this); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | BreakpointList & |
| 139 | Target::GetBreakpointList(bool internal) |
| 140 | { |
| 141 | if (internal) |
| 142 | return m_internal_breakpoint_list; |
| 143 | else |
| 144 | return m_breakpoint_list; |
| 145 | } |
| 146 | |
| 147 | const BreakpointList & |
| 148 | Target::GetBreakpointList(bool internal) const |
| 149 | { |
| 150 | if (internal) |
| 151 | return m_internal_breakpoint_list; |
| 152 | else |
| 153 | return m_breakpoint_list; |
| 154 | } |
| 155 | |
| 156 | BreakpointSP |
| 157 | Target::GetBreakpointByID (break_id_t break_id) |
| 158 | { |
| 159 | BreakpointSP bp_sp; |
| 160 | |
| 161 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
| 162 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); |
| 163 | else |
| 164 | bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); |
| 165 | |
| 166 | return bp_sp; |
| 167 | } |
| 168 | |
| 169 | BreakpointSP |
| 170 | Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal) |
| 171 | { |
| 172 | SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); |
| 173 | BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines)); |
| 174 | return CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | BreakpointSP |
Greg Clayton | 33ed170 | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 179 | Target::CreateBreakpoint (lldb::addr_t addr, bool internal) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | Address so_addr; |
| 182 | // Attempt to resolve our load address if possible, though it is ok if |
| 183 | // it doesn't resolve to section/offset. |
| 184 | |
Greg Clayton | 33ed170 | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 185 | // Try and resolve as a load address if possible |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 186 | m_section_load_list.ResolveLoadAddress(addr, so_addr); |
Greg Clayton | 33ed170 | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 187 | if (!so_addr.IsValid()) |
| 188 | { |
| 189 | // The address didn't resolve, so just set this as an absolute address |
| 190 | so_addr.SetOffset (addr); |
| 191 | } |
| 192 | BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | return bp_sp; |
| 194 | } |
| 195 | |
| 196 | BreakpointSP |
| 197 | Target::CreateBreakpoint (Address &addr, bool internal) |
| 198 | { |
| 199 | TargetSP target_sp = this->GetSP(); |
| 200 | SearchFilterSP filter_sp(new SearchFilter (target_sp)); |
| 201 | BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr)); |
| 202 | return CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 203 | } |
| 204 | |
| 205 | BreakpointSP |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 206 | Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 207 | { |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 208 | BreakpointSP bp_sp; |
| 209 | if (func_name) |
| 210 | { |
| 211 | SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); |
| 212 | BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact)); |
| 213 | bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 214 | } |
| 215 | return bp_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | SearchFilterSP |
| 220 | Target::GetSearchFilterForModule (const FileSpec *containingModule) |
| 221 | { |
| 222 | SearchFilterSP filter_sp; |
| 223 | lldb::TargetSP target_sp = this->GetSP(); |
| 224 | if (containingModule != NULL) |
| 225 | { |
| 226 | // TODO: We should look into sharing module based search filters |
| 227 | // across many breakpoints like we do for the simple target based one |
| 228 | filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule)); |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | if (m_search_filter_sp.get() == NULL) |
| 233 | m_search_filter_sp.reset (new SearchFilter (target_sp)); |
| 234 | filter_sp = m_search_filter_sp; |
| 235 | } |
| 236 | return filter_sp; |
| 237 | } |
| 238 | |
| 239 | BreakpointSP |
| 240 | Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal) |
| 241 | { |
| 242 | SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); |
| 243 | BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex)); |
| 244 | |
| 245 | return CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 246 | } |
| 247 | |
| 248 | BreakpointSP |
| 249 | Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal) |
| 250 | { |
| 251 | BreakpointSP bp_sp; |
| 252 | if (filter_sp && resolver_sp) |
| 253 | { |
| 254 | bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp)); |
| 255 | resolver_sp->SetBreakpoint (bp_sp.get()); |
| 256 | |
| 257 | if (internal) |
Greg Clayton | c7f5d5c | 2010-07-23 23:33:17 +0000 | [diff] [blame] | 258 | m_internal_breakpoint_list.Add (bp_sp, false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 259 | else |
Greg Clayton | c7f5d5c | 2010-07-23 23:33:17 +0000 | [diff] [blame] | 260 | m_breakpoint_list.Add (bp_sp, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 261 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 262 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | if (log) |
| 264 | { |
| 265 | StreamString s; |
| 266 | bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
| 267 | log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData()); |
| 268 | } |
| 269 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 270 | bp_sp->ResolveBreakpoint(); |
| 271 | } |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 272 | |
| 273 | if (!internal && bp_sp) |
| 274 | { |
| 275 | m_last_created_breakpoint = bp_sp; |
| 276 | } |
| 277 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 278 | return bp_sp; |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | Target::RemoveAllBreakpoints (bool internal_also) |
| 283 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 284 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | if (log) |
| 286 | log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); |
| 287 | |
Greg Clayton | c7f5d5c | 2010-07-23 23:33:17 +0000 | [diff] [blame] | 288 | m_breakpoint_list.RemoveAll (true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | if (internal_also) |
Greg Clayton | c7f5d5c | 2010-07-23 23:33:17 +0000 | [diff] [blame] | 290 | m_internal_breakpoint_list.RemoveAll (false); |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 291 | |
| 292 | m_last_created_breakpoint.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void |
| 296 | Target::DisableAllBreakpoints (bool internal_also) |
| 297 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 298 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 299 | if (log) |
| 300 | log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); |
| 301 | |
| 302 | m_breakpoint_list.SetEnabledAll (false); |
| 303 | if (internal_also) |
| 304 | m_internal_breakpoint_list.SetEnabledAll (false); |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | Target::EnableAllBreakpoints (bool internal_also) |
| 309 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 310 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | if (log) |
| 312 | log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); |
| 313 | |
| 314 | m_breakpoint_list.SetEnabledAll (true); |
| 315 | if (internal_also) |
| 316 | m_internal_breakpoint_list.SetEnabledAll (true); |
| 317 | } |
| 318 | |
| 319 | bool |
| 320 | Target::RemoveBreakpointByID (break_id_t break_id) |
| 321 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 322 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 323 | if (log) |
| 324 | log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); |
| 325 | |
| 326 | if (DisableBreakpointByID (break_id)) |
| 327 | { |
| 328 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
Greg Clayton | c7f5d5c | 2010-07-23 23:33:17 +0000 | [diff] [blame] | 329 | m_internal_breakpoint_list.Remove(break_id, false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 330 | else |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 331 | { |
Greg Clayton | 22c9e0d | 2011-01-24 23:35:47 +0000 | [diff] [blame] | 332 | if (m_last_created_breakpoint) |
| 333 | { |
| 334 | if (m_last_created_breakpoint->GetID() == break_id) |
| 335 | m_last_created_breakpoint.reset(); |
| 336 | } |
Greg Clayton | c7f5d5c | 2010-07-23 23:33:17 +0000 | [diff] [blame] | 337 | m_breakpoint_list.Remove(break_id, true); |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 338 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 339 | return true; |
| 340 | } |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | bool |
| 345 | Target::DisableBreakpointByID (break_id_t break_id) |
| 346 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 347 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 348 | if (log) |
| 349 | log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); |
| 350 | |
| 351 | BreakpointSP bp_sp; |
| 352 | |
| 353 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
| 354 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); |
| 355 | else |
| 356 | bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); |
| 357 | if (bp_sp) |
| 358 | { |
| 359 | bp_sp->SetEnabled (false); |
| 360 | return true; |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | bool |
| 366 | Target::EnableBreakpointByID (break_id_t break_id) |
| 367 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 368 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 369 | if (log) |
| 370 | log->Printf ("Target::%s (break_id = %i, internal = %s)\n", |
| 371 | __FUNCTION__, |
| 372 | break_id, |
| 373 | LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); |
| 374 | |
| 375 | BreakpointSP bp_sp; |
| 376 | |
| 377 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
| 378 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); |
| 379 | else |
| 380 | bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); |
| 381 | |
| 382 | if (bp_sp) |
| 383 | { |
| 384 | bp_sp->SetEnabled (true); |
| 385 | return true; |
| 386 | } |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | ModuleSP |
| 391 | Target::GetExecutableModule () |
| 392 | { |
| 393 | ModuleSP executable_sp; |
| 394 | if (m_images.GetSize() > 0) |
| 395 | executable_sp = m_images.GetModuleAtIndex(0); |
| 396 | return executable_sp; |
| 397 | } |
| 398 | |
| 399 | void |
| 400 | Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files) |
| 401 | { |
| 402 | m_images.Clear(); |
| 403 | m_scratch_ast_context_ap.reset(); |
| 404 | |
| 405 | if (executable_sp.get()) |
| 406 | { |
| 407 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 408 | "Target::SetExecutableModule (executable = '%s/%s')", |
| 409 | executable_sp->GetFileSpec().GetDirectory().AsCString(), |
| 410 | executable_sp->GetFileSpec().GetFilename().AsCString()); |
| 411 | |
| 412 | m_images.Append(executable_sp); // The first image is our exectuable file |
| 413 | |
| 414 | ArchSpec exe_arch = executable_sp->GetArchitecture(); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 415 | // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module. |
| 416 | if (!m_arch_spec.IsValid()) |
| 417 | m_arch_spec = exe_arch; |
| 418 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 419 | FileSpecList dependent_files; |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 420 | ObjectFile *executable_objfile = executable_sp->GetObjectFile(); |
| 421 | assert (executable_objfile); |
| 422 | // TODO: remote assertion above after verifying that it doesn't fire off |
| 423 | // after the platform changes. The platform is what should be selecting |
| 424 | // the right slice of an executable file, and it also should be the one |
| 425 | // to resolve any executables in their bundles. |
| 426 | // if (executable_objfile == NULL) |
| 427 | // { |
| 428 | // |
| 429 | // FileSpec bundle_executable(executable_sp->GetFileSpec()); |
| 430 | // if (Host::ResolveExecutableInBundle (bundle_executable)) |
| 431 | // { |
| 432 | // ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable, |
| 433 | // exe_arch)); |
| 434 | // SetExecutableModule (bundle_exe_module_sp, get_dependent_files); |
| 435 | // if (bundle_exe_module_sp->GetObjectFile() != NULL) |
| 436 | // executable_sp = bundle_exe_module_sp; |
| 437 | // return; |
| 438 | // } |
| 439 | // } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 440 | |
| 441 | if (executable_objfile) |
| 442 | { |
| 443 | executable_objfile->GetDependentModules(dependent_files); |
| 444 | for (uint32_t i=0; i<dependent_files.GetSize(); i++) |
| 445 | { |
| 446 | ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i), |
| 447 | exe_arch)); |
| 448 | if (image_module_sp.get()) |
| 449 | { |
| 450 | //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY |
| 451 | ObjectFile *objfile = image_module_sp->GetObjectFile(); |
| 452 | if (objfile) |
| 453 | objfile->GetDependentModules(dependent_files); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // Now see if we know the target triple, and if so, create our scratch AST context: |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 459 | if (m_arch_spec.IsValid()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 460 | { |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 461 | m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch_spec.GetTriple().str().c_str())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 464 | |
| 465 | UpdateInstanceName(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 469 | bool |
| 470 | Target::SetArchitecture (const ArchSpec &arch_spec) |
| 471 | { |
| 472 | if (m_arch_spec == arch_spec) |
| 473 | { |
| 474 | // If we're setting the architecture to our current architecture, we |
| 475 | // don't need to do anything. |
| 476 | return true; |
| 477 | } |
| 478 | else if (!m_arch_spec.IsValid()) |
| 479 | { |
| 480 | // If we haven't got a valid arch spec, then we just need to set it. |
| 481 | m_arch_spec = arch_spec; |
| 482 | return true; |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | // If we have an executable file, try to reset the executable to the desired architecture |
| 487 | m_arch_spec = arch_spec; |
| 488 | ModuleSP executable_sp = GetExecutableModule (); |
| 489 | m_images.Clear(); |
| 490 | m_scratch_ast_context_ap.reset(); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 491 | // Need to do something about unsetting breakpoints. |
| 492 | |
| 493 | if (executable_sp) |
| 494 | { |
| 495 | FileSpec exec_file_spec = executable_sp->GetFileSpec(); |
| 496 | Error error = ModuleList::GetSharedModule(exec_file_spec, |
| 497 | arch_spec, |
| 498 | NULL, |
| 499 | NULL, |
| 500 | 0, |
| 501 | executable_sp, |
| 502 | NULL, |
| 503 | NULL); |
| 504 | |
| 505 | if (!error.Fail() && executable_sp) |
| 506 | { |
| 507 | SetExecutableModule (executable_sp, true); |
| 508 | return true; |
| 509 | } |
| 510 | else |
| 511 | { |
| 512 | return false; |
| 513 | } |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | return false; |
| 518 | } |
| 519 | } |
| 520 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 521 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 522 | void |
| 523 | Target::ModuleAdded (ModuleSP &module_sp) |
| 524 | { |
| 525 | // A module is being added to this target for the first time |
| 526 | ModuleList module_list; |
| 527 | module_list.Append(module_sp); |
| 528 | ModulesDidLoad (module_list); |
| 529 | } |
| 530 | |
| 531 | void |
| 532 | Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp) |
| 533 | { |
| 534 | // A module is being added to this target for the first time |
| 535 | ModuleList module_list; |
| 536 | module_list.Append (old_module_sp); |
| 537 | ModulesDidUnload (module_list); |
| 538 | module_list.Clear (); |
| 539 | module_list.Append (new_module_sp); |
| 540 | ModulesDidLoad (module_list); |
| 541 | } |
| 542 | |
| 543 | void |
| 544 | Target::ModulesDidLoad (ModuleList &module_list) |
| 545 | { |
| 546 | m_breakpoint_list.UpdateBreakpoints (module_list, true); |
| 547 | // TODO: make event data that packages up the module_list |
| 548 | BroadcastEvent (eBroadcastBitModulesLoaded, NULL); |
| 549 | } |
| 550 | |
| 551 | void |
| 552 | Target::ModulesDidUnload (ModuleList &module_list) |
| 553 | { |
| 554 | m_breakpoint_list.UpdateBreakpoints (module_list, false); |
Greg Clayton | 7b9fcc0 | 2010-12-06 23:51:26 +0000 | [diff] [blame] | 555 | |
| 556 | // Remove the images from the target image list |
| 557 | m_images.Remove(module_list); |
| 558 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 559 | // TODO: make event data that packages up the module_list |
| 560 | BroadcastEvent (eBroadcastBitModulesUnloaded, NULL); |
| 561 | } |
| 562 | |
| 563 | size_t |
Greg Clayton | 26100dc | 2011-01-07 01:57:07 +0000 | [diff] [blame] | 564 | Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error) |
| 565 | { |
| 566 | const Section *section = addr.GetSection(); |
| 567 | if (section && section->GetModule()) |
| 568 | { |
| 569 | ObjectFile *objfile = section->GetModule()->GetObjectFile(); |
| 570 | if (objfile) |
| 571 | { |
| 572 | size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile, |
| 573 | addr.GetOffset(), |
| 574 | dst, |
| 575 | dst_len); |
| 576 | if (bytes_read > 0) |
| 577 | return bytes_read; |
| 578 | else |
| 579 | error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString()); |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | error.SetErrorString("address isn't from a object file"); |
| 584 | } |
| 585 | } |
| 586 | else |
| 587 | { |
| 588 | error.SetErrorString("address doesn't contain a section that points to a section in a object file"); |
| 589 | } |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | size_t |
| 594 | Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 595 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 596 | error.Clear(); |
Greg Clayton | 26100dc | 2011-01-07 01:57:07 +0000 | [diff] [blame] | 597 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 598 | bool process_is_valid = m_process_sp && m_process_sp->IsAlive(); |
| 599 | |
Greg Clayton | 26100dc | 2011-01-07 01:57:07 +0000 | [diff] [blame] | 600 | size_t bytes_read = 0; |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 601 | Address resolved_addr(addr); |
| 602 | if (!resolved_addr.IsSectionOffset()) |
| 603 | { |
| 604 | if (process_is_valid) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 605 | { |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 606 | m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr); |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 607 | } |
| 608 | else |
| 609 | { |
| 610 | m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr); |
| 611 | } |
| 612 | } |
| 613 | |
Greg Clayton | 26100dc | 2011-01-07 01:57:07 +0000 | [diff] [blame] | 614 | if (prefer_file_cache) |
| 615 | { |
| 616 | bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error); |
| 617 | if (bytes_read > 0) |
| 618 | return bytes_read; |
| 619 | } |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 620 | |
| 621 | if (process_is_valid) |
| 622 | { |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 623 | lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this); |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 624 | if (load_addr == LLDB_INVALID_ADDRESS) |
| 625 | { |
| 626 | if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec()) |
| 627 | error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n", |
| 628 | resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(), |
| 629 | resolved_addr.GetFileAddress()); |
| 630 | else |
| 631 | error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress()); |
| 632 | } |
| 633 | else |
| 634 | { |
Greg Clayton | 26100dc | 2011-01-07 01:57:07 +0000 | [diff] [blame] | 635 | bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 636 | if (bytes_read != dst_len) |
| 637 | { |
| 638 | if (error.Success()) |
| 639 | { |
| 640 | if (bytes_read == 0) |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 641 | error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 642 | else |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 643 | error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 646 | if (bytes_read) |
| 647 | return bytes_read; |
| 648 | // If the address is not section offset we have an address that |
| 649 | // doesn't resolve to any address in any currently loaded shared |
| 650 | // libaries and we failed to read memory so there isn't anything |
| 651 | // more we can do. If it is section offset, we might be able to |
| 652 | // read cached memory from the object file. |
| 653 | if (!resolved_addr.IsSectionOffset()) |
| 654 | return 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 655 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 656 | } |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 657 | |
Greg Clayton | 26100dc | 2011-01-07 01:57:07 +0000 | [diff] [blame] | 658 | if (!prefer_file_cache) |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 659 | { |
Greg Clayton | 26100dc | 2011-01-07 01:57:07 +0000 | [diff] [blame] | 660 | // If we didn't already try and read from the object file cache, then |
| 661 | // try it after failing to read from the process. |
| 662 | return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error); |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 663 | } |
| 664 | return 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | |
| 668 | ModuleSP |
| 669 | Target::GetSharedModule |
| 670 | ( |
| 671 | const FileSpec& file_spec, |
| 672 | const ArchSpec& arch, |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 673 | const lldb_private::UUID *uuid_ptr, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 674 | const ConstString *object_name, |
| 675 | off_t object_offset, |
| 676 | Error *error_ptr |
| 677 | ) |
| 678 | { |
| 679 | // Don't pass in the UUID so we can tell if we have a stale value in our list |
| 680 | ModuleSP old_module_sp; // This will get filled in if we have a new version of the library |
| 681 | bool did_create_module = false; |
| 682 | ModuleSP module_sp; |
| 683 | |
| 684 | // If there are image search path entries, try to use them first to acquire a suitable image. |
| 685 | |
| 686 | Error error; |
| 687 | |
| 688 | if (m_image_search_paths.GetSize()) |
| 689 | { |
| 690 | FileSpec transformed_spec; |
| 691 | if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory())) |
| 692 | { |
| 693 | transformed_spec.GetFilename() = file_spec.GetFilename(); |
| 694 | error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | // If a module hasn't been found yet, use the unmodified path. |
| 699 | |
| 700 | if (!module_sp) |
| 701 | { |
| 702 | error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module)); |
| 703 | } |
| 704 | |
| 705 | if (module_sp) |
| 706 | { |
| 707 | m_images.Append (module_sp); |
| 708 | if (did_create_module) |
| 709 | { |
| 710 | if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32) |
| 711 | ModuleUpdated(old_module_sp, module_sp); |
| 712 | else |
| 713 | ModuleAdded(module_sp); |
| 714 | } |
| 715 | } |
| 716 | if (error_ptr) |
| 717 | *error_ptr = error; |
| 718 | return module_sp; |
| 719 | } |
| 720 | |
| 721 | |
| 722 | Target * |
| 723 | Target::CalculateTarget () |
| 724 | { |
| 725 | return this; |
| 726 | } |
| 727 | |
| 728 | Process * |
| 729 | Target::CalculateProcess () |
| 730 | { |
| 731 | return NULL; |
| 732 | } |
| 733 | |
| 734 | Thread * |
| 735 | Target::CalculateThread () |
| 736 | { |
| 737 | return NULL; |
| 738 | } |
| 739 | |
| 740 | StackFrame * |
| 741 | Target::CalculateStackFrame () |
| 742 | { |
| 743 | return NULL; |
| 744 | } |
| 745 | |
| 746 | void |
Greg Clayton | a830adb | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 747 | Target::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 748 | { |
| 749 | exe_ctx.target = this; |
| 750 | exe_ctx.process = NULL; // Do NOT fill in process... |
| 751 | exe_ctx.thread = NULL; |
| 752 | exe_ctx.frame = NULL; |
| 753 | } |
| 754 | |
| 755 | PathMappingList & |
| 756 | Target::GetImageSearchPathList () |
| 757 | { |
| 758 | return m_image_search_paths; |
| 759 | } |
| 760 | |
| 761 | void |
| 762 | Target::ImageSearchPathsChanged |
| 763 | ( |
| 764 | const PathMappingList &path_list, |
| 765 | void *baton |
| 766 | ) |
| 767 | { |
| 768 | Target *target = (Target *)baton; |
| 769 | if (target->m_images.GetSize() > 1) |
| 770 | { |
| 771 | ModuleSP exe_module_sp (target->GetExecutableModule()); |
| 772 | if (exe_module_sp) |
| 773 | { |
| 774 | target->m_images.Clear(); |
| 775 | target->SetExecutableModule (exe_module_sp, true); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | ClangASTContext * |
| 781 | Target::GetScratchClangASTContext() |
| 782 | { |
| 783 | return m_scratch_ast_context_ap.get(); |
| 784 | } |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 785 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 786 | void |
Caroline Tice | 2a45681 | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 787 | Target::SettingsInitialize () |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 788 | { |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 789 | UserSettingsControllerSP &usc = GetSettingsController(); |
| 790 | usc.reset (new SettingsController); |
| 791 | UserSettingsController::InitializeSettingsController (usc, |
| 792 | SettingsController::global_settings_table, |
| 793 | SettingsController::instance_settings_table); |
Caroline Tice | 2a45681 | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 794 | |
| 795 | // Now call SettingsInitialize() on each 'child' setting of Target |
| 796 | Process::SettingsInitialize (); |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 797 | } |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 798 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 799 | void |
Caroline Tice | 2a45681 | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 800 | Target::SettingsTerminate () |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 801 | { |
Caroline Tice | 2a45681 | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 802 | |
| 803 | // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings. |
| 804 | |
| 805 | Process::SettingsTerminate (); |
| 806 | |
| 807 | // Now terminate Target Settings. |
| 808 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 809 | UserSettingsControllerSP &usc = GetSettingsController(); |
| 810 | UserSettingsController::FinalizeSettingsController (usc); |
| 811 | usc.reset(); |
| 812 | } |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 813 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 814 | UserSettingsControllerSP & |
| 815 | Target::GetSettingsController () |
| 816 | { |
| 817 | static UserSettingsControllerSP g_settings_controller; |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 818 | return g_settings_controller; |
| 819 | } |
| 820 | |
| 821 | ArchSpec |
| 822 | Target::GetDefaultArchitecture () |
| 823 | { |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 824 | lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController()); |
| 825 | |
| 826 | if (settings_controller_sp) |
| 827 | return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture (); |
| 828 | return ArchSpec(); |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | void |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 832 | Target::SetDefaultArchitecture (const ArchSpec& arch) |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 833 | { |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 834 | lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController()); |
| 835 | |
| 836 | if (settings_controller_sp) |
| 837 | static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch; |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Greg Clayton | a830adb | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 840 | Target * |
| 841 | Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr) |
| 842 | { |
| 843 | // The target can either exist in the "process" of ExecutionContext, or in |
| 844 | // the "target_sp" member of SymbolContext. This accessor helper function |
| 845 | // will get the target from one of these locations. |
| 846 | |
| 847 | Target *target = NULL; |
| 848 | if (sc_ptr != NULL) |
| 849 | target = sc_ptr->target_sp.get(); |
| 850 | if (target == NULL) |
| 851 | { |
| 852 | if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL) |
| 853 | target = &exe_ctx_ptr->process->GetTarget(); |
| 854 | } |
| 855 | return target; |
| 856 | } |
| 857 | |
| 858 | |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 859 | void |
| 860 | Target::UpdateInstanceName () |
| 861 | { |
| 862 | StreamString sstr; |
| 863 | |
| 864 | ModuleSP module_sp = GetExecutableModule(); |
| 865 | if (module_sp) |
| 866 | { |
Greg Clayton | bf6e210 | 2010-10-27 02:06:37 +0000 | [diff] [blame] | 867 | sstr.Printf ("%s_%s", |
| 868 | module_sp->GetFileSpec().GetFilename().AsCString(), |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 869 | module_sp->GetArchitecture().GetArchitectureName()); |
Greg Clayton | c0c1b0c | 2010-11-19 03:46:01 +0000 | [diff] [blame] | 870 | GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), |
| 871 | sstr.GetData()); |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 875 | const char * |
| 876 | Target::GetExpressionPrefixContentsAsCString () |
| 877 | { |
| 878 | return m_expr_prefix_contents.c_str(); |
| 879 | } |
| 880 | |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 881 | ExecutionResults |
| 882 | Target::EvaluateExpression |
| 883 | ( |
| 884 | const char *expr_cstr, |
| 885 | StackFrame *frame, |
| 886 | bool unwind_on_error, |
Sean Callanan | 6a92553 | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 887 | bool keep_in_memory, |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 888 | lldb::ValueObjectSP &result_valobj_sp |
| 889 | ) |
| 890 | { |
| 891 | ExecutionResults execution_results = eExecutionSetupError; |
| 892 | |
| 893 | result_valobj_sp.reset(); |
| 894 | |
| 895 | ExecutionContext exe_ctx; |
| 896 | if (frame) |
| 897 | { |
| 898 | frame->CalculateExecutionContext(exe_ctx); |
Greg Clayton | c3b61d2 | 2010-12-15 05:08:08 +0000 | [diff] [blame] | 899 | Error error; |
Greg Clayton | c67efa4 | 2011-01-20 19:27:18 +0000 | [diff] [blame] | 900 | const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember | |
| 901 | StackFrame::eExpressionPathOptionsNoFragileObjcIvar; |
| 902 | result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error); |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 903 | } |
| 904 | else if (m_process_sp) |
| 905 | { |
| 906 | m_process_sp->CalculateExecutionContext(exe_ctx); |
| 907 | } |
| 908 | else |
| 909 | { |
| 910 | CalculateExecutionContext(exe_ctx); |
| 911 | } |
| 912 | |
| 913 | if (result_valobj_sp) |
| 914 | { |
| 915 | execution_results = eExecutionCompleted; |
| 916 | // We got a result from the frame variable expression path above... |
| 917 | ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName()); |
| 918 | |
| 919 | lldb::ValueObjectSP const_valobj_sp; |
| 920 | |
| 921 | // Check in case our value is already a constant value |
| 922 | if (result_valobj_sp->GetIsConstant()) |
| 923 | { |
| 924 | const_valobj_sp = result_valobj_sp; |
| 925 | const_valobj_sp->SetName (persistent_variable_name); |
| 926 | } |
| 927 | else |
| 928 | const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(), |
| 929 | persistent_variable_name); |
| 930 | |
Sean Callanan | 6a92553 | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 931 | lldb::ValueObjectSP live_valobj_sp = result_valobj_sp; |
| 932 | |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 933 | result_valobj_sp = const_valobj_sp; |
| 934 | |
Sean Callanan | 6a92553 | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 935 | ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp)); |
| 936 | assert (clang_expr_variable_sp.get()); |
| 937 | |
| 938 | // Set flags and live data as appropriate |
| 939 | |
| 940 | const Value &result_value = live_valobj_sp->GetValue(); |
| 941 | |
| 942 | switch (result_value.GetValueType()) |
| 943 | { |
| 944 | case Value::eValueTypeHostAddress: |
| 945 | case Value::eValueTypeFileAddress: |
| 946 | // we don't do anything with these for now |
| 947 | break; |
| 948 | case Value::eValueTypeScalar: |
| 949 | clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated; |
| 950 | clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation; |
| 951 | break; |
| 952 | case Value::eValueTypeLoadAddress: |
| 953 | clang_expr_variable_sp->m_live_sp = live_valobj_sp; |
| 954 | clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference; |
| 955 | break; |
| 956 | } |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 957 | } |
| 958 | else |
| 959 | { |
| 960 | // Make sure we aren't just trying to see the value of a persistent |
| 961 | // variable (something like "$0") |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 962 | lldb::ClangExpressionVariableSP persistent_var_sp; |
| 963 | // Only check for persistent variables the expression starts with a '$' |
| 964 | if (expr_cstr[0] == '$') |
| 965 | persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr); |
| 966 | |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 967 | if (persistent_var_sp) |
| 968 | { |
| 969 | result_valobj_sp = persistent_var_sp->GetValueObject (); |
| 970 | execution_results = eExecutionCompleted; |
| 971 | } |
| 972 | else |
| 973 | { |
| 974 | const char *prefix = GetExpressionPrefixContentsAsCString(); |
| 975 | |
| 976 | execution_results = ClangUserExpression::Evaluate (exe_ctx, |
Sean Callanan | 6a92553 | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 977 | unwind_on_error, |
| 978 | keep_in_memory, |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 979 | expr_cstr, |
| 980 | prefix, |
| 981 | result_valobj_sp); |
| 982 | } |
| 983 | } |
| 984 | return execution_results; |
| 985 | } |
| 986 | |
Jim Ingham | d60d94a | 2011-03-11 03:53:59 +0000 | [diff] [blame^] | 987 | lldb::user_id_t |
| 988 | Target::AddStopHook (Target::StopHookSP &new_hook_sp) |
| 989 | { |
| 990 | lldb::user_id_t new_uid = ++m_stop_hook_next_id; |
| 991 | new_hook_sp.reset (new StopHook(GetSP(), new_uid)); |
| 992 | m_stop_hooks[new_uid] = new_hook_sp; |
| 993 | return new_uid; |
| 994 | } |
| 995 | |
| 996 | bool |
| 997 | Target::RemoveStopHookByID (lldb::user_id_t user_id) |
| 998 | { |
| 999 | size_t num_removed; |
| 1000 | num_removed = m_stop_hooks.erase (user_id); |
| 1001 | if (num_removed == 0) |
| 1002 | return false; |
| 1003 | else |
| 1004 | return true; |
| 1005 | } |
| 1006 | |
| 1007 | void |
| 1008 | Target::RemoveAllStopHooks () |
| 1009 | { |
| 1010 | m_stop_hooks.clear(); |
| 1011 | } |
| 1012 | |
| 1013 | Target::StopHookSP |
| 1014 | Target::GetStopHookByID (lldb::user_id_t user_id) |
| 1015 | { |
| 1016 | StopHookSP found_hook; |
| 1017 | |
| 1018 | StopHookCollection::iterator specified_hook_iter; |
| 1019 | specified_hook_iter = m_stop_hooks.find (user_id); |
| 1020 | if (specified_hook_iter != m_stop_hooks.end()) |
| 1021 | found_hook = (*specified_hook_iter).second; |
| 1022 | return found_hook; |
| 1023 | } |
| 1024 | |
| 1025 | bool |
| 1026 | Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state) |
| 1027 | { |
| 1028 | StopHookCollection::iterator specified_hook_iter; |
| 1029 | specified_hook_iter = m_stop_hooks.find (user_id); |
| 1030 | if (specified_hook_iter == m_stop_hooks.end()) |
| 1031 | return false; |
| 1032 | |
| 1033 | (*specified_hook_iter).second->SetIsActive (active_state); |
| 1034 | return true; |
| 1035 | } |
| 1036 | |
| 1037 | void |
| 1038 | Target::SetAllStopHooksActiveState (bool active_state) |
| 1039 | { |
| 1040 | StopHookCollection::iterator pos, end = m_stop_hooks.end(); |
| 1041 | for (pos = m_stop_hooks.begin(); pos != end; pos++) |
| 1042 | { |
| 1043 | (*pos).second->SetIsActive (active_state); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | void |
| 1048 | Target::RunStopHooks () |
| 1049 | { |
| 1050 | if (!m_process_sp) |
| 1051 | return; |
| 1052 | |
| 1053 | if (m_stop_hooks.empty()) |
| 1054 | return; |
| 1055 | |
| 1056 | StopHookCollection::iterator pos, end = m_stop_hooks.end(); |
| 1057 | |
| 1058 | // If there aren't any active stop hooks, don't bother either: |
| 1059 | bool any_active_hooks = false; |
| 1060 | for (pos = m_stop_hooks.begin(); pos != end; pos++) |
| 1061 | { |
| 1062 | if ((*pos).second->IsActive()) |
| 1063 | { |
| 1064 | any_active_hooks = true; |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | if (!any_active_hooks) |
| 1069 | return; |
| 1070 | |
| 1071 | CommandReturnObject result; |
| 1072 | |
| 1073 | std::vector<ExecutionContext> exc_ctx_with_reasons; |
| 1074 | std::vector<SymbolContext> sym_ctx_with_reasons; |
| 1075 | |
| 1076 | ThreadList &cur_threadlist = m_process_sp->GetThreadList(); |
| 1077 | size_t num_threads = cur_threadlist.GetSize(); |
| 1078 | for (size_t i = 0; i < num_threads; i++) |
| 1079 | { |
| 1080 | lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i); |
| 1081 | if (cur_thread_sp->ThreadStoppedForAReason()) |
| 1082 | { |
| 1083 | lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0); |
| 1084 | exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get())); |
| 1085 | sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything)); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | // If no threads stopped for a reason, don't run the stop-hooks. |
| 1090 | size_t num_exe_ctx = exc_ctx_with_reasons.size(); |
| 1091 | if (num_exe_ctx == 0) |
| 1092 | return; |
| 1093 | |
| 1094 | result.SetImmediateOutputFile (m_debugger.GetOutputFile().GetStream()); |
| 1095 | result.SetImmediateErrorFile (m_debugger.GetErrorFile().GetStream()); |
| 1096 | |
| 1097 | bool keep_going = true; |
| 1098 | bool hooks_ran = false; |
| 1099 | for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++) |
| 1100 | { |
| 1101 | // result.Clear(); |
| 1102 | StopHookSP cur_hook_sp = (*pos).second; |
| 1103 | if (!cur_hook_sp->IsActive()) |
| 1104 | continue; |
| 1105 | |
| 1106 | bool any_thread_matched = false; |
| 1107 | for (size_t i = 0; keep_going && i < num_exe_ctx; i++) |
| 1108 | { |
| 1109 | if ((cur_hook_sp->GetSpecifier () == NULL |
| 1110 | || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i])) |
| 1111 | && (cur_hook_sp->GetThreadSpecifier() == NULL |
| 1112 | || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread))) |
| 1113 | { |
| 1114 | if (!hooks_ran) |
| 1115 | { |
| 1116 | result.AppendMessage("\n** Stop Hooks **\n"); |
| 1117 | hooks_ran = true; |
| 1118 | } |
| 1119 | if (!any_thread_matched) |
| 1120 | { |
| 1121 | result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID()); |
| 1122 | any_thread_matched = true; |
| 1123 | } |
| 1124 | |
| 1125 | result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID()); |
| 1126 | |
| 1127 | bool stop_on_continue = true; |
| 1128 | bool stop_on_error = true; |
| 1129 | bool echo_commands = false; |
| 1130 | bool print_results = true; |
| 1131 | GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(), |
| 1132 | &exc_ctx_with_reasons[i], |
| 1133 | stop_on_continue, |
| 1134 | stop_on_error, |
| 1135 | echo_commands, |
| 1136 | print_results, |
| 1137 | result); |
| 1138 | |
| 1139 | // If the command started the target going again, we should bag out of |
| 1140 | // running the stop hooks. |
| 1141 | if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) |
| 1142 | || (result.GetStatus() == eReturnStatusSuccessContinuingResult)) |
| 1143 | { |
| 1144 | result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID()); |
| 1145 | keep_going = false; |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | if (hooks_ran) |
| 1151 | result.AppendMessage ("\n** End Stop Hooks **\n"); |
| 1152 | } |
| 1153 | |
| 1154 | //-------------------------------------------------------------- |
| 1155 | // class Target::StopHook |
| 1156 | //-------------------------------------------------------------- |
| 1157 | |
| 1158 | |
| 1159 | Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) : |
| 1160 | UserID (uid), |
| 1161 | m_target_sp (target_sp), |
| 1162 | m_active (true), |
| 1163 | m_commands (), |
| 1164 | m_specifier_sp (), |
| 1165 | m_thread_spec_ap(NULL) |
| 1166 | { |
| 1167 | } |
| 1168 | |
| 1169 | Target::StopHook::StopHook (const StopHook &rhs) : |
| 1170 | UserID (rhs.GetID()), |
| 1171 | m_target_sp (rhs.m_target_sp), |
| 1172 | m_commands (rhs.m_commands), |
| 1173 | m_specifier_sp (rhs.m_specifier_sp), |
| 1174 | m_active (rhs.m_active), |
| 1175 | m_thread_spec_ap (NULL) |
| 1176 | { |
| 1177 | if (rhs.m_thread_spec_ap.get() != NULL) |
| 1178 | m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get())); |
| 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | Target::StopHook::~StopHook () |
| 1183 | { |
| 1184 | } |
| 1185 | |
| 1186 | void |
| 1187 | Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier) |
| 1188 | { |
| 1189 | m_thread_spec_ap.reset (specifier); |
| 1190 | } |
| 1191 | |
| 1192 | |
| 1193 | void |
| 1194 | Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const |
| 1195 | { |
| 1196 | int indent_level = s->GetIndentLevel(); |
| 1197 | |
| 1198 | s->SetIndentLevel(indent_level + 2); |
| 1199 | |
| 1200 | s->Printf ("Hook: %d\n", GetID()); |
| 1201 | if (m_active) |
| 1202 | s->Indent ("State: enabled\n"); |
| 1203 | else |
| 1204 | s->Indent ("State: disabled\n"); |
| 1205 | |
| 1206 | if (m_specifier_sp) |
| 1207 | { |
| 1208 | s->Indent(); |
| 1209 | s->PutCString ("Specifier:\n"); |
| 1210 | s->SetIndentLevel (indent_level + 4); |
| 1211 | m_specifier_sp->GetDescription (s, level); |
| 1212 | s->SetIndentLevel (indent_level + 2); |
| 1213 | } |
| 1214 | |
| 1215 | if (m_thread_spec_ap.get() != NULL) |
| 1216 | { |
| 1217 | StreamString tmp; |
| 1218 | s->Indent("Thread:\n"); |
| 1219 | m_thread_spec_ap->GetDescription (&tmp, level); |
| 1220 | s->SetIndentLevel (indent_level + 4); |
| 1221 | s->Indent (tmp.GetData()); |
| 1222 | s->PutCString ("\n"); |
| 1223 | s->SetIndentLevel (indent_level + 2); |
| 1224 | } |
| 1225 | |
| 1226 | s->Indent ("Commands: \n"); |
| 1227 | s->SetIndentLevel (indent_level + 4); |
| 1228 | uint32_t num_commands = m_commands.GetSize(); |
| 1229 | for (uint32_t i = 0; i < num_commands; i++) |
| 1230 | { |
| 1231 | s->Indent(m_commands.GetStringAtIndex(i)); |
| 1232 | s->PutCString ("\n"); |
| 1233 | } |
| 1234 | s->SetIndentLevel (indent_level); |
| 1235 | } |
| 1236 | |
| 1237 | |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1238 | //-------------------------------------------------------------- |
| 1239 | // class Target::SettingsController |
| 1240 | //-------------------------------------------------------------- |
| 1241 | |
| 1242 | Target::SettingsController::SettingsController () : |
| 1243 | UserSettingsController ("target", Debugger::GetSettingsController()), |
| 1244 | m_default_architecture () |
| 1245 | { |
| 1246 | m_default_settings.reset (new TargetInstanceSettings (*this, false, |
| 1247 | InstanceSettings::GetDefaultName().AsCString())); |
| 1248 | } |
| 1249 | |
| 1250 | Target::SettingsController::~SettingsController () |
| 1251 | { |
| 1252 | } |
| 1253 | |
| 1254 | lldb::InstanceSettingsSP |
| 1255 | Target::SettingsController::CreateInstanceSettings (const char *instance_name) |
| 1256 | { |
Greg Clayton | c0c1b0c | 2010-11-19 03:46:01 +0000 | [diff] [blame] | 1257 | TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(), |
| 1258 | false, |
| 1259 | instance_name); |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1260 | lldb::InstanceSettingsSP new_settings_sp (new_settings); |
| 1261 | return new_settings_sp; |
| 1262 | } |
| 1263 | |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1264 | |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1265 | #define TSC_DEFAULT_ARCH "default-arch" |
| 1266 | #define TSC_EXPR_PREFIX "expr-prefix" |
| 1267 | #define TSC_EXEC_LEVEL "execution-level" |
| 1268 | #define TSC_EXEC_MODE "execution-mode" |
| 1269 | #define TSC_EXEC_OS_TYPE "execution-os-type" |
| 1270 | |
| 1271 | |
| 1272 | static const ConstString & |
| 1273 | GetSettingNameForDefaultArch () |
| 1274 | { |
| 1275 | static ConstString g_const_string (TSC_DEFAULT_ARCH); |
| 1276 | |
| 1277 | return g_const_string; |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1280 | static const ConstString & |
| 1281 | GetSettingNameForExpressionPrefix () |
| 1282 | { |
| 1283 | static ConstString g_const_string (TSC_EXPR_PREFIX); |
| 1284 | return g_const_string; |
| 1285 | } |
| 1286 | |
| 1287 | static const ConstString & |
| 1288 | GetSettingNameForExecutionLevel () |
| 1289 | { |
| 1290 | static ConstString g_const_string (TSC_EXEC_LEVEL); |
| 1291 | return g_const_string; |
| 1292 | } |
| 1293 | |
| 1294 | static const ConstString & |
| 1295 | GetSettingNameForExecutionMode () |
| 1296 | { |
| 1297 | static ConstString g_const_string (TSC_EXEC_MODE); |
| 1298 | return g_const_string; |
| 1299 | } |
| 1300 | |
| 1301 | static const ConstString & |
| 1302 | GetSettingNameForExecutionOSType () |
| 1303 | { |
| 1304 | static ConstString g_const_string (TSC_EXEC_OS_TYPE); |
| 1305 | return g_const_string; |
| 1306 | } |
| 1307 | |
| 1308 | |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1309 | bool |
| 1310 | Target::SettingsController::SetGlobalVariable (const ConstString &var_name, |
| 1311 | const char *index_value, |
| 1312 | const char *value, |
| 1313 | const SettingEntry &entry, |
| 1314 | const lldb::VarSetOperationType op, |
| 1315 | Error&err) |
| 1316 | { |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1317 | if (var_name == GetSettingNameForDefaultArch()) |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1318 | { |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 1319 | m_default_architecture.SetTriple (value); |
| 1320 | if (!m_default_architecture.IsValid()) |
| 1321 | err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value); |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1322 | } |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
| 1326 | |
| 1327 | bool |
| 1328 | Target::SettingsController::GetGlobalVariable (const ConstString &var_name, |
| 1329 | StringList &value, |
| 1330 | Error &err) |
| 1331 | { |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1332 | if (var_name == GetSettingNameForDefaultArch()) |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1333 | { |
Greg Clayton | bf6e210 | 2010-10-27 02:06:37 +0000 | [diff] [blame] | 1334 | // If the arch is invalid (the default), don't show a string for it |
| 1335 | if (m_default_architecture.IsValid()) |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 1336 | value.AppendString (m_default_architecture.GetArchitectureName()); |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1337 | return true; |
| 1338 | } |
| 1339 | else |
| 1340 | err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); |
| 1341 | |
| 1342 | return false; |
| 1343 | } |
| 1344 | |
| 1345 | //-------------------------------------------------------------- |
| 1346 | // class TargetInstanceSettings |
| 1347 | //-------------------------------------------------------------- |
| 1348 | |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 1349 | TargetInstanceSettings::TargetInstanceSettings |
| 1350 | ( |
| 1351 | UserSettingsController &owner, |
| 1352 | bool live_instance, |
| 1353 | const char *name |
| 1354 | ) : |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1355 | InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance), |
| 1356 | m_expr_prefix_path (), |
| 1357 | m_expr_prefix_contents (), |
| 1358 | m_execution_level (eExecutionLevelAuto), |
| 1359 | m_execution_mode (eExecutionModeAuto), |
| 1360 | m_execution_os_type (eExecutionOSTypeAuto) |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1361 | { |
| 1362 | // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called |
| 1363 | // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers. |
| 1364 | // For this reason it has to be called here, rather than in the initializer or in the parent constructor. |
| 1365 | // This is true for CreateInstanceName() too. |
| 1366 | |
| 1367 | if (GetInstanceName () == InstanceSettings::InvalidName()) |
| 1368 | { |
| 1369 | ChangeInstanceName (std::string (CreateInstanceName().AsCString())); |
| 1370 | m_owner.RegisterInstanceSettings (this); |
| 1371 | } |
| 1372 | |
| 1373 | if (live_instance) |
| 1374 | { |
| 1375 | const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); |
| 1376 | CopyInstanceSettings (pending_settings,false); |
| 1377 | //m_owner.RemovePendingSettings (m_instance_name); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) : |
Greg Clayton | c0c1b0c | 2010-11-19 03:46:01 +0000 | [diff] [blame] | 1382 | InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()) |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1383 | { |
| 1384 | if (m_instance_name != InstanceSettings::GetDefaultName()) |
| 1385 | { |
| 1386 | const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); |
| 1387 | CopyInstanceSettings (pending_settings,false); |
| 1388 | //m_owner.RemovePendingSettings (m_instance_name); |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | TargetInstanceSettings::~TargetInstanceSettings () |
| 1393 | { |
| 1394 | } |
| 1395 | |
| 1396 | TargetInstanceSettings& |
| 1397 | TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs) |
| 1398 | { |
| 1399 | if (this != &rhs) |
| 1400 | { |
| 1401 | } |
| 1402 | |
| 1403 | return *this; |
| 1404 | } |
| 1405 | |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1406 | void |
| 1407 | TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, |
| 1408 | const char *index_value, |
| 1409 | const char *value, |
| 1410 | const ConstString &instance_name, |
| 1411 | const SettingEntry &entry, |
| 1412 | lldb::VarSetOperationType op, |
| 1413 | Error &err, |
| 1414 | bool pending) |
| 1415 | { |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1416 | int new_enum = -1; |
| 1417 | |
| 1418 | if (var_name == GetSettingNameForExpressionPrefix ()) |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 1419 | { |
| 1420 | switch (op) |
| 1421 | { |
| 1422 | default: |
| 1423 | err.SetErrorToGenericError (); |
| 1424 | err.SetErrorString ("Unrecognized operation. Cannot update value.\n"); |
| 1425 | return; |
| 1426 | case lldb::eVarSetOperationAssign: |
| 1427 | { |
| 1428 | FileSpec file_spec(value, true); |
| 1429 | |
| 1430 | if (!file_spec.Exists()) |
| 1431 | { |
| 1432 | err.SetErrorToGenericError (); |
| 1433 | err.SetErrorStringWithFormat ("%s does not exist.\n", value); |
| 1434 | return; |
| 1435 | } |
| 1436 | |
Greg Clayton | 9f52704 | 2011-02-03 17:47:47 +0000 | [diff] [blame] | 1437 | DataBufferSP data_sp (file_spec.ReadFileContents()); |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 1438 | |
Greg Clayton | 9f52704 | 2011-02-03 17:47:47 +0000 | [diff] [blame] | 1439 | if (!data_sp && data_sp->GetByteSize() == 0) |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 1440 | { |
| 1441 | err.SetErrorToGenericError (); |
Greg Clayton | 9f52704 | 2011-02-03 17:47:47 +0000 | [diff] [blame] | 1442 | err.SetErrorStringWithFormat ("Couldn't read from %s\n", value); |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 1443 | return; |
| 1444 | } |
| 1445 | |
| 1446 | m_expr_prefix_path = value; |
Greg Clayton | 9f52704 | 2011-02-03 17:47:47 +0000 | [diff] [blame] | 1447 | m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize()); |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 1448 | } |
| 1449 | return; |
| 1450 | case lldb::eVarSetOperationAppend: |
| 1451 | err.SetErrorToGenericError (); |
| 1452 | err.SetErrorString ("Cannot append to a path.\n"); |
| 1453 | return; |
| 1454 | case lldb::eVarSetOperationClear: |
| 1455 | m_expr_prefix_path.clear (); |
| 1456 | m_expr_prefix_contents.clear (); |
| 1457 | return; |
| 1458 | } |
| 1459 | } |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1460 | else if (var_name == GetSettingNameForExecutionLevel ()) |
| 1461 | { |
| 1462 | UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err); |
| 1463 | if (err.Success()) |
| 1464 | m_execution_level = (ExecutionLevel)new_enum; |
| 1465 | } |
| 1466 | else if (var_name == GetSettingNameForExecutionMode ()) |
| 1467 | { |
| 1468 | UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err); |
| 1469 | if (err.Success()) |
| 1470 | m_execution_mode = (ExecutionMode)new_enum; |
| 1471 | } |
| 1472 | else if (var_name == GetSettingNameForExecutionOSType ()) |
| 1473 | { |
| 1474 | UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err); |
| 1475 | if (err.Success()) |
| 1476 | m_execution_os_type = (ExecutionOSType)new_enum; |
| 1477 | } |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | void |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1481 | TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending) |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1482 | { |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 1483 | TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get()); |
| 1484 | |
| 1485 | if (!new_settings_ptr) |
| 1486 | return; |
| 1487 | |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1488 | m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path; |
| 1489 | m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents; |
| 1490 | m_execution_level = new_settings_ptr->m_execution_level; |
| 1491 | m_execution_mode = new_settings_ptr->m_execution_mode; |
| 1492 | m_execution_os_type = new_settings_ptr->m_execution_os_type; |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 1495 | bool |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1496 | TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, |
| 1497 | const ConstString &var_name, |
| 1498 | StringList &value, |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 1499 | Error *err) |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1500 | { |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1501 | if (var_name == GetSettingNameForExpressionPrefix ()) |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 1502 | { |
| 1503 | value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size()); |
| 1504 | } |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1505 | else if (var_name == GetSettingNameForExecutionLevel ()) |
| 1506 | { |
| 1507 | value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_level)); |
| 1508 | } |
| 1509 | else if (var_name == GetSettingNameForExecutionMode ()) |
| 1510 | { |
| 1511 | value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_mode)); |
| 1512 | } |
| 1513 | else if (var_name == GetSettingNameForExecutionOSType ()) |
| 1514 | { |
| 1515 | value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_os_type)); |
| 1516 | } |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 1517 | else |
| 1518 | { |
| 1519 | if (err) |
| 1520 | err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); |
| 1521 | return false; |
| 1522 | } |
| 1523 | |
| 1524 | return true; |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | const ConstString |
| 1528 | TargetInstanceSettings::CreateInstanceName () |
| 1529 | { |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1530 | StreamString sstr; |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 1531 | static int instance_count = 1; |
| 1532 | |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1533 | sstr.Printf ("target_%d", instance_count); |
| 1534 | ++instance_count; |
| 1535 | |
| 1536 | const ConstString ret_val (sstr.GetData()); |
| 1537 | return ret_val; |
| 1538 | } |
| 1539 | |
| 1540 | //-------------------------------------------------- |
| 1541 | // Target::SettingsController Variable Tables |
| 1542 | //-------------------------------------------------- |
| 1543 | |
| 1544 | SettingEntry |
| 1545 | Target::SettingsController::global_settings_table[] = |
| 1546 | { |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1547 | // var-name var-type default enum init'd hidden help-text |
| 1548 | // ================= ================== =========== ==== ====== ====== ========================================================================= |
| 1549 | { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." }, |
| 1550 | { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL } |
| 1551 | }; |
| 1552 | |
| 1553 | static lldb::OptionEnumValueElement |
| 1554 | g_execution_level_enums[] = |
| 1555 | { |
| 1556 | { eExecutionLevelAuto , "auto" , "Automatically detect the execution level (user/kernel)." }, |
| 1557 | { eExecutionLevelKernel , "kernel" , "Treat executables as kernel executables." }, |
| 1558 | { eExecutionLevelUser , "user" , "Treat executables as user space executables." }, |
| 1559 | { 0 , NULL , NULL } |
| 1560 | }; |
| 1561 | |
| 1562 | static lldb::OptionEnumValueElement |
| 1563 | g_execution_mode_enums[] = |
| 1564 | { |
| 1565 | { eExecutionModeAuto , "auto" , "Automatically detect the execution mode (static/dynamic)." }, |
| 1566 | { eExecutionModeStatic , "static" , "All executables are static and addresses at the virtual addresses in the object files." }, |
| 1567 | { eExecutionModeDynamic , "dynamic" , "Executables and shared libraries are dynamically loaded.." }, |
| 1568 | { 0 , NULL , NULL } |
| 1569 | }; |
| 1570 | |
| 1571 | static lldb::OptionEnumValueElement |
| 1572 | g_execution_os_enums[] = |
| 1573 | { |
| 1574 | { eExecutionOSTypeAuto , "auto" , "Automatically detect the execution OS (none/halted/live)." }, |
| 1575 | { eExecutionOSTypeNone , "none" , "There is no operating system available (no processes or threads)." }, |
| 1576 | { eExecutionOSTypeHalted, "halted" , "There is an operating system, but it is halted when the debugger is halted. " |
| 1577 | "Processes and threads must be discovered by accessing symbols and reading " |
| 1578 | "memory." }, |
| 1579 | { eExecutionOSTypeLive , "live" , "There is a live operating system with debug services that can be used to " |
| 1580 | "get processes, threads and theirs states." }, |
| 1581 | { 0, NULL, NULL } |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1582 | }; |
| 1583 | |
| 1584 | SettingEntry |
| 1585 | Target::SettingsController::instance_settings_table[] = |
| 1586 | { |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 1587 | // var-name var-type default enum-table init'd hidden help-text |
| 1588 | // ================= ================== =========== ========================= ====== ====== ========================================================================= |
| 1589 | { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL , false, false, "Path to a file containing expressions to be prepended to all expressions." }, |
| 1590 | { TSC_EXEC_LEVEL , eSetVarTypeEnum , "auto" , g_execution_level_enums , false, false, "Sets the execution level for a target." }, |
| 1591 | { TSC_EXEC_MODE , eSetVarTypeEnum , "auto" , g_execution_mode_enums , false, false, "Sets the execution mode for a target." }, |
| 1592 | { TSC_EXEC_OS_TYPE , eSetVarTypeEnum , "auto" , g_execution_os_enums , false, false, "Sets the execution OS for a target." }, |
| 1593 | { NULL , eSetVarTypeNone , NULL , NULL , false, false, NULL } |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1594 | }; |