2017-06-05 02:02:04 +02:00
|
|
|
/*
|
2019-05-03 21:06:10 +02:00
|
|
|
* This file is part of Luma3DS.
|
2019-02-25 02:04:32 +01:00
|
|
|
* Copyright (C) 2016-2019 Aurora Wright, TuxSH
|
2017-06-05 02:02:04 +02:00
|
|
|
*
|
2019-05-03 21:06:10 +02:00
|
|
|
* SPDX-License-Identifier: (MIT OR GPL-2.0-or-later)
|
2017-06-05 02:02:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <3ds/types.h>
|
|
|
|
#include <3ds/svc.h>
|
|
|
|
#include <3ds/synchronization.h>
|
|
|
|
#include <3ds/result.h>
|
2019-04-13 19:18:47 +02:00
|
|
|
#include "pmdbgext.h"
|
2017-06-05 02:02:04 +02:00
|
|
|
#include "sock_util.h"
|
|
|
|
#include "memory.h"
|
2019-04-27 16:50:47 +02:00
|
|
|
#include "ifile.h"
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
#define MAX_DEBUG 3
|
|
|
|
#define MAX_DEBUG_THREAD 127
|
|
|
|
#define MAX_BREAKPOINT 256
|
2019-04-27 16:50:47 +02:00
|
|
|
|
2019-04-28 02:14:31 +02:00
|
|
|
#define MAX_TIO_OPEN_FILE 32
|
2019-04-27 16:50:47 +02:00
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
// 512+24 is the ideal size as IDA will try to read exactly 0x100 bytes at a time. Add 4 to this, for $#<checksum>, see below.
|
|
|
|
// IDA seems to want additional bytes as well.
|
|
|
|
// 1024 is fine enough to put all regs in the 'T' stop reply packets
|
2019-04-28 02:14:31 +02:00
|
|
|
#define GDB_BUF_LEN 2048
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
#define GDB_HANDLER(name) GDB_Handle##name
|
|
|
|
#define GDB_QUERY_HANDLER(name) GDB_HANDLER(Query##name)
|
|
|
|
#define GDB_VERBOSE_HANDLER(name) GDB_HANDLER(Verbose##name)
|
|
|
|
|
|
|
|
#define GDB_DECLARE_HANDLER(name) int GDB_HANDLER(name)(GDBContext *ctx)
|
|
|
|
#define GDB_DECLARE_QUERY_HANDLER(name) GDB_DECLARE_HANDLER(Query##name)
|
|
|
|
#define GDB_DECLARE_VERBOSE_HANDLER(name) GDB_DECLARE_HANDLER(Verbose##name)
|
|
|
|
|
|
|
|
typedef struct Breakpoint
|
|
|
|
{
|
|
|
|
u32 address;
|
|
|
|
u32 savedInstruction;
|
|
|
|
u8 instructionSize;
|
|
|
|
bool persistent;
|
|
|
|
} Breakpoint;
|
|
|
|
|
2019-04-14 21:48:15 +02:00
|
|
|
typedef struct PackedGdbHioRequest
|
|
|
|
{
|
|
|
|
char magic[4]; // "GDB\x00"
|
|
|
|
u32 version;
|
|
|
|
|
|
|
|
// Request
|
|
|
|
char functionName[16+1];
|
|
|
|
char paramFormat[8+1];
|
|
|
|
|
2019-04-17 00:18:12 +02:00
|
|
|
u64 parameters[8];
|
|
|
|
size_t stringLengths[8];
|
2019-04-14 21:48:15 +02:00
|
|
|
|
|
|
|
// Return
|
|
|
|
int retval;
|
|
|
|
int gdbErrno;
|
|
|
|
bool ctrlC;
|
|
|
|
} PackedGdbHioRequest;
|
|
|
|
|
2019-04-27 16:50:47 +02:00
|
|
|
typedef struct GdbTioFileInfo
|
|
|
|
{
|
|
|
|
IFile f;
|
|
|
|
int flags;
|
|
|
|
} GdbTioFileInfo;
|
|
|
|
|
2019-04-10 23:38:10 +02:00
|
|
|
enum
|
2017-06-05 02:02:04 +02:00
|
|
|
{
|
|
|
|
GDB_FLAG_SELECTED = 1,
|
|
|
|
GDB_FLAG_USED = 2,
|
2019-04-10 23:38:10 +02:00
|
|
|
GDB_FLAG_ALLOCATED_MASK = GDB_FLAG_SELECTED | GDB_FLAG_USED,
|
2019-04-12 23:40:51 +02:00
|
|
|
GDB_FLAG_EXTENDED_REMOTE = 4,
|
|
|
|
GDB_FLAG_NOACK = 8,
|
2019-04-13 19:18:47 +02:00
|
|
|
GDB_FLAG_PROC_RESTART_MASK = GDB_FLAG_NOACK | GDB_FLAG_EXTENDED_REMOTE | GDB_FLAG_USED,
|
2019-04-12 23:40:51 +02:00
|
|
|
GDB_FLAG_PROCESS_CONTINUING = 16,
|
|
|
|
GDB_FLAG_TERMINATE_PROCESS = 32,
|
|
|
|
GDB_FLAG_ATTACHED_AT_START = 64,
|
2019-04-13 19:18:47 +02:00
|
|
|
GDB_FLAG_CREATED = 128,
|
2019-04-10 23:38:10 +02:00
|
|
|
};
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
typedef enum GDBState
|
|
|
|
{
|
|
|
|
GDB_STATE_DISCONNECTED,
|
|
|
|
GDB_STATE_CONNECTED,
|
2019-04-13 19:18:47 +02:00
|
|
|
GDB_STATE_ATTACHED,
|
|
|
|
GDB_STATE_DETACHING,
|
2017-06-05 02:02:04 +02:00
|
|
|
} GDBState;
|
|
|
|
|
|
|
|
typedef struct ThreadInfo
|
|
|
|
{
|
|
|
|
u32 id;
|
|
|
|
u32 tls;
|
|
|
|
} ThreadInfo;
|
|
|
|
|
2019-03-31 22:58:36 +02:00
|
|
|
struct GDBServer;
|
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
typedef struct GDBContext
|
|
|
|
{
|
|
|
|
sock_ctx super;
|
2019-03-31 22:58:36 +02:00
|
|
|
struct GDBServer *parent;
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
RecursiveLock lock;
|
2019-03-31 00:24:45 +01:00
|
|
|
u16 localPort;
|
|
|
|
|
2019-04-10 23:38:10 +02:00
|
|
|
u32 flags;
|
2017-06-05 02:02:04 +02:00
|
|
|
GDBState state;
|
2019-04-13 19:18:47 +02:00
|
|
|
bool noAckSent;
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
u32 pid;
|
|
|
|
Handle debug;
|
2019-04-13 19:18:47 +02:00
|
|
|
|
|
|
|
// vRun and R (restart) info:
|
|
|
|
FS_ProgramInfo launchedProgramInfo;
|
|
|
|
u32 launchedProgramLaunchFlags;
|
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
ThreadInfo threadInfos[MAX_DEBUG_THREAD];
|
|
|
|
u32 nbThreads;
|
|
|
|
u32 currentThreadId, selectedThreadId, selectedThreadIdForContinuing;
|
2019-03-31 20:01:16 +02:00
|
|
|
u32 totalNbCreatedThreads;
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2019-03-31 22:58:36 +02:00
|
|
|
Handle processAttachedEvent, continuedEvent;
|
2017-06-05 02:02:04 +02:00
|
|
|
Handle eventToWaitFor;
|
|
|
|
|
|
|
|
bool catchThreadEvents;
|
|
|
|
bool processEnded, processExited;
|
|
|
|
|
|
|
|
DebugEventInfo latestDebugEvent;
|
|
|
|
DebugFlags continueFlags;
|
|
|
|
u32 svcMask[8];
|
|
|
|
|
|
|
|
u32 nbBreakpoints;
|
|
|
|
Breakpoint breakpoints[MAX_BREAKPOINT];
|
|
|
|
|
|
|
|
u32 nbWatchpoints;
|
|
|
|
u32 watchpoints[2];
|
|
|
|
|
2019-04-14 21:48:15 +02:00
|
|
|
u32 currentHioRequestTargetAddr;
|
|
|
|
PackedGdbHioRequest currentHioRequest;
|
|
|
|
|
2019-04-27 16:50:47 +02:00
|
|
|
GdbTioFileInfo openTioFileInfos[MAX_TIO_OPEN_FILE];
|
|
|
|
u32 numOpenTioFiles;
|
|
|
|
|
2017-11-26 02:18:59 +01:00
|
|
|
bool enableExternalMemoryAccess;
|
2017-06-05 02:02:04 +02:00
|
|
|
char *commandData, *commandEnd;
|
|
|
|
int latestSentPacketSize;
|
|
|
|
char buffer[GDB_BUF_LEN + 4];
|
|
|
|
|
|
|
|
char threadListData[0x800];
|
|
|
|
u32 threadListDataPos;
|
|
|
|
|
|
|
|
char memoryOsInfoXmlData[0x800];
|
2019-04-28 02:14:31 +02:00
|
|
|
char processesOsInfoXmlData[0x1800];
|
2017-06-05 02:02:04 +02:00
|
|
|
} GDBContext;
|
|
|
|
|
|
|
|
typedef int (*GDBCommandHandler)(GDBContext *ctx);
|
|
|
|
|
|
|
|
void GDB_InitializeContext(GDBContext *ctx);
|
|
|
|
void GDB_FinalizeContext(GDBContext *ctx);
|
2019-03-31 22:58:36 +02:00
|
|
|
|
|
|
|
Result GDB_AttachToProcess(GDBContext *ctx);
|
|
|
|
void GDB_DetachFromProcess(GDBContext *ctx);
|
2019-04-13 19:18:47 +02:00
|
|
|
Result GDB_CreateProcess(GDBContext *ctx, const FS_ProgramInfo *progInfo, u32 launchFlags);
|
2019-03-31 22:58:36 +02:00
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
GDB_DECLARE_HANDLER(Unsupported);
|
2019-04-12 23:40:51 +02:00
|
|
|
GDB_DECLARE_HANDLER(EnableExtendedMode);
|