diff options
Diffstat (limited to 'lldb/source/API')
33 files changed, 6640 insertions, 0 deletions
diff --git a/lldb/source/API/SBAddress.cpp b/lldb/source/API/SBAddress.cpp new file mode 100644 index 00000000000..482ecec1ce6 --- /dev/null +++ b/lldb/source/API/SBAddress.cpp @@ -0,0 +1,118 @@ +//===-- SBAddress.cpp -------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBAddress.h" +#include "lldb/API/SBProcess.h" +#include "lldb/Core/Address.h" + +using namespace lldb; + + +SBAddress::SBAddress () : + m_lldb_object_ap () +{ +} + +SBAddress::SBAddress (const lldb_private::Address *lldb_object_ptr) : + m_lldb_object_ap () +{ + if (lldb_object_ptr) + m_lldb_object_ap.reset (new lldb_private::Address(*lldb_object_ptr)); +} + +SBAddress::SBAddress (const SBAddress &rhs) : + m_lldb_object_ap () +{ + if (rhs.IsValid()) + m_lldb_object_ap.reset (new lldb_private::Address(*rhs.m_lldb_object_ap.get())); +} + +SBAddress::~SBAddress () +{ +} + +const SBAddress & +SBAddress::operator = (const SBAddress &rhs) +{ + if (this != &rhs) + { + if (rhs.IsValid()) + m_lldb_object_ap.reset (new lldb_private::Address(*rhs.m_lldb_object_ap.get())); + } + return *this; +} + +bool +SBAddress::IsValid () const +{ + return m_lldb_object_ap.get() != NULL && m_lldb_object_ap->IsValid(); +} + +void +SBAddress::SetAddress (const lldb_private::Address *lldb_object_ptr) +{ + if (lldb_object_ptr) + { + if (m_lldb_object_ap.get()) + *m_lldb_object_ap = *lldb_object_ptr; + else + m_lldb_object_ap.reset (new lldb_private::Address(*lldb_object_ptr)); + return; + } + if (m_lldb_object_ap.get()) + m_lldb_object_ap->Clear(); +} + +lldb::addr_t +SBAddress::GetFileAddress () const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->GetFileAddress(); + else + return LLDB_INVALID_ADDRESS; +} + +lldb::addr_t +SBAddress::GetLoadAddress (const SBProcess &process) const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->GetLoadAddress(process.get()); + else + return LLDB_INVALID_ADDRESS; +} + +bool +SBAddress::OffsetAddress (addr_t offset) +{ + if (m_lldb_object_ap.get()) + { + addr_t addr_offset = m_lldb_object_ap->GetOffset(); + if (addr_offset != LLDB_INVALID_ADDRESS) + { + m_lldb_object_ap->SetOffset(addr_offset + offset); + return true; + } + } + return false; +} + + +const lldb_private::Address * +SBAddress::operator->() const +{ + return m_lldb_object_ap.get(); +} + +const lldb_private::Address & +SBAddress::operator*() const +{ + return *m_lldb_object_ap; +} + + diff --git a/lldb/source/API/SBBlock.cpp b/lldb/source/API/SBBlock.cpp new file mode 100644 index 00000000000..536febd99b7 --- /dev/null +++ b/lldb/source/API/SBBlock.cpp @@ -0,0 +1,47 @@ +//===-- SBBlock.cpp ---------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBBlock.h" +#include "lldb/Symbol/Block.h" + +using namespace lldb; + + +SBBlock::SBBlock () : + m_lldb_object_ptr (NULL) +{ +} + +SBBlock::SBBlock (lldb_private::Block *lldb_object_ptr) : + m_lldb_object_ptr (lldb_object_ptr) +{ +} + +SBBlock::~SBBlock () +{ + m_lldb_object_ptr = NULL; +} + +bool +SBBlock::IsValid () const +{ + return m_lldb_object_ptr != NULL; +} + +void +SBBlock::AppendVariables (bool can_create, bool get_parent_variables, lldb_private::VariableList *var_list) +{ + if (IsValid()) + { + m_lldb_object_ptr->AppendVariables (can_create, get_parent_variables, var_list); + } +} + + + diff --git a/lldb/source/API/SBBreakpoint.cpp b/lldb/source/API/SBBreakpoint.cpp new file mode 100644 index 00000000000..2cf4e0b824f --- /dev/null +++ b/lldb/source/API/SBBreakpoint.cpp @@ -0,0 +1,404 @@ +//===-- SBBreakpoint.cpp ----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBBreakpoint.h" +#include "lldb/API/SBBreakpointLocation.h" +#include "lldb/API/SBDebugger.h" +#include "lldb/API/SBProcess.h" +#include "lldb/API/SBThread.h" + +#include "lldb/Breakpoint/Breakpoint.h" +#include "lldb/Breakpoint/BreakpointLocation.h" +#include "lldb/Breakpoint/StoppointCallbackContext.h" +#include "lldb/Core/Address.h" +#include "lldb/Core/Stream.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/Thread.h" +#include "lldb/Target/Target.h" + + +#include "lldb/lldb-enumerations.h" + +using namespace lldb; +using namespace lldb_private; + +struct CallbackData +{ + SBBreakpoint::BreakpointHitCallback callback; + void *callback_baton; +}; + +class SBBreakpointCallbackBaton : public Baton +{ +public: + + SBBreakpointCallbackBaton (SBBreakpoint::BreakpointHitCallback callback, void *baton) : + Baton (new CallbackData) + { + CallbackData *data = (CallbackData *)m_data; + data->callback = callback; + data->callback_baton = baton; + } + + virtual ~SBBreakpointCallbackBaton() + { + CallbackData *data = (CallbackData *)m_data; + + if (data) + { + delete data; + m_data = NULL; + } + } +}; + + +SBBreakpoint::SBBreakpoint () : + m_break_sp () +{ +} + +SBBreakpoint::SBBreakpoint (const SBBreakpoint& rhs) : + m_break_sp (rhs.m_break_sp) +{ +} + + +SBBreakpoint::SBBreakpoint (const lldb::BreakpointSP &bp_sp) : + m_break_sp (bp_sp) +{ +} + +SBBreakpoint::~SBBreakpoint() +{ +} + +const SBBreakpoint & +SBBreakpoint::operator = (const SBBreakpoint& rhs) +{ + if (this != &rhs) + { + m_break_sp = rhs.m_break_sp; + } + return *this; +} + +break_id_t +SBBreakpoint::GetID () const +{ + if (m_break_sp) + return m_break_sp->GetID(); + return LLDB_INVALID_BREAK_ID; +} + + +bool +SBBreakpoint::IsValid() const +{ + return m_break_sp; +} + +void +SBBreakpoint::Dump (FILE *f) +{ + if (m_break_sp) + { + if (f == NULL) + f = SBDebugger::GetOutputFileHandle(); + if (f == NULL) + return; + lldb_private::StreamFile str (f); + m_break_sp->Dump (&str); + } +} + +void +SBBreakpoint::ClearAllBreakpointSites () +{ + if (m_break_sp) + m_break_sp->ClearAllBreakpointSites (); +} + +SBBreakpointLocation +SBBreakpoint::FindLocationByAddress (addr_t vm_addr) +{ + SBBreakpointLocation sb_bp_location; + + if (m_break_sp) + { + if (vm_addr != LLDB_INVALID_ADDRESS) + { + Address address; + Process *sb_process = m_break_sp->GetTarget().GetProcessSP().get(); + if (sb_process == NULL || sb_process->ResolveLoadAddress (vm_addr, address) == false) + { + address.SetSection (NULL); + address.SetOffset (vm_addr); + } + sb_bp_location.SetLocation (m_break_sp->FindLocationByAddress (address)); + } + } + return sb_bp_location; +} + +break_id_t +SBBreakpoint::FindLocationIDByAddress (addr_t vm_addr) +{ + break_id_t lldb_id = (break_id_t) 0; + + if (m_break_sp) + { + if (vm_addr != LLDB_INVALID_ADDRESS) + { + Address address; + Process *sb_process = m_break_sp->GetTarget().GetProcessSP().get(); + if (sb_process == NULL || sb_process->ResolveLoadAddress (vm_addr, address) == false) + { + address.SetSection (NULL); + address.SetOffset (vm_addr); + } + lldb_id = m_break_sp->FindLocationIDByAddress (address); + } + } + + return lldb_id; +} + +SBBreakpointLocation +SBBreakpoint::FindLocationByID (break_id_t bp_loc_id) +{ + SBBreakpointLocation sb_bp_location; + + if (m_break_sp) + sb_bp_location.SetLocation (m_break_sp->FindLocationByID (bp_loc_id)); + + return sb_bp_location; +} + +SBBreakpointLocation +SBBreakpoint::GetLocationAtIndex (uint32_t index) +{ + SBBreakpointLocation sb_bp_location; + + if (m_break_sp) + sb_bp_location.SetLocation (m_break_sp->GetLocationAtIndex (index)); + + return sb_bp_location; +} + +void +SBBreakpoint::ListLocations (FILE* f, const char *description_level) +{ + if (f == NULL) + f = SBDebugger::GetOutputFileHandle(); + + if (f == NULL) + return; + + if (m_break_sp) + { + DescriptionLevel level; + if (strcmp (description_level, "brief") == 0) + level = eDescriptionLevelBrief; + else if (strcmp (description_level, "full") == 0) + level = eDescriptionLevelFull; + else if (strcmp (description_level, "verbose") == 0) + level = eDescriptionLevelVerbose; + else + level = eDescriptionLevelBrief; + + StreamFile str (f); + + str.IndentMore(); + int num_locs = m_break_sp->GetNumLocations(); + for (int i = 0; i < num_locs; ++i) + { + BreakpointLocation *loc = m_break_sp->GetLocationAtIndex (i).get(); + loc->GetDescription (&str, level); + str.EOL(); + } + } +} + +void +SBBreakpoint::SetEnabled (bool enable) +{ + if (m_break_sp) + m_break_sp->SetEnabled (enable); +} + +bool +SBBreakpoint::IsEnabled () +{ + if (m_break_sp) + return m_break_sp->IsEnabled(); + else + return false; +} + +void +SBBreakpoint::SetIgnoreCount (int32_t count) +{ + if (m_break_sp) + m_break_sp->SetIgnoreCount (count); +} + +int32_t +SBBreakpoint::GetIgnoreCount () const +{ + if (m_break_sp) + return m_break_sp->GetIgnoreCount(); + else + return 0; +} + +void +SBBreakpoint::SetThreadID (tid_t sb_thread_id) +{ + if (m_break_sp) + m_break_sp->SetThreadID (sb_thread_id); +} + +tid_t +SBBreakpoint::GetThreadID () +{ + tid_t lldb_thread_id = LLDB_INVALID_THREAD_ID; + if (m_break_sp) + lldb_thread_id = m_break_sp->GetThreadID(); + + return lldb_thread_id; +} + +size_t +SBBreakpoint::GetNumResolvedLocations() const +{ + if (m_break_sp) + return m_break_sp->GetNumResolvedLocations(); + else + return 0; +} + +size_t +SBBreakpoint::GetNumLocations() const +{ + if (m_break_sp) + return m_break_sp->GetNumLocations(); + else + return 0; +} + +void +SBBreakpoint::GetDescription (FILE *f, const char *description_level, bool describe_locations) +{ + if (f == NULL) + return; + + if (m_break_sp) + { + DescriptionLevel level; + if (strcmp (description_level, "brief") == 0) + level = eDescriptionLevelBrief; + else if (strcmp (description_level, "full") == 0) + level = eDescriptionLevelFull; + else if (strcmp (description_level, "verbose") == 0) + level = eDescriptionLevelVerbose; + else + level = eDescriptionLevelBrief; + + StreamFile str (f); + + m_break_sp->GetDescription (&str, level); + str.EOL(); + if (describe_locations) + { + //str.IndentMore(); + // int num_locs = m_break_sp->GetNumLocations(); + // for (int i = 0; i < num_locs; ++i) + // { + // BreakpointLocation *loc = m_break_sp->FindLocationByIndex (i); + // loc->GetDescription (&str, level); + // str.EOL(); + // } + ListLocations (f, description_level); + } + } +} + +bool +SBBreakpoint::PrivateBreakpointHitCallback +( + void *baton, + StoppointCallbackContext *ctx, + lldb::user_id_t break_id, + lldb::user_id_t break_loc_id +) +{ + BreakpointSP bp_sp(ctx->context.target->GetBreakpointList().FindBreakpointByID(break_id)); + if (baton && bp_sp) + { + CallbackData *data = (CallbackData *)baton; + lldb_private::Breakpoint *bp = bp_sp.get(); + if (bp && data->callback) + { + if (ctx->context.process) + { + SBProcess sb_process (ctx->context.process->GetSP()); + SBThread sb_thread; + SBBreakpointLocation sb_location; + assert (bp_sp); + sb_location.SetLocation (bp_sp->FindLocationByID (break_loc_id)); + if (ctx->context.thread) + sb_thread.SetThread(ctx->context.thread->GetSP()); + + return data->callback (data->callback_baton, + sb_process, + sb_thread, + sb_location); + } + } + } + return true; // Return true if we should stop at this breakpoint +} + +void +SBBreakpoint::SetCallback (BreakpointHitCallback callback, void *baton) +{ + if (m_break_sp.get()) + { + BatonSP baton_sp(new SBBreakpointCallbackBaton (callback, baton)); + m_break_sp->SetCallback (SBBreakpoint::PrivateBreakpointHitCallback, baton_sp, false); + } +} + + +lldb_private::Breakpoint * +SBBreakpoint::operator->() const +{ + return m_break_sp.get(); +} + +lldb_private::Breakpoint * +SBBreakpoint::get() const +{ + return m_break_sp.get(); +} + +lldb::BreakpointSP & +SBBreakpoint::operator *() +{ + return m_break_sp; +} + +const lldb::BreakpointSP & +SBBreakpoint::operator *() const +{ + return m_break_sp; +} + diff --git a/lldb/source/API/SBBreakpointLocation.cpp b/lldb/source/API/SBBreakpointLocation.cpp new file mode 100644 index 00000000000..8bb36bd7324 --- /dev/null +++ b/lldb/source/API/SBBreakpointLocation.cpp @@ -0,0 +1,162 @@ +//===-- SBBreakpointLocation.cpp --------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBBreakpointLocation.h" +#include "lldb/API/SBDefines.h" +#include "lldb/API/SBDebugger.h" + +#include "lldb/lldb-types.h" +#include "lldb/lldb-defines.h" +#include "lldb/Breakpoint/BreakpointLocation.h" +#include "lldb/Core/Stream.h" +#include "lldb/Core/StreamFile.h" + +using namespace lldb; +using namespace lldb_private; + + + +//class SBBreakpointLocation + +SBBreakpointLocation::SBBreakpointLocation () +{ +} + +SBBreakpointLocation::SBBreakpointLocation (const lldb::BreakpointLocationSP &break_loc_sp) : + m_break_loc_sp (break_loc_sp) +{ +} + +SBBreakpointLocation::~SBBreakpointLocation () +{ +} + +bool +SBBreakpointLocation::IsValid() const +{ + return m_break_loc_sp.get() != NULL; +} + +addr_t +SBBreakpointLocation::GetLoadAddress () +{ + addr_t ret_addr = LLDB_INVALID_ADDRESS; + + if (m_break_loc_sp) + { + ret_addr = m_break_loc_sp->GetLoadAddress(); + } + + return ret_addr; +} + +void +SBBreakpointLocation::SetEnabled (bool enabled) +{ + if (m_break_loc_sp) + { + m_break_loc_sp->SetEnabled (enabled); + } +} + +bool +SBBreakpointLocation::IsEnabled () +{ + if (m_break_loc_sp) + return m_break_loc_sp->IsEnabled(); + else + return false; +} + +int32_t +SBBreakpointLocation::GetIgnoreCount () +{ + if (m_break_loc_sp) + return m_break_loc_sp->GetIgnoreCount(); + else + return 0; +} + +void +SBBreakpointLocation::SetIgnoreCount (int32_t n) +{ + if (m_break_loc_sp) + m_break_loc_sp->SetIgnoreCount (n); +} + +void +SBBreakpointLocation::SetThreadID (tid_t thread_id) +{ + if (m_break_loc_sp) + m_break_loc_sp->SetThreadID (thread_id); +} + +tid_t +SBBreakpointLocation::GetThreadID () +{ + tid_t sb_thread_id = (lldb::tid_t) LLDB_INVALID_THREAD_ID; + if (m_break_loc_sp) + sb_thread_id = m_break_loc_sp->GetThreadID(); + + return sb_thread_id; +} + +bool +SBBreakpointLocation::IsResolved () +{ + if (m_break_loc_sp) + return m_break_loc_sp->IsResolved(); + else + return false; +} + +void +SBBreakpointLocation::SetLocation (const lldb::BreakpointLocationSP &break_loc_sp) +{ + if (m_break_loc_sp) + { + // Uninstall the callbacks? + } + m_break_loc_sp = break_loc_sp; +} + +void +SBBreakpointLocation::GetDescription (FILE *f, const char *description_level) +{ + if (f == NULL) + return; + + if (m_break_loc_sp) + { + DescriptionLevel level; + if (strcmp (description_level, "brief") == 0) + level = eDescriptionLevelBrief; + else if (strcmp (description_level, "full") == 0) + level = eDescriptionLevelFull; + else if (strcmp (description_level, "verbose") == 0) + level = eDescriptionLevelVerbose; + else + level = eDescriptionLevelBrief; + + StreamFile str (f); + + m_break_loc_sp->GetDescription (&str, level); + str.EOL(); + } +} + +SBBreakpoint +SBBreakpointLocation::GetBreakpoint () +{ + SBBreakpoint sb_bp; + if (m_break_loc_sp) + *sb_bp = m_break_loc_sp->GetBreakpoint ().GetSP(); + return sb_bp; +} + diff --git a/lldb/source/API/SBBroadcaster.cpp b/lldb/source/API/SBBroadcaster.cpp new file mode 100644 index 00000000000..b9debcfae22 --- /dev/null +++ b/lldb/source/API/SBBroadcaster.cpp @@ -0,0 +1,142 @@ +//===-- SBBroadcaster.cpp ---------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Core/Broadcaster.h" +#include "lldb/lldb-forward-rtti.h" + +#include "SBBroadcaster.h" +#include "SBListener.h" +#include "SBEvent.h" + +using namespace lldb; +using namespace lldb_private; + + +SBBroadcaster::SBBroadcaster () : + m_lldb_object (NULL), + m_lldb_object_owned (false) +{ +} + + +SBBroadcaster::SBBroadcaster (const char *name) : + m_lldb_object (new Broadcaster (name)), + m_lldb_object_owned (true) +{ +} + +SBBroadcaster::SBBroadcaster (lldb_private::Broadcaster *broadcaster, bool owns) : + m_lldb_object (broadcaster), + m_lldb_object_owned (owns) +{ +} + +SBBroadcaster::~SBBroadcaster() +{ + SetLLDBObjectPtr (NULL, false); +} + +void +SBBroadcaster::BroadcastEventByType (uint32_t event_type, bool unique) +{ + if (m_lldb_object == NULL) + return; + + if (unique) + m_lldb_object->BroadcastEventIfUnique (event_type); + else + m_lldb_object->BroadcastEvent (event_type); +} + +void +SBBroadcaster::BroadcastEvent (const SBEvent &event, bool unique) +{ + if (m_lldb_object == NULL) + return; + + EventSP event_sp = event.GetSharedPtr (); + if (unique) + m_lldb_object->BroadcastEventIfUnique (event_sp); + else + m_lldb_object->BroadcastEvent (event_sp); +} + +void +SBBroadcaster::AddInitialEventsToListener (const SBListener &listener, uint32_t requested_events) +{ + if (m_lldb_object) + m_lldb_object->AddInitialEventsToListener (listener.get(), requested_events); +} + +uint32_t +SBBroadcaster::AddListener (const SBListener &listener, uint32_t event_mask) +{ + if (m_lldb_object) + return m_lldb_object->AddListener (listener.get(), event_mask); + return 0; +} + +const char * +SBBroadcaster::GetName () +{ + if (m_lldb_object) + return m_lldb_object->GetBroadcasterName().AsCString(); + return NULL; +} + +bool +SBBroadcaster::EventTypeHasListeners (uint32_t event_type) +{ + if (m_lldb_object) + return m_lldb_object->EventTypeHasListeners (event_type); + return false; +} + +bool +SBBroadcaster::RemoveListener (const SBListener &listener, uint32_t event_mask) +{ + if (m_lldb_object) + return m_lldb_object->RemoveListener (listener.get(), event_mask); + return false; +} + +Broadcaster * +SBBroadcaster::GetLLDBObjectPtr () const +{ + return m_lldb_object; +} + +void +SBBroadcaster::SetLLDBObjectPtr (Broadcaster *broadcaster, bool owns) +{ + if (m_lldb_object && m_lldb_object_owned) + delete m_lldb_object; + m_lldb_object = broadcaster; + m_lldb_object_owned = owns; +} + + +bool +SBBroadcaster::IsValid () const +{ + return m_lldb_object != NULL; +} + +bool +SBBroadcaster::operator == (const SBBroadcaster &rhs) const +{ + return m_lldb_object == rhs.m_lldb_object; + +} + +bool +SBBroadcaster::operator != (const SBBroadcaster &rhs) const +{ + return m_lldb_object != rhs.m_lldb_object; +} diff --git a/lldb/source/API/SBCommandContext.cpp b/lldb/source/API/SBCommandContext.cpp new file mode 100644 index 00000000000..3a1b60aa314 --- /dev/null +++ b/lldb/source/API/SBCommandContext.cpp @@ -0,0 +1,34 @@ +//===-- SBCommandContext.cpp ------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Interpreter/CommandContext.h" +#include "lldb/Interpreter/CommandReturnObject.h" + +#include "SBCommandContext.h" + + +using namespace lldb; +using namespace lldb_private; + + +SBCommandContext::SBCommandContext (CommandContext *lldb_object) : + m_lldb_object (lldb_object) +{ +} + +SBCommandContext::~SBCommandContext () +{ +} + +bool +SBCommandContext::IsValid () const +{ + return m_lldb_object != NULL; +} + diff --git a/lldb/source/API/SBCommandInterpreter.cpp b/lldb/source/API/SBCommandInterpreter.cpp new file mode 100644 index 00000000000..1e40320ad60 --- /dev/null +++ b/lldb/source/API/SBCommandInterpreter.cpp @@ -0,0 +1,193 @@ +//===-- SBCommandInterpreter.cpp --------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/lldb-types.h" +#include "lldb/Core/Args.h" +#include "lldb/Core/SourceManager.h" +#include "lldb/Core/Listener.h" +#include "lldb/Interpreter/CommandInterpreter.h" +#include "lldb/Interpreter/CommandReturnObject.h" +#include "lldb/Target/Target.h" + +#include "SBBroadcaster.h" +#include "SBDebugger.h" +#include "SBCommandReturnObject.h" +#include "SBCommandContext.h" +#include "SBSourceManager.h" +#include "SBCommandInterpreter.h" +#include "SBProcess.h" +#include "SBTarget.h" +#include "SBListener.h" +#include "SBStringList.h" + +using namespace lldb; +using namespace lldb_private; + + +SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter &interpreter) : + m_interpreter (interpreter) +{ +} + +SBCommandInterpreter::~SBCommandInterpreter () +{ +} + +bool +SBCommandInterpreter::CommandExists (const char *cmd) +{ + return m_interpreter.CommandExists (cmd); +} + +bool +SBCommandInterpreter::AliasExists (const char *cmd) +{ + return m_interpreter.AliasExists (cmd); +} + +bool +SBCommandInterpreter::UserCommandExists (const char *cmd) +{ + return m_interpreter.UserCommandExists (cmd); +} + +lldb::ReturnStatus +SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history) +{ + result.Clear(); + m_interpreter.HandleCommand (command_line, add_to_history, result.GetLLDBObjectRef()); + return result.GetStatus(); +} + +int +SBCommandInterpreter::HandleCompletion (const char *current_line, + const char *cursor, + const char *last_char, + int match_start_point, + int max_return_elements, + SBStringList &matches) +{ + int num_completions; + lldb_private::StringList lldb_matches; + num_completions = m_interpreter.HandleCompletion (current_line, cursor, last_char, match_start_point, + max_return_elements, lldb_matches); + + SBStringList temp_list (&lldb_matches); + matches.AppendList (temp_list); + + return num_completions; +} + +const char ** +SBCommandInterpreter::GetEnvironmentVariables () +{ + const Args *env_vars = m_interpreter.GetEnvironmentVariables(); + if (env_vars) + return env_vars->GetConstArgumentVector (); + return NULL; +} + +bool +SBCommandInterpreter::HasCommands () +{ + return m_interpreter.HasCommands(); +} + +bool +SBCommandInterpreter::HasAliases () +{ + return m_interpreter.HasAliases(); +} + +bool +SBCommandInterpreter::HasUserCommands () +{ + return m_interpreter.HasUserCommands (); +} + +bool +SBCommandInterpreter::HasAliasOptions () +{ + return m_interpreter.HasAliasOptions (); +} + +bool +SBCommandInterpreter::HasInterpreterVariables () +{ + return m_interpreter.HasInterpreterVariables (); +} + +SBProcess +SBCommandInterpreter::GetProcess () +{ + SBProcess process; + CommandContext *context = m_interpreter.Context(); + if (context) + { + Target *target = context->GetTarget(); + if (target) + process.SetProcess(target->GetProcessSP()); + } + return process; +} + +ssize_t +SBCommandInterpreter::WriteToScriptInterpreter (const char *src) +{ + if (src) + return WriteToScriptInterpreter (src, strlen(src)); + return 0; +} + +ssize_t +SBCommandInterpreter::WriteToScriptInterpreter (const char *src, size_t src_len) +{ + if (src && src[0]) + { + ScriptInterpreter *script_interpreter = m_interpreter.GetScriptInterpreter(); + if (script_interpreter) + return ::write (script_interpreter->GetMasterFileDescriptor(), src, src_len); + } + return 0; +} + + +CommandInterpreter * +SBCommandInterpreter::GetLLDBObjectPtr () +{ + return &m_interpreter; +} + +CommandInterpreter & +SBCommandInterpreter::GetLLDBObjectRef () +{ + return m_interpreter; +} + +void +SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result) +{ + result.Clear(); + m_interpreter.SourceInitFile (false, result.GetLLDBObjectRef()); +} + +void +SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result) +{ + result.Clear(); + m_interpreter.SourceInitFile (true, result.GetLLDBObjectRef()); +} + +SBBroadcaster +SBCommandInterpreter::GetBroadcaster () +{ + SBBroadcaster broadcaster (&m_interpreter, false); + return broadcaster; +} + diff --git a/lldb/source/API/SBCommandReturnObject.cpp b/lldb/source/API/SBCommandReturnObject.cpp new file mode 100644 index 00000000000..3910cc4b03d --- /dev/null +++ b/lldb/source/API/SBCommandReturnObject.cpp @@ -0,0 +1,148 @@ +//===-- SBCommandReturnObject.cpp -------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Interpreter/CommandReturnObject.h" + +#include "SBCommandReturnObject.h" + +using namespace lldb; + +SBCommandReturnObject::SBCommandReturnObject () : + m_return_object_ap (new lldb_private::CommandReturnObject ()) +{ +} + +SBCommandReturnObject::~SBCommandReturnObject () +{ + // m_return_object_ap will automatically delete any pointer it owns +} + +bool +SBCommandReturnObject::IsValid() const +{ + return m_return_object_ap.get() != NULL; +} + + +const char * +SBCommandReturnObject::GetOutput () +{ + if (m_return_object_ap.get()) + return m_return_object_ap->GetOutputStream().GetData(); + return NULL; +} + +const char * +SBCommandReturnObject::GetError () +{ + if (m_return_object_ap.get()) + return m_return_object_ap->GetErrorStream().GetData(); + return NULL; +} + +size_t +SBCommandReturnObject::GetOutputSize () +{ + if (m_return_object_ap.get()) + return m_return_object_ap->GetOutputStream().GetSize(); + return 0; +} + +size_t +SBCommandReturnObject::GetErrorSize () +{ + if (m_return_object_ap.get()) + return m_return_object_ap->GetErrorStream().GetSize(); + return 0; +} + +size_t +SBCommandReturnObject::PutOutput (FILE *fh) +{ + if (fh) + { + size_t num_bytes = GetOutputSize (); + if (num_bytes) + return ::fprintf (fh, "%s", GetOutput()); + } + return 0; +} + +size_t +SBCommandReturnObject::PutError (FILE *fh) +{ + if (fh) + { + size_t num_bytes = GetErrorSize (); + if (num_bytes) + return ::fprintf (fh, "%s", GetError()); + } + return 0; +} + +void +SBCommandReturnObject::Clear() +{ + if (m_return_object_ap.get()) + m_return_object_ap->Clear(); +} + +lldb::ReturnStatus +SBCommandReturnObject::GetStatus() +{ + if (m_return_object_ap.get()) + return m_return_object_ap->GetStatus(); + return lldb::eReturnStatusInvalid; +} + +bool +SBCommandReturnObject::Succeeded () +{ + if (m_return_object_ap.get()) + return m_return_object_ap->Succeeded(); + return false; +} + +bool +SBCommandReturnObject::HasResult () +{ + if (m_return_object_ap.get()) + return m_return_object_ap->HasResult(); + return false; +} + +void +SBCommandReturnObject::AppendMessage (const char *message) +{ + if (m_return_object_ap.get()) + m_return_object_ap->AppendMessage (message); +} + +lldb_private::CommandReturnObject * +SBCommandReturnObject::GetLLDBObjectPtr() +{ + return m_return_object_ap.get(); +} + + +lldb_private::CommandReturnObject & +SBCommandReturnObject::GetLLDBObjectRef() +{ + assert(m_return_object_ap.get()); + return *(m_return_object_ap.get()); +} + + +void +SBCommandReturnObject::SetLLDBObjectPtr (lldb_private::CommandReturnObject *ptr) +{ + if (m_return_object_ap.get()) + m_return_object_ap.reset (ptr); +} + diff --git a/lldb/source/API/SBCommunication.cpp b/lldb/source/API/SBCommunication.cpp new file mode 100644 index 00000000000..e2a791750a8 --- /dev/null +++ b/lldb/source/API/SBCommunication.cpp @@ -0,0 +1,194 @@ +//===-- SBCommunication.cpp -------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBCommunication.h" +#include "lldb/API/SBBroadcaster.h" +#include "lldb/Core/Communication.h" +#include "lldb/Core/ConnectionFileDescriptor.h" + +using namespace lldb; +using namespace lldb_private; + + + +SBCommunication::SBCommunication() : + m_lldb_object (NULL), + m_lldb_object_owned (false) +{ +} + +SBCommunication::SBCommunication(const char * broadcaster_name) : + m_lldb_object (new Communication (broadcaster_name)), + m_lldb_object_owned (true) +{ +} + +SBCommunication::~SBCommunication() +{ + if (m_lldb_object && m_lldb_object_owned) + delete m_lldb_object; + m_lldb_object = NULL; + m_lldb_object_owned = false; +} + +ConnectionStatus +SBCommunication::CheckIfBytesAvailable () +{ + if (m_lldb_object) + return m_lldb_object->BytesAvailable (0, NULL); + return eConnectionStatusNoConnection; +} + +ConnectionStatus +SBCommunication::WaitForBytesAvailableInfinite () +{ + if (m_lldb_object) + return m_lldb_object->BytesAvailable (UINT32_MAX, NULL); + return eConnectionStatusNoConnection; +} + +ConnectionStatus +SBCommunication::WaitForBytesAvailableWithTimeout (uint32_t timeout_usec) +{ + if (m_lldb_object) + return m_lldb_object->BytesAvailable (timeout_usec, NULL); + return eConnectionStatusNoConnection; +} + +ConnectionStatus +SBCommunication::Connect (const char *url) +{ + if (m_lldb_object) + { + if (!m_lldb_object->HasConnection ()) + m_lldb_object->SetConnection (new ConnectionFileDescriptor()); + return m_lldb_object->Connect (url, NULL); + } + return eConnectionStatusNoConnection; +} + +ConnectionStatus +SBCommunication::AdoptFileDesriptor (int fd, bool owns_fd) +{ + if (m_lldb_object) + { + if (m_lldb_object->HasConnection ()) + { + if (m_lldb_object->IsConnected()) + m_lldb_object->Disconnect (); + } + m_lldb_object->SetConnection (new ConnectionFileDescriptor (fd, owns_fd)); + if (m_lldb_object->IsConnected()) + return eConnectionStatusSuccess; + else + return eConnectionStatusLostConnection; + } + return eConnectionStatusNoConnection; +} + + +ConnectionStatus +SBCommunication::Disconnect () +{ + if (m_lldb_object) + return m_lldb_object->Disconnect (); + return eConnectionStatusNoConnection; +} + +bool +SBCommunication::IsConnected () const +{ + if (m_lldb_object) + return m_lldb_object->IsConnected (); + return false; +} + +size_t +SBCommunication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status) +{ + if (m_lldb_object) + return m_lldb_object->Read (dst, dst_len, timeout_usec, status, NULL); + status = eConnectionStatusNoConnection; + return 0; +} + + +size_t +SBCommunication::Write (const void *src, size_t src_len, ConnectionStatus &status) +{ + if (m_lldb_object) + return m_lldb_object->Write (src, src_len, status, NULL); + status = eConnectionStatusNoConnection; + return 0; +} + +bool +SBCommunication::ReadThreadStart () +{ + if (m_lldb_object) + return m_lldb_object->StartReadThread (); + return false; +} + + +bool +SBCommunication::ReadThreadStop () +{ + if (m_lldb_object) + return m_lldb_object->StopReadThread (); + return false; +} + +bool +SBCommunication::ReadThreadIsRunning () +{ + if (m_lldb_object) + return m_lldb_object->ReadThreadIsRunning (); + return false; +} + +bool +SBCommunication::SetReadThreadBytesReceivedCallback +( + ReadThreadBytesReceived callback, + void *callback_baton +) +{ + if (m_lldb_object) + { + m_lldb_object->SetReadThreadBytesReceivedCallback (callback, callback_baton); + return true; + } + return false; +} + +SBBroadcaster +SBCommunication::GetBroadcaster () +{ + SBBroadcaster broadcaster (m_lldb_object, false); + return broadcaster; +} + + +// +//void +//SBCommunication::CreateIfNeeded () +//{ +// if (m_lldb_object == NULL) +// { +// static uint32_t g_broadcaster_num; +// char broadcaster_name[256]; +// ::snprintf (name, broadcaster_name, "%p SBCommunication", this); +// m_lldb_object = new Communication (broadcaster_name); +// m_lldb_object_owned = true; +// } +// assert (m_lldb_object); +//} +// +// diff --git a/lldb/source/API/SBCompileUnit.cpp b/lldb/source/API/SBCompileUnit.cpp new file mode 100644 index 00000000000..a12934a0587 --- /dev/null +++ b/lldb/source/API/SBCompileUnit.cpp @@ -0,0 +1,120 @@ +//===-- SBCompileUnit.cpp ---------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBCompileUnit.h" +#include "lldb/API/SBLineEntry.h" +#include "lldb/Symbol/CompileUnit.h" +#include "lldb/Symbol/LineEntry.h" +#include "lldb/Symbol/LineTable.h" + +using namespace lldb; +using namespace lldb_private; + + +SBCompileUnit::SBCompileUnit () : + m_lldb_object_ptr (NULL) +{ +} + +SBCompileUnit::SBCompileUnit (lldb_private::CompileUnit *lldb_object_ptr) : + m_lldb_object_ptr (lldb_object_ptr) +{ +} + +SBCompileUnit::~SBCompileUnit () +{ + m_lldb_object_ptr = NULL; +} + +SBFileSpec +SBCompileUnit::GetFileSpec () const +{ + SBFileSpec file_spec; + if (m_lldb_object_ptr) + file_spec.SetFileSpec(*m_lldb_object_ptr); + return file_spec; +} + +uint32_t +SBCompileUnit::GetNumLineEntries () const +{ + if (m_lldb_object_ptr) + { + LineTable *line_table = m_lldb_object_ptr->GetLineTable (); + if (line_table) + return line_table->GetSize(); + } + return 0; +} + +SBLineEntry +SBCompileUnit::GetLineEntryAtIndex (uint32_t idx) const +{ + SBLineEntry sb_line_entry; + if (m_lldb_object_ptr) + { + LineTable *line_table = m_lldb_object_ptr->GetLineTable (); + if (line_table) + { + LineEntry line_entry; + if (line_table->GetLineEntryAtIndex(idx, line_entry)) + sb_line_entry.SetLineEntry(line_entry); + } + } + return sb_line_entry; +} + +uint32_t +SBCompileUnit::FindLineEntryIndex (uint32_t start_idx, uint32_t line, SBFileSpec *inline_file_spec) const +{ + if (m_lldb_object_ptr) + { + FileSpec file_spec; + if (inline_file_spec && inline_file_spec->IsValid()) + file_spec = inline_file_spec->ref(); + else + file_spec = *m_lldb_object_ptr; + + return m_lldb_object_ptr->FindLineEntry (start_idx, + line, + inline_file_spec ? inline_file_spec->get() : NULL, + NULL); + } + return UINT32_MAX; +} + +bool +SBCompileUnit::IsValid () const +{ + return m_lldb_object_ptr != NULL; +} + +bool +SBCompileUnit::operator == (const SBCompileUnit &rhs) const +{ + return m_lldb_object_ptr == rhs.m_lldb_object_ptr; +} + +bool +SBCompileUnit::operator != (const SBCompileUnit &rhs) const +{ + return m_lldb_object_ptr != rhs.m_lldb_object_ptr; +} + +const lldb_private::CompileUnit * +SBCompileUnit::operator->() const +{ + return m_lldb_object_ptr; +} + +const lldb_private::CompileUnit & +SBCompileUnit::operator*() const +{ + return *m_lldb_object_ptr; +} diff --git a/lldb/source/API/SBDebugger.cpp b/lldb/source/API/SBDebugger.cpp new file mode 100644 index 00000000000..d31c93cddb8 --- /dev/null +++ b/lldb/source/API/SBDebugger.cpp @@ -0,0 +1,569 @@ +//===-- SBDebugger.cpp ------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "SBDebugger.h" + +#include "lldb/lldb-include.h" +#include "lldb/Core/Args.h" +#include "lldb/Core/Debugger.h" +#include "lldb/Core/State.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/TargetList.h" + +#include "SBListener.h" +#include "SBBroadcaster.h" +#include "SBCommandInterpreter.h" +#include "SBCommandReturnObject.h" +#include "SBEvent.h" +#include "SBFrame.h" +#include "SBTarget.h" +#include "SBProcess.h" +#include "SBThread.h" +#include "SBSourceManager.h" +#include "SBInputReader.h" + +using namespace lldb; +using namespace lldb_private; + +void +SBDebugger::Initialize () +{ + Debugger::Initialize(); +} + +void +SBDebugger::Terminate () +{ + Debugger::Terminate(); +} + +void +SBDebugger::SetAsync (bool b) +{ + static bool value_set_once = false; + + if (!value_set_once) + { + value_set_once = true; + Debugger::GetSharedInstance().SetAsyncExecution(b); + } +} + +void +SBDebugger::SetInputFile (const char *tty_name) +{ + // DEPRECATED: will be removed in next submission + FILE *fh = ::fopen (tty_name, "r"); + SetInputFileHandle (fh, true); +} + +void +SBDebugger::SetOutputFile (const char *tty_name) +{ + // DEPRECATED: will be removed in next submission + FILE *fh = ::fopen (tty_name, "w"); + SetOutputFileHandle (fh, true); + SetErrorFileHandle (fh, false); +} + +void +SBDebugger::SetErrorFile (const char *tty_name) +{ + // DEPRECATED: will be removed in next submission +} + + +// Shouldn't really be settable after initialization as this could cause lots of problems; don't want users +// trying to switch modes in the middle of a debugging session. +void +SBDebugger::SetInputFileHandle (FILE *fh, bool transfer_ownership) +{ + Debugger::GetSharedInstance().SetInputFileHandle (fh, transfer_ownership); +} + +void +SBDebugger::SetOutputFileHandle (FILE *fh, bool transfer_ownership) +{ + Debugger::GetSharedInstance().SetOutputFileHandle (fh, transfer_ownership); +} + +void +SBDebugger::SetErrorFileHandle (FILE *fh, bool transfer_ownership) +{ + Debugger::GetSharedInstance().SetErrorFileHandle (fh, transfer_ownership); +} + +FILE * +SBDebugger::GetInputFileHandle () +{ + return Debugger::GetSharedInstance().GetInputFileHandle(); +} + +FILE * +SBDebugger::GetOutputFileHandle () +{ + return Debugger::GetSharedInstance().GetOutputFileHandle(); +} + +FILE * +SBDebugger::GetErrorFileHandle () +{ + return Debugger::GetSharedInstance().GetErrorFileHandle(); +} + +SBCommandInterpreter +SBDebugger::GetCommandInterpreter () +{ + SBCommandInterpreter sb_interpreter(Debugger::GetSharedInstance().GetCommandInterpreter()); + return sb_interpreter; +} + +void +SBDebugger::HandleCommand (const char *command) +{ + SBProcess process; + SBCommandInterpreter sb_interpreter(Debugger::GetSharedInstance().GetCommandInterpreter()); + SBCommandReturnObject result; + + sb_interpreter.HandleCommand (command, result, false); + + if (GetErrorFileHandle() != NULL) + result.PutError (GetErrorFileHandle()); + if (GetOutputFileHandle() != NULL) + result.PutOutput (GetOutputFileHandle()); + + if (Debugger::GetSharedInstance().GetAsyncExecution() == false) + { + process = GetCommandInterpreter().GetProcess (); + if (process.IsValid()) + { + EventSP event_sp; + Listener &lldb_listener = Debugger::GetSharedInstance().GetListener(); + while (lldb_listener.GetNextEventForBroadcaster (process.get(), event_sp)) + { + SBEvent event(event_sp); + HandleProcessEvent (process, event, GetOutputFileHandle(), GetErrorFileHandle()); + } + } + } +} + +SBListener +SBDebugger::GetListener () +{ + SBListener sb_listener(Debugger::GetSharedInstance().GetListener()); + return sb_listener; +} + +void +SBDebugger::HandleProcessEvent (const SBProcess &process, const SBEvent &event, FILE *out, FILE *err) +{ + const uint32_t event_type = event.GetType(); + char stdio_buffer[1024]; + size_t len; + + if (event_type & Process::eBroadcastBitSTDOUT) + { + while ((len = process.GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0) + if (out != NULL) + ::fwrite (stdio_buffer, 1, len, out); + } + else if (event_type & Process::eBroadcastBitSTDERR) + { + while ((len = process.GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0) + if (out != NULL) + ::fwrite (stdio_buffer, 1, len, out); + } + else if (event_type & Process::eBroadcastBitStateChanged) + { + // Drain any stdout messages. + while ((len = process.GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0) + if (out != NULL) + ::fwrite (stdio_buffer, 1, len, out); + + // Drain any stderr messages. + while ((len = process.GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0) + if (out != NULL) + ::fwrite (stdio_buffer, 1, len, out); + + StateType event_state = SBProcess::GetStateFromEvent (event); + + if (event_state == eStateInvalid) + return; + + bool is_stopped = StateIsStoppedState (event_state); + if (!is_stopped) + process.ReportCurrentState (event, out); + } +} + +void +SBDebugger::UpdateCurrentThread (SBProcess &process) +{ + if (process.IsValid()) + { + SBThread curr_thread = process.GetCurrentThread (); + SBThread thread; + StopReason curr_thread_stop_reason = eStopReasonInvalid; + if (curr_thread.IsValid()) + { + if (curr_thread.GetStopReason() != eStopReasonInvalid) + curr_thread_stop_reason = curr_thread.GetStopReason (); + } + + if (! curr_thread.IsValid() + || curr_thread_stop_reason == eStopReasonInvalid + || curr_thread_stop_reason == eStopReasonNone) + { + // Prefer a thread that has just completed its plan over another thread as current thread. + SBThread plan_thread; + SBThread other_thread; + const size_t num_threads = process.GetNumThreads (); + size_t i; + for (i = 0; i < num_threads; ++i) + { + thread = process.GetThreadAtIndex(i); + if (thread.GetStopReason () != eStopReasonInvalid) + { + switch (thread.GetStopReason ()) + { + default: + case eStopReasonInvalid: + case eStopReasonNone: + break; + + case eStopReasonTrace: + case eStopReasonBreakpoint: + case eStopReasonWatchpoint: + case eStopReasonSignal: + case eStopReasonException: + if (! other_thread.IsValid()) + other_thread = thread; + break; + case eStopReasonPlanComplete: + if (! plan_thread.IsValid()) + plan_thread = thread; + break; + } + } + } + if (plan_thread.IsValid()) + process.SetCurrentThreadByID (plan_thread.GetThreadID()); + else if (other_thread.IsValid()) + process.SetCurrentThreadByID (other_thread.GetThreadID()); + else + { + if (curr_thread.IsValid()) + thread = curr_thread; + else + thread = process.GetThreadAtIndex(0); + + if (thread.IsValid()) + process.SetCurrentThreadByID (thread.GetThreadID()); + } + } + } +} + +void +SBDebugger::ReportCurrentLocation (FILE *out, FILE *err) +{ + if ((out == NULL) || (err == NULL)) + return; + + SBTarget sb_target (GetCurrentTarget()); + if (!sb_target.IsValid()) + { + fprintf (out, "no target\n"); + return; + } + + SBProcess process = sb_target.GetProcess (); + if (process.IsValid()) + { + StateType state = process.GetState(); + + if (StateIsStoppedState (state)) + { + if (state == eStateExited) + { + int exit_status = process.GetExitStatus(); + const char *exit_description = process.GetExitDescription(); + ::fprintf (out, "Process %d exited with status = %i (0x%8.8x) %s\n", + process.GetProcessID(), + exit_status, + exit_status, + exit_description ? exit_description : ""); + } + else + { + fprintf (out, "Process %d %s\n", process.GetProcessID(), StateAsCString (state)); + SBThread current_thread = process.GetThreadAtIndex (0); + if (current_thread.IsValid()) + { + process.DisplayThreadsInfo (out, err, true); + } + else + fprintf (out, "No valid thread found in current process\n"); + } + } + else + fprintf (out, "No current location or status available\n"); + } +} + +SBSourceManager & +SBDebugger::GetSourceManager () +{ + static SourceManager g_lldb_source_manager; + static SBSourceManager g_sb_source_manager (g_lldb_source_manager); + return g_sb_source_manager; +} + + +bool +SBDebugger::GetDefaultArchitecture (char *arch_name, size_t arch_name_len) +{ + if (arch_name && arch_name_len) + { + ArchSpec &default_arch = lldb_private::GetDefaultArchitecture (); + if (default_arch.IsValid()) + { + ::snprintf (arch_name, arch_name_len, "%s", default_arch.AsCString()); + return true; + } + } + if (arch_name && arch_name_len) + arch_name[0] = '\0'; + return false; +} + + +bool +SBDebugger::SetDefaultArchitecture (const char *arch_name) +{ + if (arch_name) + { + ArchSpec arch (arch_name); + if (arch.IsValid()) + { + lldb_private::GetDefaultArchitecture () = arch; + return true; + } + } + return false; +} + +ScriptLanguage +SBDebugger::GetScriptingLanguage (const char *script_language_name) +{ + return Args::StringToScriptLanguage (script_language_name, + eScriptLanguageDefault, + NULL); +} +//pid_t +/* +SBDebugger::AttachByName (const char *process_name, const char *filename) +{ + SBTarget *temp_target = GetCurrentTarget(); + SBTarget sb_target; + pid_t return_pid = (pid_t) LLDB_INVALID_PROCESS_ID; + + if (temp_target == NULL) + { + if (filename != NULL) + { + sb_target = CreateWithFile (filename); + sb_target.SetArch (LLDB_ARCH_DEFAULT); + } + } + else + { + sb_target = *temp_target; + } + + if (sb_target.IsValid()) + { + SBProcess process = sb_target.GetProcess (); + if (process.IsValid()) + { + return_pid = process.AttachByName (process_name); + } + } + return return_pid; +} +*/ + +const char * +SBDebugger::GetVersionString () +{ + return lldb_private::GetVersion(); +} + +const char * +SBDebugger::StateAsCString (lldb::StateType state) +{ + return lldb_private::StateAsCString (state); +} + +bool +SBDebugger::StateIsRunningState (lldb::StateType state) +{ + return lldb_private::StateIsRunningState (state); +} + +bool +SBDebugger::StateIsStoppedState (lldb::StateType state) +{ + return lldb_private::StateIsStoppedState (state); +} + + +SBTarget +SBDebugger::CreateTargetWithFileAndTargetTriple (const char *filename, + const char *target_triple) +{ + ArchSpec arch; + FileSpec file_spec (filename); + arch.SetArchFromTargetTriple(target_triple); + TargetSP target_sp; + Error error (Debugger::GetSharedInstance().GetTargetList().CreateTarget (file_spec, arch, NULL, true, target_sp)); + SBTarget target(target_sp); + return target; +} + +SBTarget +SBDebugger::CreateTargetWithFileAndArch (const char *filename, const char *archname) +{ + FileSpec file (filename); + ArchSpec arch = lldb_private::GetDefaultArchitecture(); + TargetSP target_sp; + Error error; + + if (archname != NULL) + { + ArchSpec arch2 (archname); + error = Debugger::GetSharedInstance().GetTargetList().CreateTarget (file, arch2, NULL, true, target_sp); + } + else + { + if (!arch.IsValid()) + arch = LLDB_ARCH_DEFAULT; + + error = Debugger::GetSharedInstance().GetTargetList().CreateTarget (file, arch, NULL, true, target_sp); + + if (error.Fail()) + { + if (arch == LLDB_ARCH_DEFAULT_32BIT) + arch = LLDB_ARCH_DEFAULT_64BIT; + else + arch = LLDB_ARCH_DEFAULT_32BIT; + + error = Debugger::GetSharedInstance().GetTargetList().CreateTarget (file, arch, NULL, true, target_sp); + } + } + + if (error.Success()) + Debugger::GetSharedInstance().GetTargetList().SetCurrentTarget (target_sp.get()); + else + target_sp.reset(); + + SBTarget sb_target (target_sp); + return sb_target; +} + +SBTarget +SBDebugger::CreateTarget (const char *filename) +{ + FileSpec file (filename); + ArchSpec arch = lldb_private::GetDefaultArchitecture(); + TargetSP target_sp; + Error error; + + if (!arch.IsValid()) + arch = LLDB_ARCH_DEFAULT; + + error = Debugger::GetSharedInstance().GetTargetList().CreateTarget (file, arch, NULL, true, target_sp); + + if (error.Fail()) + { + if (arch == LLDB_ARCH_DEFAULT_32BIT) + arch = LLDB_ARCH_DEFAULT_64BIT; + else + arch = LLDB_ARCH_DEFAULT_32BIT; + + error = Debugger::GetSharedInstance().GetTargetList().CreateTarget (file, arch, NULL, true, target_sp); + } + + if (!error.Fail()) + Debugger::GetSharedInstance().GetTargetList().SetCurrentTarget (target_sp.get()); + + SBTarget sb_target (target_sp); + return sb_target; +} + +SBTarget +SBDebugger::GetTargetAtIndex (uint32_t idx) +{ + SBTarget sb_target (Debugger::GetSharedInstance().GetTargetList().GetTargetAtIndex (idx)); + return sb_target; +} + +SBTarget +SBDebugger::FindTargetWithProcessID (pid_t pid) +{ + SBTarget sb_target(Debugger::GetSharedInstance().GetTargetList().FindTargetWithProcessID (pid)); + return sb_target; +} + +SBTarget +SBDebugger::FindTargetWithFileAndArch (const char *filename, const char *arch_name) +{ + ArchSpec arch; + if (arch_name) + arch.SetArch(arch_name); + return SBTarget (Debugger::GetSharedInstance().GetTargetList().FindTargetWithExecutableAndArchitecture (FileSpec(filename), + arch_name ? &arch : NULL)); +} + +SBTarget +SBDebugger::FindTargetWithLLDBProcess (const lldb::ProcessSP &process_sp) +{ + SBTarget sb_target(Debugger::GetSharedInstance().GetTargetList().FindTargetWithProcess (process_sp.get())); + return sb_target; +} + + +uint32_t +SBDebugger::GetNumTargets () +{ + return Debugger::GetSharedInstance().GetTargetList().GetNumTargets ();} + +SBTarget +SBDebugger::GetCurrentTarget () +{ + SBTarget sb_target(Debugger::GetSharedInstance().GetTargetList().GetCurrentTarget ()); + return sb_target; +} + +void +SBDebugger::DispatchInput (void *baton, const void *data, size_t data_len) +{ + Debugger::GetSharedInstance().DispatchInput ((const char *) data, data_len); +} + +void +SBDebugger::PushInputReader (SBInputReader &reader) +{ + if (reader.IsValid()) + { + InputReaderSP reader_sp(*reader); + Debugger::GetSharedInstance().PushInputReader (reader_sp); + } +} diff --git a/lldb/source/API/SBError.cpp b/lldb/source/API/SBError.cpp new file mode 100644 index 00000000000..7c257c94b37 --- /dev/null +++ b/lldb/source/API/SBError.cpp @@ -0,0 +1,179 @@ +//===-- SBError.cpp ---------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBError.h" +#include "lldb/Core/Error.h" + +using namespace lldb; +using namespace lldb_private; + + +SBError::SBError () : + m_lldb_object_ap () +{ +} + +SBError::SBError (const SBError &rhs) : + m_lldb_object_ap () +{ + if (rhs.IsValid()) + m_lldb_object_ap.reset (new Error(*rhs)); +} + + +SBError::~SBError() +{ +} + +const SBError & +SBError::operator = (const SBError &rhs) +{ + if (rhs.IsValid()) + { + if (m_lldb_object_ap.get()) + *m_lldb_object_ap = *rhs; + else + m_lldb_object_ap.reset (new Error(*rhs)); + } + else + { + m_lldb_object_ap.reset(); + } + return *this; +} + + +const char * +SBError::GetCString () const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->AsCString(); + return NULL; +} + +void +SBError::Clear () +{ + if (m_lldb_object_ap.get()) + m_lldb_object_ap->Clear(); +} + +bool +SBError::Fail () const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->Fail(); + return false; +} + +bool +SBError::Success () const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->Success(); + return false; +} + +uint32_t +SBError::GetError () const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->GetError(); + return true; +} + +ErrorType +SBError::GetType () const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->GetType(); + return eErrorTypeInvalid; +} + +void +SBError::SetError (uint32_t err, ErrorType type) +{ + CreateIfNeeded (); + m_lldb_object_ap->SetError (err, type); +} + +void +SBError::SetError (const Error &lldb_error) +{ + CreateIfNeeded (); + *m_lldb_object_ap = lldb_error; +} + + +void +SBError::SetErrorToErrno () +{ + CreateIfNeeded (); + m_lldb_object_ap->SetErrorToErrno (); +} + +void +SBError::SetErrorToGenericError () +{ + CreateIfNeeded (); + m_lldb_object_ap->SetErrorToErrno (); +} + +void +SBError::SetErrorString (const char *err_str) +{ + CreateIfNeeded (); + m_lldb_object_ap->SetErrorString (err_str); +} + +int +SBError::SetErrorStringWithFormat (const char *format, ...) +{ + CreateIfNeeded (); + va_list args; + va_start (args, format); + int num_chars = m_lldb_object_ap->SetErrorStringWithVarArg (format, args); + va_end (args); + return num_chars; +} + +bool +SBError::IsValid () const +{ + return m_lldb_object_ap.get() != NULL; +} + +void +SBError::CreateIfNeeded () +{ + if (m_lldb_object_ap.get() == NULL) + m_lldb_object_ap.reset(new Error ()); +} + + +lldb_private::Error * +SBError::operator->() +{ + return m_lldb_object_ap.get(); +} + +lldb_private::Error * +SBError::get() +{ + return m_lldb_object_ap.get(); +} + + +const lldb_private::Error & +SBError::operator*() const +{ + // Be sure to call "IsValid()" before calling this function or it will crash + return *m_lldb_object_ap; +} + diff --git a/lldb/source/API/SBEvent.cpp b/lldb/source/API/SBEvent.cpp new file mode 100644 index 00000000000..c69957ebefc --- /dev/null +++ b/lldb/source/API/SBEvent.cpp @@ -0,0 +1,176 @@ +//===-- SBEvent.cpp ---------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "SBEvent.h" +#include "SBBroadcaster.h" + +#include "lldb/Core/Event.h" +#include "lldb/Core/Stream.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Core/ConstString.h" +#include "lldb/Target/Process.h" +#include "lldb/Breakpoint/Breakpoint.h" +#include "lldb/Interpreter/CommandInterpreter.h" + +using namespace lldb; +using namespace lldb_private; + + +SBEvent::SBEvent () : + m_event_sp (), + m_lldb_object (NULL) +{ +} + +SBEvent::SBEvent (uint32_t event_type, const char *cstr, uint32_t cstr_len) : + m_event_sp (new Event (event_type, new EventDataBytes (cstr, cstr_len))), + m_lldb_object (m_event_sp.get()) +{ +} + +SBEvent::SBEvent (EventSP &event_sp) : + m_event_sp (event_sp), + m_lldb_object (event_sp.get()) +{ +} + +SBEvent::~SBEvent() +{ +} + +void +SBEvent::Dump (FILE *f) const +{ + const Event *lldb_event = GetLLDBObjectPtr(); + if (lldb_event) + { + StreamFile str(f); + lldb_event->Dump ((Stream *) &str); + } +} + +const char * +SBEvent::GetDataFlavor () +{ + Event *lldb_event = SBEvent::GetLLDBObjectPtr(); + if (lldb_event) + return lldb_event->GetData()->GetFlavor().AsCString(); + return NULL; +} + +uint32_t +SBEvent::GetType () const +{ + const Event *lldb_event = SBEvent::GetLLDBObjectPtr(); + if (lldb_event) + return lldb_event->GetType(); + return 0; +} + +SBBroadcaster +SBEvent::GetBroadcaster () const +{ + SBBroadcaster broadcaster; + const Event *lldb_event = SBEvent::GetLLDBObjectPtr(); + if (lldb_event) + broadcaster.SetLLDBObjectPtr (lldb_event->GetBroadcaster(), false); + return broadcaster; +} + +bool +SBEvent::BroadcasterMatchesPtr (const SBBroadcaster *broadcaster) +{ + if (broadcaster) + { + Event *lldb_event = SBEvent::GetLLDBObjectPtr(); + if (lldb_event) + return lldb_event->BroadcasterIs (broadcaster->GetLLDBObjectPtr ()); + } + return false; +} + +bool +SBEvent::BroadcasterMatchesRef (const SBBroadcaster &broadcaster) +{ + Event *lldb_event = SBEvent::GetLLDBObjectPtr(); + if (lldb_event) + return lldb_event->BroadcasterIs (broadcaster.GetLLDBObjectPtr ()); + return false; +} + +void +SBEvent::Clear() +{ + Event *lldb_event = SBEvent::GetLLDBObjectPtr(); + if (lldb_event) + lldb_event->Clear(); +} + +EventSP & +SBEvent::GetSharedPtr () const +{ + return m_event_sp; +} + +Event * +SBEvent::GetLLDBObjectPtr () +{ + // There is a dangerous accessor call GetSharedPtr which can be used, so if + // we have anything valid in m_event_sp, we must use that since if it gets + // used by a function that puts something in there, then it won't update + // m_lldb_object... + if (m_event_sp) + m_lldb_object = m_event_sp.get(); + + return m_lldb_object; +} + +const Event * +SBEvent::GetLLDBObjectPtr () const +{ + // There is a dangerous accessor call GetSharedPtr which can be used, so if + // we have anything valid in m_event_sp, we must use that since if it gets + // used by a function that puts something in there, then it won't update + // m_lldb_object... + if (m_event_sp) + m_lldb_object = m_event_sp.get(); + + return m_lldb_object; +} + +void +SBEvent::SetEventSP (EventSP &event_sp) +{ + m_event_sp = event_sp; + m_lldb_object = m_event_sp.get(); +} + +void +SBEvent::SetLLDBObjectPtr (Event* event_ptr) +{ + m_lldb_object = event_ptr; + m_event_sp.reset(); +} + +bool +SBEvent::IsValid() const +{ + // Do NOT use m_lldb_object directly!!! Must use the SBEvent::GetLLDBObjectPtr() + // accessor. See comments in SBEvent::GetLLDBObjectPtr().... + return SBEvent::GetLLDBObjectPtr() != NULL; + +} + +const char * +SBEvent::GetCStringFromEvent (const SBEvent &event) +{ + return reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event.GetLLDBObjectPtr())); +} + + diff --git a/lldb/source/API/SBFileSpec.cpp b/lldb/source/API/SBFileSpec.cpp new file mode 100644 index 00000000000..48c751177c0 --- /dev/null +++ b/lldb/source/API/SBFileSpec.cpp @@ -0,0 +1,133 @@ +//===-- SBFileSpec.cpp ------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBFileSpec.h" +#include "lldb/Core/FileSpec.h" + +using namespace lldb; +using namespace lldb_private; + + + +SBFileSpec::SBFileSpec () : + m_lldb_object_ap() +{ +} + +SBFileSpec::SBFileSpec (const SBFileSpec &rhs) : + m_lldb_object_ap() +{ + if (rhs.m_lldb_object_ap.get()) + m_lldb_object_ap.reset (new FileSpec (*m_lldb_object_ap)); +} + +SBFileSpec::SBFileSpec (const char *path) : + m_lldb_object_ap(new FileSpec (path)) +{ +} + +SBFileSpec::~SBFileSpec () +{ +} + +const SBFileSpec & +SBFileSpec::operator = (const SBFileSpec &rhs) +{ + if (this != &rhs) + { + if (rhs.IsValid()) + m_lldb_object_ap.reset (new lldb_private::FileSpec(*rhs.m_lldb_object_ap.get())); + } + return *this; +} + +bool +SBFileSpec::IsValid() const +{ + return m_lldb_object_ap.get() != NULL; +} + +bool +SBFileSpec::Exists () const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->Exists(); + return false; +} + + +int +SBFileSpec::ResolvePath (const char *src_path, char *dst_path, size_t dst_len) +{ + return lldb_private::FileSpec::Resolve (src_path, dst_path, dst_len); +} + +const char * +SBFileSpec::GetFileName() const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->GetFilename().AsCString(); + return NULL; +} + +const char * +SBFileSpec::GetDirectory() const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->GetDirectory().AsCString(); + return NULL; +} + +uint32_t +SBFileSpec::GetPath (char *dst_path, size_t dst_len) const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->GetPath (dst_path, dst_len); + + if (dst_path && dst_len) + *dst_path = '\0'; + return 0; +} + + +const lldb_private::FileSpec * +SBFileSpec::operator->() const +{ + return m_lldb_object_ap.get(); +} + +const lldb_private::FileSpec * +SBFileSpec::get() const +{ + return m_lldb_object_ap.get(); +} + + +const lldb_private::FileSpec & +SBFileSpec::operator*() const +{ + return *m_lldb_object_ap.get(); +} + +const lldb_private::FileSpec & +SBFileSpec::ref() const +{ + return *m_lldb_object_ap.get(); +} + + +void +SBFileSpec::SetFileSpec (const lldb_private::FileSpec& fs) +{ + if (m_lldb_object_ap.get()) + *m_lldb_object_ap = fs; + else + m_lldb_object_ap.reset (new FileSpec (fs)); +} + diff --git a/lldb/source/API/SBFrame.cpp b/lldb/source/API/SBFrame.cpp new file mode 100644 index 00000000000..d513e5b726d --- /dev/null +++ b/lldb/source/API/SBFrame.cpp @@ -0,0 +1,394 @@ +//===-- SBFrame.cpp ---------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "SBFrame.h" + +#include <string> +#include <algorithm> + +#include "lldb/lldb-types.h" + +#include "lldb/Core/Address.h" +#include "lldb/Core/ConstString.h" +#include "lldb/Core/Stream.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Core/ValueObjectRegister.h" +#include "lldb/Core/ValueObjectVariable.h" +#include "lldb/Symbol/Block.h" +#include "lldb/Symbol/SymbolContext.h" +#include "lldb/Symbol/VariableList.h" +#include "lldb/Symbol/Variable.h" +#include "lldb/Target/ExecutionContext.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/StackFrame.h" +#include "lldb/Target/Thread.h" + +#include "SBDebugger.h" +#include "SBValue.h" +#include "SBAddress.h" +#include "SBSymbolContext.h" +#include "SBThread.h" + +using namespace lldb; +using namespace lldb_private; + +SBFrame::SBFrame () : + m_lldb_object_sp () +{ +} + +SBFrame::SBFrame (const lldb::StackFrameSP &lldb_object_sp) : + m_lldb_object_sp (lldb_object_sp) +{ +} + +SBFrame::~SBFrame() +{ +} + + +void +SBFrame::SetFrame (const lldb::StackFrameSP &lldb_object_sp) +{ + m_lldb_object_sp = lldb_object_sp; +} + + +bool +SBFrame::IsValid() const +{ + return (m_lldb_object_sp.get() != NULL); +} + +SBSymbolContext +SBFrame::GetSymbolContext (uint32_t resolve_scope) const +{ + SBSymbolContext sb_sym_ctx; + if (m_lldb_object_sp) + sb_sym_ctx.SetSymbolContext(&m_lldb_object_sp->GetSymbolContext (resolve_scope)); + return sb_sym_ctx; +} + +SBModule +SBFrame::GetModule () const +{ + SBModule sb_module (m_lldb_object_sp->GetSymbolContext (eSymbolContextModule).module_sp); + return sb_module; +} + +SBCompileUnit +SBFrame::GetCompileUnit () const +{ + SBCompileUnit sb_comp_unit(m_lldb_object_sp->GetSymbolContext (eSymbolContextCompUnit).comp_unit); + return sb_comp_unit; +} + +SBFunction +SBFrame::GetFunction () const +{ + SBFunction sb_function(m_lldb_object_sp->GetSymbolContext (eSymbolContextFunction).function); + return sb_function; +} + +SBBlock +SBFrame::GetBlock () const +{ + SBBlock sb_block(m_lldb_object_sp->GetSymbolContext (eSymbolContextBlock).block); + return sb_block; +} + +SBLineEntry +SBFrame::GetLineEntry () const +{ + SBLineEntry sb_line_entry(&m_lldb_object_sp->GetSymbolContext (eSymbolContextLineEntry).line_entry); + return sb_line_entry; +} + +uint32_t +SBFrame::GetFrameID () const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetID(); + else + return UINT32_MAX; +} + + +lldb::addr_t +SBFrame::GetPC () const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetPC().GetLoadAddress (&m_lldb_object_sp->GetThread().GetProcess()); + return LLDB_INVALID_ADDRESS; +} + +bool +SBFrame::SetPC (lldb::addr_t new_pc) +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetRegisterContext()->SetPC (new_pc); + return false; +} + +lldb::addr_t +SBFrame::GetSP () const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetRegisterContext()->GetSP(); + return LLDB_INVALID_ADDRESS; +} + + +lldb::addr_t +SBFrame::GetFP () const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetRegisterContext()->GetFP(); + return LLDB_INVALID_ADDRESS; +} + + +SBAddress +SBFrame::GetPCAddress () const +{ + SBAddress sb_addr; + if (m_lldb_object_sp) + sb_addr.SetAddress (&m_lldb_object_sp->GetPC()); + return sb_addr; +} + +void +SBFrame::Clear() +{ + m_lldb_object_sp.reset(); +} + +SBValue +SBFrame::LookupVar (const char *var_name) +{ + lldb::VariableSP var_sp; + if (IsValid ()) + { + lldb_private::VariableList variable_list; + SBSymbolContext sc = GetSymbolContext (eSymbolContextEverything); + + SBBlock block = sc.GetBlock(); + if (block.IsValid()) + block.AppendVariables (true, true, &variable_list); + + const uint32_t num_variables = variable_list.GetSize(); + + bool found = false; + for (int i = 0; i < num_variables && !found; ++i) + { + var_sp = variable_list.GetVariableAtIndex(i); + if (var_sp + && (var_sp.get()->GetName() == lldb_private::ConstString(var_name))) + found = true; + } + if (!found) + var_sp.reset(); + } + SBValue sb_value (ValueObjectSP (new ValueObjectVariable (var_sp))); + return sb_value; +} + +SBValue +SBFrame::LookupVarInScope (const char *var_name, const char *scope) +{ + lldb::VariableSP var_sp; + if (IsValid()) + { + std::string scope_str = scope; + lldb::ValueType var_scope = eValueTypeInvalid; + // Convert scope_str to be all lowercase; + std::transform (scope_str.begin(), scope_str.end(), scope_str.begin(), ::tolower); + + if (scope_str.compare ("global") == 0) + var_scope = eValueTypeVariableGlobal; + else if (scope_str.compare ("local") == 0) + var_scope = eValueTypeVariableLocal; + else if (scope_str.compare ("parameter") == 0) + var_scope = eValueTypeVariableArgument; + + if (var_scope != eValueTypeInvalid) + { + lldb_private::VariableList variable_list; + SBSymbolContext sc = GetSymbolContext (eSymbolContextEverything); + + SBBlock block = sc.GetBlock(); + if (block.IsValid()) + block.AppendVariables (true, true, &variable_list); + + const uint32_t num_variables = variable_list.GetSize(); + + bool found = false; + for (int i = 0; i < num_variables && !found; ++i) + { + var_sp = variable_list.GetVariableAtIndex(i); + if (var_sp + && (var_sp.get()->GetName() == lldb_private::ConstString(var_name)) + && var_sp.get()->GetScope() == var_scope) + found = true; + } + if (!found) + var_sp.reset(); + } + } + SBValue sb_value (ValueObjectSP (new ValueObjectVariable (var_sp))); + return sb_value; +} + +bool +SBFrame::operator == (const SBFrame &rhs) const +{ + return m_lldb_object_sp.get() == rhs.m_lldb_object_sp.get(); +} + +bool +SBFrame::operator != (const SBFrame &rhs) const +{ + return m_lldb_object_sp.get() != rhs.m_lldb_object_sp.get(); +} + +lldb_private::StackFrame * +SBFrame::operator->() const +{ + return m_lldb_object_sp.get(); +} + +lldb_private::StackFrame * +SBFrame::get() const +{ + return m_lldb_object_sp.get(); +} + + +SBThread +SBFrame::GetThread () const +{ + SBThread sb_thread (m_lldb_object_sp->GetThread().GetSP()); + return sb_thread; +} + +const char * +SBFrame::Disassemble () const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->Disassemble(); + return NULL; +} + + + +lldb_private::StackFrame * +SBFrame::GetLLDBObjectPtr () +{ + return m_lldb_object_sp.get(); +} + +SBValueList +SBFrame::GetVariables (bool arguments, + bool locals, + bool statics, + bool in_scope_only) +{ + SBValueList value_list; + if (m_lldb_object_sp) + { + size_t i; + VariableList *variable_list = m_lldb_object_sp->GetVariableList(); + if (variable_list) + { + const size_t num_variables = variable_list->GetSize(); + if (num_variables) + { + for (i = 0; i < num_variables; ++i) + { + VariableSP variable_sp (variable_list->GetVariableAtIndex(i)); + if (variable_sp) + { + bool add_variable = false; + switch (variable_sp->GetScope()) + { + case eValueTypeVariableGlobal: + case eValueTypeVariableStatic: + add_variable = statics; + break; + + case eValueTypeVariableArgument: + add_variable = arguments; + break; + + case eValueTypeVariableLocal: + add_variable = locals; + break; + } + if (add_variable) + { + if (in_scope_only && !variable_sp->IsInScope(m_lldb_object_sp.get())) + continue; + + value_list.Append(ValueObjectSP (new ValueObjectVariable (variable_sp))); + } + } + } + } + } + + if (statics) + { + CompileUnit *frame_comp_unit = m_lldb_object_sp->GetSymbolContext (eSymbolContextCompUnit).comp_unit; + + if (frame_comp_unit) + { + variable_list = frame_comp_unit->GetVariableList(true).get(); + + if (variable_list) + { + const size_t num_variables = variable_list->GetSize(); + if (num_variables) + { + for (i = 0; i < num_variables; ++i) + { + VariableSP variable_sp (variable_list->GetVariableAtIndex(i)); + if (variable_sp) + { + value_list.Append(ValueObjectSP (new ValueObjectVariable (variable_sp))); + } + } + } + } + } + } + } + return value_list; +} + +lldb::SBValueList +SBFrame::GetRegisters () +{ + SBValueList value_list; + if (m_lldb_object_sp) + { + RegisterContext *reg_ctx = m_lldb_object_sp->GetRegisterContext(); + if (reg_ctx) + { + const uint32_t num_sets = reg_ctx->GetRegisterSetCount(); + for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) + { + value_list.Append(ValueObjectSP (new ValueObjectRegisterSet (reg_ctx, set_idx))); + } + } + } + return value_list; +} + diff --git a/lldb/source/API/SBFunction.cpp b/lldb/source/API/SBFunction.cpp new file mode 100644 index 00000000000..010a5ec17df --- /dev/null +++ b/lldb/source/API/SBFunction.cpp @@ -0,0 +1,64 @@ +//===-- SBFunction.cpp ------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBFunction.h" +#include "lldb/API/SBProcess.h" +#include "lldb/Symbol/Function.h" + +using namespace lldb; + + +SBFunction::SBFunction () : + m_lldb_object_ptr (NULL) +{ +} + +SBFunction::SBFunction (lldb_private::Function *lldb_object_ptr) : + m_lldb_object_ptr (lldb_object_ptr) +{ +} + +SBFunction::~SBFunction () +{ + m_lldb_object_ptr = NULL; +} + +bool +SBFunction::IsValid () const +{ + return m_lldb_object_ptr != NULL; +} + +const char * +SBFunction::GetName() const +{ + if (m_lldb_object_ptr) + return m_lldb_object_ptr->GetMangled().GetName().AsCString(); + return NULL; +} + +const char * +SBFunction::GetMangledName () const +{ + if (m_lldb_object_ptr) + return m_lldb_object_ptr->GetMangled().GetMangledName().AsCString(); + return NULL; +} + +bool +SBFunction::operator == (const SBFunction &rhs) const +{ + return m_lldb_object_ptr == rhs.m_lldb_object_ptr; +} + +bool +SBFunction::operator != (const SBFunction &rhs) const +{ + return m_lldb_object_ptr != rhs.m_lldb_object_ptr; +} diff --git a/lldb/source/API/SBHostOS.cpp b/lldb/source/API/SBHostOS.cpp new file mode 100644 index 00000000000..f0af9cd5069 --- /dev/null +++ b/lldb/source/API/SBHostOS.cpp @@ -0,0 +1,64 @@ +//===-- SBHostOS.cpp --------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBHostOS.h" +#include "lldb/API/SBError.h" +#include "lldb/Core/FileSpec.h" +#include "lldb/Host/Host.h" + +using namespace lldb; +using namespace lldb_private; + + + +SBFileSpec +SBHostOS::GetProgramFileSpec () +{ + SBFileSpec sb_filespec; + sb_filespec.SetFileSpec (Host::GetProgramFileSpec ()); + return sb_filespec; +} + +lldb::thread_t +SBHostOS::ThreadCreate +( + const char *name, + void *(*thread_function)(void *), + void *thread_arg, + SBError *error_ptr +) +{ + return Host::ThreadCreate (name, thread_function, thread_arg, error_ptr ? error_ptr->get() : NULL); +} + +void +SBHostOS::ThreadCreated (const char *name) +{ + Host::ThreadCreated (name); +} + +bool +SBHostOS::ThreadCancel (lldb::thread_t thread, SBError *error_ptr) +{ + return Host::ThreadCancel (thread, error_ptr ? error_ptr->get() : NULL); +} + +bool +SBHostOS::ThreadDetach (lldb::thread_t thread, SBError *error_ptr) +{ + return Host::ThreadDetach (thread, error_ptr ? error_ptr->get() : NULL); +} + +bool +SBHostOS::ThreadJoin (lldb::thread_t thread, void **result, SBError *error_ptr) +{ + return Host::ThreadJoin (thread, result, error_ptr ? error_ptr->get() : NULL); +} + + diff --git a/lldb/source/API/SBInputReader.cpp b/lldb/source/API/SBInputReader.cpp new file mode 100644 index 00000000000..022d73207ad --- /dev/null +++ b/lldb/source/API/SBInputReader.cpp @@ -0,0 +1,169 @@ +//===-- SBInputReader.cpp ---------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "lldb/lldb-enumerations.h" + +#include "lldb/API/SBInputReader.h" +#include "lldb/API/SBError.h" +#include "lldb/API/SBStringList.h" +#include "lldb/Core/InputReader.h" + + +using namespace lldb; +using namespace lldb_private; + +SBInputReader::SBInputReader () : + m_reader_sp (), + m_callback_function (NULL), + m_callback_baton (NULL) + +{ +} + +SBInputReader::SBInputReader (const lldb::InputReaderSP &reader_sp) : + m_reader_sp (reader_sp) +{ +} + +SBInputReader::SBInputReader (const SBInputReader &rhs) : + m_reader_sp (rhs.m_reader_sp) +{ +} + +SBInputReader::~SBInputReader () +{ +} + +size_t +SBInputReader::PrivateCallback +( + void *baton, + InputReader *reader, + lldb::InputReaderAction notification, + const char *bytes, + size_t bytes_len +) +{ + SBInputReader *sb_reader = (SBInputReader *)baton; + return sb_reader->m_callback_function (sb_reader->m_callback_baton, + sb_reader, + notification, + bytes, + bytes_len); +} + +SBError +SBInputReader::Initialize +( + Callback callback_function, + void *callback_baton, + lldb::InputReaderGranularity granularity, + const char *end_token, + const char *prompt, + bool echo +) +{ + SBError sb_error; + m_reader_sp.reset (new InputReader ()); + + m_callback_function = callback_function; + m_callback_baton = callback_baton; + + if (m_reader_sp) + { + sb_error.SetError (m_reader_sp->Initialize (SBInputReader::PrivateCallback, + this, + granularity, + end_token, + prompt, + echo)); + } + + if (sb_error.Fail()) + { + m_reader_sp.reset (); + m_callback_function = NULL; + m_callback_baton = NULL; + } + + return sb_error; +} + +bool +SBInputReader::IsValid () const +{ + return (m_reader_sp.get() != NULL); +} + +const SBInputReader & +SBInputReader::operator = (const SBInputReader &rhs) +{ + if (this != &rhs) + m_reader_sp = rhs.m_reader_sp; + return *this; +} + +lldb_private::InputReader * +SBInputReader::operator->() const +{ + return m_reader_sp.get(); +} + +lldb::InputReaderSP & +SBInputReader::operator *() +{ + return m_reader_sp; +} + +const lldb::InputReaderSP & +SBInputReader::operator *() const +{ + return m_reader_sp; +} + +lldb_private::InputReader * +SBInputReader::get() const +{ + return m_reader_sp.get(); +} + +bool +SBInputReader::IsDone () const +{ + if (m_reader_sp) + return m_reader_sp->IsDone(); + else + return true; +} + +void +SBInputReader::SetIsDone (bool value) +{ + if (m_reader_sp) + m_reader_sp->SetIsDone (value); +} + +bool +SBInputReader::IsActive () const +{ + if (m_reader_sp) + return m_reader_sp->IsActive(); + else + return false; +} + +InputReaderGranularity +SBInputReader::GetGranularity () +{ + if (m_reader_sp) + return m_reader_sp->GetGranularity(); + else + return eInputReaderGranularityInvalid; +} diff --git a/lldb/source/API/SBInstruction.cpp b/lldb/source/API/SBInstruction.cpp new file mode 100644 index 00000000000..564fda0b8cb --- /dev/null +++ b/lldb/source/API/SBInstruction.cpp @@ -0,0 +1,74 @@ +//===-- SBInstruction.cpp ---------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBInstruction.h" + +#include "lldb/Core/Disassembler.h" + +using namespace lldb; +using namespace lldb_private; + +//SBInstruction::SBInstruction (lldb_private::Disassembler::Instruction *lldb_insn) : +// m_lldb_object_sp (lldb_insn); +//{ +//} + +SBInstruction::SBInstruction () +{ +} + +SBInstruction::~SBInstruction () +{ +} + +//bool +//SBInstruction::IsValid() +//{ +// return (m_lldb_object_sp.get() != NULL); +//} + +//size_t +//SBInstruction::GetByteSize () +//{ +// if (IsValid()) +// { +// return m_lldb_object_sp->GetByteSize(); +// } +// return 0; +//} + +//void +//SBInstruction::SetByteSize (size_T byte_size) +//{ +// if (IsValid ()) +// { +// m_lldb_object_sp->SetByteSize (byte_size); +// } +//} + +//bool +//SBInstruction::DoesBranch () +//{ +// if (IsValid ()) +// { +// return m_lldb_object_sp->DoesBranch (); +// } +// return false; +//} + +void +SBInstruction::Print (FILE *out) +{ + if (out == NULL) + return; + + //StreamFile out_strem (out); + + //m_lldb_object_sp->Dump (out, LLDB_INVALID_ADDRESS, NULL, 0); +} diff --git a/lldb/source/API/SBInstructionList.cpp b/lldb/source/API/SBInstructionList.cpp new file mode 100644 index 00000000000..d34e9d100a9 --- /dev/null +++ b/lldb/source/API/SBInstructionList.cpp @@ -0,0 +1,53 @@ +//===-- SBInstructionList.cpp -----------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBInstructionList.h" +#include "lldb/API/SBInstruction.h" + +using namespace lldb; + + +SBInstructionList::SBInstructionList () +{ +} + +SBInstructionList::~SBInstructionList () +{ +} + +size_t +SBInstructionList::GetSize () +{ + return 0; +} + +SBInstruction +SBInstructionList::GetInstructionAtIndex (uint32_t idx) +{ + SBInstruction inst; + return inst; +} + +void +SBInstructionList::Clear () +{ +} + +void +SBInstructionList::AppendInstruction (SBInstruction insn) +{ +} + +void +SBInstructionList::Print (FILE *out) +{ + if (out == NULL) + return; +} + diff --git a/lldb/source/API/SBLineEntry.cpp b/lldb/source/API/SBLineEntry.cpp new file mode 100644 index 00000000000..483540050a3 --- /dev/null +++ b/lldb/source/API/SBLineEntry.cpp @@ -0,0 +1,158 @@ +//===-- SBLineEntry.cpp -----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBLineEntry.h" +#include "lldb/Symbol/LineEntry.h" + +using namespace lldb; + + +SBLineEntry::SBLineEntry () : + m_lldb_object_ap () +{ +} + +SBLineEntry::SBLineEntry (const SBLineEntry &rhs) : + m_lldb_object_ap () +{ + if (rhs.IsValid()) + { + m_lldb_object_ap.reset (new lldb_private::LineEntry (*rhs)); + } +} + + + +SBLineEntry::SBLineEntry (const lldb_private::LineEntry *lldb_object_ptr) : + m_lldb_object_ap () +{ + if (lldb_object_ptr) + m_lldb_object_ap.reset (new lldb_private::LineEntry(*lldb_object_ptr)); +} + +const SBLineEntry & +SBLineEntry::operator = (const SBLineEntry &rhs) +{ + if (this != &rhs) + { + if (rhs.IsValid()) + m_lldb_object_ap.reset (new lldb_private::LineEntry(*rhs)); + } + return *this; +} + +void +SBLineEntry::SetLineEntry (const lldb_private::LineEntry &lldb_object_ref) +{ + if (m_lldb_object_ap.get()) + (*m_lldb_object_ap.get()) = lldb_object_ref; + else + m_lldb_object_ap.reset (new lldb_private::LineEntry (lldb_object_ref)); +} + + +SBLineEntry::~SBLineEntry () +{ +} + + +SBAddress +SBLineEntry::GetStartAddress () const +{ + SBAddress sb_address; + if (m_lldb_object_ap.get()) + sb_address.SetAddress(&m_lldb_object_ap->range.GetBaseAddress()); + return sb_address; +} + +SBAddress +SBLineEntry::GetEndAddress () const +{ + SBAddress sb_address; + if (m_lldb_object_ap.get()) + { + sb_address.SetAddress(&m_lldb_object_ap->range.GetBaseAddress()); + sb_address.OffsetAddress(m_lldb_object_ap->range.GetByteSize()); + } + return sb_address; +} + +bool +SBLineEntry::IsValid () const +{ + return m_lldb_object_ap.get() != NULL; +} + + +SBFileSpec +SBLineEntry::GetFileSpec () const +{ + SBFileSpec sb_file_spec; + if (m_lldb_object_ap.get() && m_lldb_object_ap->file) + sb_file_spec.SetFileSpec(m_lldb_object_ap->file); + return sb_file_spec; +} + +uint32_t +SBLineEntry::GetLine () const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->line; + return 0; +} + + +uint32_t +SBLineEntry::GetColumn () const +{ + if (m_lldb_object_ap.get()) + return m_lldb_object_ap->column; + return 0; +} + +bool +SBLineEntry::operator == (const SBLineEntry &rhs) const +{ + lldb_private::LineEntry *lhs_ptr = m_lldb_object_ap.get(); + lldb_private::LineEntry *rhs_ptr = rhs.m_lldb_object_ap.get(); + + if (lhs_ptr && rhs_ptr) + return lldb_private::LineEntry::Compare (*lhs_ptr, *rhs_ptr) == 0; + + return lhs_ptr == rhs_ptr; +} + +bool +SBLineEntry::operator != (const SBLineEntry &rhs) const +{ + lldb_private::LineEntry *lhs_ptr = m_lldb_object_ap.get(); + lldb_private::LineEntry *rhs_ptr = rhs.m_lldb_object_ap.get(); + + if (lhs_ptr && rhs_ptr) + return lldb_private::LineEntry::Compare (*lhs_ptr, *rhs_ptr) != 0; + + return lhs_ptr != rhs_ptr; +} + +const lldb_private::LineEntry * +SBLineEntry::operator->() const +{ + return m_lldb_object_ap.get(); +} + +const lldb_private::LineEntry & +SBLineEntry::operator*() const +{ + return *m_lldb_object_ap; +} + + + + + diff --git a/lldb/source/API/SBListener.cpp b/lldb/source/API/SBListener.cpp new file mode 100644 index 00000000000..6f2cf7bc911 --- /dev/null +++ b/lldb/source/API/SBListener.cpp @@ -0,0 +1,299 @@ +//===-- SBListener.cpp ------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Core/Listener.h" +#include "lldb/lldb-forward-rtti.h" +#include "lldb/Host/TimeValue.h" + +#include "SBListener.h" +#include "SBEvent.h" +#include "SBBroadcaster.h" + +using namespace lldb; +using namespace lldb_private; + + +SBListener::SBListener () +{ +} + +SBListener::SBListener (const char *name) : + m_lldb_object_ptr (new Listener (name)), + m_lldb_object_ptr_owned (true) +{ +} + +SBListener::SBListener (Listener &listener) : + m_lldb_object_ptr (&listener), + m_lldb_object_ptr_owned (false) +{ +} + +SBListener::~SBListener () +{ + if (m_lldb_object_ptr_owned) + { + if (m_lldb_object_ptr) + { + delete m_lldb_object_ptr; + m_lldb_object_ptr = NULL; + } + } +} + +bool +SBListener::IsValid() const +{ + return m_lldb_object_ptr != NULL; +} + +void +SBListener::AddEvent (const SBEvent &event) +{ + EventSP &event_sp = event.GetSharedPtr (); + if (event_sp) + m_lldb_object_ptr->AddEvent (event_sp); +} + +void +SBListener::Clear () +{ + if (IsValid()) + m_lldb_object_ptr->Clear (); +} + +uint32_t +SBListener::StartListeningForEvents (const SBBroadcaster& broadcaster, uint32_t event_mask) +{ + if (IsValid() && broadcaster.IsValid()) + { + return m_lldb_object_ptr->StartListeningForEvents (broadcaster.GetLLDBObjectPtr (), event_mask); + } + return false; +} + +bool +SBListener::StopListeningForEvents (const SBBroadcaster& broadcaster, uint32_t event_mask) +{ + if (IsValid() && broadcaster.IsValid()) + { + return m_lldb_object_ptr->StopListeningForEvents (broadcaster.GetLLDBObjectPtr (), event_mask); + } + return false; +} + +bool +SBListener::WaitForEvent (uint32_t num_seconds, SBEvent &event) +{ + if (IsValid()) + { + TimeValue time_value; + if (num_seconds != UINT32_MAX) + { + assert (num_seconds != 0); // Take this out after all calls with timeout set to zero have been removed.... + time_value = TimeValue::Now(); + time_value.OffsetWithSeconds (num_seconds); + } + EventSP event_sp; + if (m_lldb_object_ptr->WaitForEvent (time_value.IsValid() ? &time_value : NULL, event_sp)) + { + event.SetEventSP (event_sp); + return true; + } + } + event.SetLLDBObjectPtr (NULL); + return false; +} + +bool +SBListener::WaitForEventForBroadcaster +( + uint32_t num_seconds, + const SBBroadcaster &broadcaster, + SBEvent &event +) +{ + if (IsValid() && broadcaster.IsValid()) + { + TimeValue time_value; + if (num_seconds != UINT32_MAX) + { + time_value = TimeValue::Now(); + time_value.OffsetWithSeconds (num_seconds); + } + EventSP event_sp; + if (m_lldb_object_ptr->WaitForEventForBroadcaster (time_value.IsValid() ? &time_value : NULL, + broadcaster.GetLLDBObjectPtr (), + event_sp)) + { + event.SetEventSP (event_sp); + return true; + } + + } + event.SetLLDBObjectPtr (NULL); + return false; +} + +bool +SBListener::WaitForEventForBroadcasterWithType +( + uint32_t num_seconds, + const SBBroadcaster &broadcaster, + uint32_t event_type_mask, + SBEvent &event +) +{ + if (IsValid() && broadcaster.IsValid()) + { + TimeValue time_value; + if (num_seconds != UINT32_MAX) + { + time_value = TimeValue::Now(); + time_value.OffsetWithSeconds (num_seconds); + } + EventSP event_sp; + if (m_lldb_object_ptr->WaitForEventForBroadcasterWithType (time_value.IsValid() ? &time_value : NULL, + broadcaster.GetLLDBObjectPtr (), + event_type_mask, + event_sp)) + { + event.SetEventSP (event_sp); + return true; + } + } + event.SetLLDBObjectPtr (NULL); + return false; +} + +bool +SBListener::PeekAtNextEvent (SBEvent &event) +{ + if (m_lldb_object_ptr) + { + event.SetLLDBObjectPtr (m_lldb_object_ptr->PeekAtNextEvent ()); + return event.IsValid(); + } + event.SetLLDBObjectPtr (NULL); + return false; +} + +bool +SBListener::PeekAtNextEventForBroadcaster (const SBBroadcaster &broadcaster, SBEvent &event) +{ + if (IsValid() && broadcaster.IsValid()) + { + event.SetLLDBObjectPtr (m_lldb_object_ptr->PeekAtNextEventForBroadcaster (broadcaster.GetLLDBObjectPtr ())); + return event.IsValid(); + } + event.SetLLDBObjectPtr (NULL); + return false; +} + +bool +SBListener::PeekAtNextEventForBroadcasterWithType (const SBBroadcaster &broadcaster, uint32_t event_type_mask, + SBEvent &event) +{ + if (IsValid() && broadcaster.IsValid()) + { + event.SetLLDBObjectPtr(m_lldb_object_ptr->PeekAtNextEventForBroadcasterWithType (broadcaster.GetLLDBObjectPtr (), event_type_mask)); + return event.IsValid(); + } + event.SetLLDBObjectPtr (NULL); + return false; +} + +bool +SBListener::GetNextEvent (SBEvent &event) +{ + if (m_lldb_object_ptr) + { + EventSP event_sp; + if (m_lldb_object_ptr->GetNextEvent (event_sp)) + { + event.SetEventSP (event_sp); + return true; + } + } + event.SetLLDBObjectPtr (NULL); + return false; +} + +bool +SBListener::GetNextEventForBroadcaster (const SBBroadcaster &broadcaster, SBEvent &event) +{ + if (IsValid() && broadcaster.IsValid()) + { + EventSP event_sp; + if (m_lldb_object_ptr->GetNextEventForBroadcaster (broadcaster.GetLLDBObjectPtr (), event_sp)) + { + event.SetEventSP (event_sp); + return true; + } + } + event.SetLLDBObjectPtr (NULL); + return false; +} + +bool +SBListener::GetNextEventForBroadcasterWithType +( + const SBBroadcaster &broadcaster, + uint32_t event_type_mask, + SBEvent &event +) +{ + if (IsValid() && broadcaster.IsValid()) + { + EventSP event_sp; + if (m_lldb_object_ptr->GetNextEventForBroadcasterWithType (broadcaster.GetLLDBObjectPtr (), + event_type_mask, + event_sp)) + { + event.SetEventSP (event_sp); + return true; + } + } + event.SetLLDBObjectPtr (NULL); + return false; +} + +bool +SBListener::HandleBroadcastEvent (const SBEvent &event) +{ + if (m_lldb_object_ptr) + return m_lldb_object_ptr->HandleBroadcastEvent (event.GetSharedPtr()); + return false; +} + +lldb_private::Listener * +SBListener::operator->() const +{ + return m_lldb_object_ptr; +} + +lldb_private::Listener * +SBListener::get() const +{ + return m_lldb_object_ptr; +} + +lldb_private::Listener & +SBListener::operator *() +{ + return *m_lldb_object_ptr; +} + +const lldb_private::Listener & +SBListener::operator *() const +{ + return *m_lldb_object_ptr; +} + + diff --git a/lldb/source/API/SBModule.cpp b/lldb/source/API/SBModule.cpp new file mode 100644 index 00000000000..6a54c2177bf --- /dev/null +++ b/lldb/source/API/SBModule.cpp @@ -0,0 +1,107 @@ +//===-- SBModule.cpp --------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBModule.h" +#include "lldb/API/SBFileSpec.h" +#include "lldb/Core/Module.h" + +using namespace lldb; + + +SBModule::SBModule () : + m_lldb_object_sp () +{ +} + +SBModule::SBModule (const lldb::ModuleSP& module_sp) : + m_lldb_object_sp (module_sp) +{ +} + +SBModule::~SBModule () +{ +} + +bool +SBModule::IsValid () const +{ + return m_lldb_object_sp.get() != NULL; +} + +SBFileSpec +SBModule::GetFileSpec () const +{ + SBFileSpec file_spec; + if (m_lldb_object_sp) + file_spec.SetFileSpec(m_lldb_object_sp->GetFileSpec()); + return file_spec; +} + +const uint8_t * +SBModule::GetUUIDBytes () const +{ + if (m_lldb_object_sp) + return (const uint8_t *)m_lldb_object_sp->GetUUID().GetBytes(); + return NULL; +} + + +bool +SBModule::operator == (const SBModule &rhs) const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp.get() == rhs.m_lldb_object_sp.get(); + return false; +} + +bool +SBModule::operator != (const SBModule &rhs) const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp.get() != rhs.m_lldb_object_sp.get(); + return false; +} + +lldb::ModuleSP & +SBModule::operator *() +{ + return m_lldb_object_sp; +} + +lldb_private::Module * +SBModule::operator ->() +{ + return m_lldb_object_sp.get(); +} + +const lldb_private::Module * +SBModule::operator ->() const +{ + return m_lldb_object_sp.get(); +} + +lldb_private::Module * +SBModule::get() +{ + return m_lldb_object_sp.get(); +} + +const lldb_private::Module * +SBModule::get() const +{ + return m_lldb_object_sp.get(); +} + + +void +SBModule::SetModule (const lldb::ModuleSP& module_sp) +{ + m_lldb_object_sp = module_sp; +} + diff --git a/lldb/source/API/SBProcess.cpp b/lldb/source/API/SBProcess.cpp new file mode 100644 index 00000000000..776939c9ea6 --- /dev/null +++ b/lldb/source/API/SBProcess.cpp @@ -0,0 +1,604 @@ +//===-- SBProcess.cpp -------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "SBProcess.h" + +#include "lldb/lldb-defines.h" +#include "lldb/lldb-types.h" + +#include "lldb/Core/Args.h" +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Core/DataExtractor.h" +#include "lldb/Core/State.h" +#include "lldb/Core/Stream.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/Thread.h" +#include "lldb/Target/RegisterContext.h" + +// Project includes + +#include "SBBroadcaster.h" +#include "SBDebugger.h" +#include "SBCommandReturnObject.h" +#include "SBEvent.h" +#include "SBThread.h" +#include "SBStringList.h" + +using namespace lldb; +using namespace lldb_private; + + + +SBProcess::SBProcess () : + m_lldb_object_sp() +{ +} + + +//---------------------------------------------------------------------- +// SBProcess constructor +//---------------------------------------------------------------------- + +SBProcess::SBProcess (const SBProcess& rhs) : + m_lldb_object_sp (rhs.m_lldb_object_sp) +{ +} + + +SBProcess::SBProcess (const lldb::ProcessSP &process_sp) : + m_lldb_object_sp (process_sp) +{ +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +SBProcess::~SBProcess() +{ +} + +void +SBProcess::SetProcess (const ProcessSP &process_sp) +{ + m_lldb_object_sp = process_sp; +} + +void +SBProcess::Clear () +{ + m_lldb_object_sp.reset(); +} + + +bool +SBProcess::IsValid() const +{ + return m_lldb_object_sp.get() != NULL; +} + + +uint32_t +SBProcess::GetNumThreads () +{ + if (m_lldb_object_sp) + { + const bool can_update = true; + return m_lldb_object_sp->GetThreadList().GetSize(can_update); + } + return 0; +} + +SBThread +SBProcess::GetCurrentThread () const +{ + SBThread sb_thread; + if (m_lldb_object_sp) + sb_thread.SetThread (m_lldb_object_sp->GetThreadList().GetCurrentThread()); + return sb_thread; +} + +SBTarget +SBProcess::GetTarget() const +{ + SBTarget sb_target; + if (m_lldb_object_sp) + sb_target = SBDebugger::FindTargetWithLLDBProcess (m_lldb_object_sp); + return sb_target; +} + + +size_t +SBProcess::PutSTDIN (const char *src, size_t src_len) +{ + if (m_lldb_object_sp != NULL) + { + Error error; + return m_lldb_object_sp->PutSTDIN (src, src_len, error); + } + else + return 0; +} + +size_t +SBProcess::GetSTDOUT (char *dst, size_t dst_len) const +{ + if (m_lldb_object_sp != NULL) + { + Error error; + return m_lldb_object_sp->GetSTDOUT (dst, dst_len, error); + } + else + return 0; +} + +size_t +SBProcess::GetSTDERR (char *dst, size_t dst_len) const +{ + if (m_lldb_object_sp != NULL) + { + Error error; + return m_lldb_object_sp->GetSTDERR (dst, dst_len, error); + } + else + return 0; +} + +void +SBProcess::ReportCurrentState (const SBEvent &event, FILE *out) const +{ + if (out == NULL) + return; + + if (m_lldb_object_sp != NULL) + { + const StateType event_state = SBProcess::GetStateFromEvent (event); + char message[1024]; + int message_len = ::snprintf (message, + sizeof (message), + "Process %d %s\n", + m_lldb_object_sp->GetID(), + SBDebugger::StateAsCString (event_state)); + + if (message_len > 0) + ::fwrite (message, 1, message_len, out); + } +} + +void +SBProcess::AppendCurrentStateReport (const SBEvent &event, SBCommandReturnObject &result) +{ + if (m_lldb_object_sp != NULL) + { + const StateType event_state = SBProcess::GetStateFromEvent (event); + char message[1024]; + ::snprintf (message, + sizeof (message), + "Process %d %s\n", + m_lldb_object_sp->GetID(), + SBDebugger::StateAsCString (event_state)); + + result.AppendMessage (message); + } +} + +bool +SBProcess::SetCurrentThread (const SBThread &thread) +{ + if (m_lldb_object_sp != NULL) + return m_lldb_object_sp->GetThreadList().SetCurrentThreadByID (thread.GetThreadID()); + return false; +} + +bool +SBProcess::SetCurrentThreadByID (uint32_t tid) +{ + if (m_lldb_object_sp != NULL) + return m_lldb_object_sp->GetThreadList().SetCurrentThreadByID (tid); + return false; +} + +SBThread +SBProcess::GetThreadAtIndex (size_t index) +{ + SBThread thread; + if (m_lldb_object_sp) + thread.SetThread (m_lldb_object_sp->GetThreadList().GetThreadAtIndex(index)); + return thread; +} + +StateType +SBProcess::GetState () +{ + if (m_lldb_object_sp != NULL) + return m_lldb_object_sp->GetState(); + else + return eStateInvalid; +} + + +int +SBProcess::GetExitStatus () +{ + if (m_lldb_object_sp != NULL) + return m_lldb_object_sp->GetExitStatus (); + else + return 0; +} + +const char * +SBProcess::GetExitDescription () +{ + if (m_lldb_object_sp != NULL) + return m_lldb_object_sp->GetExitDescription (); + else + return NULL; +} + +lldb::pid_t +SBProcess::GetProcessID () +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetID(); + else + return LLDB_INVALID_PROCESS_ID; +} + +uint32_t +SBProcess::GetAddressByteSize () const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetAddressByteSize(); + else + return 0; +} + + +void +SBProcess::DisplayThreadsInfo (FILE *out, FILE *err, bool only_threads_with_stop_reason) +{ + if (m_lldb_object_sp != NULL) + { + size_t num_thread_infos_dumped = 0; + size_t num_threads = GetNumThreads(); + + if (out == NULL) + out = SBDebugger::GetOutputFileHandle(); + + if (err == NULL) + err = SBDebugger::GetErrorFileHandle(); + + if ((out == NULL) ||(err == NULL)) + return; + + if (num_threads > 0) + { + Thread::StopInfo thread_stop_info; + SBThread curr_thread (m_lldb_object_sp->GetThreadList().GetCurrentThread()); + for (int i = 0; i < num_threads; ++i) + { + SBThread thread (m_lldb_object_sp->GetThreadList().GetThreadAtIndex(i)); + if (thread.IsValid()) + { + bool is_current_thread = false; + StreamFile str (out); + if (thread == curr_thread) + is_current_thread = true; + StopReason thread_stop_reason = eStopReasonNone; + if (thread->GetStopInfo (&thread_stop_info)) + { + thread_stop_reason = thread_stop_info.GetStopReason(); + if (thread_stop_reason == eStopReasonNone) + { + if (only_threads_with_stop_reason && !is_current_thread) + continue; + } + } + ++num_thread_infos_dumped; + fprintf (out, " %c thread #%u: tid = 0x%4.4x, pc = 0x%16.16llx", + (is_current_thread ? '*' : ' '), + thread->GetIndexID(), thread->GetID(), thread->GetRegisterContext()->GetPC()); + + StackFrameSP frame_sp(thread->GetStackFrameAtIndex (0)); + if (frame_sp) + { + SymbolContext sc (frame_sp->GetSymbolContext (eSymbolContextEverything)); + fprintf (out, ", where = "); + sc.DumpStopContext (&str, m_lldb_object_sp.get(), frame_sp->GetPC ()); + } + + if (thread_stop_reason != eStopReasonNone) + { + fprintf (out, ", stop reason = "); + thread_stop_info.Dump (&str); + } + + const char *thread_name = thread->GetName(); + if (thread_name && thread_name[0]) + fprintf (out, ", thread_name = '%s'", thread_name); + + fprintf (out, "\n"); + + SBThread sb_thread (thread); + sb_thread.DisplayFramesForCurrentContext (out, err, 0, 1, false, 1); + } + } + } + } +} +bool +SBProcess::WaitUntilProcessHasStopped (SBCommandReturnObject &result) +{ + bool state_changed = false; + + if (IsValid()) + { + EventSP event_sp; + StateType state = m_lldb_object_sp->WaitForStateChangedEvents (NULL, event_sp); + + while (StateIsStoppedState (state)) + { + state = m_lldb_object_sp->WaitForStateChangedEvents (NULL, event_sp); + SBEvent event (event_sp); + AppendCurrentStateReport (event, result); + state_changed = true; + } + } + return state_changed; +} + +SBError +SBProcess::Continue () +{ + SBError sb_error; + if (IsValid()) + sb_error.SetError(m_lldb_object_sp->Resume()); + else + sb_error.SetErrorString ("SBProcess is invalid"); + + return sb_error; +} + + +SBError +SBProcess::Destroy () +{ + SBError sb_error; + if (m_lldb_object_sp) + sb_error.SetError(m_lldb_object_sp->Destroy()); + else + sb_error.SetErrorString ("SBProcess is invalid"); + + return sb_error; +} + + +SBError +SBProcess::Stop () +{ + SBError sb_error; + if (IsValid()) + sb_error.SetError (m_lldb_object_sp->Halt()); + else + sb_error.SetErrorString ("SBProcess is invalid"); + return sb_error; +} + +SBError +SBProcess::Kill () +{ + SBError sb_error; + if (m_lldb_object_sp) + sb_error.SetError (m_lldb_object_sp->Destroy()); + else + sb_error.SetErrorString ("SBProcess is invalid"); + return sb_error; +} + + +SBError +SBProcess::AttachByName (const char *name, bool wait_for_launch) +{ + SBError sb_error; + if (m_lldb_object_sp) + sb_error.SetError (m_lldb_object_sp->Attach (name, wait_for_launch)); + else + sb_error.SetErrorString ("SBProcess is invalid"); + return sb_error; +} + +lldb::pid_t +SBProcess::AttachByPID (lldb::pid_t attach_pid) // DEPRECATED: will be removed in a few builds in favor of SBError AttachByPID(pid_t) +{ + Attach (attach_pid); + return GetProcessID(); +} + + +SBError +SBProcess::Attach (lldb::pid_t attach_pid) +{ + SBError sb_error; + if (m_lldb_object_sp) + sb_error.SetError (m_lldb_object_sp->Attach (attach_pid)); + else + sb_error.SetErrorString ("SBProcess is invalid"); + return sb_error; +} + +SBError +SBProcess::Detach () +{ + SBError sb_error; + if (m_lldb_object_sp) + sb_error.SetError (m_lldb_object_sp->Detach()); + else + sb_error.SetErrorString ("SBProcess is invalid"); + + return sb_error; +} + +SBError +SBProcess::Signal (int signal) +{ + SBError sb_error; + if (m_lldb_object_sp) + sb_error.SetError (m_lldb_object_sp->Signal (signal)); + else + sb_error.SetErrorString ("SBProcess is invalid"); + return sb_error; +} + +void +SBProcess::ListThreads () +{ + FILE *out = SBDebugger::GetOutputFileHandle(); + if (out == NULL) + return; + + if (m_lldb_object_sp) + { + size_t num_threads = GetNumThreads (); + if (num_threads > 0) + { + Thread *cur_thread = m_lldb_object_sp->GetThreadList().GetCurrentThread().get(); + for (int i = 0; i < num_threads; ++i) + { + Thread *thread = m_lldb_object_sp->GetThreadList().GetThreadAtIndex(i).get(); + if (thread) + { + bool is_current_thread = false; + if (thread == cur_thread) + is_current_thread = true; + fprintf (out, " [%u] %c tid = 0x%4.4x, pc = 0x%16.16llx", + i, + (is_current_thread ? '*' : ' '), + thread->GetID(), + thread->GetRegisterContext()->GetPC()); + const char *thread_name = thread->GetName(); + if (thread_name && thread_name[0]) + fprintf (out, ", name = %s", thread_name); + const char *queue_name = thread->GetQueueName(); + if (queue_name && queue_name[0]) + fprintf (out, ", queue = %s", queue_name); + fprintf (out, "\n"); + } + } + } + } +} + +SBThread +SBProcess::GetThreadByID (tid_t sb_thread_id) +{ + SBThread thread; + if (m_lldb_object_sp) + thread.SetThread (m_lldb_object_sp->GetThreadList().FindThreadByID ((tid_t) sb_thread_id)); + return thread; +} + +void +SBProcess::Backtrace (bool all_threads, uint32_t num_frames) +{ + if (m_lldb_object_sp) + { + if (!all_threads) + { + SBDebugger::UpdateCurrentThread (*this); + SBThread cur_thread = GetCurrentThread(); + if (cur_thread.IsValid()) + cur_thread.Backtrace (num_frames); + } + else + { + int num_threads = GetNumThreads (); + for (int i = 0; i < num_threads; ++i) + { + SBThread sb_thread = GetThreadAtIndex (i); + sb_thread.Backtrace (num_frames); + } + } + } +} + +StateType +SBProcess::GetStateFromEvent (const SBEvent &event) +{ + return Process::ProcessEventData::GetStateFromEvent (event.GetLLDBObjectPtr()); +} + + +bool +SBProcess::GetRestartedFromEvent (const SBEvent &event) +{ + return Process::ProcessEventData::GetRestartedFromEvent (event.GetLLDBObjectPtr()); +} + +SBProcess +SBProcess::GetProcessFromEvent (const SBEvent &event) +{ + SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.GetLLDBObjectPtr())); + return process; +} + + +SBBroadcaster +SBProcess::GetBroadcaster () const +{ + SBBroadcaster broadcaster(m_lldb_object_sp.get(), false); + return broadcaster; +} + +lldb_private::Process * +SBProcess::operator->() const +{ + return m_lldb_object_sp.get(); +} + +size_t +SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error) +{ + size_t bytes_read = 0; + + if (IsValid()) + { + Error error; + bytes_read = m_lldb_object_sp->ReadMemory (addr, dst, dst_len, error); + sb_error.SetError (error); + } + else + { + sb_error.SetErrorString ("SBProcess is invalid"); + } + + return bytes_read; +} + +size_t +SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error) +{ + size_t bytes_written = 0; + + if (IsValid()) + { + Error error; + bytes_written = m_lldb_object_sp->WriteMemory (addr, src, src_len, error); + sb_error.SetError (error); + } + + return bytes_written; +} + +// Mimic shared pointer... +lldb_private::Process * +SBProcess::get() const +{ + return m_lldb_object_sp.get(); +} + diff --git a/lldb/source/API/SBSourceManager.cpp b/lldb/source/API/SBSourceManager.cpp new file mode 100644 index 00000000000..f2dcf6525ff --- /dev/null +++ b/lldb/source/API/SBSourceManager.cpp @@ -0,0 +1,65 @@ +//===-- SBSourceManager.cpp -------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "SBSourceManager.h" + +#include "lldb/API/SBFileSpec.h" +#include "lldb/Core/Stream.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Core/SourceManager.h" + + +using namespace lldb; +using namespace lldb_private; + + +SBSourceManager::SBSourceManager (SourceManager& source_manager) : + m_source_manager (source_manager) +{ +} + +SBSourceManager::~SBSourceManager() +{ +} + +size_t +SBSourceManager::DisplaySourceLinesWithLineNumbers +( + const SBFileSpec &file, + uint32_t line, + uint32_t context_before, + uint32_t context_after, + const char* current_line_cstr, + FILE *f +) +{ + if (f == NULL) + return 0; + + if (file.IsValid()) + { + StreamFile str (f); + + + return m_source_manager.DisplaySourceLinesWithLineNumbers (*file, + line, + context_before, + context_after, + current_line_cstr, + &str); + } + return 0; +} + +SourceManager & +SBSourceManager::GetLLDBManager () +{ + return m_source_manager; +} diff --git a/lldb/source/API/SBStringList.cpp b/lldb/source/API/SBStringList.cpp new file mode 100644 index 00000000000..b4cfb9b0c75 --- /dev/null +++ b/lldb/source/API/SBStringList.cpp @@ -0,0 +1,134 @@ +//===-- SBStringList.cpp ----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBStringList.h" + +#include "lldb/Core/StringList.h" + +using namespace lldb; +using namespace lldb_private; + +SBStringList::SBStringList () : + m_lldb_object_ap () +{ +} + +SBStringList::SBStringList (const lldb_private::StringList *lldb_strings_ptr) : + m_lldb_object_ap () +{ + if (lldb_strings_ptr) + m_lldb_object_ap.reset (new lldb_private::StringList (*lldb_strings_ptr)); +} + +SBStringList::SBStringList (const SBStringList &rhs) : + m_lldb_object_ap () +{ + if (rhs.IsValid()) + m_lldb_object_ap.reset (new lldb_private::StringList(*rhs)); +} + + + +SBStringList::~SBStringList () +{ +} + + +const SBStringList & +SBStringList::operator = (const SBStringList &rhs) +{ + if (rhs.IsValid()) + m_lldb_object_ap.reset (new lldb_private::StringList(*rhs)); + + return *this; +} + +const lldb_private::StringList * +SBStringList::operator->() const +{ + return m_lldb_object_ap.get(); +} + +const lldb_private::StringList & +SBStringList::operator*() const +{ + return *m_lldb_object_ap; +} + +bool +SBStringList::IsValid() const +{ + return (m_lldb_object_ap.get() != NULL); +} + +void +SBStringList::AppendString (const char *str) +{ + if (str != NULL) + { + if (IsValid()) + m_lldb_object_ap->AppendString (str); + else + m_lldb_object_ap.reset (new lldb_private::StringList (str)); + } + +} + +void +SBStringList::AppendList (const char **strv, int strc) +{ + if ((strv != NULL) + && (strc > 0)) + { + if (IsValid()) + m_lldb_object_ap->AppendList (strv, strc); + else + m_lldb_object_ap.reset (new lldb_private::StringList (strv, strc)); + } +} + +void +SBStringList::AppendList (SBStringList strings) +{ + if (strings.IsValid()) + { + if (! IsValid()) + m_lldb_object_ap.reset (new lldb_private::StringList()); + m_lldb_object_ap->AppendList (*(strings.m_lldb_object_ap)); + } +} + +uint32_t +SBStringList::GetSize () const +{ + if (IsValid()) + { + return m_lldb_object_ap->GetSize(); + } + return 0; +} + +const char * +SBStringList::GetStringAtIndex (size_t idx) +{ + if (IsValid()) + { + return m_lldb_object_ap->GetStringAtIndex (idx); + } + return NULL; +} + +void +SBStringList::Clear () +{ + if (IsValid()) + { + m_lldb_object_ap->Clear(); + } +} diff --git a/lldb/source/API/SBSymbol.cpp b/lldb/source/API/SBSymbol.cpp new file mode 100644 index 00000000000..8500c8bd8a6 --- /dev/null +++ b/lldb/source/API/SBSymbol.cpp @@ -0,0 +1,64 @@ +//===-- SBSymbol.cpp --------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBSymbol.h" +#include "lldb/Symbol/Symbol.h" + +using namespace lldb; + + +SBSymbol::SBSymbol () : + m_lldb_object_ptr (NULL) +{ +} + +SBSymbol::SBSymbol (lldb_private::Symbol *lldb_object_ptr) : + m_lldb_object_ptr (lldb_object_ptr) +{ +} + +SBSymbol::~SBSymbol () +{ + m_lldb_object_ptr = NULL; +} + +bool +SBSymbol::IsValid () const +{ + return m_lldb_object_ptr != NULL; +} + +const char * +SBSymbol::GetName() const +{ + if (m_lldb_object_ptr) + return m_lldb_object_ptr->GetMangled().GetName().AsCString(); + return NULL; +} + +const char * +SBSymbol::GetMangledName () const +{ + if (m_lldb_object_ptr) + return m_lldb_object_ptr->GetMangled().GetMangledName().AsCString(); + return NULL; +} + + +bool +SBSymbol::operator == (const SBSymbol &rhs) const +{ + return m_lldb_object_ptr == rhs.m_lldb_object_ptr; +} + +bool +SBSymbol::operator != (const SBSymbol &rhs) const +{ + return m_lldb_object_ptr != rhs.m_lldb_object_ptr; +} diff --git a/lldb/source/API/SBSymbolContext.cpp b/lldb/source/API/SBSymbolContext.cpp new file mode 100644 index 00000000000..dec85295213 --- /dev/null +++ b/lldb/source/API/SBSymbolContext.cpp @@ -0,0 +1,133 @@ +//===-- SBSymbolContext.cpp -------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBSymbolContext.h" +#include "lldb/Symbol/SymbolContext.h" + +using namespace lldb; +using namespace lldb_private; + + + +SBSymbolContext::SBSymbolContext () : + m_lldb_object_ap () +{ +} + +SBSymbolContext::SBSymbolContext (const SymbolContext *sc_ptr) : + m_lldb_object_ap () +{ + if (sc_ptr) + m_lldb_object_ap.reset (new SymbolContext (*sc_ptr)); +} + +SBSymbolContext::SBSymbolContext (const SBSymbolContext& rhs) : + m_lldb_object_ap () +{ + if (rhs.IsValid()) + *m_lldb_object_ap = *rhs.m_lldb_object_ap; +} + +SBSymbolContext::~SBSymbolContext () +{ +} + +const SBSymbolContext & +SBSymbolContext::operator = (const SBSymbolContext &rhs) +{ + if (this != &rhs) + { + if (rhs.IsValid()) + m_lldb_object_ap.reset (new lldb_private::SymbolContext(*rhs.m_lldb_object_ap.get())); + } + return *this; +} + +void +SBSymbolContext::SetSymbolContext (const SymbolContext *sc_ptr) +{ + if (sc_ptr) + { + if (m_lldb_object_ap.get()) + *m_lldb_object_ap = *sc_ptr; + else + m_lldb_object_ap.reset (new SymbolContext (*sc_ptr)); + } + else + { + if (m_lldb_object_ap.get()) + m_lldb_object_ap->Clear(); + } +} + +bool +SBSymbolContext::IsValid () const +{ + return m_lldb_object_ap.get() != NULL; +} + + + +SBModule +SBSymbolContext::GetModule () +{ + SBModule sb_module; + if (m_lldb_object_ap.get()) + sb_module.SetModule(m_lldb_object_ap->module_sp); + return sb_module; +} + +SBCompileUnit +SBSymbolContext::GetCompileUnit () +{ + return SBCompileUnit (m_lldb_object_ap.get() ? m_lldb_object_ap->comp_unit : NULL); +} + +SBFunction +SBSymbolContext::GetFunction () +{ + return SBFunction (m_lldb_object_ap.get() ? m_lldb_object_ap->function : NULL); +} + +SBBlock +SBSymbolContext::GetBlock () +{ + return SBBlock (m_lldb_object_ap.get() ? m_lldb_object_ap->block : NULL); +} + +SBLineEntry +SBSymbolContext::GetLineEntry () +{ + SBLineEntry sb_line_entry; + if (m_lldb_object_ap.get()) + sb_line_entry.SetLineEntry (m_lldb_object_ap->line_entry); + + return sb_line_entry; +} + +SBSymbol +SBSymbolContext::GetSymbol () +{ + return SBSymbol (m_lldb_object_ap.get() ? m_lldb_object_ap->symbol : NULL); +} + +lldb_private::SymbolContext* +SBSymbolContext::operator->() const +{ + return m_lldb_object_ap.get(); +} + +lldb_private::SymbolContext * +SBSymbolContext::GetLLDBObjectPtr() const +{ + return m_lldb_object_ap.get(); +} + + + diff --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp new file mode 100644 index 00000000000..799be37e873 --- /dev/null +++ b/lldb/source/API/SBTarget.cpp @@ -0,0 +1,553 @@ +//===-- SBTarget.cpp --------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "SBTarget.h" + +#include "lldb/lldb-include.h" + +#include "lldb/API/SBFileSpec.h" +#include "lldb/API/SBModule.h" +#include "lldb/Breakpoint/BreakpointID.h" +#include "lldb/Breakpoint/BreakpointIDList.h" +#include "lldb/Breakpoint/BreakpointList.h" +#include "lldb/Breakpoint/BreakpointLocation.h" +#include "lldb/Core/Address.h" +#include "lldb/Core/AddressResolver.h" +#include "lldb/Core/AddressResolverName.h" +#include "lldb/Core/Args.h" +#include "lldb/Core/ArchSpec.h" +#include "lldb/Core/Debugger.h" +#include "lldb/Core/Disassembler.h" +#include "lldb/Core/FileSpec.h" +#include "lldb/Core/RegularExpression.h" +#include "lldb/Core/SearchFilter.h" +#include "lldb/Core/STLUtils.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/TargetList.h" + +#include "lldb/Interpreter/CommandReturnObject.h" +#include "../source/Commands/CommandObjectBreakpoint.h" + +#include "SBDebugger.h" +#include "SBProcess.h" +#include "SBListener.h" +#include "SBBreakpoint.h" + +using namespace lldb; +using namespace lldb_private; + +#define DEFAULT_DISASM_BYTE_SIZE 32 + +//---------------------------------------------------------------------- +// SBTarget constructor +//---------------------------------------------------------------------- +SBTarget::SBTarget () +{ +} + +SBTarget::SBTarget (const SBTarget& rhs) : + m_target_sp (rhs.m_target_sp) +{ +} + +SBTarget::SBTarget(const TargetSP& target_sp) : + m_target_sp (target_sp) +{ +} + +const SBTarget& +SBTarget::Assign (const SBTarget& rhs) +{ + if (this != &rhs) + { + m_target_sp = rhs.m_target_sp; + } + return *this; +} + + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +SBTarget::~SBTarget() +{ +} + +bool +SBTarget::IsValid () const +{ + return m_target_sp.get() != NULL; +} + +SBProcess +SBTarget::GetProcess () +{ + SBProcess sb_process; + if (IsValid()) + sb_process.SetProcess (m_target_sp->GetProcessSP()); + return sb_process; +} + +SBProcess +SBTarget::CreateProcess () +{ + SBProcess sb_process; + + if (IsValid()) + { + SBListener sb_listener = SBDebugger::GetListener(); + if (sb_listener.IsValid()) + sb_process.SetProcess (m_target_sp->CreateProcess (*sb_listener)); + } + return sb_process; +} + +SBProcess +SBTarget::LaunchProcess +( + char const **argv, + char const **envp, + const char *tty, + bool stop_at_entry +) +{ + SBProcess process(GetProcess ()); + if (!process.IsValid()) + process = CreateProcess(); + if (process.IsValid()) + { + Error error (process->Launch (argv, envp, tty, tty, tty)); + if (error.Success()) + { + if (!stop_at_entry) + { + StateType state = process->WaitForProcessToStop (NULL); + if (state == eStateStopped) + process->Resume(); + } + } + } + return process; +} + +SBFileSpec +SBTarget::GetExecutable () +{ + SBFileSpec exe_file_spec; + if (IsValid()) + { + ModuleSP exe_module_sp (m_target_sp->GetExecutableModule ()); + if (exe_module_sp) + exe_file_spec.SetFileSpec (exe_module_sp->GetFileSpec()); + } + return exe_file_spec; +} + + +bool +SBTarget::DeleteTargetFromList (TargetList *list) +{ + if (IsValid()) + return list->DeleteTarget (m_target_sp); + else + return false; +} + +bool +SBTarget::MakeCurrentTarget () +{ + if (IsValid()) + { + Debugger::GetSharedInstance().GetTargetList().SetCurrentTarget (m_target_sp.get()); + return true; + } + return false; +} + +bool +SBTarget::operator == (const SBTarget &rhs) const +{ + return m_target_sp.get() == rhs.m_target_sp.get(); +} + +bool +SBTarget::operator != (const SBTarget &rhs) const +{ + return m_target_sp.get() != rhs.m_target_sp.get(); +} + +lldb_private::Target * +SBTarget::GetLLDBObjectPtr() +{ + return m_target_sp.get(); +} +const lldb_private::Target * +SBTarget::GetLLDBObjectPtr() const +{ + return m_target_sp.get(); +} + +SBBreakpoint +SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line) +{ + SBBreakpoint sb_bp; + if (file != NULL && line != 0) + sb_bp = BreakpointCreateByLocation (SBFileSpec (file), line); + return sb_bp; +} + +SBBreakpoint +SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line) +{ + SBBreakpoint sb_bp; + if (m_target_sp.get() && line != 0) + *sb_bp = m_target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false); + return sb_bp; +} + +SBBreakpoint +SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name) +{ + SBBreakpoint sb_bp; + if (m_target_sp.get() && symbol_name && symbol_name[0]) + { + if (module_name && module_name[0]) + { + FileSpec module_file_spec(module_name); + *sb_bp = m_target_sp->CreateBreakpoint (&module_file_spec, symbol_name, false); + } + else + { + *sb_bp = m_target_sp->CreateBreakpoint (NULL, symbol_name, false); + } + } + return sb_bp; +} + +SBBreakpoint +SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name) +{ + SBBreakpoint sb_bp; + if (m_target_sp.get() && symbol_name_regex && symbol_name_regex[0]) + { + RegularExpression regexp(symbol_name_regex); + + if (module_name && module_name[0]) + { + FileSpec module_file_spec(module_name); + + *sb_bp = m_target_sp->CreateBreakpoint (&module_file_spec, regexp, false); + } + else + { + *sb_bp = m_target_sp->CreateBreakpoint (NULL, regexp, false); + } + } + return sb_bp; +} + + + +SBBreakpoint +SBTarget::BreakpointCreateByAddress (addr_t address) +{ + SBBreakpoint sb_bp; + if (m_target_sp.get()) + *sb_bp = m_target_sp->CreateBreakpoint (address, false); + return sb_bp; +} + +void +SBTarget::ListAllBreakpoints () +{ + FILE *out_file = SBDebugger::GetOutputFileHandle(); + + if (out_file == NULL) + return; + + if (IsValid()) + { + const BreakpointList &bp_list = m_target_sp->GetBreakpointList(); + size_t num_bps = bp_list.GetSize(); + for (int i = 0; i < num_bps; ++i) + { + SBBreakpoint sb_breakpoint (bp_list.GetBreakpointByIndex (i)); + sb_breakpoint.GetDescription (out_file, "full"); + } + } +} + +SBBreakpoint +SBTarget::FindBreakpointByID (break_id_t bp_id) +{ + SBBreakpoint sb_breakpoint; + if (m_target_sp && bp_id != LLDB_INVALID_BREAK_ID) + *sb_breakpoint = m_target_sp->GetBreakpointByID (bp_id); + return sb_breakpoint; +} + + +bool +SBTarget::BreakpointDelete (break_id_t bp_id) +{ + if (m_target_sp) + return m_target_sp->RemoveBreakpointByID (bp_id); + return false; +} + +bool +SBTarget::EnableAllBreakpoints () +{ + if (m_target_sp) + { + m_target_sp->EnableAllBreakpoints (); + return true; + } + return false; +} + +bool +SBTarget::DisableAllBreakpoints () +{ + if (m_target_sp) + { + m_target_sp->DisableAllBreakpoints (); + return true; + } + return false; +} + +bool +SBTarget::DeleteAllBreakpoints () +{ + if (m_target_sp) + { + m_target_sp->RemoveAllBreakpoints (); + return true; + } + return false; +} + + +uint32_t +SBTarget::GetNumModules () const +{ + if (m_target_sp) + return m_target_sp->GetImages().GetSize(); + return 0; +} + +SBModule +SBTarget::FindModule (const SBFileSpec &sb_file_spec) +{ + SBModule sb_module; + if (m_target_sp && sb_file_spec.IsValid()) + sb_module.SetModule (m_target_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL)); + return sb_module; +} + +SBModule +SBTarget::GetModuleAtIndex (uint32_t idx) +{ + SBModule sb_module; + if (m_target_sp) + sb_module.SetModule(m_target_sp->GetImages().GetModuleAtIndex(idx)); + return sb_module; +} + + +SBBroadcaster +SBTarget::GetBroadcaster () const +{ + SBBroadcaster broadcaster(m_target_sp.get(), false); + return broadcaster; +} + +void +SBTarget::Disassemble (lldb::addr_t file_address_start, lldb::addr_t file_address_end, const char *module_name) +{ + if (file_address_start == LLDB_INVALID_ADDRESS) + return; + + FILE *out = SBDebugger::GetOutputFileHandle(); + if (out == NULL) + return; + + if (IsValid()) + { + SBModule module; + if (module_name != NULL) + { + SBFileSpec file_spec (module_name); + module = FindModule (file_spec); + } + ArchSpec arch (m_target_sp->GetArchitecture()); + if (!arch.IsValid()) + return; + Disassembler *disassembler = Disassembler::FindPlugin (arch); + if (disassembler == NULL) + return; + + // For now, we need a process; the disassembly functions insist. If we don't have one already, + // make one. + + SBProcess process = GetProcess(); + if (! process.IsValid()) + process = CreateProcess(); + + ExecutionContext exe_context (process.get()); + + if (file_address_end == LLDB_INVALID_ADDRESS + || file_address_end < file_address_start) + file_address_end = file_address_start + DEFAULT_DISASM_BYTE_SIZE; + + // TO BE FIXED: SOMEHOW WE NEED TO SPECIFY/USE THE MODULE, IF THE USER SPECIFIED ONE. I'M NOT + // SURE HOW TO DO THAT AT THE MOMENT. WE ALSO NEED TO FIGURE OUT WHAT TO DO IF THERE ARE MULTIPLE + // MODULES CONTAINING THE SPECIFIED ADDRESSES (E.G. THEY HAVEN'T ALL LOADED & BEEN GIVEN UNIQUE + // ADDRESSES YET). + + DataExtractor data; + size_t bytes_disassembled = disassembler->ParseInstructions (&exe_context, eAddressTypeLoad, + file_address_start, + file_address_end - file_address_start, data); + + if (bytes_disassembled > 0) + { + size_t num_instructions = disassembler->GetInstructionList().GetSize(); + uint32_t offset = 0; + StreamFile out_stream (out); + + for (size_t i = 0; i < num_instructions; ++i) + { + Disassembler::Instruction *inst = disassembler->GetInstructionList().GetInstructionAtIndex (i); + if (inst) + { + lldb::addr_t cur_addr = file_address_start + offset; + size_t inst_byte_size = inst->GetByteSize(); + inst->Dump (&out_stream, cur_addr, &data, offset, exe_context, false); + out_stream.EOL(); + offset += inst_byte_size; + } + } + } + } +} + +void +SBTarget::Disassemble (const char *function_name, const char *module_name) +{ + if (function_name == NULL) + return; + + FILE *out = SBDebugger::GetOutputFileHandle(); + if (out == NULL) + return; + + if (IsValid()) + { + SBModule module; + + if (module_name != NULL) + { + SBFileSpec file_spec (module_name); + module = FindModule (file_spec); + } + + ArchSpec arch (m_target_sp->GetArchitecture()); + if (!arch.IsValid()) + return; + + Disassembler *disassembler = Disassembler::FindPlugin (arch); + if (disassembler == NULL) + return; + + // For now, we need a process; the disassembly functions insist. If we don't have one already, + // make one. + + SBProcess process = GetProcess(); + if (! process.IsValid() + || process.GetProcessID() == 0) + { + fprintf (out, "Cannot disassemble functions until after process has launched.\n"); + return; + } + + ExecutionContext exe_context (process.get()); + + FileSpec *containing_module = NULL; + + if (module_name != NULL) + containing_module = new FileSpec (module_name); + + SearchFilterSP filter_sp (m_target_sp->GetSearchFilterForModule (containing_module)); + AddressResolverSP resolver_sp (new AddressResolverName (function_name)); + + resolver_sp->ResolveAddress (*filter_sp); + + size_t num_matches_found = resolver_sp->GetNumberOfAddresses(); + + if (num_matches_found == 1) + { + DataExtractor data; + + AddressRange func_addresses = resolver_sp->GetAddressRangeAtIndex (0); + Address start_addr = func_addresses.GetBaseAddress(); + lldb::addr_t num_bytes = func_addresses.GetByteSize(); + + lldb::addr_t addr = LLDB_INVALID_ADDRESS; + size_t bytes_disassembled = 0; + + + if (process.GetProcessID() == 0) + { + // Leave this branch in for now, but it should not be reached, since we exit above if the PID is 0. + addr = start_addr.GetFileAddress (); + bytes_disassembled = disassembler->ParseInstructions (&exe_context, eAddressTypeFile, addr, + num_bytes, data); + + } + else + { + addr = start_addr.GetLoadAddress (process.get()); + bytes_disassembled = disassembler->ParseInstructions (&exe_context, eAddressTypeLoad, addr, + num_bytes, data); + + } + + if (bytes_disassembled > 0) + { + size_t num_instructions = disassembler->GetInstructionList().GetSize(); + uint32_t offset = 0; + StreamFile out_stream (out); + + for (size_t i = 0; i < num_instructions; ++i) + { + Disassembler::Instruction *inst = disassembler->GetInstructionList().GetInstructionAtIndex (i); + if (inst) + { + lldb::addr_t cur_addr = addr + offset; + size_t inst_byte_size = inst->GetByteSize(); + inst->Dump (&out_stream, cur_addr, &data, offset, exe_context, false); + out_stream.EOL(); + offset += inst_byte_size; + } + } + } + } + else if (num_matches_found > 1) + { + // TO BE FIXED: Eventually we want to list/disassemble all functions found. + fprintf (out, "Function '%s' was found in multiple modules; please specify the desired module name.\n", + function_name); + } + else + fprintf (out, "Function '%s' was not found.\n", function_name); + } +} diff --git a/lldb/source/API/SBThread.cpp b/lldb/source/API/SBThread.cpp new file mode 100644 index 00000000000..4ae9ba17525 --- /dev/null +++ b/lldb/source/API/SBThread.cpp @@ -0,0 +1,551 @@ +//===-- SBThread.cpp --------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "SBThread.h" + +#include "lldb/API/SBSymbolContext.h" +#include "lldb/API/SBFileSpec.h" +#include "lldb/Core/Stream.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Target/Thread.h" +#include "lldb/Target/Process.h" +#include "lldb/Symbol/SymbolContext.h" +#include "lldb/Symbol/CompileUnit.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/ThreadPlan.h" +#include "lldb/Target/ThreadPlanContinue.h" +#include "lldb/Target/ThreadPlanStepInstruction.h" +#include "lldb/Target/ThreadPlanStepOut.h" +#include "lldb/Target/ThreadPlanStepRange.h" +#include "lldb/Target/ThreadPlanStepInRange.h" + + +#include "SBAddress.h" +#include "SBFrame.h" +#include "SBSourceManager.h" +#include "SBDebugger.h" +#include "SBProcess.h" + +using namespace lldb; +using namespace lldb_private; + +SBThread::SBThread () : + m_lldb_object_sp () +{ +} + +//---------------------------------------------------------------------- +// Thread constructor +//---------------------------------------------------------------------- +SBThread::SBThread (const ThreadSP& lldb_object_sp) : + m_lldb_object_sp (lldb_object_sp) +{ +} + +SBThread::SBThread (const SBThread &rhs) +{ + m_lldb_object_sp = rhs.m_lldb_object_sp; +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +SBThread::~SBThread() +{ +} + +bool +SBThread::IsValid() const +{ + return m_lldb_object_sp != NULL; +} + +StopReason +SBThread::GetStopReason() +{ + if (m_lldb_object_sp) + { + lldb_private::Thread::StopInfo thread_stop_info; + if (m_lldb_object_sp->GetStopInfo(&thread_stop_info)) + return thread_stop_info.GetStopReason(); + } + return eStopReasonInvalid; +} + +size_t +SBThread::GetStopDescription (char *dst, size_t dst_len) +{ + if (m_lldb_object_sp) + { + lldb_private::Thread::StopInfo thread_stop_info; + if (m_lldb_object_sp->GetStopInfo(&thread_stop_info)) + { + const char *stop_desc = thread_stop_info.GetStopDescription(); + if (stop_desc) + { + if (dst) + return ::snprintf (dst, dst_len, "%s", stop_desc); + else + { + // NULL dst passed in, return the length needed to contain the description + return ::strlen (stop_desc) + 1; // Include the NULL byte for size + } + } + else + { + const char *stop_desc = NULL; + size_t stop_desc_len = 0; + switch (thread_stop_info.GetStopReason()) + { + case eStopReasonTrace: + case eStopReasonPlanComplete: + { + static char trace_desc[] = "step"; + stop_desc = trace_desc; + stop_desc_len = sizeof(trace_desc); // Include the NULL byte for size + } + break; + + case eStopReasonBreakpoint: + { + static char bp_desc[] = "breakpoint hit"; + stop_desc = bp_desc; + stop_desc_len = sizeof(bp_desc); // Include the NULL byte for size + } + break; + + case eStopReasonWatchpoint: + { + static char wp_desc[] = "watchpoint hit"; + stop_desc = wp_desc; + stop_desc_len = sizeof(wp_desc); // Include the NULL byte for size + } + break; + + case eStopReasonSignal: + { + stop_desc = m_lldb_object_sp->GetProcess().GetUnixSignals ().GetSignalAsCString (thread_stop_info.GetSignal()); + if (stop_desc == NULL || stop_desc[0] == '\0') + { + static char signal_desc[] = "signal"; + stop_desc = signal_desc; + stop_desc_len = sizeof(signal_desc); // Include the NULL byte for size + } + } + break; + + case eStopReasonException: + { + char exc_desc[] = "exception"; + stop_desc = exc_desc; + stop_desc_len = sizeof(exc_desc); // Include the NULL byte for size + } + break; + } + + if (stop_desc && stop_desc[0]) + { + if (dst) + return ::snprintf (dst, dst_len, "%s", stop_desc) + 1; // Include the NULL byte + + if (stop_desc_len == 0) + stop_desc_len = ::strlen (stop_desc) + 1; // Include the NULL byte + + return stop_desc_len; + } + } + } + } + if (dst) + *dst = 0; + return 0; +} + +void +SBThread::SetThread (const ThreadSP& lldb_object_sp) +{ + m_lldb_object_sp = lldb_object_sp; +} + + +lldb::tid_t +SBThread::GetThreadID () const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetID(); + else + return LLDB_INVALID_THREAD_ID; +} + +uint32_t +SBThread::GetIndexID () const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetIndexID(); + return LLDB_INVALID_INDEX32; +} +const char * +SBThread::GetName () const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetName(); + return NULL; +} + +const char * +SBThread::GetQueueName () const +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetQueueName(); + return NULL; +} + + +void +SBThread::DisplayFramesForCurrentContext (FILE *out, + FILE *err, + uint32_t first_frame, + uint32_t num_frames, + bool show_frame_info, + uint32_t num_frames_with_source, + uint32_t source_lines_before, + uint32_t source_lines_after) +{ + if ((out == NULL) || (err == NULL)) + return; + + if (m_lldb_object_sp) + { + uint32_t num_stack_frames = m_lldb_object_sp->GetStackFrameCount (); + StackFrameSP frame_sp; + int frame_idx = 0; + + for (frame_idx = first_frame; frame_idx < first_frame + num_frames; ++frame_idx) + { + if (frame_idx >= num_stack_frames) + break; + + frame_sp = m_lldb_object_sp->GetStackFrameAtIndex (frame_idx); + if (!frame_sp) + break; + + SBFrame sb_frame (frame_sp); + if (DisplaySingleFrameForCurrentContext (out, + err, + sb_frame, + show_frame_info, + num_frames_with_source > first_frame - frame_idx, + source_lines_before, + source_lines_after) == false) + break; + } + } +} + +bool +SBThread::DisplaySingleFrameForCurrentContext (FILE *out, + FILE *err, + SBFrame &frame, + bool show_frame_info, + bool show_source, + uint32_t source_lines_after, + uint32_t source_lines_before) +{ + bool success = false; + + if ((out == NULL) || (err == NULL)) + return false; + + if (m_lldb_object_sp && frame.IsValid()) + { + + StreamFile str (out); + + SBSymbolContext sc(frame.GetSymbolContext(eSymbolContextEverything)); + + if (show_frame_info && sc.IsValid()) + { + user_id_t frame_idx = (user_id_t) frame.GetFrameID(); + lldb::addr_t pc = frame.GetPC(); + ::fprintf (out, + " frame #%u: tid = 0x%4.4x, pc = 0x%llx ", + frame_idx, + GetThreadID(), + pc); + sc->DumpStopContext (&str, &m_lldb_object_sp->GetProcess(), *frame.GetPCAddress()); + fprintf (out, "\n"); + success = true; + } + + SBCompileUnit comp_unit(sc.GetCompileUnit()); + if (show_source && comp_unit.IsValid()) + { + success = false; + SBLineEntry line_entry; + if (line_entry.IsValid()) + { + SBSourceManager& source_manager = SBDebugger::GetSourceManager(); + SBFileSpec line_entry_file_spec = line_entry.GetFileSpec(); + + if (line_entry_file_spec.IsValid()) + { + source_manager.DisplaySourceLinesWithLineNumbers (line_entry_file_spec, + line_entry.GetLine(), + source_lines_after, + source_lines_before, "->", + out); + success = true; + } + } + } + } + return success; +} + +void +SBThread::StepOver (lldb::RunMode stop_other_threads) +{ + if (m_lldb_object_sp) + { + bool abort_other_plans = true; + StackFrameSP frame_sp(m_lldb_object_sp->GetStackFrameAtIndex (0)); + + if (frame_sp) + { + if (frame_sp->HasDebugInformation ()) + { + SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything)); + m_lldb_object_sp->QueueThreadPlanForStepRange (abort_other_plans, + eStepTypeOver, + sc.line_entry.range, + sc, + stop_other_threads); + + } + else + { + m_lldb_object_sp->QueueThreadPlanForStepSingleInstruction (true, + abort_other_plans, + stop_other_threads); + } + } + + Process &process = m_lldb_object_sp->GetProcess(); + // Why do we need to set the current thread by ID here??? + process.GetThreadList().SetCurrentThreadByID (m_lldb_object_sp->GetID()); + process.Resume(); + } +} + +void +SBThread::StepInto (lldb::RunMode stop_other_threads) +{ + if (m_lldb_object_sp) + { + bool abort_other_plans = true; + + StackFrameSP frame_sp(m_lldb_object_sp->GetStackFrameAtIndex (0)); + + if (frame_sp && frame_sp->HasDebugInformation ()) + { + SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything)); + ThreadPlan *new_plan = m_lldb_object_sp->QueueThreadPlanForStepRange (abort_other_plans, + eStepTypeInto, + sc.line_entry.range, + sc, + stop_other_threads); + if (new_plan) + { + ThreadPlanStepInRange *real_plan = dynamic_cast<ThreadPlanStepInRange *> (new_plan); + if (real_plan) + { + bool avoid_no_debug = true; + if (avoid_no_debug) + real_plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug); + else + real_plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug); + } + } + } + else + { + m_lldb_object_sp->QueueThreadPlanForStepSingleInstruction (false, + abort_other_plans, + stop_other_threads); + } + + Process &process = m_lldb_object_sp->GetProcess(); + // Why do we need to set the current thread by ID here??? + process.GetThreadList().SetCurrentThreadByID (m_lldb_object_sp->GetID()); + process.Resume(); + + } +} + +void +SBThread::StepOut () +{ + if (m_lldb_object_sp) + { + bool abort_other_plans = true; + bool stop_other_threads = true; + + m_lldb_object_sp->QueueThreadPlanForStepOut (abort_other_plans, NULL, false, stop_other_threads, eVoteYes, eVoteNoOpinion); + + Process &process = m_lldb_object_sp->GetProcess(); + process.GetThreadList().SetCurrentThreadByID (m_lldb_object_sp->GetID()); + process.Resume(); + } +} + +void +SBThread::StepInstruction (bool step_over) +{ + if (m_lldb_object_sp) + { + m_lldb_object_sp->QueueThreadPlanForStepSingleInstruction (step_over, true, true); + Process &process = m_lldb_object_sp->GetProcess(); + process.GetThreadList().SetCurrentThreadByID (m_lldb_object_sp->GetID()); + process.Resume(); + } +} + +void +SBThread::RunToAddress (lldb::addr_t addr) +{ + if (m_lldb_object_sp) + { + bool abort_other_plans = true; + bool stop_other_threads = true; + + Address target_addr (NULL, addr); + + m_lldb_object_sp->QueueThreadPlanForRunToAddress (abort_other_plans, target_addr, stop_other_threads); + Process &process = m_lldb_object_sp->GetProcess(); + process.GetThreadList().SetCurrentThreadByID (m_lldb_object_sp->GetID()); + process.Resume(); + } + +} + +void +SBThread::Backtrace (uint32_t num_frames) +{ + bool all_frames = false; + if (num_frames < 1) + all_frames = true; + + FILE *out = SBDebugger::GetOutputFileHandle(); + FILE *err = SBDebugger::GetErrorFileHandle(); + + if ((out == NULL) || (err == NULL)) + return; + + if (m_lldb_object_sp) + { + if (out && err) + { + int max_num_frames = m_lldb_object_sp->GetStackFrameCount(); + int last_frame = max_num_frames; + + if (!all_frames && (num_frames < last_frame)) + last_frame = num_frames; + + StackFrameSP frame_sp; + for (int i = 0; i < last_frame; ++i) + { + frame_sp = m_lldb_object_sp->GetStackFrameAtIndex (i); + if (!frame_sp) + break; + + SBFrame sb_frame (frame_sp); + if (DisplaySingleFrameForCurrentContext ((FILE *) out, (FILE *) err, sb_frame, true, false, 0, 0) == false) + break; + } + } + } +} + +SBProcess +SBThread::GetProcess () +{ + SBProcess process; + if (m_lldb_object_sp) + { + // Have to go up to the target so we can get a shared pointer to our process... + process.SetProcess(m_lldb_object_sp->GetProcess().GetTarget().GetProcessSP()); + } + return process; +} + +uint32_t +SBThread::GetNumFrames () +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetStackFrameCount(); + return 0; +} + +SBFrame +SBThread::GetFrameAtIndex (uint32_t idx) +{ + SBFrame sb_frame; + if (m_lldb_object_sp) + sb_frame.SetFrame (m_lldb_object_sp->GetStackFrameAtIndex (idx)); + return sb_frame; +} + +const lldb::SBThread & +SBThread::operator = (const lldb::SBThread &rhs) +{ + m_lldb_object_sp = rhs.m_lldb_object_sp; + return *this; +} + +bool +SBThread::operator == (const SBThread &rhs) const +{ + return m_lldb_object_sp.get() == rhs.m_lldb_object_sp.get(); +} + +bool +SBThread::operator != (const SBThread &rhs) const +{ + return m_lldb_object_sp.get() != rhs.m_lldb_object_sp.get(); +} + +lldb_private::Thread * +SBThread::GetLLDBObjectPtr () +{ + return m_lldb_object_sp.get(); +} + +const lldb_private::Thread * +SBThread::operator->() const +{ + return m_lldb_object_sp.get(); +} + +const lldb_private::Thread & +SBThread::operator*() const +{ + return *m_lldb_object_sp; +} + +lldb_private::Thread * +SBThread::operator->() +{ + return m_lldb_object_sp.get(); +} + +lldb_private::Thread & +SBThread::operator*() +{ + return *m_lldb_object_sp; +} diff --git a/lldb/source/API/SBType.cpp b/lldb/source/API/SBType.cpp new file mode 100644 index 00000000000..d8e836e3a8c --- /dev/null +++ b/lldb/source/API/SBType.cpp @@ -0,0 +1,23 @@ +//===-- SBType.cpp ----------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBType.h" +#include "lldb/Symbol/ClangASTContext.h" + +using namespace lldb; +using namespace lldb_private; + + +bool +SBType::IsPointerType (void *opaque_type) +{ + return ClangASTContext::IsPointerType (opaque_type); +} + + diff --git a/lldb/source/API/SBValue.cpp b/lldb/source/API/SBValue.cpp new file mode 100644 index 00000000000..21f9a0eed94 --- /dev/null +++ b/lldb/source/API/SBValue.cpp @@ -0,0 +1,372 @@ +//===-- SBValue.cpp ---------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "SBValue.h" + +#include "lldb/Core/DataExtractor.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/Stream.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Core/Value.h" +#include "lldb/Core/ValueObject.h" +#include "lldb/Symbol/Block.h" +#include "lldb/Symbol/ObjectFile.h" +#include "lldb/Symbol/Variable.h" +#include "lldb/Target/ExecutionContext.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/StackFrame.h" +#include "lldb/Target/Thread.h" + +#include "SBProcess.h" +#include "SBTarget.h" +#include "SBThread.h" +#include "SBFrame.h" +#include "SBDebugger.h" + +using namespace lldb; +using namespace lldb_private; + +SBValue::SBValue () : + m_lldb_object_sp () +{ +} + +SBValue::SBValue (const lldb::ValueObjectSP &value_sp) : + m_lldb_object_sp (value_sp) +{ +} + +SBValue::~SBValue() +{ +} + +bool +SBValue::IsValid () const +{ + return (m_lldb_object_sp.get() != NULL); +} + +void +SBValue::Print (FILE *out_file, SBFrame *frame, bool print_type, bool print_value) +{ + if (out_file == NULL) + return; + + if (IsValid()) + { + + SBThread sb_thread = frame->GetThread(); + SBProcess sb_process = sb_thread.GetProcess(); + + lldb_private::StackFrame *lldb_frame = frame->GetLLDBObjectPtr(); + lldb_private::Thread *lldb_thread = sb_thread.GetLLDBObjectPtr(); + lldb_private::Process *lldb_process = sb_process.get(); + + lldb_private::ExecutionContext context (lldb_process, lldb_thread, lldb_frame); + + lldb_private::StreamFile out_stream (out_file); + + out_stream.Printf ("%s ", m_lldb_object_sp->GetName().AsCString (NULL)); + if (! m_lldb_object_sp->IsInScope (lldb_frame)) + out_stream.Printf ("[out-of-scope] "); + if (print_type) + { + out_stream.Printf ("(%s) ", m_lldb_object_sp->GetTypeName().AsCString ("<unknown-type>")); + } + + if (print_value) + { + ExecutionContextScope *exe_scope = frame->get(); + const char *val_cstr = m_lldb_object_sp->GetValueAsCString(exe_scope); + const char *err_cstr = m_lldb_object_sp->GetError().AsCString(); + + if (!err_cstr) + { + const char *sum_cstr = m_lldb_object_sp->GetSummaryAsCString(exe_scope); + const bool is_aggregate = + ClangASTContext::IsAggregateType (m_lldb_object_sp->GetOpaqueClangQualType()); + if (val_cstr) + out_stream.Printf ("= %s ", val_cstr); + + if (sum_cstr) + out_stream.Printf ("%s ", sum_cstr); + + if (is_aggregate) + { + out_stream.PutChar ('{'); + const uint32_t num_children = m_lldb_object_sp->GetNumChildren(); + if (num_children) + { + out_stream.IndentMore(); + for (uint32_t idx = 0; idx < num_children; ++idx) + { + lldb::ValueObjectSP child_sp (m_lldb_object_sp->GetChildAtIndex (idx, true)); + if (child_sp.get()) + { + out_stream.EOL(); + out_stream.Indent(); + out_stream.Printf ("%s (%s) = %s", child_sp.get()->GetName().AsCString (""), + child_sp.get()->GetTypeName().AsCString ("<unknown type>"), + child_sp.get()->GetValueAsCString(exe_scope)); + } + } + out_stream.IndentLess(); + } + out_stream.EOL(); + out_stream.Indent ("}"); + } + } + } + out_stream.EOL (); + } +} + +const char * +SBValue::GetName() +{ + if (IsValid()) + return m_lldb_object_sp->GetName().AsCString(); + else + return NULL; +} + +const char * +SBValue::GetTypeName () +{ + if (IsValid()) + return m_lldb_object_sp->GetTypeName().AsCString(); + else + return NULL; +} + +size_t +SBValue::GetByteSize () +{ + size_t result = 0; + + if (IsValid()) + result = m_lldb_object_sp->GetByteSize(); + + return result; +} + +bool +SBValue::IsInScope (const SBFrame &frame) +{ + bool result = false; + + if (IsValid()) + result = m_lldb_object_sp->IsInScope (frame.get()); + + return result; +} + +const char * +SBValue::GetValue (const SBFrame &frame) +{ + const char *value_string = NULL; + if ( m_lldb_object_sp) + value_string = m_lldb_object_sp->GetValueAsCString(frame.get()); + return value_string; +} + +bool +SBValue::GetValueDidChange () +{ + if (IsValid()) + return m_lldb_object_sp->GetValueDidChange(); + return false; +} + +const char * +SBValue::GetSummary (const SBFrame &frame) +{ + const char *value_string = NULL; + if ( m_lldb_object_sp) + value_string = m_lldb_object_sp->GetSummaryAsCString(frame.get()); + return value_string; +} + +const char * +SBValue::GetLocation (const SBFrame &frame) +{ + const char *value_string = NULL; + if (IsValid()) + value_string = m_lldb_object_sp->GetLocationAsCString(frame.get()); + return value_string; +} + +bool +SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str) +{ + bool success = false; + if (IsValid()) + success = m_lldb_object_sp->SetValueFromCString (frame.get(), value_str); + return success; +} + +SBValue +SBValue::GetChildAtIndex (uint32_t idx) +{ + lldb::ValueObjectSP child_sp; + + if (IsValid()) + { + child_sp = m_lldb_object_sp->GetChildAtIndex (idx, true); + } + + SBValue sb_value (child_sp); + return sb_value; +} + +uint32_t +SBValue::GetIndexOfChildWithName (const char *name) +{ + if (IsValid()) + return m_lldb_object_sp->GetIndexOfChildWithName (ConstString(name)); + return UINT32_MAX; +} + +SBValue +SBValue::GetChildMemberWithName (const char *name) +{ + lldb::ValueObjectSP child_sp; + const ConstString str_name (name); + + if (IsValid()) + { + child_sp = m_lldb_object_sp->GetChildMemberWithName (str_name, true); + } + + SBValue sb_value (child_sp); + return sb_value; +} + + +uint32_t +SBValue::GetNumChildren () +{ + uint32_t num_children = 0; + + if (IsValid()) + { + num_children = m_lldb_object_sp->GetNumChildren(); + } + + return num_children; +} + +bool +SBValue::ValueIsStale () +{ + bool result = true; + + if (IsValid()) + { + result = m_lldb_object_sp->GetValueIsValid(); + } + + return result; +} + + +SBValue +SBValue::Dereference () +{ + if (IsValid()) + { + if (m_lldb_object_sp->IsPointerType()) + { + return GetChildAtIndex(0); + } + } + return *this; +} + +bool +SBValue::TypeIsPtrType () +{ + bool is_ptr_type = false; + + if (IsValid()) + { + is_ptr_type = m_lldb_object_sp->IsPointerType(); + } + + return is_ptr_type; +} + + +lldb_private::ExecutionContext +SBValue::GetCurrentExecutionContext () +{ + lldb_private::Process *process = NULL; + lldb_private::Thread *thread = NULL; + lldb_private::StackFrame *frame = NULL; + + SBTarget sb_target = SBDebugger::GetCurrentTarget(); + if (sb_target.IsValid()) + { + SBProcess sb_process = sb_target.GetProcess(); + if (sb_process.IsValid()) + { + process = sb_process.get(); + SBThread sb_thread = sb_process.GetCurrentThread(); + if (sb_thread.IsValid()) + { + thread = sb_thread.GetLLDBObjectPtr(); + frame = thread->GetStackFrameAtIndex(0).get(); + lldb_private::ExecutionContext exe_context (process, thread, frame); + return exe_context; + } + else + { + lldb_private::ExecutionContext exe_context (process, NULL, NULL); + return exe_context; + } + } + } + + lldb_private::ExecutionContext exe_context (NULL, NULL, NULL); + return exe_context; +} + + +void * +SBValue::GetOpaqueType() +{ + if (m_lldb_object_sp) + return m_lldb_object_sp->GetOpaqueClangQualType(); + return NULL; +} + +// Mimic shared pointer... +lldb_private::ValueObject * +SBValue::get() const +{ + return m_lldb_object_sp.get(); +} + +lldb_private::ValueObject * +SBValue::operator->() const +{ + return m_lldb_object_sp.get(); +} + +lldb::ValueObjectSP & +SBValue::operator*() +{ + return m_lldb_object_sp; +} + +const lldb::ValueObjectSP & +SBValue::operator*() const +{ + return m_lldb_object_sp; +} diff --git a/lldb/source/API/SBValueList.cpp b/lldb/source/API/SBValueList.cpp new file mode 100644 index 00000000000..e7cbfad5308 --- /dev/null +++ b/lldb/source/API/SBValueList.cpp @@ -0,0 +1,140 @@ +//===-- SBValueList.cpp -----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "lldb/API/SBValueList.h" +#include "lldb/API/SBValue.h" + +#include "lldb/Core/ValueObjectList.h" + +using namespace lldb; +using namespace lldb_private; + +SBValueList::SBValueList () : + m_lldb_object_ap () +{ +} + +SBValueList::SBValueList (const SBValueList &rhs) : + m_lldb_object_ap () +{ + if (rhs.IsValid()) + m_lldb_object_ap.reset (new lldb_private::ValueObjectList (*rhs)); +} + +SBValueList::SBValueList (const lldb_private::ValueObjectList *lldb_object_ptr) : + m_lldb_object_ap () +{ + if (lldb_object_ptr) + m_lldb_object_ap.reset (new lldb_private::ValueObjectList (*lldb_object_ptr)); +} + +SBValueList::~SBValueList () +{ +} + +bool +SBValueList::IsValid () const +{ + return (m_lldb_object_ap.get() != NULL); +} + +const SBValueList & +SBValueList::operator = (const SBValueList &rhs) +{ + if (this != &rhs) + { + if (rhs.IsValid()) + m_lldb_object_ap.reset (new lldb_private::ValueObjectList (*rhs)); + else + m_lldb_object_ap.reset (); + } + return *this; +} + +lldb_private::ValueObjectList * +SBValueList::operator->() +{ + return m_lldb_object_ap.get(); +} + +lldb_private::ValueObjectList & +SBValueList::operator*() +{ + return *m_lldb_object_ap; +} + +const lldb_private::ValueObjectList * +SBValueList::operator->() const +{ + return m_lldb_object_ap.get(); +} + +const lldb_private::ValueObjectList & +SBValueList::operator*() const +{ + return *m_lldb_object_ap; +} + +void +SBValueList::Append (const SBValue &val_obj) +{ + if (val_obj.get()) + { + CreateIfNeeded (); + m_lldb_object_ap->Append (*val_obj); + } +} + +void +SBValueList::Append (lldb::ValueObjectSP& val_obj_sp) +{ + if (val_obj_sp) + { + CreateIfNeeded (); + m_lldb_object_ap->Append (val_obj_sp); + } +} + + +SBValue +SBValueList::GetValueAtIndex (uint32_t idx) const +{ + SBValue sb_value; + if (m_lldb_object_ap.get()) + *sb_value = m_lldb_object_ap->GetValueObjectAtIndex (idx); + return sb_value; +} + +uint32_t +SBValueList::GetSize () const +{ + uint32_t size = 0; + if (m_lldb_object_ap.get()) + size = m_lldb_object_ap->GetSize(); + return size; +} + +void +SBValueList::CreateIfNeeded () +{ + if (m_lldb_object_ap.get() == NULL) + m_lldb_object_ap.reset (new ValueObjectList()); +} + + +SBValue +SBValueList::FindValueObjectByUID (lldb::user_id_t uid) +{ + SBValue sb_value; + if ( m_lldb_object_ap.get()) + *sb_value = m_lldb_object_ap->FindValueObjectByUID (uid); + return sb_value; +} + |