-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpsp-dbg-hlp.c
236 lines (191 loc) · 6.13 KB
/
psp-dbg-hlp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/** @file
* PSP Emulator - Debug helper API.
*/
/*
* Copyright (C) 2020 Alexander Eichner <alexander.eichner@campus.tu-berlin.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <common/status.h>
#include <psp-dbg-hlp.h>
/**
* Comand descriptor record.
*/
typedef struct PSPDBGHLPCMDREC
{
/** Pointer to the next record. */
struct PSPDBGHLPCMDREC *pNext;
/** Pointer to the array of registered commands. */
PCDBGHLPCMD paCmds;
/** Number of commands in the array. */
uint32_t cCmds;
/** Opaque user data for the commands. */
void *pvUser;
} PSPDBGHLPCMDREC;
/** Pointer to a command descriptor record. */
typedef PSPDBGHLPCMDREC *PPSPDBGHLPCMDREC;
/** Pointer to a const command descriptor record. */
typedef const PSPDBGHLPCMDREC *PCPSPDBGHLPCMDREC;
/**
* The debug helper module instance data.
*/
typedef struct PSPDBGHLPINT
{
/** Reference count. */
volatile uint32_t cRefs;
/** Overall number of commands registered. */
uint32_t cCmds;
/** Head to the list of registered commands. */
PPSPDBGHLPCMDREC pCmdsHead;
} PSPDBGHLPINT;
/** Pointer to the tracer instance data. */
typedef PSPDBGHLPINT *PPSPDBGHLPINT;
/** Pointer to a const tracer instance. */
typedef const PSPDBGHLPINT *PCPSPDBGHLPINT;
/**
* Destroys the given debug helper module instance.
*
* @returns nothing.
* @param pThis The debug helper module instance.
*/
static void pspEmuDbgHlpDestroy(PPSPDBGHLPINT pThis)
{
while (pThis->pCmdsHead)
{
PPSPDBGHLPCMDREC pCmdRec = pThis->pCmdsHead;
pThis->pCmdsHead = pCmdRec->pNext;
free(pCmdRec);
}
free(pThis);
}
/**
* Tries to find the given command in the set of registered commands and returns the
* descriptor or NULL if not found.
*
* @returns Pointer to the command descriptor or NULL if not found.
* @param pThis The debug helper module instance.
* @param pszCmd The command to look for.
* @param ppvUser Where to store the opaque user data the command was registered with if found, optional.
*/
static PCDBGHLPCMD pspEmuDbgHlpCmdFind(PPSPDBGHLPINT pThis, const char *pszCmd, void **ppvUser)
{
PPSPDBGHLPCMDREC pCmdRecCur = pThis->pCmdsHead;
while (pCmdRecCur)
{
for (uint32_t i = 0; i < pCmdRecCur->cCmds; i++)
{
if (!strcmp(pCmdRecCur->paCmds[i].pszCmd, pszCmd))
{
if (ppvUser)
*ppvUser = pCmdRecCur->pvUser;
return &pCmdRecCur->paCmds[i];
}
}
pCmdRecCur = pCmdRecCur->pNext;
}
return NULL; /* Nothing found. */
}
int PSPEmuDbgHlpCreate(PPSPDBGHLP phDbgHlp)
{
int rc = STS_INF_SUCCESS;
PPSPDBGHLPINT pThis = (PPSPDBGHLPINT)calloc(1, sizeof(*pThis));
if (pThis)
{
pThis->cRefs = 1;
pThis->cCmds = 0;
pThis->pCmdsHead = NULL;
*phDbgHlp = pThis;
}
else
rc = STS_ERR_NO_MEMORY;
return rc;
}
uint32_t PSPEmuDbgHlpRetain(PSPDBGHLP hDbgHlp)
{
PPSPDBGHLPINT pThis = hDbgHlp;
return ++pThis->cRefs; /** @todo Atomics when going multi threaded. */
}
uint32_t PSPEmuDbgHlpRelease(PSPDBGHLP hDbgHlp)
{
PPSPDBGHLPINT pThis = hDbgHlp;
uint32_t cRefs = --pThis->cRefs; /** @todo Atomics when going multi threaded. */
if (!cRefs)
pspEmuDbgHlpDestroy(pThis);
return cRefs;
}
int PSPEmuDbgHlpCmdRegister(PSPDBGHLP hDbgHlp, PCDBGHLPCMD paCmds, uint32_t cCmds, void *pvUser)
{
PPSPDBGHLPINT pThis = hDbgHlp;
/* No debugger, no command registration. */
if (!pThis)
return STS_INF_SUCCESS;
/* Check that there is no command existing with the same name already. */
for (uint32_t i = 0; i < cCmds; i++)
{
if (pspEmuDbgHlpCmdFind(pThis, paCmds[i].pszCmd, NULL /*ppvUser*/) != NULL)
return STS_ERR_INVALID_PARAMETER; /** @todo New status code. */
}
int rc = STS_INF_SUCCESS;
PPSPDBGHLPCMDREC pCmdRec = (PPSPDBGHLPCMDREC)calloc(1, sizeof(*pCmdRec));
if (pCmdRec)
{
pCmdRec->paCmds = paCmds;
pCmdRec->cCmds = cCmds;
pCmdRec->pvUser = pvUser;
pCmdRec->pNext = pThis->pCmdsHead;
pThis->pCmdsHead = pCmdRec;
pThis->cCmds += cCmds;
}
else
rc = STS_ERR_NO_MEMORY;
return rc;
}
int PSPEmuDbgHlpCmdDeregister(PSPDBGHLP hDbgHlp, PCDBGHLPCMD paCmds)
{
return STS_ERR_GENERAL_ERROR; /** @todo */
}
int PSPEmuDbgHlpCmdExec(PSPDBGHLP hDbgHlp, const char *pszCmd, const char *pszArgs, PCPSPDBGOUTHLP pOutHlp)
{
PPSPDBGHLPINT pThis = hDbgHlp;
void *pvCmdUser = NULL;
PCDBGHLPCMD pCmd = pspEmuDbgHlpCmdFind(pThis, pszCmd, &pvCmdUser);
int rc = STS_ERR_NOT_FOUND;
if (pCmd)
rc = pCmd->pfnCmd(pThis, pOutHlp, pszArgs, pvCmdUser);
return rc;
}
uint32_t PSPEmuDbgHlpCmdGetCount(PSPDBGHLP hDbgHlp)
{
PPSPDBGHLPINT pThis = hDbgHlp;
return pThis->cCmds;
}
int PSPEmuDbgHlpCmdQueryDesc(PSPDBGHLP hDbgHlp, PDBGHLPCMD paCmds, uint32_t cCmds, uint32_t *pcCmds)
{
PPSPDBGHLPINT pThis = hDbgHlp;
if (!paCmds || !cCmds)
return STS_ERR_INVALID_PARAMETER;
if (pcCmds)
*pcCmds = MIN(pThis->cCmds, cCmds);
PPSPDBGHLPCMDREC pRecCur = pThis->pCmdsHead;
while (pRecCur)
{
size_t cThisCopy = MIN(cCmds, pRecCur->cCmds);
memcpy(&paCmds[0], &pRecCur->paCmds[0], cThisCopy * sizeof(*paCmds));
pRecCur = pRecCur->pNext;
}
return STS_INF_SUCCESS;
}