Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@ -0,0 +1,404 @@
//===-- AppleGetItemInfoHandler.cpp -------------------------------*- C++
//-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "AppleGetItemInfoHandler.h"
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/Module.h"
#include "lldb/Core/Value.h"
#include "lldb/Expression/DiagnosticManager.h"
#include "lldb/Expression/FunctionCaller.h"
#include "lldb/Expression/UtilityFunction.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
using namespace lldb;
using namespace lldb_private;
const char *AppleGetItemInfoHandler::g_get_item_info_function_name =
"__lldb_backtrace_recording_get_item_info";
const char *AppleGetItemInfoHandler::g_get_item_info_function_code =
" \n\
extern \"C\" \n\
{ \n\
/* \n\
* mach defines \n\
*/ \n\
\n\
typedef unsigned int uint32_t; \n\
typedef unsigned long long uint64_t; \n\
typedef uint32_t mach_port_t; \n\
typedef mach_port_t vm_map_t; \n\
typedef int kern_return_t; \n\
typedef uint64_t mach_vm_address_t; \n\
typedef uint64_t mach_vm_size_t; \n\
\n\
mach_port_t mach_task_self (); \n\
kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
\n\
/* \n\
* libBacktraceRecording defines \n\
*/ \n\
\n\
typedef uint32_t queue_list_scope_t; \n\
typedef void *dispatch_queue_t; \n\
typedef void *introspection_dispatch_queue_info_t; \n\
typedef void *introspection_dispatch_item_info_ref; \n\
\n\
extern uint64_t __introspection_dispatch_queue_item_get_info (introspection_dispatch_item_info_ref item_info_ref, \n\
introspection_dispatch_item_info_ref *returned_queues_buffer, \n\
uint64_t *returned_queues_buffer_size); \n\
extern int printf(const char *format, ...); \n\
\n\
/* \n\
* return type define \n\
*/ \n\
\n\
struct get_item_info_return_values \n\
{ \n\
uint64_t item_info_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */ \n\
uint64_t item_info_buffer_size; /* the size of the items buffer from libBacktraceRecording */ \n\
}; \n\
\n\
void __lldb_backtrace_recording_get_item_info \n\
(struct get_item_info_return_values *return_buffer, \n\
int debug, \n\
uint64_t /* introspection_dispatch_item_info_ref item_info_ref */ item, \n\
void *page_to_free, \n\
uint64_t page_to_free_size) \n\
{ \n\
if (debug) \n\
printf (\"entering get_item_info with args return_buffer == %p, debug == %d, item == 0x%llx, page_to_free == %p, page_to_free_size == 0x%llx\\n\", return_buffer, debug, item, page_to_free, page_to_free_size); \n\
if (page_to_free != 0) \n\
{ \n\
mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
} \n\
\n\
__introspection_dispatch_queue_item_get_info ((void*) item, \n\
(void**)&return_buffer->item_info_buffer_ptr, \n\
&return_buffer->item_info_buffer_size); \n\
} \n\
} \n\
";
AppleGetItemInfoHandler::AppleGetItemInfoHandler(Process *process)
: m_process(process), m_get_item_info_impl_code(),
m_get_item_info_function_mutex(),
m_get_item_info_return_buffer_addr(LLDB_INVALID_ADDRESS),
m_get_item_info_retbuffer_mutex() {}
AppleGetItemInfoHandler::~AppleGetItemInfoHandler() {}
void AppleGetItemInfoHandler::Detach() {
if (m_process && m_process->IsAlive() &&
m_get_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS) {
std::unique_lock<std::mutex> lock(m_get_item_info_retbuffer_mutex,
std::defer_lock);
lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
m_process->DeallocateMemory(m_get_item_info_return_buffer_addr);
}
}
// Compile our __lldb_backtrace_recording_get_item_info() function (from the
// source above in g_get_item_info_function_code) if we don't find that function
// in the inferior
// already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for
// testing.)
//
// Insert the __lldb_backtrace_recording_get_item_info into the inferior process
// if needed.
//
// Write the get_item_info_arglist into the inferior's memory space to prepare
// for the call.
//
// Returns the address of the arguments written down in the inferior process,
// which can be used to
// make the function call.
lldb::addr_t AppleGetItemInfoHandler::SetupGetItemInfoFunction(
Thread &thread, ValueList &get_item_info_arglist) {
ExecutionContext exe_ctx(thread.shared_from_this());
DiagnosticManager diagnostics;
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
FunctionCaller *get_item_info_caller = nullptr;
// Scope for mutex locker:
{
std::lock_guard<std::mutex> guard(m_get_item_info_function_mutex);
// First stage is to make the UtilityFunction to hold our injected function:
if (!m_get_item_info_impl_code.get()) {
if (g_get_item_info_function_code != NULL) {
Status error;
m_get_item_info_impl_code.reset(
exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
g_get_item_info_function_code, eLanguageTypeObjC,
g_get_item_info_function_name, error));
if (error.Fail()) {
if (log)
log->Printf("Failed to get utility function: %s.",
error.AsCString());
return args_addr;
}
if (!m_get_item_info_impl_code->Install(diagnostics, exe_ctx)) {
if (log) {
log->Printf("Failed to install get-item-info introspection.");
diagnostics.Dump(log);
}
m_get_item_info_impl_code.reset();
return args_addr;
}
} else {
if (log)
log->Printf("No get-item-info introspection code found.");
return LLDB_INVALID_ADDRESS;
}
// Next make the runner function for our implementation utility function.
Status error;
TypeSystem *type_system =
thread.GetProcess()->GetTarget().GetScratchTypeSystemForLanguage(
nullptr, eLanguageTypeC);
CompilerType get_item_info_return_type =
type_system->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
get_item_info_caller = m_get_item_info_impl_code->MakeFunctionCaller(
get_item_info_return_type, get_item_info_arglist,
thread.shared_from_this(), error);
if (error.Fail() || get_item_info_caller == nullptr) {
if (log)
log->Printf("Error Inserting get-item-info function: \"%s\".",
error.AsCString());
return args_addr;
}
} else {
// If it's already made, then we can just retrieve the caller:
get_item_info_caller = m_get_item_info_impl_code->GetFunctionCaller();
if (!get_item_info_caller) {
if (log)
log->Printf("Failed to get get-item-info introspection caller.");
m_get_item_info_impl_code.reset();
return args_addr;
}
}
}
diagnostics.Clear();
// Now write down the argument values for this particular call. This looks
// like it might be a race condition
// if other threads were calling into here, but actually it isn't because we
// allocate a new args structure for
// this call by passing args_addr = LLDB_INVALID_ADDRESS...
if (!get_item_info_caller->WriteFunctionArguments(
exe_ctx, args_addr, get_item_info_arglist, diagnostics)) {
if (log) {
log->Printf("Error writing get-item-info function arguments.");
diagnostics.Dump(log);
}
return args_addr;
}
return args_addr;
}
AppleGetItemInfoHandler::GetItemInfoReturnInfo
AppleGetItemInfoHandler::GetItemInfo(Thread &thread, uint64_t item,
addr_t page_to_free,
uint64_t page_to_free_size,
Status &error) {
lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
ProcessSP process_sp(thread.CalculateProcess());
TargetSP target_sp(thread.CalculateTarget());
ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
GetItemInfoReturnInfo return_value;
return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
return_value.item_buffer_size = 0;
error.Clear();
if (thread.SafeToCallFunctions() == false) {
if (log)
log->Printf("Not safe to call functions on thread 0x%" PRIx64,
thread.GetID());
error.SetErrorString("Not safe to call functions on this thread.");
return return_value;
}
// Set up the arguments for a call to
// struct get_item_info_return_values
// {
// uint64_t item_info_buffer_ptr; /* the address of the items buffer
// from libBacktraceRecording */
// uint64_t item_info_buffer_size; /* the size of the items buffer from
// libBacktraceRecording */
// };
//
// void __lldb_backtrace_recording_get_item_info
// (struct
// get_item_info_return_values
// *return_buffer,
// int debug,
// uint64_t item,
// void *page_to_free,
// uint64_t page_to_free_size)
// Where the return_buffer argument points to a 24 byte region of memory
// already allocated by lldb in
// the inferior process.
CompilerType clang_void_ptr_type =
clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
Value return_buffer_ptr_value;
return_buffer_ptr_value.SetValueType(Value::eValueTypeScalar);
return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
Value debug_value;
debug_value.SetValueType(Value::eValueTypeScalar);
debug_value.SetCompilerType(clang_int_type);
CompilerType clang_uint64_type =
clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
Value item_value;
item_value.SetValueType(Value::eValueTypeScalar);
item_value.SetCompilerType(clang_uint64_type);
Value page_to_free_value;
page_to_free_value.SetValueType(Value::eValueTypeScalar);
page_to_free_value.SetCompilerType(clang_void_ptr_type);
Value page_to_free_size_value;
page_to_free_size_value.SetValueType(Value::eValueTypeScalar);
page_to_free_size_value.SetCompilerType(clang_uint64_type);
std::lock_guard<std::mutex> guard(m_get_item_info_retbuffer_mutex);
if (m_get_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS) {
addr_t bufaddr = process_sp->AllocateMemory(
32, ePermissionsReadable | ePermissionsWritable, error);
if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
if (log)
log->Printf("Failed to allocate memory for return buffer for get "
"current queues func call");
return return_value;
}
m_get_item_info_return_buffer_addr = bufaddr;
}
ValueList argument_values;
return_buffer_ptr_value.GetScalar() = m_get_item_info_return_buffer_addr;
argument_values.PushValue(return_buffer_ptr_value);
debug_value.GetScalar() = 0;
argument_values.PushValue(debug_value);
item_value.GetScalar() = item;
argument_values.PushValue(item_value);
if (page_to_free != LLDB_INVALID_ADDRESS)
page_to_free_value.GetScalar() = page_to_free;
else
page_to_free_value.GetScalar() = 0;
argument_values.PushValue(page_to_free_value);
page_to_free_size_value.GetScalar() = page_to_free_size;
argument_values.PushValue(page_to_free_size_value);
addr_t args_addr = SetupGetItemInfoFunction(thread, argument_values);
DiagnosticManager diagnostics;
ExecutionContext exe_ctx;
EvaluateExpressionOptions options;
options.SetUnwindOnError(true);
options.SetIgnoreBreakpoints(true);
options.SetStopOthers(true);
options.SetTimeout(std::chrono::milliseconds(500));
options.SetTryAllThreads(false);
thread.CalculateExecutionContext(exe_ctx);
if (!m_get_item_info_impl_code) {
error.SetErrorString("Unable to compile function to call "
"__introspection_dispatch_queue_item_get_info");
return return_value;
}
ExpressionResults func_call_ret;
Value results;
FunctionCaller *func_caller = m_get_item_info_impl_code->GetFunctionCaller();
if (!func_caller) {
if (log)
log->Printf("Could not retrieve function caller for "
"__introspection_dispatch_queue_item_get_info.");
error.SetErrorString("Could not retrieve function caller for "
"__introspection_dispatch_queue_item_get_info.");
return return_value;
}
func_call_ret = func_caller->ExecuteFunction(exe_ctx, &args_addr, options,
diagnostics, results);
if (func_call_ret != eExpressionCompleted || !error.Success()) {
if (log)
log->Printf("Unable to call "
"__introspection_dispatch_queue_item_get_info(), got "
"ExpressionResults %d, error contains %s",
func_call_ret, error.AsCString(""));
error.SetErrorString("Unable to call "
"__introspection_dispatch_queue_get_item_info() for "
"list of queues");
return return_value;
}
return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
m_get_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
if (!error.Success() ||
return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS) {
return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
return return_value;
}
return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
m_get_item_info_return_buffer_addr + 8, 8, 0, error);
if (!error.Success()) {
return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
return return_value;
}
if (log)
log->Printf("AppleGetItemInfoHandler called "
"__introspection_dispatch_queue_item_get_info (page_to_free == "
"0x%" PRIx64 ", size = %" PRId64
"), returned page is at 0x%" PRIx64 ", size %" PRId64,
page_to_free, page_to_free_size, return_value.item_buffer_ptr,
return_value.item_buffer_size);
return return_value;
}

View File

@ -0,0 +1,119 @@
//===-- AppleGetItemInfoHandler.h ----------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef lldb_AppleGetItemInfoHandler_h_
#define lldb_AppleGetItemInfoHandler_h_
// C Includes
// C++ Includes
#include <map>
#include <mutex>
#include <vector>
// Other libraries and framework includes
// Project includes
#include "lldb/Expression/UtilityFunction.h"
#include "lldb/Symbol/CompilerType.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-public.h"
// This class will insert a UtilityFunction into the inferior process for
// calling libBacktraceRecording's
// __introspection_dispatch_queue_item_get_info()
// function. The function in the inferior will return a struct by value
// with these members:
//
// struct get_item_info_return_values
// {
// introspection_dispatch_item_info_ref *item_buffer;
// uint64_t item_buffer_size;
// };
//
// The item_buffer pointer is an address in the inferior program's address
// space (item_buffer_size in size) which must be mach_vm_deallocate'd by
// lldb.
//
// The AppleGetItemInfoHandler object should persist so that the UtilityFunction
// can be reused multiple times.
namespace lldb_private {
class AppleGetItemInfoHandler {
public:
AppleGetItemInfoHandler(lldb_private::Process *process);
~AppleGetItemInfoHandler();
struct GetItemInfoReturnInfo {
lldb::addr_t item_buffer_ptr; /* the address of the item buffer from
libBacktraceRecording */
lldb::addr_t item_buffer_size; /* the size of the item buffer from
libBacktraceRecording */
GetItemInfoReturnInfo()
: item_buffer_ptr(LLDB_INVALID_ADDRESS), item_buffer_size(0) {}
};
//----------------------------------------------------------
/// Get the information about a work item by calling
/// __introspection_dispatch_queue_item_get_info. If there's a page of
/// memory that needs to be freed, pass in the address and size and it will
/// be freed before getting the list of queues.
///
/// @param [in] thread
/// The thread to run this plan on.
///
/// @param [in] item
/// The introspection_dispatch_item_info_ref value for the item of
/// interest.
///
/// @param [in] page_to_free
/// An address of an inferior process vm page that needs to be
/// deallocated,
/// LLDB_INVALID_ADDRESS if this is not needed.
///
/// @param [in] page_to_free_size
/// The size of the vm page that needs to be deallocated if an address was
/// passed in to page_to_free.
///
/// @param [out] error
/// This object will be updated with the error status / error string from
/// any failures encountered.
///
/// @returns
/// The result of the inferior function call execution. If there was a
/// failure of any kind while getting
/// the information, the item_buffer_ptr value will be
/// LLDB_INVALID_ADDRESS.
//----------------------------------------------------------
GetItemInfoReturnInfo GetItemInfo(Thread &thread, lldb::addr_t item,
lldb::addr_t page_to_free,
uint64_t page_to_free_size,
lldb_private::Status &error);
void Detach();
private:
lldb::addr_t SetupGetItemInfoFunction(Thread &thread,
ValueList &get_item_info_arglist);
static const char *g_get_item_info_function_name;
static const char *g_get_item_info_function_code;
lldb_private::Process *m_process;
std::unique_ptr<UtilityFunction> m_get_item_info_impl_code;
std::mutex m_get_item_info_function_mutex;
lldb::addr_t m_get_item_info_return_buffer_addr;
std::mutex m_get_item_info_retbuffer_mutex;
};
} // using namespace lldb_private
#endif // lldb_AppleGetItemInfoHandler_h_

View File

@ -0,0 +1,413 @@
//===-- AppleGetPendingItemsHandler.cpp -------------------------------*- C++
//-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "AppleGetPendingItemsHandler.h"
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/Module.h"
#include "lldb/Core/Value.h"
#include "lldb/Expression/DiagnosticManager.h"
#include "lldb/Expression/FunctionCaller.h"
#include "lldb/Expression/UtilityFunction.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
using namespace lldb;
using namespace lldb_private;
const char *AppleGetPendingItemsHandler::g_get_pending_items_function_name =
"__lldb_backtrace_recording_get_pending_items";
const char *AppleGetPendingItemsHandler::g_get_pending_items_function_code =
" \n\
extern \"C\" \n\
{ \n\
/* \n\
* mach defines \n\
*/ \n\
\n\
typedef unsigned int uint32_t; \n\
typedef unsigned long long uint64_t; \n\
typedef uint32_t mach_port_t; \n\
typedef mach_port_t vm_map_t; \n\
typedef int kern_return_t; \n\
typedef uint64_t mach_vm_address_t; \n\
typedef uint64_t mach_vm_size_t; \n\
\n\
mach_port_t mach_task_self (); \n\
kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
\n\
/* \n\
* libBacktraceRecording defines \n\
*/ \n\
\n\
typedef uint32_t queue_list_scope_t; \n\
typedef void *dispatch_queue_t; \n\
typedef void *introspection_dispatch_queue_info_t; \n\
typedef void *introspection_dispatch_item_info_ref; \n\
\n\
extern uint64_t __introspection_dispatch_queue_get_pending_items (dispatch_queue_t queue, \n\
introspection_dispatch_item_info_ref *returned_queues_buffer, \n\
uint64_t *returned_queues_buffer_size); \n\
extern int printf(const char *format, ...); \n\
\n\
/* \n\
* return type define \n\
*/ \n\
\n\
struct get_pending_items_return_values \n\
{ \n\
uint64_t pending_items_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */ \n\
uint64_t pending_items_buffer_size; /* the size of the items buffer from libBacktraceRecording */ \n\
uint64_t count; /* the number of items included in the queues buffer */ \n\
}; \n\
\n\
void __lldb_backtrace_recording_get_pending_items \n\
(struct get_pending_items_return_values *return_buffer, \n\
int debug, \n\
uint64_t /* dispatch_queue_t */ queue, \n\
void *page_to_free, \n\
uint64_t page_to_free_size) \n\
{ \n\
if (debug) \n\
printf (\"entering get_pending_items with args return_buffer == %p, debug == %d, queue == 0x%llx, page_to_free == %p, page_to_free_size == 0x%llx\\n\", return_buffer, debug, queue, page_to_free, page_to_free_size); \n\
if (page_to_free != 0) \n\
{ \n\
mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
} \n\
\n\
return_buffer->count = __introspection_dispatch_queue_get_pending_items ( \n\
(void*) queue, \n\
(void**)&return_buffer->pending_items_buffer_ptr, \n\
&return_buffer->pending_items_buffer_size); \n\
if (debug) \n\
printf(\"result was count %lld\\n\", return_buffer->count); \n\
} \n\
} \n\
";
AppleGetPendingItemsHandler::AppleGetPendingItemsHandler(Process *process)
: m_process(process), m_get_pending_items_impl_code(),
m_get_pending_items_function_mutex(),
m_get_pending_items_return_buffer_addr(LLDB_INVALID_ADDRESS),
m_get_pending_items_retbuffer_mutex() {}
AppleGetPendingItemsHandler::~AppleGetPendingItemsHandler() {}
void AppleGetPendingItemsHandler::Detach() {
if (m_process && m_process->IsAlive() &&
m_get_pending_items_return_buffer_addr != LLDB_INVALID_ADDRESS) {
std::unique_lock<std::mutex> lock(m_get_pending_items_retbuffer_mutex,
std::defer_lock);
lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
m_process->DeallocateMemory(m_get_pending_items_return_buffer_addr);
}
}
// Compile our __lldb_backtrace_recording_get_pending_items() function (from the
// source above in g_get_pending_items_function_code) if we don't find that
// function in the inferior
// already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for
// testing.)
//
// Insert the __lldb_backtrace_recording_get_pending_items into the inferior
// process if needed.
//
// Write the get_pending_items_arglist into the inferior's memory space to
// prepare for the call.
//
// Returns the address of the arguments written down in the inferior process,
// which can be used to
// make the function call.
lldb::addr_t AppleGetPendingItemsHandler::SetupGetPendingItemsFunction(
Thread &thread, ValueList &get_pending_items_arglist) {
ThreadSP thread_sp(thread.shared_from_this());
ExecutionContext exe_ctx(thread_sp);
DiagnosticManager diagnostics;
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
FunctionCaller *get_pending_items_caller = nullptr;
// Scope for mutex locker:
{
std::lock_guard<std::mutex> guard(m_get_pending_items_function_mutex);
// First stage is to make the ClangUtility to hold our injected function:
if (!m_get_pending_items_impl_code.get()) {
if (g_get_pending_items_function_code != NULL) {
Status error;
m_get_pending_items_impl_code.reset(
exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
g_get_pending_items_function_code, eLanguageTypeObjC,
g_get_pending_items_function_name, error));
if (error.Fail()) {
if (log)
log->Printf("Failed to get UtilityFunction for pending-items "
"introspection: %s.",
error.AsCString());
return args_addr;
}
if (!m_get_pending_items_impl_code->Install(diagnostics, exe_ctx)) {
if (log) {
log->Printf("Failed to install pending-items introspection.");
diagnostics.Dump(log);
}
m_get_pending_items_impl_code.reset();
return args_addr;
}
} else {
if (log)
log->Printf("No pending-items introspection code found.");
return LLDB_INVALID_ADDRESS;
}
// Next make the runner function for our implementation utility function.
Status error;
ClangASTContext *clang_ast_context =
thread.GetProcess()->GetTarget().GetScratchClangASTContext();
CompilerType get_pending_items_return_type =
clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
get_pending_items_caller =
m_get_pending_items_impl_code->MakeFunctionCaller(
get_pending_items_return_type, get_pending_items_arglist,
thread_sp, error);
if (error.Fail() || get_pending_items_caller == nullptr) {
if (log)
log->Printf("Failed to install pending-items introspection function "
"caller: %s.",
error.AsCString());
m_get_pending_items_impl_code.reset();
return args_addr;
}
}
}
diagnostics.Clear();
if (get_pending_items_caller == nullptr) {
if (log)
log->Printf("Failed to get get_pending_items_caller.");
return LLDB_INVALID_ADDRESS;
}
// Now write down the argument values for this particular call. This looks
// like it might be a race condition
// if other threads were calling into here, but actually it isn't because we
// allocate a new args structure for
// this call by passing args_addr = LLDB_INVALID_ADDRESS...
if (!get_pending_items_caller->WriteFunctionArguments(
exe_ctx, args_addr, get_pending_items_arglist, diagnostics)) {
if (log) {
log->Printf("Error writing pending-items function arguments.");
diagnostics.Dump(log);
}
return args_addr;
}
return args_addr;
}
AppleGetPendingItemsHandler::GetPendingItemsReturnInfo
AppleGetPendingItemsHandler::GetPendingItems(Thread &thread, addr_t queue,
addr_t page_to_free,
uint64_t page_to_free_size,
Status &error) {
lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
ProcessSP process_sp(thread.CalculateProcess());
TargetSP target_sp(thread.CalculateTarget());
ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
GetPendingItemsReturnInfo return_value;
return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
return_value.items_buffer_size = 0;
return_value.count = 0;
error.Clear();
if (thread.SafeToCallFunctions() == false) {
if (log)
log->Printf("Not safe to call functions on thread 0x%" PRIx64,
thread.GetID());
error.SetErrorString("Not safe to call functions on this thread.");
return return_value;
}
// Set up the arguments for a call to
// struct get_pending_items_return_values
// {
// uint64_t pending_items_buffer_ptr; /* the address of the items
// buffer from libBacktraceRecording */
// uint64_t pending_items_buffer_size; /* the size of the items buffer
// from libBacktraceRecording */
// uint64_t count; /* the number of items included in the
// queues buffer */
// };
//
// void __lldb_backtrace_recording_get_pending_items
// (struct
// get_pending_items_return_values
// *return_buffer,
// int debug,
// uint64_t /* dispatch_queue_t */
// queue
// void *page_to_free,
// uint64_t page_to_free_size)
// Where the return_buffer argument points to a 24 byte region of memory
// already allocated by lldb in
// the inferior process.
CompilerType clang_void_ptr_type =
clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
Value return_buffer_ptr_value;
return_buffer_ptr_value.SetValueType(Value::eValueTypeScalar);
return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
Value debug_value;
debug_value.SetValueType(Value::eValueTypeScalar);
debug_value.SetCompilerType(clang_int_type);
CompilerType clang_uint64_type =
clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
Value queue_value;
queue_value.SetValueType(Value::eValueTypeScalar);
queue_value.SetCompilerType(clang_uint64_type);
Value page_to_free_value;
page_to_free_value.SetValueType(Value::eValueTypeScalar);
page_to_free_value.SetCompilerType(clang_void_ptr_type);
Value page_to_free_size_value;
page_to_free_size_value.SetValueType(Value::eValueTypeScalar);
page_to_free_size_value.SetCompilerType(clang_uint64_type);
std::lock_guard<std::mutex> guard(m_get_pending_items_retbuffer_mutex);
if (m_get_pending_items_return_buffer_addr == LLDB_INVALID_ADDRESS) {
addr_t bufaddr = process_sp->AllocateMemory(
32, ePermissionsReadable | ePermissionsWritable, error);
if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
if (log)
log->Printf("Failed to allocate memory for return buffer for get "
"current queues func call");
return return_value;
}
m_get_pending_items_return_buffer_addr = bufaddr;
}
ValueList argument_values;
return_buffer_ptr_value.GetScalar() = m_get_pending_items_return_buffer_addr;
argument_values.PushValue(return_buffer_ptr_value);
debug_value.GetScalar() = 0;
argument_values.PushValue(debug_value);
queue_value.GetScalar() = queue;
argument_values.PushValue(queue_value);
if (page_to_free != LLDB_INVALID_ADDRESS)
page_to_free_value.GetScalar() = page_to_free;
else
page_to_free_value.GetScalar() = 0;
argument_values.PushValue(page_to_free_value);
page_to_free_size_value.GetScalar() = page_to_free_size;
argument_values.PushValue(page_to_free_size_value);
addr_t args_addr = SetupGetPendingItemsFunction(thread, argument_values);
DiagnosticManager diagnostics;
ExecutionContext exe_ctx;
FunctionCaller *get_pending_items_caller =
m_get_pending_items_impl_code->GetFunctionCaller();
EvaluateExpressionOptions options;
options.SetUnwindOnError(true);
options.SetIgnoreBreakpoints(true);
options.SetStopOthers(true);
options.SetTimeout(std::chrono::milliseconds(500));
options.SetTryAllThreads(false);
thread.CalculateExecutionContext(exe_ctx);
if (get_pending_items_caller == NULL) {
error.SetErrorString("Unable to compile function to call "
"__introspection_dispatch_queue_get_pending_items");
return return_value;
}
ExpressionResults func_call_ret;
Value results;
func_call_ret = get_pending_items_caller->ExecuteFunction(
exe_ctx, &args_addr, options, diagnostics, results);
if (func_call_ret != eExpressionCompleted || !error.Success()) {
if (log)
log->Printf("Unable to call "
"__introspection_dispatch_queue_get_pending_items(), got "
"ExpressionResults %d, error contains %s",
func_call_ret, error.AsCString(""));
error.SetErrorString("Unable to call "
"__introspection_dispatch_queue_get_pending_items() "
"for list of queues");
return return_value;
}
return_value.items_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
m_get_pending_items_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
if (!error.Success() ||
return_value.items_buffer_ptr == LLDB_INVALID_ADDRESS) {
return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
return return_value;
}
return_value.items_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
m_get_pending_items_return_buffer_addr + 8, 8, 0, error);
if (!error.Success()) {
return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
return return_value;
}
return_value.count = m_process->ReadUnsignedIntegerFromMemory(
m_get_pending_items_return_buffer_addr + 16, 8, 0, error);
if (!error.Success()) {
return_value.items_buffer_ptr = LLDB_INVALID_ADDRESS;
return return_value;
}
if (log)
log->Printf("AppleGetPendingItemsHandler called "
"__introspection_dispatch_queue_get_pending_items "
"(page_to_free == 0x%" PRIx64 ", size = %" PRId64
"), returned page is at 0x%" PRIx64 ", size %" PRId64
", count = %" PRId64,
page_to_free, page_to_free_size, return_value.items_buffer_ptr,
return_value.items_buffer_size, return_value.count);
return return_value;
}

View File

@ -0,0 +1,124 @@
//===-- AppleGetPendingItemsHandler.h ----------------------------*- C++
//-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef lldb_AppleGetPendingItemsHandler_h_
#define lldb_AppleGetPendingItemsHandler_h_
// C Includes
// C++ Includes
#include <map>
#include <mutex>
#include <vector>
// Other libraries and framework includes
// Project includes
#include "lldb/Symbol/CompilerType.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-public.h"
// This class will insert a UtilityFunction into the inferior process for
// calling libBacktraceRecording's
// __introspection_dispatch_queue_get_pending_items()
// function. The function in the inferior will return a struct by value
// with these members:
//
// struct get_pending_items_return_values
// {
// introspection_dispatch_item_info_ref *items_buffer;
// uint64_t items_buffer_size;
// uint64_t count;
// };
//
// The items_buffer pointer is an address in the inferior program's address
// space (items_buffer_size in size) which must be mach_vm_deallocate'd by
// lldb. count is the number of items that were stored in the buffer.
//
// The AppleGetPendingItemsHandler object should persist so that the
// UtilityFunction
// can be reused multiple times.
namespace lldb_private {
class AppleGetPendingItemsHandler {
public:
AppleGetPendingItemsHandler(lldb_private::Process *process);
~AppleGetPendingItemsHandler();
struct GetPendingItemsReturnInfo {
lldb::addr_t items_buffer_ptr; /* the address of the pending items buffer
from libBacktraceRecording */
lldb::addr_t
items_buffer_size; /* the size of the pending items buffer from
libBacktraceRecording */
uint64_t count; /* the number of pending items included in the buffer */
GetPendingItemsReturnInfo()
: items_buffer_ptr(LLDB_INVALID_ADDRESS), items_buffer_size(0),
count(0) {}
};
//----------------------------------------------------------
/// Get the list of pending items for a given queue via a call to
/// __introspection_dispatch_queue_get_pending_items. If there's a page of
/// memory that needs to be freed, pass in the address and size and it will
/// be freed before getting the list of queues.
///
/// @param [in] thread
/// The thread to run this plan on.
///
/// @param [in] queue
/// The dispatch_queue_t value for the queue of interest.
///
/// @param [in] page_to_free
/// An address of an inferior process vm page that needs to be
/// deallocated,
/// LLDB_INVALID_ADDRESS if this is not needed.
///
/// @param [in] page_to_free_size
/// The size of the vm page that needs to be deallocated if an address was
/// passed in to page_to_free.
///
/// @param [out] error
/// This object will be updated with the error status / error string from
/// any failures encountered.
///
/// @returns
/// The result of the inferior function call execution. If there was a
/// failure of any kind while getting
/// the information, the items_buffer_ptr value will be
/// LLDB_INVALID_ADDRESS.
//----------------------------------------------------------
GetPendingItemsReturnInfo GetPendingItems(Thread &thread, lldb::addr_t queue,
lldb::addr_t page_to_free,
uint64_t page_to_free_size,
lldb_private::Status &error);
void Detach();
private:
lldb::addr_t
SetupGetPendingItemsFunction(Thread &thread,
ValueList &get_pending_items_arglist);
static const char *g_get_pending_items_function_name;
static const char *g_get_pending_items_function_code;
lldb_private::Process *m_process;
std::unique_ptr<UtilityFunction> m_get_pending_items_impl_code;
std::mutex m_get_pending_items_function_mutex;
lldb::addr_t m_get_pending_items_return_buffer_addr;
std::mutex m_get_pending_items_retbuffer_mutex;
};
} // using namespace lldb_private
#endif // lldb_AppleGetPendingItemsHandler_h_

View File

@ -0,0 +1,411 @@
//===-- AppleGetQueuesHandler.cpp -------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "AppleGetQueuesHandler.h"
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/Module.h"
#include "lldb/Core/Value.h"
#include "lldb/Expression/DiagnosticManager.h"
#include "lldb/Expression/FunctionCaller.h"
#include "lldb/Expression/UtilityFunction.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
using namespace lldb;
using namespace lldb_private;
const char *AppleGetQueuesHandler::g_get_current_queues_function_name =
"__lldb_backtrace_recording_get_current_queues";
const char *AppleGetQueuesHandler::g_get_current_queues_function_code =
" \n\
extern \"C\" \n\
{ \n\
/* \n\
* mach defines \n\
*/ \n\
\n\
typedef unsigned int uint32_t; \n\
typedef unsigned long long uint64_t; \n\
typedef uint32_t mach_port_t; \n\
typedef mach_port_t vm_map_t; \n\
typedef int kern_return_t; \n\
typedef uint64_t mach_vm_address_t; \n\
typedef uint64_t mach_vm_size_t; \n\
\n\
mach_port_t mach_task_self (); \n\
kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
\n\
/* \n\
* libBacktraceRecording defines \n\
*/ \n\
\n\
typedef uint32_t queue_list_scope_t; \n\
typedef void *introspection_dispatch_queue_info_t; \n\
\n\
extern uint64_t __introspection_dispatch_get_queues (queue_list_scope_t scope, \n\
introspection_dispatch_queue_info_t *returned_queues_buffer, \n\
uint64_t *returned_queues_buffer_size); \n\
extern int printf(const char *format, ...); \n\
\n\
/* \n\
* return type define \n\
*/ \n\
\n\
struct get_current_queues_return_values \n\
{ \n\
uint64_t queues_buffer_ptr; /* the address of the queues buffer from libBacktraceRecording */ \n\
uint64_t queues_buffer_size; /* the size of the queues buffer from libBacktraceRecording */ \n\
uint64_t count; /* the number of queues included in the queues buffer */ \n\
}; \n\
\n\
void __lldb_backtrace_recording_get_current_queues \n\
(struct get_current_queues_return_values *return_buffer, \n\
int debug, \n\
void *page_to_free, \n\
uint64_t page_to_free_size) \n\
{ \n\
if (debug) \n\
printf (\"entering get_current_queues with args %p, %d, 0x%p, 0x%llx\\n\", return_buffer, debug, page_to_free, page_to_free_size); \n\
if (page_to_free != 0) \n\
{ \n\
mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
} \n\
\n\
return_buffer->count = __introspection_dispatch_get_queues ( \n\
/* QUEUES_WITH_ANY_ITEMS */ 2, \n\
(void**)&return_buffer->queues_buffer_ptr, \n\
&return_buffer->queues_buffer_size); \n\
if (debug) \n\
printf(\"result was count %lld\\n\", return_buffer->count); \n\
} \n\
} \n\
";
AppleGetQueuesHandler::AppleGetQueuesHandler(Process *process)
: m_process(process), m_get_queues_impl_code_up(),
m_get_queues_function_mutex(),
m_get_queues_return_buffer_addr(LLDB_INVALID_ADDRESS),
m_get_queues_retbuffer_mutex() {}
AppleGetQueuesHandler::~AppleGetQueuesHandler() {}
void AppleGetQueuesHandler::Detach() {
if (m_process && m_process->IsAlive() &&
m_get_queues_return_buffer_addr != LLDB_INVALID_ADDRESS) {
std::unique_lock<std::mutex> lock(m_get_queues_retbuffer_mutex,
std::defer_lock);
lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
m_process->DeallocateMemory(m_get_queues_return_buffer_addr);
}
}
// Construct a CompilerType for the structure that
// g_get_current_queues_function_code will return by value
// so we can extract the fields after performing the function call.
// i.e. we are getting this struct returned to us:
//
// struct get_current_queues_return_values
// {
// introspection_dispatch_queue_info_t *queues_buffer;
// uint64_t queues_buffer_size;
// uint64_t count;
// };
// Compile our __lldb_backtrace_recording_get_current_queues() function (from
// the
// source above in g_get_current_queues_function_code) if we don't find that
// function in the inferior
// already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for
// testing.)
//
// Insert the __lldb_backtrace_recording_get_current_queues into the inferior
// process if needed.
//
// Write the get_queues_arglist into the inferior's memory space to prepare for
// the call.
//
// Returns the address of the arguments written down in the inferior process,
// which can be used to
// make the function call.
lldb::addr_t
AppleGetQueuesHandler::SetupGetQueuesFunction(Thread &thread,
ValueList &get_queues_arglist) {
ThreadSP thread_sp(thread.shared_from_this());
ExecutionContext exe_ctx(thread_sp);
Address impl_code_address;
DiagnosticManager diagnostics;
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
FunctionCaller *get_queues_caller = nullptr;
// Scope for mutex locker:
{
std::lock_guard<std::mutex> guard(m_get_queues_function_mutex);
// First stage is to make the ClangUtility to hold our injected function:
if (!m_get_queues_impl_code_up.get()) {
if (g_get_current_queues_function_code != NULL) {
Status error;
m_get_queues_impl_code_up.reset(
exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
g_get_current_queues_function_code, eLanguageTypeC,
g_get_current_queues_function_name, error));
if (error.Fail()) {
if (log)
log->Printf(
"Failed to get UtilityFunction for queues introspection: %s.",
error.AsCString());
return args_addr;
}
if (!m_get_queues_impl_code_up->Install(diagnostics, exe_ctx)) {
if (log) {
log->Printf("Failed to install queues introspection");
diagnostics.Dump(log);
}
m_get_queues_impl_code_up.reset();
return args_addr;
}
} else {
if (log) {
log->Printf("No queues introspection code found.");
diagnostics.Dump(log);
}
return LLDB_INVALID_ADDRESS;
}
}
// Next make the runner function for our implementation utility function.
ClangASTContext *clang_ast_context =
thread.GetProcess()->GetTarget().GetScratchClangASTContext();
CompilerType get_queues_return_type =
clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
Status error;
get_queues_caller = m_get_queues_impl_code_up->MakeFunctionCaller(
get_queues_return_type, get_queues_arglist, thread_sp, error);
if (error.Fail() || get_queues_caller == nullptr) {
if (log)
log->Printf(
"Could not get function caller for get-queues function: %s.",
error.AsCString());
return args_addr;
}
}
diagnostics.Clear();
// Now write down the argument values for this particular call. This looks
// like it might be a race condition
// if other threads were calling into here, but actually it isn't because we
// allocate a new args structure for
// this call by passing args_addr = LLDB_INVALID_ADDRESS...
if (!get_queues_caller->WriteFunctionArguments(
exe_ctx, args_addr, get_queues_arglist, diagnostics)) {
if (log) {
log->Printf("Error writing get-queues function arguments.");
diagnostics.Dump(log);
}
return args_addr;
}
return args_addr;
}
AppleGetQueuesHandler::GetQueuesReturnInfo
AppleGetQueuesHandler::GetCurrentQueues(Thread &thread, addr_t page_to_free,
uint64_t page_to_free_size,
Status &error) {
lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
ProcessSP process_sp(thread.CalculateProcess());
TargetSP target_sp(thread.CalculateTarget());
ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
GetQueuesReturnInfo return_value;
return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
return_value.queues_buffer_size = 0;
return_value.count = 0;
error.Clear();
if (thread.SafeToCallFunctions() == false) {
if (log)
log->Printf("Not safe to call functions on thread 0x%" PRIx64,
thread.GetID());
error.SetErrorString("Not safe to call functions on this thread.");
return return_value;
}
// Set up the arguments for a call to
// struct get_current_queues_return_values
// {
// uint64_t queues_buffer_ptr; /* the address of the queues buffer from
// libBacktraceRecording */
// uint64_t queues_buffer_size; /* the size of the queues buffer from
// libBacktraceRecording */
// uint64_t count; /* the number of queues included in the
// queues buffer */
// };
//
// void
// __lldb_backtrace_recording_get_current_queues
// (struct
// get_current_queues_return_values
// *return_buffer,
// void *page_to_free,
// uint64_t page_to_free_size);
// Where the return_buffer argument points to a 24 byte region of memory
// already allocated by lldb in
// the inferior process.
CompilerType clang_void_ptr_type =
clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
Value return_buffer_ptr_value;
return_buffer_ptr_value.SetValueType(Value::eValueTypeScalar);
return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
Value debug_value;
debug_value.SetValueType(Value::eValueTypeScalar);
debug_value.SetCompilerType(clang_int_type);
Value page_to_free_value;
page_to_free_value.SetValueType(Value::eValueTypeScalar);
page_to_free_value.SetCompilerType(clang_void_ptr_type);
CompilerType clang_uint64_type =
clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
Value page_to_free_size_value;
page_to_free_size_value.SetValueType(Value::eValueTypeScalar);
page_to_free_size_value.SetCompilerType(clang_uint64_type);
std::lock_guard<std::mutex> guard(m_get_queues_retbuffer_mutex);
if (m_get_queues_return_buffer_addr == LLDB_INVALID_ADDRESS) {
addr_t bufaddr = process_sp->AllocateMemory(
32, ePermissionsReadable | ePermissionsWritable, error);
if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
if (log)
log->Printf("Failed to allocate memory for return buffer for get "
"current queues func call");
return return_value;
}
m_get_queues_return_buffer_addr = bufaddr;
}
ValueList argument_values;
return_buffer_ptr_value.GetScalar() = m_get_queues_return_buffer_addr;
argument_values.PushValue(return_buffer_ptr_value);
debug_value.GetScalar() = 0;
argument_values.PushValue(debug_value);
if (page_to_free != LLDB_INVALID_ADDRESS)
page_to_free_value.GetScalar() = page_to_free;
else
page_to_free_value.GetScalar() = 0;
argument_values.PushValue(page_to_free_value);
page_to_free_size_value.GetScalar() = page_to_free_size;
argument_values.PushValue(page_to_free_size_value);
addr_t args_addr = SetupGetQueuesFunction(thread, argument_values);
if (!m_get_queues_impl_code_up) {
error.SetErrorString(
"Unable to compile __introspection_dispatch_get_queues.");
return return_value;
}
FunctionCaller *get_queues_caller =
m_get_queues_impl_code_up->GetFunctionCaller();
if (get_queues_caller == NULL) {
error.SetErrorString(
"Unable to get caller for call __introspection_dispatch_get_queues");
return return_value;
}
DiagnosticManager diagnostics;
ExecutionContext exe_ctx;
EvaluateExpressionOptions options;
options.SetUnwindOnError(true);
options.SetIgnoreBreakpoints(true);
options.SetStopOthers(true);
options.SetTimeout(std::chrono::milliseconds(500));
options.SetTryAllThreads(false);
thread.CalculateExecutionContext(exe_ctx);
ExpressionResults func_call_ret;
Value results;
func_call_ret = get_queues_caller->ExecuteFunction(
exe_ctx, &args_addr, options, diagnostics, results);
if (func_call_ret != eExpressionCompleted || !error.Success()) {
if (log)
log->Printf("Unable to call introspection_get_dispatch_queues(), got "
"ExpressionResults %d, error contains %s",
func_call_ret, error.AsCString(""));
error.SetErrorString("Unable to call introspection_get_dispatch_queues() "
"for list of queues");
return return_value;
}
return_value.queues_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
m_get_queues_return_buffer_addr, 8, LLDB_INVALID_ADDRESS, error);
if (!error.Success() ||
return_value.queues_buffer_ptr == LLDB_INVALID_ADDRESS) {
return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
return return_value;
}
return_value.queues_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
m_get_queues_return_buffer_addr + 8, 8, 0, error);
if (!error.Success()) {
return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
return return_value;
}
return_value.count = m_process->ReadUnsignedIntegerFromMemory(
m_get_queues_return_buffer_addr + 16, 8, 0, error);
if (!error.Success()) {
return_value.queues_buffer_ptr = LLDB_INVALID_ADDRESS;
return return_value;
}
if (log)
log->Printf("AppleGetQueuesHandler called "
"__introspection_dispatch_get_queues (page_to_free == "
"0x%" PRIx64 ", size = %" PRId64
"), returned page is at 0x%" PRIx64 ", size %" PRId64
", count = %" PRId64,
page_to_free, page_to_free_size, return_value.queues_buffer_ptr,
return_value.queues_buffer_size, return_value.count);
return return_value;
}

View File

@ -0,0 +1,116 @@
//===-- AppleGetQueuesHandler.h ----------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef lldb_AppleGetQueuesHandler_h_
#define lldb_AppleGetQueuesHandler_h_
// C Includes
// C++ Includes
#include <map>
#include <mutex>
#include <vector>
// Other libraries and framework includes
// Project includes
#include "lldb/Symbol/CompilerType.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-public.h"
// This class will insert a UtilityFunction into the inferior process for
// calling libBacktraceRecording's introspection_get_dispatch_queues()
// function. The function in the inferior will return a struct by value
// with these members:
//
// struct get_current_queues_return_values
// {
// introspection_dispatch_queue_info_t *queues_buffer;
// uint64_t queues_buffer_size;
// uint64_t count;
// };
//
// The queues_buffer pointer is an address in the inferior program's address
// space (queues_buffer_size in size) which must be mach_vm_deallocate'd by
// lldb. count is the number of queues that were stored in the buffer.
//
// The AppleGetQueuesHandler object should persist so that the UtilityFunction
// can be reused multiple times.
namespace lldb_private {
class AppleGetQueuesHandler {
public:
AppleGetQueuesHandler(lldb_private::Process *process);
~AppleGetQueuesHandler();
struct GetQueuesReturnInfo {
lldb::addr_t queues_buffer_ptr; /* the address of the queues buffer from
libBacktraceRecording */
lldb::addr_t queues_buffer_size; /* the size of the queues buffer from
libBacktraceRecording */
uint64_t count; /* the number of queues included in the queues buffer */
GetQueuesReturnInfo()
: queues_buffer_ptr(LLDB_INVALID_ADDRESS), queues_buffer_size(0),
count(0) {}
};
//----------------------------------------------------------
/// Get the list of queues that exist (with any active or pending items) via
/// a call to introspection_get_dispatch_queues(). If there's a page of
/// memory that needs to be freed, pass in the address and size and it will
/// be freed before getting the list of queues.
///
/// @param [in] thread
/// The thread to run this plan on.
///
/// @param [in] page_to_free
/// An address of an inferior process vm page that needs to be
/// deallocated,
/// LLDB_INVALID_ADDRESS if this is not needed.
///
/// @param [in] page_to_free_size
/// The size of the vm page that needs to be deallocated if an address was
/// passed in to page_to_free.
///
/// @param [out] error
/// This object will be updated with the error status / error string from
/// any failures encountered.
///
/// @returns
/// The result of the inferior function call execution. If there was a
/// failure of any kind while getting
/// the information, the queues_buffer_ptr value will be
/// LLDB_INVALID_ADDRESS.
//----------------------------------------------------------
GetQueuesReturnInfo GetCurrentQueues(Thread &thread,
lldb::addr_t page_to_free,
uint64_t page_to_free_size,
lldb_private::Status &error);
void Detach();
private:
lldb::addr_t SetupGetQueuesFunction(Thread &thread,
ValueList &get_queues_arglist);
static const char *g_get_current_queues_function_name;
static const char *g_get_current_queues_function_code;
lldb_private::Process *m_process;
std::unique_ptr<UtilityFunction> m_get_queues_impl_code_up;
std::mutex m_get_queues_function_mutex;
lldb::addr_t m_get_queues_return_buffer_addr;
std::mutex m_get_queues_retbuffer_mutex;
};
} // using namespace lldb_private
#endif // lldb_AppleGetQueuesHandler_h_

View File

@ -0,0 +1,419 @@
//===-- AppleGetThreadItemInfoHandler.cpp -------------------------------*- C++
//-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "AppleGetThreadItemInfoHandler.h"
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/Module.h"
#include "lldb/Core/Value.h"
#include "lldb/Expression/DiagnosticManager.h"
#include "lldb/Expression/Expression.h"
#include "lldb/Expression/FunctionCaller.h"
#include "lldb/Expression/UtilityFunction.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/lldb-private.h"
using namespace lldb;
using namespace lldb_private;
const char
*AppleGetThreadItemInfoHandler::g_get_thread_item_info_function_name =
"__lldb_backtrace_recording_get_thread_item_info";
const char
*AppleGetThreadItemInfoHandler::g_get_thread_item_info_function_code =
" \n\
extern \"C\" \n\
{ \n\
/* \n\
* mach defines \n\
*/ \n\
\n\
typedef unsigned int uint32_t; \n\
typedef unsigned long long uint64_t; \n\
typedef uint32_t mach_port_t; \n\
typedef mach_port_t vm_map_t; \n\
typedef int kern_return_t; \n\
typedef uint64_t mach_vm_address_t; \n\
typedef uint64_t mach_vm_size_t; \n\
\n\
mach_port_t mach_task_self (); \n\
kern_return_t mach_vm_deallocate (vm_map_t target, mach_vm_address_t address, mach_vm_size_t size); \n\
\n\
typedef void *pthread_t; \n\
extern int printf(const char *format, ...); \n\
extern pthread_t pthread_self(void); \n\
\n\
/* \n\
* libBacktraceRecording defines \n\
*/ \n\
\n\
typedef uint32_t queue_list_scope_t; \n\
typedef void *dispatch_queue_t; \n\
typedef void *introspection_dispatch_queue_info_t; \n\
typedef void *introspection_dispatch_item_info_ref; \n\
\n\
extern void __introspection_dispatch_thread_get_item_info (uint64_t thread_id, \n\
introspection_dispatch_item_info_ref *returned_queues_buffer, \n\
uint64_t *returned_queues_buffer_size); \n\
\n\
/* \n\
* return type define \n\
*/ \n\
\n\
struct get_thread_item_info_return_values \n\
{ \n\
uint64_t item_info_buffer_ptr; /* the address of the items buffer from libBacktraceRecording */ \n\
uint64_t item_info_buffer_size; /* the size of the items buffer from libBacktraceRecording */ \n\
}; \n\
\n\
void __lldb_backtrace_recording_get_thread_item_info \n\
(struct get_thread_item_info_return_values *return_buffer, \n\
int debug, \n\
uint64_t thread_id, \n\
void *page_to_free, \n\
uint64_t page_to_free_size) \n\
{ \n\
void *pthread_id = pthread_self (); \n\
if (debug) \n\
printf (\"entering get_thread_item_info with args return_buffer == %p, debug == %d, thread id == 0x%llx, page_to_free == %p, page_to_free_size == 0x%llx\\n\", return_buffer, debug, (uint64_t) thread_id, page_to_free, page_to_free_size); \n\
if (page_to_free != 0) \n\
{ \n\
mach_vm_deallocate (mach_task_self(), (mach_vm_address_t) page_to_free, (mach_vm_size_t) page_to_free_size); \n\
} \n\
\n\
__introspection_dispatch_thread_get_item_info (thread_id, \n\
(void**)&return_buffer->item_info_buffer_ptr, \n\
&return_buffer->item_info_buffer_size); \n\
} \n\
} \n\
";
AppleGetThreadItemInfoHandler::AppleGetThreadItemInfoHandler(Process *process)
: m_process(process), m_get_thread_item_info_impl_code(),
m_get_thread_item_info_function_mutex(),
m_get_thread_item_info_return_buffer_addr(LLDB_INVALID_ADDRESS),
m_get_thread_item_info_retbuffer_mutex() {}
AppleGetThreadItemInfoHandler::~AppleGetThreadItemInfoHandler() {}
void AppleGetThreadItemInfoHandler::Detach() {
if (m_process && m_process->IsAlive() &&
m_get_thread_item_info_return_buffer_addr != LLDB_INVALID_ADDRESS) {
std::unique_lock<std::mutex> lock(m_get_thread_item_info_retbuffer_mutex,
std::defer_lock);
lock.try_lock(); // Even if we don't get the lock, deallocate the buffer
m_process->DeallocateMemory(m_get_thread_item_info_return_buffer_addr);
}
}
// Compile our __lldb_backtrace_recording_get_thread_item_info() function (from
// the
// source above in g_get_thread_item_info_function_code) if we don't find that
// function in the inferior
// already with USE_BUILTIN_FUNCTION defined. (e.g. this would be the case for
// testing.)
//
// Insert the __lldb_backtrace_recording_get_thread_item_info into the inferior
// process if needed.
//
// Write the get_thread_item_info_arglist into the inferior's memory space to
// prepare for the call.
//
// Returns the address of the arguments written down in the inferior process,
// which can be used to
// make the function call.
lldb::addr_t AppleGetThreadItemInfoHandler::SetupGetThreadItemInfoFunction(
Thread &thread, ValueList &get_thread_item_info_arglist) {
ThreadSP thread_sp(thread.shared_from_this());
ExecutionContext exe_ctx(thread_sp);
Address impl_code_address;
DiagnosticManager diagnostics;
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
FunctionCaller *get_thread_item_info_caller = nullptr;
// Scope for mutex locker:
{
std::lock_guard<std::mutex> guard(m_get_thread_item_info_function_mutex);
// First stage is to make the ClangUtility to hold our injected function:
if (!m_get_thread_item_info_impl_code.get()) {
Status error;
if (g_get_thread_item_info_function_code != NULL) {
m_get_thread_item_info_impl_code.reset(
exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
g_get_thread_item_info_function_code, eLanguageTypeC,
g_get_thread_item_info_function_name, error));
if (error.Fail()) {
if (log)
log->Printf("Failed to get UtilityFunction for "
"get-thread-item-info introspection: %s.",
error.AsCString());
m_get_thread_item_info_impl_code.reset();
return args_addr;
}
if (!m_get_thread_item_info_impl_code->Install(diagnostics, exe_ctx)) {
if (log) {
log->Printf(
"Failed to install get-thread-item-info introspection.");
diagnostics.Dump(log);
}
m_get_thread_item_info_impl_code.reset();
return args_addr;
}
} else {
if (log)
log->Printf("No get-thread-item-info introspection code found.");
return LLDB_INVALID_ADDRESS;
}
// Also make the FunctionCaller for this UtilityFunction:
ClangASTContext *clang_ast_context =
thread.GetProcess()->GetTarget().GetScratchClangASTContext();
CompilerType get_thread_item_info_return_type =
clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
get_thread_item_info_caller =
m_get_thread_item_info_impl_code->MakeFunctionCaller(
get_thread_item_info_return_type, get_thread_item_info_arglist,
thread_sp, error);
if (error.Fail() || get_thread_item_info_caller == nullptr) {
if (log)
log->Printf("Failed to install get-thread-item-info introspection "
"caller: %s.",
error.AsCString());
m_get_thread_item_info_impl_code.reset();
return args_addr;
}
} else {
get_thread_item_info_caller =
m_get_thread_item_info_impl_code->GetFunctionCaller();
}
}
diagnostics.Clear();
// Now write down the argument values for this particular call. This looks
// like it might be a race condition
// if other threads were calling into here, but actually it isn't because we
// allocate a new args structure for
// this call by passing args_addr = LLDB_INVALID_ADDRESS...
if (!get_thread_item_info_caller->WriteFunctionArguments(
exe_ctx, args_addr, get_thread_item_info_arglist, diagnostics)) {
if (log) {
log->Printf("Error writing get-thread-item-info function arguments");
diagnostics.Dump(log);
}
return args_addr;
}
return args_addr;
}
AppleGetThreadItemInfoHandler::GetThreadItemInfoReturnInfo
AppleGetThreadItemInfoHandler::GetThreadItemInfo(Thread &thread,
tid_t thread_id,
addr_t page_to_free,
uint64_t page_to_free_size,
Status &error) {
lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0);
ProcessSP process_sp(thread.CalculateProcess());
TargetSP target_sp(thread.CalculateTarget());
ClangASTContext *clang_ast_context = target_sp->GetScratchClangASTContext();
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME));
GetThreadItemInfoReturnInfo return_value;
return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
return_value.item_buffer_size = 0;
error.Clear();
if (thread.SafeToCallFunctions() == false) {
if (log)
log->Printf("Not safe to call functions on thread 0x%" PRIx64,
thread.GetID());
error.SetErrorString("Not safe to call functions on this thread.");
return return_value;
}
// Set up the arguments for a call to
// struct get_thread_item_info_return_values
// {
// uint64_t item_info_buffer_ptr; /* the address of the items buffer
// from libBacktraceRecording */
// uint64_t item_info_buffer_size; /* the size of the items buffer from
// libBacktraceRecording */
// };
//
// void __lldb_backtrace_recording_get_thread_item_info
// (struct
// get_thread_item_info_return_values
// *return_buffer,
// int debug,
// void *page_to_free,
// uint64_t page_to_free_size)
// Where the return_buffer argument points to a 24 byte region of memory
// already allocated by lldb in
// the inferior process.
CompilerType clang_void_ptr_type =
clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
Value return_buffer_ptr_value;
return_buffer_ptr_value.SetValueType(Value::eValueTypeScalar);
return_buffer_ptr_value.SetCompilerType(clang_void_ptr_type);
CompilerType clang_int_type = clang_ast_context->GetBasicType(eBasicTypeInt);
Value debug_value;
debug_value.SetValueType(Value::eValueTypeScalar);
debug_value.SetCompilerType(clang_int_type);
CompilerType clang_uint64_type =
clang_ast_context->GetBasicType(eBasicTypeUnsignedLongLong);
Value thread_id_value;
thread_id_value.SetValueType(Value::eValueTypeScalar);
thread_id_value.SetCompilerType(clang_uint64_type);
Value page_to_free_value;
page_to_free_value.SetValueType(Value::eValueTypeScalar);
page_to_free_value.SetCompilerType(clang_void_ptr_type);
Value page_to_free_size_value;
page_to_free_size_value.SetValueType(Value::eValueTypeScalar);
page_to_free_size_value.SetCompilerType(clang_uint64_type);
std::lock_guard<std::mutex> guard(m_get_thread_item_info_retbuffer_mutex);
if (m_get_thread_item_info_return_buffer_addr == LLDB_INVALID_ADDRESS) {
addr_t bufaddr = process_sp->AllocateMemory(
32, ePermissionsReadable | ePermissionsWritable, error);
if (!error.Success() || bufaddr == LLDB_INVALID_ADDRESS) {
if (log)
log->Printf("Failed to allocate memory for return buffer for get "
"current queues func call");
return return_value;
}
m_get_thread_item_info_return_buffer_addr = bufaddr;
}
ValueList argument_values;
return_buffer_ptr_value.GetScalar() =
m_get_thread_item_info_return_buffer_addr;
argument_values.PushValue(return_buffer_ptr_value);
debug_value.GetScalar() = 0;
argument_values.PushValue(debug_value);
thread_id_value.GetScalar() = thread_id;
argument_values.PushValue(thread_id_value);
if (page_to_free != LLDB_INVALID_ADDRESS)
page_to_free_value.GetScalar() = page_to_free;
else
page_to_free_value.GetScalar() = 0;
argument_values.PushValue(page_to_free_value);
page_to_free_size_value.GetScalar() = page_to_free_size;
argument_values.PushValue(page_to_free_size_value);
addr_t args_addr = SetupGetThreadItemInfoFunction(thread, argument_values);
DiagnosticManager diagnostics;
ExecutionContext exe_ctx;
EvaluateExpressionOptions options;
FunctionCaller *get_thread_item_info_caller = nullptr;
options.SetUnwindOnError(true);
options.SetIgnoreBreakpoints(true);
options.SetStopOthers(true);
options.SetTimeout(std::chrono::milliseconds(500));
options.SetTryAllThreads(false);
thread.CalculateExecutionContext(exe_ctx);
if (!m_get_thread_item_info_impl_code) {
error.SetErrorString("Unable to compile function to call "
"__introspection_dispatch_thread_get_item_info");
return return_value;
}
get_thread_item_info_caller =
m_get_thread_item_info_impl_code->GetFunctionCaller();
if (!get_thread_item_info_caller) {
error.SetErrorString("Unable to compile function caller for "
"__introspection_dispatch_thread_get_item_info");
return return_value;
}
ExpressionResults func_call_ret;
Value results;
func_call_ret = get_thread_item_info_caller->ExecuteFunction(
exe_ctx, &args_addr, options, diagnostics, results);
if (func_call_ret != eExpressionCompleted || !error.Success()) {
if (log)
log->Printf("Unable to call "
"__introspection_dispatch_thread_get_item_info(), got "
"ExpressionResults %d, error contains %s",
func_call_ret, error.AsCString(""));
error.SetErrorString("Unable to call "
"__introspection_dispatch_thread_get_item_info() for "
"list of queues");
return return_value;
}
return_value.item_buffer_ptr = m_process->ReadUnsignedIntegerFromMemory(
m_get_thread_item_info_return_buffer_addr, 8, LLDB_INVALID_ADDRESS,
error);
if (!error.Success() ||
return_value.item_buffer_ptr == LLDB_INVALID_ADDRESS) {
return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
return return_value;
}
return_value.item_buffer_size = m_process->ReadUnsignedIntegerFromMemory(
m_get_thread_item_info_return_buffer_addr + 8, 8, 0, error);
if (!error.Success()) {
return_value.item_buffer_ptr = LLDB_INVALID_ADDRESS;
return return_value;
}
if (log)
log->Printf("AppleGetThreadItemInfoHandler called "
"__introspection_dispatch_thread_get_item_info (page_to_free "
"== 0x%" PRIx64 ", size = %" PRId64
"), returned page is at 0x%" PRIx64 ", size %" PRId64,
page_to_free, page_to_free_size, return_value.item_buffer_ptr,
return_value.item_buffer_size);
return return_value;
}

View File

@ -0,0 +1,118 @@
//===-- AppleGetThreadItemInfoHandler.h ----------------------------*- C++
//-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef lldb_AppleGetThreadItemInfoHandler_h_
#define lldb_AppleGetThreadItemInfoHandler_h_
// C Includes
// C++ Includes
#include <map>
#include <mutex>
#include <vector>
// Other libraries and framework includes
// Project includes
#include "lldb/Symbol/CompilerType.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-public.h"
// This class will insert a UtilityFunction into the inferior process for
// calling libBacktraceRecording's
// __introspection_dispatch_thread_get_item_info()
// function. The function in the inferior will return a struct by value
// with these members:
//
// struct get_thread_item_info_return_values
// {
// introspection_dispatch_item_info_ref *item_buffer;
// uint64_t item_buffer_size;
// };
//
// The item_buffer pointer is an address in the inferior program's address
// space (item_buffer_size in size) which must be mach_vm_deallocate'd by
// lldb.
//
// The AppleGetThreadItemInfoHandler object should persist so that the
// UtilityFunction
// can be reused multiple times.
namespace lldb_private {
class AppleGetThreadItemInfoHandler {
public:
AppleGetThreadItemInfoHandler(lldb_private::Process *process);
~AppleGetThreadItemInfoHandler();
struct GetThreadItemInfoReturnInfo {
lldb::addr_t item_buffer_ptr; /* the address of the item buffer from
libBacktraceRecording */
lldb::addr_t item_buffer_size; /* the size of the item buffer from
libBacktraceRecording */
GetThreadItemInfoReturnInfo()
: item_buffer_ptr(LLDB_INVALID_ADDRESS), item_buffer_size(0) {}
};
//----------------------------------------------------------
/// Get the information about a work item by calling
/// __introspection_dispatch_thread_get_item_info. If there's a page of
/// memory that needs to be freed, pass in the address and size and it will
/// be freed before getting the list of queues.
///
/// @param [in] thread_id
/// The thread to get the extended backtrace for.
///
/// @param [in] page_to_free
/// An address of an inferior process vm page that needs to be
/// deallocated,
/// LLDB_INVALID_ADDRESS if this is not needed.
///
/// @param [in] page_to_free_size
/// The size of the vm page that needs to be deallocated if an address was
/// passed in to page_to_free.
///
/// @param [out] error
/// This object will be updated with the error status / error string from
/// any failures encountered.
///
/// @returns
/// The result of the inferior function call execution. If there was a
/// failure of any kind while getting
/// the information, the item_buffer_ptr value will be
/// LLDB_INVALID_ADDRESS.
//----------------------------------------------------------
GetThreadItemInfoReturnInfo GetThreadItemInfo(Thread &thread,
lldb::tid_t thread_id,
lldb::addr_t page_to_free,
uint64_t page_to_free_size,
lldb_private::Status &error);
void Detach();
private:
lldb::addr_t
SetupGetThreadItemInfoFunction(Thread &thread,
ValueList &get_thread_item_info_arglist);
static const char *g_get_thread_item_info_function_name;
static const char *g_get_thread_item_info_function_code;
lldb_private::Process *m_process;
std::unique_ptr<UtilityFunction> m_get_thread_item_info_impl_code;
std::mutex m_get_thread_item_info_function_mutex;
lldb::addr_t m_get_thread_item_info_return_buffer_addr;
std::mutex m_get_thread_item_info_retbuffer_mutex;
};
} // using namespace lldb_private
#endif // lldb_AppleGetThreadItemInfoHandler_h_

View File

@ -0,0 +1,17 @@
add_lldb_library(lldbPluginSystemRuntimeMacOSX PLUGIN
AppleGetItemInfoHandler.cpp
AppleGetPendingItemsHandler.cpp
AppleGetQueuesHandler.cpp
AppleGetThreadItemInfoHandler.cpp
SystemRuntimeMacOSX.cpp
LINK_LIBS
lldbBreakpoint
lldbCore
lldbExpression
lldbHost
lldbSymbol
lldbTarget
lldbUtility
lldbPluginProcessUtility
)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,302 @@
//===-- SystemRuntimeMacOSX.h -----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_SystemRuntimeMacOSX_h_
#define liblldb_SystemRuntimeMacOSX_h_
// C Includes
// C++ Includes
#include <mutex>
#include <string>
#include <vector>
// Other libraries and framework include
// Project includes
#include "lldb/Core/ModuleList.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/QueueItem.h"
#include "lldb/Target/SystemRuntime.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/StructuredData.h"
#include "lldb/Utility/UUID.h"
#include "AppleGetItemInfoHandler.h"
#include "AppleGetPendingItemsHandler.h"
#include "AppleGetQueuesHandler.h"
#include "AppleGetThreadItemInfoHandler.h"
class SystemRuntimeMacOSX : public lldb_private::SystemRuntime {
public:
SystemRuntimeMacOSX(lldb_private::Process *process);
~SystemRuntimeMacOSX() override;
//------------------------------------------------------------------
// Static Functions
//------------------------------------------------------------------
static void Initialize();
static void Terminate();
static lldb_private::ConstString GetPluginNameStatic();
static const char *GetPluginDescriptionStatic();
static lldb_private::SystemRuntime *
CreateInstance(lldb_private::Process *process);
//------------------------------------------------------------------
// instance methods
//------------------------------------------------------------------
void Clear(bool clear_process);
void Detach() override;
const std::vector<lldb_private::ConstString> &
GetExtendedBacktraceTypes() override;
lldb::ThreadSP
GetExtendedBacktraceThread(lldb::ThreadSP thread,
lldb_private::ConstString type) override;
lldb::ThreadSP
GetExtendedBacktraceForQueueItem(lldb::QueueItemSP queue_item_sp,
lldb_private::ConstString type) override;
lldb::ThreadSP GetExtendedBacktraceFromItemRef(lldb::addr_t item_ref);
void PopulateQueueList(lldb_private::QueueList &queue_list) override;
void PopulateQueuesUsingLibBTR(lldb::addr_t queues_buffer,
uint64_t queues_buffer_size, uint64_t count,
lldb_private::QueueList &queue_list);
void PopulatePendingQueuesUsingLibBTR(lldb::addr_t items_buffer,
uint64_t items_buffer_size,
uint64_t count,
lldb_private::Queue *queue);
std::string
GetQueueNameFromThreadQAddress(lldb::addr_t dispatch_qaddr) override;
lldb::queue_id_t
GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr) override;
lldb::addr_t GetLibdispatchQueueAddressFromThreadQAddress(
lldb::addr_t dispatch_qaddr) override;
void PopulatePendingItemsForQueue(lldb_private::Queue *queue) override;
void CompleteQueueItem(lldb_private::QueueItem *queue_item,
lldb::addr_t item_ref) override;
lldb::QueueKind GetQueueKind(lldb::addr_t dispatch_queue_addr) override;
void AddThreadExtendedInfoPacketHints(
lldb_private::StructuredData::ObjectSP dict) override;
bool SafeToCallFunctionsOnThisThread(lldb::ThreadSP thread_sp) override;
//------------------------------------------------------------------
// PluginInterface protocol
//------------------------------------------------------------------
lldb_private::ConstString GetPluginName() override;
uint32_t GetPluginVersion() override;
protected:
lldb::user_id_t m_break_id;
mutable std::recursive_mutex m_mutex;
private:
struct libBacktraceRecording_info {
uint16_t queue_info_version;
uint16_t queue_info_data_offset;
uint16_t item_info_version;
uint16_t item_info_data_offset;
libBacktraceRecording_info()
: queue_info_version(0), queue_info_data_offset(0),
item_info_version(0), item_info_data_offset(0) {}
};
// A structure which reflects the data recorded in the
// libBacktraceRecording introspection_dispatch_item_info_s.
struct ItemInfo {
lldb::addr_t item_that_enqueued_this;
lldb::addr_t function_or_block;
uint64_t enqueuing_thread_id;
uint64_t enqueuing_queue_serialnum;
uint64_t target_queue_serialnum;
uint32_t enqueuing_callstack_frame_count;
uint32_t stop_id;
std::vector<lldb::addr_t> enqueuing_callstack;
std::string enqueuing_thread_label;
std::string enqueuing_queue_label;
std::string target_queue_label;
};
// The offsets of different fields of the dispatch_queue_t structure in
// a thread/queue process.
// Based on libdispatch src/queue_private.h, struct dispatch_queue_offsets_s
// With dqo_version 1-3, the dqo_label field is a per-queue value and cannot
// be cached.
// With dqo_version 4 (Mac OS X 10.9 / iOS 7), dqo_label is a constant value
// that can be cached.
struct LibdispatchOffsets {
uint16_t dqo_version;
uint16_t dqo_label;
uint16_t dqo_label_size;
uint16_t dqo_flags;
uint16_t dqo_flags_size;
uint16_t dqo_serialnum;
uint16_t dqo_serialnum_size;
uint16_t dqo_width;
uint16_t dqo_width_size;
uint16_t dqo_running;
uint16_t dqo_running_size;
uint16_t dqo_suspend_cnt; // version 5 and later, starting with Mac OS X
// 10.10/iOS 8
uint16_t dqo_suspend_cnt_size; // version 5 and later, starting with Mac OS
// X 10.10/iOS 8
uint16_t dqo_target_queue; // version 5 and later, starting with Mac OS X
// 10.10/iOS 8
uint16_t dqo_target_queue_size; // version 5 and later, starting with Mac OS
// X 10.10/iOS 8
uint16_t
dqo_priority; // version 5 and later, starting with Mac OS X 10.10/iOS 8
uint16_t dqo_priority_size; // version 5 and later, starting with Mac OS X
// 10.10/iOS 8
LibdispatchOffsets() {
dqo_version = UINT16_MAX;
dqo_flags = UINT16_MAX;
dqo_serialnum = UINT16_MAX;
dqo_label = UINT16_MAX;
dqo_width = UINT16_MAX;
dqo_running = UINT16_MAX;
dqo_suspend_cnt = UINT16_MAX;
dqo_target_queue = UINT16_MAX;
dqo_target_queue = UINT16_MAX;
dqo_priority = UINT16_MAX;
}
bool IsValid() { return dqo_version != UINT16_MAX; }
bool LabelIsValid() { return dqo_label != UINT16_MAX; }
};
struct LibdispatchVoucherOffsets {
uint16_t vo_version;
uint16_t vo_activity_ids_count;
uint16_t vo_activity_ids_count_size;
uint16_t vo_activity_ids_array;
uint16_t vo_activity_ids_array_entry_size;
LibdispatchVoucherOffsets()
: vo_version(UINT16_MAX), vo_activity_ids_count(UINT16_MAX),
vo_activity_ids_count_size(UINT16_MAX),
vo_activity_ids_array(UINT16_MAX),
vo_activity_ids_array_entry_size(UINT16_MAX) {}
bool IsValid() { return vo_version != UINT16_MAX; }
};
struct LibdispatchTSDIndexes {
uint16_t dti_version;
uint64_t dti_queue_index;
uint64_t dti_voucher_index;
uint64_t dti_qos_class_index;
LibdispatchTSDIndexes()
: dti_version(UINT16_MAX), dti_queue_index(UINT64_MAX),
dti_voucher_index(UINT64_MAX), dti_qos_class_index(UINT64_MAX) {}
bool IsValid() { return dti_version != UINT16_MAX; }
};
struct LibpthreadOffsets {
uint16_t plo_version;
uint16_t plo_pthread_tsd_base_offset;
uint16_t plo_pthread_tsd_base_address_offset;
uint16_t plo_pthread_tsd_entry_size;
LibpthreadOffsets()
: plo_version(UINT16_MAX), plo_pthread_tsd_base_offset(UINT16_MAX),
plo_pthread_tsd_base_address_offset(UINT16_MAX),
plo_pthread_tsd_entry_size(UINT16_MAX) {}
bool IsValid() { return plo_version != UINT16_MAX; }
};
// The libBacktraceRecording function
// __introspection_dispatch_queue_get_pending_items has
// two forms. It can either return a simple array of item_refs (void *) size
// or it can return
// a header with uint32_t version, a uint32_t size of item, and then an array
// of item_refs (void*)
// and code addresses (void*) for all the pending blocks.
struct ItemRefAndCodeAddress {
lldb::addr_t item_ref;
lldb::addr_t code_address;
};
struct PendingItemsForQueue {
bool new_style; // new-style means both item_refs and code_addresses avail
// old-style means only item_refs is filled in
std::vector<ItemRefAndCodeAddress> item_refs_and_code_addresses;
};
bool BacktraceRecordingHeadersInitialized();
void ReadLibdispatchOffsetsAddress();
void ReadLibdispatchOffsets();
void ReadLibpthreadOffsetsAddress();
void ReadLibpthreadOffsets();
void ReadLibdispatchTSDIndexesAddress();
void ReadLibdispatchTSDIndexes();
PendingItemsForQueue GetPendingItemRefsForQueue(lldb::addr_t queue);
ItemInfo ExtractItemInfoFromBuffer(lldb_private::DataExtractor &extractor);
lldb_private::AppleGetQueuesHandler m_get_queues_handler;
lldb_private::AppleGetPendingItemsHandler m_get_pending_items_handler;
lldb_private::AppleGetItemInfoHandler m_get_item_info_handler;
lldb_private::AppleGetThreadItemInfoHandler m_get_thread_item_info_handler;
lldb::addr_t m_page_to_free;
uint64_t m_page_to_free_size;
libBacktraceRecording_info m_lib_backtrace_recording_info;
lldb::addr_t m_dispatch_queue_offsets_addr;
struct LibdispatchOffsets m_libdispatch_offsets;
lldb::addr_t m_libpthread_layout_offsets_addr;
struct LibpthreadOffsets m_libpthread_offsets;
lldb::addr_t m_dispatch_tsd_indexes_addr;
struct LibdispatchTSDIndexes m_libdispatch_tsd_indexes;
lldb::addr_t m_dispatch_voucher_offsets_addr;
struct LibdispatchVoucherOffsets m_libdispatch_voucher_offsets;
DISALLOW_COPY_AND_ASSIGN(SystemRuntimeMacOSX);
};
#endif // liblldb_SystemRuntimeMacOSX_h_