Jason Molenda 705b180964 Initial merge of some of the iOS 8 / Mac OS X Yosemite specific
lldb support.  I'll be doing more testing & cleanup but I wanted to
get the initial checkin done.

This adds a new SBExpressionOptions::SetLanguage API for selecting a
language of an expression.

I added adds a new SBThread::GetInfoItemByPathString for retriving
information about a thread from that thread's StructuredData.

I added a new StructuredData class for representing
key-value/array/dictionary information (e.g. JSON formatted data).
Helper functions to read JSON and create a StructuredData object,
and to print a StructuredData object in JSON format are included.

A few Cocoa / Cocoa Touch data formatters were updated by Enrico
to track changes in iOS 8 / Yosemite.

Before we query a thread's extended information, the system runtime may 
provide hints to the remote debug stub that it will use to retrieve values
out of runtime structures.  I added a new SystemRuntime method 
AddThreadExtendedInfoPacketHints which allows the SystemRuntime to add 
key-value type data to the initial request that we send to the remote stub.

The thread-format formatter string can now retrieve values out of a thread's
extended info structured data.  The default thread-format string picks up
two of these - thread.info.activity.name and thread.info.trace_messages.

I added a new "jThreadExtendedInfo" packet in debugserver; I will
add documentation to the lldb-gdb-remote.txt doc soon.  It accepts
JSON formatted arguments (most importantly, "thread":threadnum) and
it returns a variety of information regarding the thread to lldb
in JSON format.  This JSON return is scanned into a StructuredData
object that is associated with the thread; UI layers can query the
thread's StructuredData to see if key-values are present, and if
so, show them to the user.  These key-values are likely to be
specific to different targets with some commonality among many
targets.  For instance, many targets will be able to advertise the
pthread_t value for a thread.

I added an initial rough cut of "thread info" command which will print
the information about a thread from the jThreadExtendedInfo result.
I need to do more work to make this format reasonably.

Han Ming added calls into the pmenergy and pmsample libraries if
debugserver is run on Mac OS X Yosemite to get information about the
inferior's power use.

I added support to debugserver for gathering the Genealogy information
about threads, if it exists, and returning it in the jThreadExtendedInfo
JSON result.

llvm-svn: 210874
2014-06-13 02:37:02 +00:00

295 lines
8.8 KiB
C++

//===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "ThreadGDBRemote.h"
#include "lldb/Breakpoint/Watchpoint.h"
#include "lldb/Core/ArchSpec.h"
#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/State.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/StopInfo.h"
#include "lldb/Target/SystemRuntime.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Unwind.h"
#include "ProcessGDBRemote.h"
#include "ProcessGDBRemoteLog.h"
#include "Utility/StringExtractorGDBRemote.h"
using namespace lldb;
using namespace lldb_private;
//----------------------------------------------------------------------
// Thread Registers
//----------------------------------------------------------------------
ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
Thread(process, tid),
m_thread_name (),
m_dispatch_queue_name (),
m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
{
ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
this,
process.GetID(),
GetID());
}
ThreadGDBRemote::~ThreadGDBRemote ()
{
ProcessSP process_sp(GetProcess());
ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
this,
process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID,
GetID());
DestroyThread();
}
const char *
ThreadGDBRemote::GetName ()
{
if (m_thread_name.empty())
return NULL;
return m_thread_name.c_str();
}
const char *
ThreadGDBRemote::GetQueueName ()
{
// Always re-fetch the dispatch queue name since it can change
if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
{
ProcessSP process_sp (GetProcess());
if (process_sp)
{
SystemRuntime *runtime = process_sp->GetSystemRuntime ();
if (runtime)
{
m_dispatch_queue_name = runtime->GetQueueNameFromThreadQAddress (m_thread_dispatch_qaddr);
}
if (m_dispatch_queue_name.length() > 0)
{
return m_dispatch_queue_name.c_str();
}
}
}
return NULL;
}
queue_id_t
ThreadGDBRemote::GetQueueID ()
{
if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
{
ProcessSP process_sp (GetProcess());
if (process_sp)
{
SystemRuntime *runtime = process_sp->GetSystemRuntime ();
if (runtime)
{
return runtime->GetQueueIDFromThreadQAddress (m_thread_dispatch_qaddr);
}
}
}
return LLDB_INVALID_QUEUE_ID;
}
QueueSP
ThreadGDBRemote::GetQueue ()
{
queue_id_t queue_id = GetQueueID();
QueueSP queue;
if (queue_id != LLDB_INVALID_QUEUE_ID)
{
ProcessSP process_sp (GetProcess());
if (process_sp)
{
queue = process_sp->GetQueueList().FindQueueByID (queue_id);
}
}
return queue;
}
addr_t
ThreadGDBRemote::GetQueueLibdispatchQueueAddress ()
{
addr_t dispatch_queue_t_addr = LLDB_INVALID_ADDRESS;
if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
{
ProcessSP process_sp (GetProcess());
if (process_sp)
{
SystemRuntime *runtime = process_sp->GetSystemRuntime ();
if (runtime)
{
dispatch_queue_t_addr = runtime->GetLibdispatchQueueAddressFromThreadQAddress (m_thread_dispatch_qaddr);
}
}
}
return dispatch_queue_t_addr;
}
StructuredData::ObjectSP
ThreadGDBRemote::FetchThreadExtendedInfo ()
{
StructuredData::ObjectSP object_sp;
const lldb::user_id_t tid = GetProtocolID();
Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
if (log)
log->Printf ("Fetching extended information for thread %4.4" PRIx64, tid);
ProcessSP process_sp (GetProcess());
if (process_sp)
{
ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
object_sp = gdb_process->GetExtendedInfoForThread (tid);
}
return object_sp;
}
void
ThreadGDBRemote::WillResume (StateType resume_state)
{
int signo = GetResumeSignal();
const lldb::user_id_t tid = GetProtocolID();
Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
if (log)
log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
ProcessSP process_sp (GetProcess());
if (process_sp)
{
ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
switch (resume_state)
{
case eStateSuspended:
case eStateStopped:
// Don't append anything for threads that should stay stopped.
break;
case eStateRunning:
if (gdb_process->GetUnixSignals().SignalIsValid (signo))
gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
else
gdb_process->m_continue_c_tids.push_back(tid);
break;
case eStateStepping:
if (gdb_process->GetUnixSignals().SignalIsValid (signo))
gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
else
gdb_process->m_continue_s_tids.push_back(tid);
break;
default:
break;
}
}
}
void
ThreadGDBRemote::RefreshStateAfterStop()
{
// Invalidate all registers in our register context. We don't set "force" to
// true because the stop reply packet might have had some register values
// that were expedited and these will already be copied into the register
// context by the time this function gets called. The GDBRemoteRegisterContext
// class has been made smart enough to detect when it needs to invalidate
// which registers are valid by putting hooks in the register read and
// register supply functions where they check the process stop ID and do
// the right thing.
const bool force = false;
GetRegisterContext()->InvalidateIfNeeded (force);
}
bool
ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
{
return thread != 0;
}
void
ThreadGDBRemote::Dump(Log *log, uint32_t index)
{
}
bool
ThreadGDBRemote::ShouldStop (bool &step_more)
{
return true;
}
lldb::RegisterContextSP
ThreadGDBRemote::GetRegisterContext ()
{
if (m_reg_context_sp.get() == NULL)
m_reg_context_sp = CreateRegisterContextForFrame (NULL);
return m_reg_context_sp;
}
lldb::RegisterContextSP
ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
{
lldb::RegisterContextSP reg_ctx_sp;
uint32_t concrete_frame_idx = 0;
if (frame)
concrete_frame_idx = frame->GetConcreteFrameIndex ();
if (concrete_frame_idx == 0)
{
ProcessSP process_sp (GetProcess());
if (process_sp)
{
ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
// read_all_registers_at_once will be true if 'p' packet is not supported.
bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID());
reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
}
}
else
{
Unwind *unwinder = GetUnwinder ();
if (unwinder)
reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
}
return reg_ctx_sp;
}
bool
ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
{
GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
assert (gdb_reg_ctx);
return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
}
bool
ThreadGDBRemote::CalculateStopInfo ()
{
ProcessSP process_sp (GetProcess());
if (process_sp)
{
StringExtractorGDBRemote stop_packet;
ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;
}
return false;
}