Moving debugger.

This commit is contained in:
Ben Vanik
2015-05-04 20:57:46 -07:00
parent b07d5b8ed3
commit 499bed21c0
18 changed files with 200 additions and 67 deletions

View File

@@ -30,9 +30,11 @@ void X64Function::Setup(void* machine_code, size_t code_size) {
code_size_ = code_size;
}
int X64Function::AddBreakpointImpl(Breakpoint* breakpoint) { return 0; }
int X64Function::AddBreakpointImpl(debug::Breakpoint* breakpoint) { return 0; }
int X64Function::RemoveBreakpointImpl(Breakpoint* breakpoint) { return 0; }
int X64Function::RemoveBreakpointImpl(debug::Breakpoint* breakpoint) {
return 0;
}
int X64Function::CallImpl(ThreadState* thread_state, uint32_t return_address) {
auto backend =

View File

@@ -30,8 +30,8 @@ class X64Function : public Function {
void Setup(void* machine_code, size_t code_size);
protected:
virtual int AddBreakpointImpl(Breakpoint* breakpoint);
virtual int RemoveBreakpointImpl(Breakpoint* breakpoint);
virtual int AddBreakpointImpl(debug::Breakpoint* breakpoint);
virtual int RemoveBreakpointImpl(debug::Breakpoint* breakpoint);
virtual int CallImpl(ThreadState* thread_state, uint32_t return_address);
private:

View File

@@ -1,199 +0,0 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "xenia/cpu/debugger.h"
#include <mutex>
#include "xenia/cpu/function.h"
#include "xenia/cpu/processor.h"
namespace xe {
namespace cpu {
Breakpoint::Breakpoint(Type type, uint32_t address)
: type_(type), address_(address) {}
Breakpoint::~Breakpoint() = default;
Debugger::Debugger(Processor* processor) : processor_(processor) {}
Debugger::~Debugger() = default;
int Debugger::SuspendAllThreads(uint32_t timeout_ms) {
std::lock_guard<std::mutex> guard(threads_lock_);
int result = 0;
for (auto thread_state : threads_) {
if (thread_state.second->Suspend(timeout_ms)) {
result = 1;
}
}
return result;
}
int Debugger::ResumeThread(uint32_t thread_id) {
std::lock_guard<std::mutex> guard(threads_lock_);
auto it = threads_.find(thread_id);
if (it == threads_.end()) {
return 1;
}
// Found thread. Note that it could be deleted as soon as we unlock.
ThreadState* thread_state = it->second;
int result = thread_state->Resume();
return result;
}
int Debugger::ResumeAllThreads(bool force) {
std::lock_guard<std::mutex> guard(threads_lock_);
int result = 0;
for (auto thread_state : threads_) {
if (thread_state.second->Resume(force)) {
result = 1;
}
}
return result;
}
void Debugger::ForEachThread(std::function<void(ThreadState*)> callback) {
std::lock_guard<std::mutex> guard(threads_lock_);
for (auto thread_state : threads_) {
callback(thread_state.second);
}
}
int Debugger::AddBreakpoint(Breakpoint* breakpoint) {
// Add to breakpoints map.
{
std::lock_guard<std::mutex> guard(breakpoints_lock_);
breakpoints_.insert(
std::pair<uint32_t, Breakpoint*>(breakpoint->address(), breakpoint));
}
// Find all functions that contain the breakpoint address.
auto fns = processor_->FindFunctionsWithAddress(breakpoint->address());
// Add.
for (auto fn : fns) {
if (fn->AddBreakpoint(breakpoint)) {
return 1;
}
}
return 0;
}
int Debugger::RemoveBreakpoint(Breakpoint* breakpoint) {
// Remove from breakpoint map.
{
std::lock_guard<std::mutex> guard(breakpoints_lock_);
auto range = breakpoints_.equal_range(breakpoint->address());
if (range.first == range.second) {
return 1;
}
bool found = false;
for (auto it = range.first; it != range.second; ++it) {
if (it->second == breakpoint) {
breakpoints_.erase(it);
found = true;
break;
}
}
if (!found) {
return 1;
}
}
// Find all functions that have the breakpoint set.
auto fns = processor_->FindFunctionsWithAddress(breakpoint->address());
// Remove.
for (auto fn : fns) {
fn->RemoveBreakpoint(breakpoint);
}
return 0;
}
void Debugger::FindBreakpoints(uint32_t address,
std::vector<Breakpoint*>& out_breakpoints) {
std::lock_guard<std::mutex> guard(breakpoints_lock_);
out_breakpoints.clear();
auto range = breakpoints_.equal_range(address);
if (range.first == range.second) {
return;
}
for (auto it = range.first; it != range.second; ++it) {
Breakpoint* breakpoint = it->second;
out_breakpoints.push_back(breakpoint);
}
}
void Debugger::OnThreadCreated(ThreadState* thread_state) {
std::lock_guard<std::mutex> guard(threads_lock_);
threads_[thread_state->thread_id()] = thread_state;
}
void Debugger::OnThreadDestroyed(ThreadState* thread_state) {
std::lock_guard<std::mutex> guard(threads_lock_);
auto it = threads_.find(thread_state->thread_id());
if (it != threads_.end()) {
threads_.erase(it);
}
}
void Debugger::OnFunctionDefined(FunctionInfo* symbol_info,
Function* function) {
// Man, I'd love not to take this lock.
std::vector<Breakpoint*> breakpoints;
{
std::lock_guard<std::mutex> guard(breakpoints_lock_);
for (uint32_t address = symbol_info->address();
address <= symbol_info->end_address(); address += 4) {
auto range = breakpoints_.equal_range(address);
if (range.first == range.second) {
continue;
}
for (auto it = range.first; it != range.second; ++it) {
Breakpoint* breakpoint = it->second;
breakpoints.push_back(breakpoint);
}
}
}
if (breakpoints.size()) {
// Breakpoints to add!
for (auto breakpoint : breakpoints) {
function->AddBreakpoint(breakpoint);
}
}
}
void Debugger::OnBreakpointHit(ThreadState* thread_state,
Breakpoint* breakpoint) {
// Suspend all threads immediately.
SuspendAllThreads();
// Notify listeners.
BreakpointHitEvent e(this, thread_state, breakpoint);
breakpoint_hit(e);
// Note that we stay suspended.
}
} // namespace cpu
} // namespace xe

View File

@@ -1,122 +0,0 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_CPU_DEBUGGER_H_
#define XENIA_CPU_DEBUGGER_H_
#include <map>
#include <mutex>
#include <string>
#include <unordered_map>
#include "xenia/base/delegate.h"
#include "xenia/cpu/thread_state.h"
namespace xe {
namespace cpu {
class Debugger;
class Function;
class FunctionInfo;
class Processor;
class Breakpoint {
public:
enum Type {
TEMP_TYPE,
CODE_TYPE,
};
public:
Breakpoint(Type type, uint32_t address);
~Breakpoint();
Type type() const { return type_; }
uint32_t address() const { return address_; }
const char* id() const { return id_.c_str(); }
void set_id(const char* id) { id_ = std::string(id); }
private:
Type type_;
uint32_t address_;
std::string id_;
};
class DebugEvent {
public:
DebugEvent(Debugger* debugger) : debugger_(debugger) {}
virtual ~DebugEvent() = default;
Debugger* debugger() const { return debugger_; }
protected:
Debugger* debugger_;
};
class BreakpointHitEvent : public DebugEvent {
public:
BreakpointHitEvent(Debugger* debugger, ThreadState* thread_state,
Breakpoint* breakpoint)
: DebugEvent(debugger),
thread_state_(thread_state),
breakpoint_(breakpoint) {}
~BreakpointHitEvent() override = default;
ThreadState* thread_state() const { return thread_state_; }
Breakpoint* breakpoint() const { return breakpoint_; }
protected:
ThreadState* thread_state_;
Breakpoint* breakpoint_;
};
class Debugger {
public:
Debugger(Processor* processor);
~Debugger();
Processor* processor() const { return processor_; }
int SuspendAllThreads(uint32_t timeout_ms = UINT_MAX);
int ResumeThread(uint32_t thread_id);
int ResumeAllThreads(bool force = false);
void ForEachThread(std::function<void(ThreadState*)> callback);
int AddBreakpoint(Breakpoint* breakpoint);
int RemoveBreakpoint(Breakpoint* breakpoint);
void FindBreakpoints(uint32_t address,
std::vector<Breakpoint*>& out_breakpoints);
// TODO(benvanik): utility functions for modification (make function ignored,
// etc).
void OnThreadCreated(ThreadState* thread_state);
void OnThreadDestroyed(ThreadState* thread_state);
void OnFunctionDefined(FunctionInfo* symbol_info, Function* function);
void OnBreakpointHit(ThreadState* thread_state, Breakpoint* breakpoint);
public:
Delegate<BreakpointHitEvent> breakpoint_hit;
private:
Processor* processor_;
std::mutex threads_lock_;
std::unordered_map<uint32_t, ThreadState*> threads_;
std::mutex breakpoints_lock_;
std::multimap<uint32_t, Breakpoint*> breakpoints_;
};
} // namespace cpu
} // namespace xe
#endif // XENIA_CPU_DEBUGGER_H_

View File

@@ -19,8 +19,6 @@ namespace xe {
namespace cpu {
namespace frontend {
using xe::cpu::Runtime;
void InitializeIfNeeded();
void CleanupOnShutdown();

View File

@@ -10,13 +10,14 @@
#include "xenia/cpu/function.h"
#include "xenia/base/logging.h"
#include "xenia/cpu/debugger.h"
#include "xenia/cpu/symbol_info.h"
#include "xenia/cpu/thread_state.h"
namespace xe {
namespace cpu {
using xe::debug::Breakpoint;
Function::Function(FunctionInfo* symbol_info)
: address_(symbol_info->address()), symbol_info_(symbol_info) {}

View File

@@ -16,11 +16,11 @@
#include "xenia/cpu/debug_info.h"
#include "xenia/cpu/thread_state.h"
#include "xenia/debug/breakpoint.h"
namespace xe {
namespace cpu {
class Breakpoint;
class FunctionInfo;
class Function {
@@ -36,15 +36,15 @@ class Function {
debug_info_ = std::move(debug_info);
}
int AddBreakpoint(Breakpoint* breakpoint);
int RemoveBreakpoint(Breakpoint* breakpoint);
int AddBreakpoint(debug::Breakpoint* breakpoint);
int RemoveBreakpoint(debug::Breakpoint* breakpoint);
int Call(ThreadState* thread_state, uint32_t return_address);
protected:
Breakpoint* FindBreakpoint(uint32_t address);
virtual int AddBreakpointImpl(Breakpoint* breakpoint) { return 0; }
virtual int RemoveBreakpointImpl(Breakpoint* breakpoint) { return 0; }
debug::Breakpoint* FindBreakpoint(uint32_t address);
virtual int AddBreakpointImpl(debug::Breakpoint* breakpoint) { return 0; }
virtual int RemoveBreakpointImpl(debug::Breakpoint* breakpoint) { return 0; }
virtual int CallImpl(ThreadState* thread_state, uint32_t return_address) = 0;
protected:
@@ -54,7 +54,7 @@ class Function {
// TODO(benvanik): move elsewhere? DebugData?
std::mutex lock_;
std::vector<Breakpoint*> breakpoints_;
std::vector<debug::Breakpoint*> breakpoints_;
};
} // namespace cpu

View File

@@ -22,6 +22,7 @@
#include "xenia/cpu/module.h"
#include "xenia/cpu/thread_state.h"
#include "xenia/cpu/xex_module.h"
#include "xenia/debug/debugger.h"
#include "xenia/profiling.h"
// TODO(benvanik): based on compiler support
@@ -108,7 +109,7 @@ int Processor::Setup() {
assert_not_null(memory_);
// Create debugger first. Other types hook up to it.
debugger_.reset(new Debugger(this));
debugger_.reset(new xe::debug::Debugger(this));
std::unique_ptr<Module> builtin_module(new BuiltinModule(this));
builtin_module_ = builtin_module.get();

View File

@@ -14,7 +14,6 @@
#include <vector>
#include "xenia/cpu/backend/backend.h"
#include "xenia/cpu/debugger.h"
#include "xenia/cpu/entry_table.h"
#include "xenia/cpu/export_resolver.h"
#include "xenia/cpu/frontend/ppc_frontend.h"
@@ -23,10 +22,15 @@
#include "xenia/cpu/thread_state.h"
#include "xenia/memory.h"
namespace xe {
namespace debug {
class Debugger;
} // namespace debug
} // namespace xe
namespace xe {
namespace cpu {
class Runtime;
class ThreadState;
class XexModule;
@@ -43,7 +47,7 @@ class Processor {
~Processor();
Memory* memory() const { return memory_; }
Debugger* debugger() const { return debugger_.get(); }
debug::Debugger* debugger() const { return debugger_.get(); }
frontend::PPCFrontend* frontend() const { return frontend_.get(); }
backend::Backend* backend() const { return backend_.get(); }
ExportResolver* export_resolver() const { return export_resolver_; }
@@ -85,7 +89,7 @@ class Processor {
uint32_t debug_info_flags_;
uint32_t trace_flags_;
std::unique_ptr<Debugger> debugger_;
std::unique_ptr<debug::Debugger> debugger_;
std::unique_ptr<frontend::PPCFrontend> frontend_;
std::unique_ptr<backend::Backend> backend_;

View File

@@ -6,8 +6,6 @@
'cpu.h',
'debug_info.cc',
'debug_info.h',
'debugger.cc',
'debugger.h',
'entry_table.cc',
'entry_table.h',
'export_resolver.cc',

View File

@@ -12,6 +12,7 @@
#include "xenia/base/assert.h"
#include "xenia/base/threading.h"
#include "xenia/cpu/processor.h"
#include "xenia/debug/debugger.h"
namespace xe {
namespace cpu {