Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [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" |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 | [diff] [blame] | 20 | #include "lldb/Core/DataBufferMemoryMap.h" |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 | [diff] [blame] | 21 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 22 | #include "lldb/Core/Event.h" |
| 23 | #include "lldb/Core/Log.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 24 | #include "lldb/Core/StreamString.h" |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 | [diff] [blame] | 25 | #include "lldb/Core/Timer.h" |
| 26 | #include "lldb/Core/ValueObject.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 27 | #include "lldb/Host/Host.h" |
| 28 | #include "lldb/lldb-private-log.h" |
| 29 | #include "lldb/Symbol/ObjectFile.h" |
| 30 | #include "lldb/Target/Process.h" |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 | [diff] [blame] | 31 | #include "lldb/Target/StackFrame.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 32 | |
| 33 | using namespace lldb; |
| 34 | using namespace lldb_private; |
| 35 | |
| 36 | //---------------------------------------------------------------------- |
| 37 | // Target constructor |
| 38 | //---------------------------------------------------------------------- |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 | [diff] [blame] | 39 | Target::Target(Debugger &debugger) : |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 | [diff] [blame] | 40 | Broadcaster("lldb.target"), |
Greg Clayton | dbe5450 | 2010-11-19 03:46:01 | [diff] [blame] | 41 | TargetInstanceSettings (*GetSettingsController()), |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 | [diff] [blame] | 42 | m_debugger (debugger), |
Greg Clayton | af67cec | 2010-12-20 20:49:23 | [diff] [blame^] | 43 | m_mutex (Mutex::eMutexTypeRecursive), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 44 | m_images(), |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 | [diff] [blame] | 45 | m_section_load_list (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 46 | m_breakpoint_list (false), |
| 47 | m_internal_breakpoint_list (true), |
| 48 | m_process_sp(), |
| 49 | m_triple(), |
| 50 | m_search_filter_sp(), |
| 51 | m_image_search_paths (ImageSearchPathsChanged, this), |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 | [diff] [blame] | 52 | m_scratch_ast_context_ap (NULL), |
| 53 | m_persistent_variables () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 54 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 | [diff] [blame] | 55 | SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed"); |
| 56 | SetEventName (eBroadcastBitModulesLoaded, "modules-loaded"); |
| 57 | SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded"); |
| 58 | |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 | [diff] [blame] | 59 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 60 | if (log) |
| 61 | log->Printf ("%p Target::Target()", this); |
| 62 | } |
| 63 | |
| 64 | //---------------------------------------------------------------------- |
| 65 | // Destructor |
| 66 | //---------------------------------------------------------------------- |
| 67 | Target::~Target() |
| 68 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 | [diff] [blame] | 69 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 70 | if (log) |
| 71 | log->Printf ("%p Target::~Target()", this); |
| 72 | DeleteCurrentProcess (); |
| 73 | } |
| 74 | |
| 75 | void |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 | [diff] [blame] | 76 | Target::Dump (Stream *s, lldb::DescriptionLevel description_level) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 77 | { |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 | [diff] [blame] | 78 | // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 | [diff] [blame] | 79 | if (description_level != lldb::eDescriptionLevelBrief) |
| 80 | { |
| 81 | s->Indent(); |
| 82 | s->PutCString("Target\n"); |
| 83 | s->IndentMore(); |
Greg Clayton | 93aa84e | 2010-10-29 04:59:35 | [diff] [blame] | 84 | m_images.Dump(s); |
| 85 | m_breakpoint_list.Dump(s); |
| 86 | m_internal_breakpoint_list.Dump(s); |
| 87 | s->IndentLess(); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 | [diff] [blame] | 88 | } |
| 89 | else |
| 90 | { |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 | [diff] [blame] | 91 | s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString()); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 | [diff] [blame] | 92 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
| 96 | Target::DeleteCurrentProcess () |
| 97 | { |
| 98 | if (m_process_sp.get()) |
| 99 | { |
Greg Clayton | 17f6920 | 2010-09-14 23:52:43 | [diff] [blame] | 100 | m_section_load_list.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 101 | if (m_process_sp->IsAlive()) |
| 102 | m_process_sp->Destroy(); |
| 103 | else |
| 104 | m_process_sp->Finalize(); |
| 105 | |
| 106 | // Do any cleanup of the target we need to do between process instances. |
| 107 | // NB It is better to do this before destroying the process in case the |
| 108 | // clean up needs some help from the process. |
| 109 | m_breakpoint_list.ClearAllBreakpointSites(); |
| 110 | m_internal_breakpoint_list.ClearAllBreakpointSites(); |
| 111 | m_process_sp.reset(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | const lldb::ProcessSP & |
| 116 | Target::CreateProcess (Listener &listener, const char *plugin_name) |
| 117 | { |
| 118 | DeleteCurrentProcess (); |
| 119 | m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener)); |
| 120 | return m_process_sp; |
| 121 | } |
| 122 | |
| 123 | const lldb::ProcessSP & |
| 124 | Target::GetProcessSP () const |
| 125 | { |
| 126 | return m_process_sp; |
| 127 | } |
| 128 | |
| 129 | lldb::TargetSP |
| 130 | Target::GetSP() |
| 131 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 | [diff] [blame] | 132 | return m_debugger.GetTargetList().GetTargetSP(this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | BreakpointList & |
| 136 | Target::GetBreakpointList(bool internal) |
| 137 | { |
| 138 | if (internal) |
| 139 | return m_internal_breakpoint_list; |
| 140 | else |
| 141 | return m_breakpoint_list; |
| 142 | } |
| 143 | |
| 144 | const BreakpointList & |
| 145 | Target::GetBreakpointList(bool internal) const |
| 146 | { |
| 147 | if (internal) |
| 148 | return m_internal_breakpoint_list; |
| 149 | else |
| 150 | return m_breakpoint_list; |
| 151 | } |
| 152 | |
| 153 | BreakpointSP |
| 154 | Target::GetBreakpointByID (break_id_t break_id) |
| 155 | { |
| 156 | BreakpointSP bp_sp; |
| 157 | |
| 158 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
| 159 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); |
| 160 | else |
| 161 | bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); |
| 162 | |
| 163 | return bp_sp; |
| 164 | } |
| 165 | |
| 166 | BreakpointSP |
| 167 | Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal) |
| 168 | { |
| 169 | SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); |
| 170 | BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines)); |
| 171 | return CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | BreakpointSP |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 | [diff] [blame] | 176 | Target::CreateBreakpoint (lldb::addr_t addr, bool internal) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 177 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 178 | Address so_addr; |
| 179 | // Attempt to resolve our load address if possible, though it is ok if |
| 180 | // it doesn't resolve to section/offset. |
| 181 | |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 | [diff] [blame] | 182 | // Try and resolve as a load address if possible |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 | [diff] [blame] | 183 | m_section_load_list.ResolveLoadAddress(addr, so_addr); |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 | [diff] [blame] | 184 | if (!so_addr.IsValid()) |
| 185 | { |
| 186 | // The address didn't resolve, so just set this as an absolute address |
| 187 | so_addr.SetOffset (addr); |
| 188 | } |
| 189 | BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 190 | return bp_sp; |
| 191 | } |
| 192 | |
| 193 | BreakpointSP |
| 194 | Target::CreateBreakpoint (Address &addr, bool internal) |
| 195 | { |
| 196 | TargetSP target_sp = this->GetSP(); |
| 197 | SearchFilterSP filter_sp(new SearchFilter (target_sp)); |
| 198 | BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr)); |
| 199 | return CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 200 | } |
| 201 | |
| 202 | BreakpointSP |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 | [diff] [blame] | 203 | Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 204 | { |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 | [diff] [blame] | 205 | BreakpointSP bp_sp; |
| 206 | if (func_name) |
| 207 | { |
| 208 | SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); |
| 209 | BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact)); |
| 210 | bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 211 | } |
| 212 | return bp_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
| 216 | SearchFilterSP |
| 217 | Target::GetSearchFilterForModule (const FileSpec *containingModule) |
| 218 | { |
| 219 | SearchFilterSP filter_sp; |
| 220 | lldb::TargetSP target_sp = this->GetSP(); |
| 221 | if (containingModule != NULL) |
| 222 | { |
| 223 | // TODO: We should look into sharing module based search filters |
| 224 | // across many breakpoints like we do for the simple target based one |
| 225 | filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule)); |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | if (m_search_filter_sp.get() == NULL) |
| 230 | m_search_filter_sp.reset (new SearchFilter (target_sp)); |
| 231 | filter_sp = m_search_filter_sp; |
| 232 | } |
| 233 | return filter_sp; |
| 234 | } |
| 235 | |
| 236 | BreakpointSP |
| 237 | Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal) |
| 238 | { |
| 239 | SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); |
| 240 | BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex)); |
| 241 | |
| 242 | return CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 243 | } |
| 244 | |
| 245 | BreakpointSP |
| 246 | Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal) |
| 247 | { |
| 248 | BreakpointSP bp_sp; |
| 249 | if (filter_sp && resolver_sp) |
| 250 | { |
| 251 | bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp)); |
| 252 | resolver_sp->SetBreakpoint (bp_sp.get()); |
| 253 | |
| 254 | if (internal) |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 255 | m_internal_breakpoint_list.Add (bp_sp, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 256 | else |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 257 | m_breakpoint_list.Add (bp_sp, true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 258 | |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 | [diff] [blame] | 259 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 260 | if (log) |
| 261 | { |
| 262 | StreamString s; |
| 263 | bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
| 264 | log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData()); |
| 265 | } |
| 266 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 267 | bp_sp->ResolveBreakpoint(); |
| 268 | } |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 | [diff] [blame] | 269 | |
| 270 | if (!internal && bp_sp) |
| 271 | { |
| 272 | m_last_created_breakpoint = bp_sp; |
| 273 | } |
| 274 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 275 | return bp_sp; |
| 276 | } |
| 277 | |
| 278 | void |
| 279 | Target::RemoveAllBreakpoints (bool internal_also) |
| 280 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 | [diff] [blame] | 281 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 282 | if (log) |
| 283 | log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); |
| 284 | |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 285 | m_breakpoint_list.RemoveAll (true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 286 | if (internal_also) |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 287 | m_internal_breakpoint_list.RemoveAll (false); |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 | [diff] [blame] | 288 | |
| 289 | m_last_created_breakpoint.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void |
| 293 | Target::DisableAllBreakpoints (bool internal_also) |
| 294 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 | [diff] [blame] | 295 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 296 | if (log) |
| 297 | log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); |
| 298 | |
| 299 | m_breakpoint_list.SetEnabledAll (false); |
| 300 | if (internal_also) |
| 301 | m_internal_breakpoint_list.SetEnabledAll (false); |
| 302 | } |
| 303 | |
| 304 | void |
| 305 | Target::EnableAllBreakpoints (bool internal_also) |
| 306 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 | [diff] [blame] | 307 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 308 | if (log) |
| 309 | log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); |
| 310 | |
| 311 | m_breakpoint_list.SetEnabledAll (true); |
| 312 | if (internal_also) |
| 313 | m_internal_breakpoint_list.SetEnabledAll (true); |
| 314 | } |
| 315 | |
| 316 | bool |
| 317 | Target::RemoveBreakpointByID (break_id_t break_id) |
| 318 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 | [diff] [blame] | 319 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 320 | if (log) |
| 321 | log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); |
| 322 | |
| 323 | if (DisableBreakpointByID (break_id)) |
| 324 | { |
| 325 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 326 | m_internal_breakpoint_list.Remove(break_id, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 327 | else |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 | [diff] [blame] | 328 | { |
| 329 | if (m_last_created_breakpoint->GetID() == break_id) |
| 330 | m_last_created_breakpoint.reset(); |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 331 | m_breakpoint_list.Remove(break_id, true); |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 | [diff] [blame] | 332 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 333 | return true; |
| 334 | } |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | bool |
| 339 | Target::DisableBreakpointByID (break_id_t break_id) |
| 340 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 | [diff] [blame] | 341 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 342 | if (log) |
| 343 | log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); |
| 344 | |
| 345 | BreakpointSP bp_sp; |
| 346 | |
| 347 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
| 348 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); |
| 349 | else |
| 350 | bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); |
| 351 | if (bp_sp) |
| 352 | { |
| 353 | bp_sp->SetEnabled (false); |
| 354 | return true; |
| 355 | } |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | bool |
| 360 | Target::EnableBreakpointByID (break_id_t break_id) |
| 361 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 | [diff] [blame] | 362 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 363 | if (log) |
| 364 | log->Printf ("Target::%s (break_id = %i, internal = %s)\n", |
| 365 | __FUNCTION__, |
| 366 | break_id, |
| 367 | LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); |
| 368 | |
| 369 | BreakpointSP bp_sp; |
| 370 | |
| 371 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
| 372 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); |
| 373 | else |
| 374 | bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); |
| 375 | |
| 376 | if (bp_sp) |
| 377 | { |
| 378 | bp_sp->SetEnabled (true); |
| 379 | return true; |
| 380 | } |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | ModuleSP |
| 385 | Target::GetExecutableModule () |
| 386 | { |
| 387 | ModuleSP executable_sp; |
| 388 | if (m_images.GetSize() > 0) |
| 389 | executable_sp = m_images.GetModuleAtIndex(0); |
| 390 | return executable_sp; |
| 391 | } |
| 392 | |
| 393 | void |
| 394 | Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files) |
| 395 | { |
| 396 | m_images.Clear(); |
| 397 | m_scratch_ast_context_ap.reset(); |
| 398 | |
| 399 | if (executable_sp.get()) |
| 400 | { |
| 401 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 402 | "Target::SetExecutableModule (executable = '%s/%s')", |
| 403 | executable_sp->GetFileSpec().GetDirectory().AsCString(), |
| 404 | executable_sp->GetFileSpec().GetFilename().AsCString()); |
| 405 | |
| 406 | m_images.Append(executable_sp); // The first image is our exectuable file |
| 407 | |
| 408 | ArchSpec exe_arch = executable_sp->GetArchitecture(); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 | [diff] [blame] | 409 | // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module. |
| 410 | if (!m_arch_spec.IsValid()) |
| 411 | m_arch_spec = exe_arch; |
| 412 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 413 | FileSpecList dependent_files; |
| 414 | ObjectFile * executable_objfile = executable_sp->GetObjectFile(); |
| 415 | if (executable_objfile == NULL) |
| 416 | { |
| 417 | |
| 418 | FileSpec bundle_executable(executable_sp->GetFileSpec()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 | [diff] [blame] | 419 | if (Host::ResolveExecutableInBundle (bundle_executable)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 420 | { |
| 421 | ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable, |
| 422 | exe_arch)); |
| 423 | SetExecutableModule (bundle_exe_module_sp, get_dependent_files); |
| 424 | if (bundle_exe_module_sp->GetObjectFile() != NULL) |
| 425 | executable_sp = bundle_exe_module_sp; |
| 426 | return; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | if (executable_objfile) |
| 431 | { |
| 432 | executable_objfile->GetDependentModules(dependent_files); |
| 433 | for (uint32_t i=0; i<dependent_files.GetSize(); i++) |
| 434 | { |
| 435 | ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i), |
| 436 | exe_arch)); |
| 437 | if (image_module_sp.get()) |
| 438 | { |
| 439 | //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY |
| 440 | ObjectFile *objfile = image_module_sp->GetObjectFile(); |
| 441 | if (objfile) |
| 442 | objfile->GetDependentModules(dependent_files); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // Now see if we know the target triple, and if so, create our scratch AST context: |
| 448 | ConstString target_triple; |
| 449 | if (GetTargetTriple(target_triple)) |
| 450 | { |
| 451 | m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString())); |
| 452 | } |
| 453 | } |
Caroline Tice | 1559a46 | 2010-09-27 00:30:10 | [diff] [blame] | 454 | |
| 455 | UpdateInstanceName(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
| 459 | ModuleList& |
| 460 | Target::GetImages () |
| 461 | { |
| 462 | return m_images; |
| 463 | } |
| 464 | |
| 465 | ArchSpec |
| 466 | Target::GetArchitecture () const |
| 467 | { |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 | [diff] [blame] | 468 | return m_arch_spec; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 469 | } |
| 470 | |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 | [diff] [blame] | 471 | bool |
| 472 | Target::SetArchitecture (const ArchSpec &arch_spec) |
| 473 | { |
| 474 | if (m_arch_spec == arch_spec) |
| 475 | { |
| 476 | // If we're setting the architecture to our current architecture, we |
| 477 | // don't need to do anything. |
| 478 | return true; |
| 479 | } |
| 480 | else if (!m_arch_spec.IsValid()) |
| 481 | { |
| 482 | // If we haven't got a valid arch spec, then we just need to set it. |
| 483 | m_arch_spec = arch_spec; |
| 484 | return true; |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | // If we have an executable file, try to reset the executable to the desired architecture |
| 489 | m_arch_spec = arch_spec; |
| 490 | ModuleSP executable_sp = GetExecutableModule (); |
| 491 | m_images.Clear(); |
| 492 | m_scratch_ast_context_ap.reset(); |
| 493 | m_triple.Clear(); |
| 494 | // Need to do something about unsetting breakpoints. |
| 495 | |
| 496 | if (executable_sp) |
| 497 | { |
| 498 | FileSpec exec_file_spec = executable_sp->GetFileSpec(); |
| 499 | Error error = ModuleList::GetSharedModule(exec_file_spec, |
| 500 | arch_spec, |
| 501 | NULL, |
| 502 | NULL, |
| 503 | 0, |
| 504 | executable_sp, |
| 505 | NULL, |
| 506 | NULL); |
| 507 | |
| 508 | if (!error.Fail() && executable_sp) |
| 509 | { |
| 510 | SetExecutableModule (executable_sp, true); |
| 511 | return true; |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | return false; |
| 516 | } |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | return false; |
| 521 | } |
| 522 | } |
| 523 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 524 | |
| 525 | bool |
| 526 | Target::GetTargetTriple(ConstString &triple) |
| 527 | { |
| 528 | triple.Clear(); |
| 529 | |
| 530 | if (m_triple) |
| 531 | { |
| 532 | triple = m_triple; |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | Module *exe_module = GetExecutableModule().get(); |
| 537 | if (exe_module) |
| 538 | { |
| 539 | ObjectFile *objfile = exe_module->GetObjectFile(); |
| 540 | if (objfile) |
| 541 | { |
| 542 | objfile->GetTargetTriple(m_triple); |
| 543 | triple = m_triple; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | return !triple.IsEmpty(); |
| 548 | } |
| 549 | |
| 550 | void |
| 551 | Target::ModuleAdded (ModuleSP &module_sp) |
| 552 | { |
| 553 | // A module is being added to this target for the first time |
| 554 | ModuleList module_list; |
| 555 | module_list.Append(module_sp); |
| 556 | ModulesDidLoad (module_list); |
| 557 | } |
| 558 | |
| 559 | void |
| 560 | Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp) |
| 561 | { |
| 562 | // A module is being added to this target for the first time |
| 563 | ModuleList module_list; |
| 564 | module_list.Append (old_module_sp); |
| 565 | ModulesDidUnload (module_list); |
| 566 | module_list.Clear (); |
| 567 | module_list.Append (new_module_sp); |
| 568 | ModulesDidLoad (module_list); |
| 569 | } |
| 570 | |
| 571 | void |
| 572 | Target::ModulesDidLoad (ModuleList &module_list) |
| 573 | { |
| 574 | m_breakpoint_list.UpdateBreakpoints (module_list, true); |
| 575 | // TODO: make event data that packages up the module_list |
| 576 | BroadcastEvent (eBroadcastBitModulesLoaded, NULL); |
| 577 | } |
| 578 | |
| 579 | void |
| 580 | Target::ModulesDidUnload (ModuleList &module_list) |
| 581 | { |
| 582 | m_breakpoint_list.UpdateBreakpoints (module_list, false); |
Greg Clayton | a4d7830 | 2010-12-06 23:51:26 | [diff] [blame] | 583 | |
| 584 | // Remove the images from the target image list |
| 585 | m_images.Remove(module_list); |
| 586 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 587 | // TODO: make event data that packages up the module_list |
| 588 | BroadcastEvent (eBroadcastBitModulesUnloaded, NULL); |
| 589 | } |
| 590 | |
| 591 | size_t |
Greg Clayton | 35f3dd2 | 2010-06-30 23:04:24 | [diff] [blame] | 592 | Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 593 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 594 | error.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 595 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 596 | bool process_is_valid = m_process_sp && m_process_sp->IsAlive(); |
| 597 | |
| 598 | Address resolved_addr(addr); |
| 599 | if (!resolved_addr.IsSectionOffset()) |
| 600 | { |
| 601 | if (process_is_valid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 602 | { |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 | [diff] [blame] | 603 | m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 604 | } |
| 605 | else |
| 606 | { |
| 607 | m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | |
| 612 | if (process_is_valid) |
| 613 | { |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 | [diff] [blame] | 614 | lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 615 | if (load_addr == LLDB_INVALID_ADDRESS) |
| 616 | { |
| 617 | if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec()) |
| 618 | error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n", |
| 619 | resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(), |
| 620 | resolved_addr.GetFileAddress()); |
| 621 | else |
| 622 | error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress()); |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | size_t bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 627 | if (bytes_read != dst_len) |
| 628 | { |
| 629 | if (error.Success()) |
| 630 | { |
| 631 | if (bytes_read == 0) |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 632 | error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 633 | else |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 634 | error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 635 | } |
| 636 | } |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 637 | if (bytes_read) |
| 638 | return bytes_read; |
| 639 | // If the address is not section offset we have an address that |
| 640 | // doesn't resolve to any address in any currently loaded shared |
| 641 | // libaries and we failed to read memory so there isn't anything |
| 642 | // more we can do. If it is section offset, we might be able to |
| 643 | // read cached memory from the object file. |
| 644 | if (!resolved_addr.IsSectionOffset()) |
| 645 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 646 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 647 | } |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 648 | |
| 649 | const Section *section = resolved_addr.GetSection(); |
| 650 | if (section && section->GetModule()) |
| 651 | { |
| 652 | ObjectFile *objfile = section->GetModule()->GetObjectFile(); |
| 653 | return section->ReadSectionDataFromObjectFile (objfile, |
| 654 | resolved_addr.GetOffset(), |
| 655 | dst, |
| 656 | dst_len); |
| 657 | } |
| 658 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | |
| 662 | ModuleSP |
| 663 | Target::GetSharedModule |
| 664 | ( |
| 665 | const FileSpec& file_spec, |
| 666 | const ArchSpec& arch, |
| 667 | const UUID *uuid_ptr, |
| 668 | const ConstString *object_name, |
| 669 | off_t object_offset, |
| 670 | Error *error_ptr |
| 671 | ) |
| 672 | { |
| 673 | // Don't pass in the UUID so we can tell if we have a stale value in our list |
| 674 | ModuleSP old_module_sp; // This will get filled in if we have a new version of the library |
| 675 | bool did_create_module = false; |
| 676 | ModuleSP module_sp; |
| 677 | |
| 678 | // If there are image search path entries, try to use them first to acquire a suitable image. |
| 679 | |
| 680 | Error error; |
| 681 | |
| 682 | if (m_image_search_paths.GetSize()) |
| 683 | { |
| 684 | FileSpec transformed_spec; |
| 685 | if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory())) |
| 686 | { |
| 687 | transformed_spec.GetFilename() = file_spec.GetFilename(); |
| 688 | error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | // If a module hasn't been found yet, use the unmodified path. |
| 693 | |
| 694 | if (!module_sp) |
| 695 | { |
| 696 | error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module)); |
| 697 | } |
| 698 | |
| 699 | if (module_sp) |
| 700 | { |
| 701 | m_images.Append (module_sp); |
| 702 | if (did_create_module) |
| 703 | { |
| 704 | if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32) |
| 705 | ModuleUpdated(old_module_sp, module_sp); |
| 706 | else |
| 707 | ModuleAdded(module_sp); |
| 708 | } |
| 709 | } |
| 710 | if (error_ptr) |
| 711 | *error_ptr = error; |
| 712 | return module_sp; |
| 713 | } |
| 714 | |
| 715 | |
| 716 | Target * |
| 717 | Target::CalculateTarget () |
| 718 | { |
| 719 | return this; |
| 720 | } |
| 721 | |
| 722 | Process * |
| 723 | Target::CalculateProcess () |
| 724 | { |
| 725 | return NULL; |
| 726 | } |
| 727 | |
| 728 | Thread * |
| 729 | Target::CalculateThread () |
| 730 | { |
| 731 | return NULL; |
| 732 | } |
| 733 | |
| 734 | StackFrame * |
| 735 | Target::CalculateStackFrame () |
| 736 | { |
| 737 | return NULL; |
| 738 | } |
| 739 | |
| 740 | void |
Greg Clayton | 0603aa9 | 2010-10-04 01:05:56 | [diff] [blame] | 741 | Target::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 742 | { |
| 743 | exe_ctx.target = this; |
| 744 | exe_ctx.process = NULL; // Do NOT fill in process... |
| 745 | exe_ctx.thread = NULL; |
| 746 | exe_ctx.frame = NULL; |
| 747 | } |
| 748 | |
| 749 | PathMappingList & |
| 750 | Target::GetImageSearchPathList () |
| 751 | { |
| 752 | return m_image_search_paths; |
| 753 | } |
| 754 | |
| 755 | void |
| 756 | Target::ImageSearchPathsChanged |
| 757 | ( |
| 758 | const PathMappingList &path_list, |
| 759 | void *baton |
| 760 | ) |
| 761 | { |
| 762 | Target *target = (Target *)baton; |
| 763 | if (target->m_images.GetSize() > 1) |
| 764 | { |
| 765 | ModuleSP exe_module_sp (target->GetExecutableModule()); |
| 766 | if (exe_module_sp) |
| 767 | { |
| 768 | target->m_images.Clear(); |
| 769 | target->SetExecutableModule (exe_module_sp, true); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | ClangASTContext * |
| 775 | Target::GetScratchClangASTContext() |
| 776 | { |
| 777 | return m_scratch_ast_context_ap.get(); |
| 778 | } |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 779 | |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 | [diff] [blame] | 780 | void |
| 781 | Target::Initialize () |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 782 | { |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 | [diff] [blame] | 783 | UserSettingsControllerSP &usc = GetSettingsController(); |
| 784 | usc.reset (new SettingsController); |
| 785 | UserSettingsController::InitializeSettingsController (usc, |
| 786 | SettingsController::global_settings_table, |
| 787 | SettingsController::instance_settings_table); |
| 788 | } |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 789 | |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 | [diff] [blame] | 790 | void |
| 791 | Target::Terminate () |
| 792 | { |
| 793 | UserSettingsControllerSP &usc = GetSettingsController(); |
| 794 | UserSettingsController::FinalizeSettingsController (usc); |
| 795 | usc.reset(); |
| 796 | } |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 797 | |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 | [diff] [blame] | 798 | UserSettingsControllerSP & |
| 799 | Target::GetSettingsController () |
| 800 | { |
| 801 | static UserSettingsControllerSP g_settings_controller; |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 802 | return g_settings_controller; |
| 803 | } |
| 804 | |
| 805 | ArchSpec |
| 806 | Target::GetDefaultArchitecture () |
| 807 | { |
Greg Clayton | dbe5450 | 2010-11-19 03:46:01 | [diff] [blame] | 808 | lldb::UserSettingsControllerSP &settings_controller = GetSettingsController(); |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 809 | lldb::SettableVariableType var_type; |
| 810 | Error err; |
| 811 | StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err); |
| 812 | |
| 813 | const char *default_name = ""; |
| 814 | if (result.GetSize() == 1 && err.Success()) |
| 815 | default_name = result.GetStringAtIndex (0); |
| 816 | |
| 817 | ArchSpec default_arch (default_name); |
| 818 | return default_arch; |
| 819 | } |
| 820 | |
| 821 | void |
| 822 | Target::SetDefaultArchitecture (ArchSpec new_arch) |
| 823 | { |
| 824 | if (new_arch.IsValid()) |
Greg Clayton | dbe5450 | 2010-11-19 03:46:01 | [diff] [blame] | 825 | GetSettingsController ()->SetVariable ("target.default-arch", |
| 826 | new_arch.AsCString(), |
| 827 | lldb::eVarSetOperationAssign, |
| 828 | false, |
| 829 | "[]"); |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 830 | } |
| 831 | |
Greg Clayton | 0603aa9 | 2010-10-04 01:05:56 | [diff] [blame] | 832 | Target * |
| 833 | Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr) |
| 834 | { |
| 835 | // The target can either exist in the "process" of ExecutionContext, or in |
| 836 | // the "target_sp" member of SymbolContext. This accessor helper function |
| 837 | // will get the target from one of these locations. |
| 838 | |
| 839 | Target *target = NULL; |
| 840 | if (sc_ptr != NULL) |
| 841 | target = sc_ptr->target_sp.get(); |
| 842 | if (target == NULL) |
| 843 | { |
| 844 | if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL) |
| 845 | target = &exe_ctx_ptr->process->GetTarget(); |
| 846 | } |
| 847 | return target; |
| 848 | } |
| 849 | |
| 850 | |
Caroline Tice | 1559a46 | 2010-09-27 00:30:10 | [diff] [blame] | 851 | void |
| 852 | Target::UpdateInstanceName () |
| 853 | { |
| 854 | StreamString sstr; |
| 855 | |
| 856 | ModuleSP module_sp = GetExecutableModule(); |
| 857 | if (module_sp) |
| 858 | { |
Greg Clayton | 307de25 | 2010-10-27 02:06:37 | [diff] [blame] | 859 | sstr.Printf ("%s_%s", |
| 860 | module_sp->GetFileSpec().GetFilename().AsCString(), |
Caroline Tice | 1559a46 | 2010-09-27 00:30:10 | [diff] [blame] | 861 | module_sp->GetArchitecture().AsCString()); |
Greg Clayton | dbe5450 | 2010-11-19 03:46:01 | [diff] [blame] | 862 | GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), |
| 863 | sstr.GetData()); |
Caroline Tice | 1559a46 | 2010-09-27 00:30:10 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 | [diff] [blame] | 867 | const char * |
| 868 | Target::GetExpressionPrefixContentsAsCString () |
| 869 | { |
| 870 | return m_expr_prefix_contents.c_str(); |
| 871 | } |
| 872 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 | [diff] [blame] | 873 | ExecutionResults |
| 874 | Target::EvaluateExpression |
| 875 | ( |
| 876 | const char *expr_cstr, |
| 877 | StackFrame *frame, |
| 878 | bool unwind_on_error, |
| 879 | lldb::ValueObjectSP &result_valobj_sp |
| 880 | ) |
| 881 | { |
| 882 | ExecutionResults execution_results = eExecutionSetupError; |
| 883 | |
| 884 | result_valobj_sp.reset(); |
| 885 | |
| 886 | ExecutionContext exe_ctx; |
| 887 | if (frame) |
| 888 | { |
| 889 | frame->CalculateExecutionContext(exe_ctx); |
Greg Clayton | 54979cd | 2010-12-15 05:08:08 | [diff] [blame] | 890 | Error error; |
| 891 | const bool check_ptr_vs_member = true; |
| 892 | result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, check_ptr_vs_member, error); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 | [diff] [blame] | 893 | } |
| 894 | else if (m_process_sp) |
| 895 | { |
| 896 | m_process_sp->CalculateExecutionContext(exe_ctx); |
| 897 | } |
| 898 | else |
| 899 | { |
| 900 | CalculateExecutionContext(exe_ctx); |
| 901 | } |
| 902 | |
| 903 | if (result_valobj_sp) |
| 904 | { |
| 905 | execution_results = eExecutionCompleted; |
| 906 | // We got a result from the frame variable expression path above... |
| 907 | ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName()); |
| 908 | |
| 909 | lldb::ValueObjectSP const_valobj_sp; |
| 910 | |
| 911 | // Check in case our value is already a constant value |
| 912 | if (result_valobj_sp->GetIsConstant()) |
| 913 | { |
| 914 | const_valobj_sp = result_valobj_sp; |
| 915 | const_valobj_sp->SetName (persistent_variable_name); |
| 916 | } |
| 917 | else |
| 918 | const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(), |
| 919 | persistent_variable_name); |
| 920 | |
| 921 | result_valobj_sp = const_valobj_sp; |
| 922 | |
| 923 | ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp)); |
| 924 | assert (clang_expr_variable_sp.get()); |
| 925 | } |
| 926 | else |
| 927 | { |
| 928 | // Make sure we aren't just trying to see the value of a persistent |
| 929 | // variable (something like "$0") |
| 930 | lldb::ClangExpressionVariableSP persistent_var_sp (m_persistent_variables.GetVariable (expr_cstr)); |
| 931 | if (persistent_var_sp) |
| 932 | { |
| 933 | result_valobj_sp = persistent_var_sp->GetValueObject (); |
| 934 | execution_results = eExecutionCompleted; |
| 935 | } |
| 936 | else |
| 937 | { |
| 938 | const char *prefix = GetExpressionPrefixContentsAsCString(); |
| 939 | |
| 940 | execution_results = ClangUserExpression::Evaluate (exe_ctx, |
| 941 | unwind_on_error, |
| 942 | expr_cstr, |
| 943 | prefix, |
| 944 | result_valobj_sp); |
| 945 | } |
| 946 | } |
| 947 | return execution_results; |
| 948 | } |
| 949 | |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 950 | //-------------------------------------------------------------- |
| 951 | // class Target::SettingsController |
| 952 | //-------------------------------------------------------------- |
| 953 | |
| 954 | Target::SettingsController::SettingsController () : |
| 955 | UserSettingsController ("target", Debugger::GetSettingsController()), |
| 956 | m_default_architecture () |
| 957 | { |
| 958 | m_default_settings.reset (new TargetInstanceSettings (*this, false, |
| 959 | InstanceSettings::GetDefaultName().AsCString())); |
| 960 | } |
| 961 | |
| 962 | Target::SettingsController::~SettingsController () |
| 963 | { |
| 964 | } |
| 965 | |
| 966 | lldb::InstanceSettingsSP |
| 967 | Target::SettingsController::CreateInstanceSettings (const char *instance_name) |
| 968 | { |
Greg Clayton | dbe5450 | 2010-11-19 03:46:01 | [diff] [blame] | 969 | TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(), |
| 970 | false, |
| 971 | instance_name); |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 972 | lldb::InstanceSettingsSP new_settings_sp (new_settings); |
| 973 | return new_settings_sp; |
| 974 | } |
| 975 | |
| 976 | const ConstString & |
| 977 | Target::SettingsController::DefArchVarName () |
| 978 | { |
| 979 | static ConstString def_arch_var_name ("default-arch"); |
| 980 | |
| 981 | return def_arch_var_name; |
| 982 | } |
| 983 | |
| 984 | bool |
| 985 | Target::SettingsController::SetGlobalVariable (const ConstString &var_name, |
| 986 | const char *index_value, |
| 987 | const char *value, |
| 988 | const SettingEntry &entry, |
| 989 | const lldb::VarSetOperationType op, |
| 990 | Error&err) |
| 991 | { |
| 992 | if (var_name == DefArchVarName()) |
| 993 | { |
| 994 | ArchSpec tmp_spec (value); |
| 995 | if (tmp_spec.IsValid()) |
| 996 | m_default_architecture = tmp_spec; |
| 997 | else |
| 998 | err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value); |
| 999 | } |
| 1000 | return true; |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | bool |
| 1005 | Target::SettingsController::GetGlobalVariable (const ConstString &var_name, |
| 1006 | StringList &value, |
| 1007 | Error &err) |
| 1008 | { |
| 1009 | if (var_name == DefArchVarName()) |
| 1010 | { |
Greg Clayton | 307de25 | 2010-10-27 02:06:37 | [diff] [blame] | 1011 | // If the arch is invalid (the default), don't show a string for it |
| 1012 | if (m_default_architecture.IsValid()) |
| 1013 | value.AppendString (m_default_architecture.AsCString()); |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1014 | return true; |
| 1015 | } |
| 1016 | else |
| 1017 | err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); |
| 1018 | |
| 1019 | return false; |
| 1020 | } |
| 1021 | |
| 1022 | //-------------------------------------------------------------- |
| 1023 | // class TargetInstanceSettings |
| 1024 | //-------------------------------------------------------------- |
| 1025 | |
Greg Clayton | 85851dd | 2010-12-04 00:10:17 | [diff] [blame] | 1026 | TargetInstanceSettings::TargetInstanceSettings |
| 1027 | ( |
| 1028 | UserSettingsController &owner, |
| 1029 | bool live_instance, |
| 1030 | const char *name |
| 1031 | ) : |
| 1032 | InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance) |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1033 | { |
| 1034 | // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called |
| 1035 | // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers. |
| 1036 | // For this reason it has to be called here, rather than in the initializer or in the parent constructor. |
| 1037 | // This is true for CreateInstanceName() too. |
| 1038 | |
| 1039 | if (GetInstanceName () == InstanceSettings::InvalidName()) |
| 1040 | { |
| 1041 | ChangeInstanceName (std::string (CreateInstanceName().AsCString())); |
| 1042 | m_owner.RegisterInstanceSettings (this); |
| 1043 | } |
| 1044 | |
| 1045 | if (live_instance) |
| 1046 | { |
| 1047 | const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); |
| 1048 | CopyInstanceSettings (pending_settings,false); |
| 1049 | //m_owner.RemovePendingSettings (m_instance_name); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) : |
Greg Clayton | dbe5450 | 2010-11-19 03:46:01 | [diff] [blame] | 1054 | InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()) |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1055 | { |
| 1056 | if (m_instance_name != InstanceSettings::GetDefaultName()) |
| 1057 | { |
| 1058 | const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); |
| 1059 | CopyInstanceSettings (pending_settings,false); |
| 1060 | //m_owner.RemovePendingSettings (m_instance_name); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | TargetInstanceSettings::~TargetInstanceSettings () |
| 1065 | { |
| 1066 | } |
| 1067 | |
| 1068 | TargetInstanceSettings& |
| 1069 | TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs) |
| 1070 | { |
| 1071 | if (this != &rhs) |
| 1072 | { |
| 1073 | } |
| 1074 | |
| 1075 | return *this; |
| 1076 | } |
| 1077 | |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 | [diff] [blame] | 1078 | #define EXPR_PREFIX_STRING "expr-prefix" |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1079 | |
| 1080 | void |
| 1081 | TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, |
| 1082 | const char *index_value, |
| 1083 | const char *value, |
| 1084 | const ConstString &instance_name, |
| 1085 | const SettingEntry &entry, |
| 1086 | lldb::VarSetOperationType op, |
| 1087 | Error &err, |
| 1088 | bool pending) |
| 1089 | { |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 | [diff] [blame] | 1090 | static ConstString expr_prefix_str (EXPR_PREFIX_STRING); |
| 1091 | |
| 1092 | if (var_name == expr_prefix_str) |
| 1093 | { |
| 1094 | switch (op) |
| 1095 | { |
| 1096 | default: |
| 1097 | err.SetErrorToGenericError (); |
| 1098 | err.SetErrorString ("Unrecognized operation. Cannot update value.\n"); |
| 1099 | return; |
| 1100 | case lldb::eVarSetOperationAssign: |
| 1101 | { |
| 1102 | FileSpec file_spec(value, true); |
| 1103 | |
| 1104 | if (!file_spec.Exists()) |
| 1105 | { |
| 1106 | err.SetErrorToGenericError (); |
| 1107 | err.SetErrorStringWithFormat ("%s does not exist.\n", value); |
| 1108 | return; |
| 1109 | } |
| 1110 | |
| 1111 | DataBufferMemoryMap buf; |
| 1112 | |
| 1113 | if (!buf.MemoryMapFromFileSpec(&file_spec) && |
| 1114 | buf.GetError().Fail()) |
| 1115 | { |
| 1116 | err.SetErrorToGenericError (); |
| 1117 | err.SetErrorStringWithFormat ("Couldn't read from %s: %s\n", value, buf.GetError().AsCString()); |
| 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | m_expr_prefix_path = value; |
| 1122 | m_expr_prefix_contents.assign(reinterpret_cast<const char *>(buf.GetBytes()), buf.GetByteSize()); |
| 1123 | } |
| 1124 | return; |
| 1125 | case lldb::eVarSetOperationAppend: |
| 1126 | err.SetErrorToGenericError (); |
| 1127 | err.SetErrorString ("Cannot append to a path.\n"); |
| 1128 | return; |
| 1129 | case lldb::eVarSetOperationClear: |
| 1130 | m_expr_prefix_path.clear (); |
| 1131 | m_expr_prefix_contents.clear (); |
| 1132 | return; |
| 1133 | } |
| 1134 | } |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | void |
| 1138 | TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 | [diff] [blame] | 1139 | bool pending) |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1140 | { |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 | [diff] [blame] | 1141 | TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get()); |
| 1142 | |
| 1143 | if (!new_settings_ptr) |
| 1144 | return; |
| 1145 | |
| 1146 | m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path; |
| 1147 | m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents; |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1148 | } |
| 1149 | |
Caroline Tice | 12cecd7 | 2010-09-20 21:37:42 | [diff] [blame] | 1150 | bool |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1151 | TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, |
| 1152 | const ConstString &var_name, |
| 1153 | StringList &value, |
Caroline Tice | 12cecd7 | 2010-09-20 21:37:42 | [diff] [blame] | 1154 | Error *err) |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1155 | { |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 | [diff] [blame] | 1156 | static ConstString expr_prefix_str (EXPR_PREFIX_STRING); |
| 1157 | |
| 1158 | if (var_name == expr_prefix_str) |
| 1159 | { |
| 1160 | value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size()); |
| 1161 | } |
| 1162 | else |
| 1163 | { |
| 1164 | if (err) |
| 1165 | err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); |
| 1166 | return false; |
| 1167 | } |
| 1168 | |
| 1169 | return true; |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | const ConstString |
| 1173 | TargetInstanceSettings::CreateInstanceName () |
| 1174 | { |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1175 | StreamString sstr; |
Caroline Tice | 1559a46 | 2010-09-27 00:30:10 | [diff] [blame] | 1176 | static int instance_count = 1; |
| 1177 | |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1178 | sstr.Printf ("target_%d", instance_count); |
| 1179 | ++instance_count; |
| 1180 | |
| 1181 | const ConstString ret_val (sstr.GetData()); |
| 1182 | return ret_val; |
| 1183 | } |
| 1184 | |
| 1185 | //-------------------------------------------------- |
| 1186 | // Target::SettingsController Variable Tables |
| 1187 | //-------------------------------------------------- |
| 1188 | |
| 1189 | SettingEntry |
| 1190 | Target::SettingsController::global_settings_table[] = |
| 1191 | { |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 | [diff] [blame] | 1192 | //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, |
| 1193 | { "default-arch", eSetVarTypeString, NULL, NULL, false, false, "Default architecture to choose, when there's a choice." }, |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1194 | { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } |
| 1195 | }; |
| 1196 | |
| 1197 | SettingEntry |
| 1198 | Target::SettingsController::instance_settings_table[] = |
| 1199 | { |
Sean Callanan | 322f529 | 2010-10-29 00:29:03 | [diff] [blame] | 1200 | //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, |
| 1201 | { EXPR_PREFIX_STRING, eSetVarTypeString, NULL, NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." }, |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 | [diff] [blame] | 1202 | { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } |
| 1203 | }; |