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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
|
/* $Id: DBGCIo.cpp $ */
/** @file
* DBGC - Debugger Console, I/O provider handling.
*/
/*
* Copyright (C) 2020-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* 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, in version 3 of the
* License.
*
* 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 <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <VBox/dbg.h>
#include <VBox/vmm/cfgm.h>
#include <VBox/err.h>
#include <iprt/mem.h>
#include <iprt/thread.h>
#include <VBox/log.h>
#include <iprt/assert.h>
#include <iprt/string.h>
#include "DBGCIoProvInternal.h"
#include "DBGCInternal.h"
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/**
* Stub descriptor.
*/
typedef struct DBGCSTUB
{
/** Name of the stub. */
const char *pszName;
/** Flag whether this is an ASCII based protocol which requires some newline handling. */
bool fAscii;
/**
* The runloop callback.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param pIo Pointer to the I/O callback table.
* @param fFlags Flags for the runloop, MBZ for now.
*/
DECLCALLBACKMEMBER(int, pfnRunloop, (PUVM pUVM, PCDBGCIO pIo, unsigned fFlags));
} DBGCSTUB;
/** Pointer to a stub descriptor. */
typedef DBGCSTUB *PDBGCSTUB;
/** Pointer to a const stub descriptor. */
typedef const DBGCSTUB *PCDBGCSTUB;
/** Pointer to the instance data of the debug console I/O. */
typedef struct DBGCIOINT *PDBGCIOINT;
/**
* A single debug console I/O service.
*/
typedef struct DBGCIOSVC
{
/** Pointer to the owning structure. */
PDBGCIOINT pDbgcIo;
/** The user mode VM handle this service belongs to. */
PUVM pUVM;
/** The I/O provider registration record for this service. */
PCDBGCIOPROVREG pIoProvReg;
/** The I/O provider instance. */
DBGCIOPROV hDbgcIoProv;
/** The stub type. */
PCDBGCSTUB pStub;
/** The thread managing the service. */
RTTHREAD hThreadSvc;
/** Pointer to the I/O callback table currently being served. */
PCDBGCIO pIo;
/** The wrapping DBGC I/O callback table for ASCII based protocols. */
DBGCIO IoAscii;
} DBGCIOSVC;
/** Pointer to a single debug console I/O service. */
typedef DBGCIOSVC *PDBGCIOSVC;
/** Poitner to a const single debug console I/O service. */
typedef const DBGCIOSVC *PCDBGCIOSVC;
/**
* Debug console I/O instance data.
*/
typedef struct DBGCIOINT
{
/** Number of configured I/O service instances. */
volatile uint32_t cSvcsCfg;
/** Number of running I/O service instances. */
volatile uint32_t cSvcsRunning;
/** Flag whether the services were asked to shut down. */
volatile bool fShutdown;
/** Array of active I/O service instances. */
RT_FLEXIBLE_ARRAY_EXTENSION
DBGCIOSVC aSvc[RT_FLEXIBLE_ARRAY];
} DBGCIOINT;
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/**
* Array of supported I/O providers.
*/
static PCDBGCIOPROVREG g_aIoProv[] =
{
&g_DbgcIoProvTcp,
&g_DbgcIoProvUdp,
&g_DbgcIoProvIpc
};
static DECLCALLBACK(int) dbgcIoNativeStubRunloop(PUVM pUVM, PCDBGCIO pIo, unsigned fFlags);
/**
* Array of supported stubs.
*/
static const DBGCSTUB g_aStubs[] =
{
/** pszName fAscii pfnRunloop */
{ "Native", true, dbgcIoNativeStubRunloop },
{ "Gdb", false, dbgcGdbStubRunloop },
{ "Kd", false, dbgcKdStubRunloop }
};
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
/**
* Destroys all allocated data for the given dbeugger console I/O instance.
*
* @returns nothing.
* @param pDbgcIo Pointer to the dbeugger console I/O instance data.
*/
static void dbgcIoDestroy(PDBGCIOINT pDbgcIo)
{
for (uint32_t i = 0; i < pDbgcIo->cSvcsCfg; i++)
{
PDBGCIOSVC pIoSvc = &pDbgcIo->aSvc[i];
if (pIoSvc->hThreadSvc != NIL_RTTHREAD)
{
int rc = RTThreadWait(pIoSvc->hThreadSvc, RT_MS_10SEC, NULL /*prc*/);
AssertRC(rc);
pIoSvc->hThreadSvc = NIL_RTTHREAD;
pIoSvc->pIoProvReg->pfnDestroy(pIoSvc->hDbgcIoProv);
}
}
RTMemFree(pDbgcIo);
}
/**
* Returns the number of I/O services configured.
*
* @returns I/O service count.
* @param pCfgRoot The root of the config.
*/
static uint32_t dbgcIoGetSvcCount(PCFGMNODE pCfgRoot)
{
uint32_t cSvcs = 0;
PCFGMNODE pNd = CFGMR3GetFirstChild(pCfgRoot);
while (pNd)
{
cSvcs++;
pNd = CFGMR3GetNextChild(pNd);
}
return cSvcs;
}
/**
* Returns a pointer to the I/O provider registration record matching the given name.
*
* @returns Pointer to the registration record or NULL if not found.
* @param pszName The name to look for (case insensitive matching).
*/
static PCDBGCIOPROVREG dbgcIoProvFindRegByName(const char *pszName)
{
for (uint32_t i = 0; i < RT_ELEMENTS(g_aIoProv); i++)
{
if (!RTStrICmp(g_aIoProv[i]->pszName, pszName))
return g_aIoProv[i];
}
return NULL;
}
/**
* Returns a pointer to the stub record matching the given name.
*
* @returns Pointer to the stub record or NULL if not found.
* @param pszName The name to look for (case insensitive matching).
*/
static PCDBGCSTUB dbgcIoFindStubByName(const char *pszName)
{
for (uint32_t i = 0; i < RT_ELEMENTS(g_aStubs); i++)
{
if (!RTStrICmp(g_aStubs[i].pszName, pszName))
return &g_aStubs[i];
}
return NULL;
}
/**
* Wrapper around DBGCCreate() to get it working as a callback.
*/
static DECLCALLBACK(int) dbgcIoNativeStubRunloop(PUVM pUVM, PCDBGCIO pIo, unsigned fFlags)
{
return DBGCCreate(pUVM, pIo, fFlags);
}
/**
* @interface_method_impl{DBGCIO,pfnDestroy}
*/
static DECLCALLBACK(void) dbgcIoAsciiDestroy(PCDBGCIO pIo)
{
PDBGCIOSVC pIoSvc = RT_FROM_MEMBER(pIo, DBGCIOSVC, IoAscii);
pIoSvc->pIo->pfnDestroy(pIoSvc->pIo);
}
/**
* @interface_method_impl{DBGCIO,pfnInput}
*/
static DECLCALLBACK(bool) dbgcIoAsciiInput(PCDBGCIO pIo, uint32_t cMillies)
{
PDBGCIOSVC pIoSvc = RT_FROM_MEMBER(pIo, DBGCIOSVC, IoAscii);
return pIoSvc->pIo->pfnInput(pIoSvc->pIo, cMillies);
}
/**
* @interface_method_impl{DBGCIO,pfnRead}
*/
static DECLCALLBACK(int) dbgcIoAsciiRead(PCDBGCIO pIo, void *pvBuf, size_t cbBuf, size_t *pcbRead)
{
PDBGCIOSVC pIoSvc = RT_FROM_MEMBER(pIo, DBGCIOSVC, IoAscii);
return pIoSvc->pIo->pfnRead(pIoSvc->pIo, pvBuf, cbBuf, pcbRead);
}
/**
* @interface_method_impl{DBGCIO,pfnWrite}
*/
static DECLCALLBACK(int) dbgcIoAsciiWrite(PCDBGCIO pIo, const void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
PDBGCIOSVC pIoSvc = RT_FROM_MEMBER(pIo, DBGCIOSVC, IoAscii);
/*
* convert '\n' to '\r\n' while writing.
*/
int rc = 0;
size_t cbLeft = cbBuf;
while (cbLeft)
{
size_t cb = cbLeft;
/* write newlines */
if (*(const char *)pvBuf == '\n')
{
rc = pIoSvc->pIo->pfnWrite(pIoSvc->pIo, "\r\n", 2, NULL);
cb = 1;
}
/* write till next newline */
else
{
const char *pszNL = (const char *)memchr(pvBuf, '\n', cbLeft);
if (pszNL)
cb = (uintptr_t)pszNL - (uintptr_t)pvBuf;
rc = pIoSvc->pIo->pfnWrite(pIoSvc->pIo, pvBuf, cb, NULL);
}
if (RT_FAILURE(rc))
break;
/* advance */
cbLeft -= cb;
pvBuf = (const char *)pvBuf + cb;
}
/*
* Set returned value and return.
*/
if (pcbWritten)
*pcbWritten = cbBuf - cbLeft;
return rc;
}
/**
* @interface_method_impl{DBGCIO,pfnSetReady}
*/
static DECLCALLBACK(void) dbgcIoAsciiSetReady(PCDBGCIO pIo, bool fReady)
{
PDBGCIOSVC pIoSvc = RT_FROM_MEMBER(pIo, DBGCIOSVC, IoAscii);
return pIoSvc->pIo->pfnSetReady(pIoSvc->pIo, fReady);
}
/**
* The I/O thread handling the service.
*/
static DECLCALLBACK(int) dbgcIoSvcThread(RTTHREAD hThreadSelf, void *pvUser)
{
RT_NOREF(hThreadSelf);
int rc = VINF_SUCCESS;
PDBGCIOSVC pIoSvc = (PDBGCIOSVC)pvUser;
PDBGCIOINT pDbgcIo = pIoSvc->pDbgcIo;
PCDBGCIOPROVREG pIoProvReg = pIoSvc->pIoProvReg;
while (!ASMAtomicReadBool(&pDbgcIo->fShutdown))
{
/* Wait until someone connects. */
rc = pIoProvReg->pfnWaitForConnect(pIoSvc->hDbgcIoProv, RT_INDEFINITE_WAIT, &pIoSvc->pIo);
if (RT_SUCCESS(rc))
{
PCDBGCIO pIo = pIoSvc->pIo;
if (pIoSvc->pStub->fAscii)
{
pIoSvc->IoAscii.pfnDestroy = dbgcIoAsciiDestroy;
pIoSvc->IoAscii.pfnInput = dbgcIoAsciiInput;
pIoSvc->IoAscii.pfnRead = dbgcIoAsciiRead;
pIoSvc->IoAscii.pfnWrite = dbgcIoAsciiWrite;
pIoSvc->IoAscii.pfnSetReady = dbgcIoAsciiSetReady;
pIo = &pIoSvc->IoAscii;
}
/* call the runloop for the connection. */
pIoSvc->pStub->pfnRunloop(pIoSvc->pUVM, pIo, 0 /*fFlags*/);
pIo->pfnDestroy(pIo);
}
else if ( rc != VERR_TIMEOUT
&& rc != VERR_INTERRUPTED)
break;
}
if (!ASMAtomicDecU32(&pDbgcIo->cSvcsRunning))
dbgcIoDestroy(pDbgcIo);
return rc;
}
static int dbgcIoSvcInitWorker(PUVM pUVM, PDBGCIOSVC pIoSvc, PCDBGCIOPROVREG pIoProvReg,
PCDBGCSTUB pStub, PCFGMNODE pCfg, const char *pszName,
bool fIgnoreNetAddrInUse)
{
pIoSvc->pUVM = pUVM;
pIoSvc->pIoProvReg = pIoProvReg;
pIoSvc->pStub = pStub;
/* Create the provider instance and spawn the dedicated thread handling that service. */
int rc = pIoProvReg->pfnCreate(&pIoSvc->hDbgcIoProv, pCfg);
if (RT_SUCCESS(rc))
{
rc = RTThreadCreateF(&pIoSvc->hThreadSvc, dbgcIoSvcThread, pIoSvc, 0 /*cbStack*/,
RTTHREADTYPE_DEBUGGER, RTTHREADFLAGS_WAITABLE, "DbgcThrd-%s", pszName);
if (RT_SUCCESS(rc))
{
ASMAtomicIncU32(&pIoSvc->pDbgcIo->cSvcsRunning);
return VINF_SUCCESS;
}
else
rc = VMR3SetError(pUVM, rc, RT_SRC_POS,
"Configuration error: Creating an instance of the service \"%s\" failed",
pszName);
pIoProvReg->pfnDestroy(pIoSvc->hDbgcIoProv);
}
else if ( rc != VERR_NET_ADDRESS_IN_USE
|| !fIgnoreNetAddrInUse)
rc = VMR3SetError(pUVM, rc, RT_SRC_POS,
"Configuration error: Creating an instance of the I/O provider \"%s\" failed",
pIoProvReg->pszName);
return rc;
}
/**
* Tries to initialize the given I/O service from the given config.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param pIoSvc The I/O service instance to initialize.
* @param pCfg The config for the instance.
*/
static int dbgcIoSvcInit(PUVM pUVM, PDBGCIOSVC pIoSvc, PCFGMNODE pCfg)
{
char szName[32 + 1]; RT_ZERO(szName);
int rc = CFGMR3GetName(pCfg, &szName[0], sizeof(szName));
if (RT_SUCCESS(rc))
{
char szIoProvName[32 + 1]; RT_ZERO(szIoProvName);
rc = CFGMR3QueryString(pCfg, "Provider", &szIoProvName[0], sizeof(szIoProvName));
if (RT_SUCCESS(rc))
{
char szStub[32 + 1]; RT_ZERO(szStub);
rc = CFGMR3QueryString(pCfg, "StubType", &szStub[0], sizeof(szStub));
if (RT_SUCCESS(rc))
{
PCDBGCIOPROVREG pIoProvReg = dbgcIoProvFindRegByName(szIoProvName);
if (pIoProvReg)
{
PCDBGCSTUB pStub = dbgcIoFindStubByName(szStub);
if (pStub)
rc = dbgcIoSvcInitWorker(pUVM, pIoSvc, pIoProvReg, pStub, pCfg, szName,
false /*fIgnoreNetAddrInUse*/);
else
rc = VMR3SetError(pUVM, VERR_NOT_FOUND, RT_SRC_POS, "Configuration error: The stub type \"%s\" could not be found",
szStub);
}
else
rc = VMR3SetError(pUVM, VERR_NOT_FOUND, RT_SRC_POS, "Configuration error: The provider \"%s\" could not be found",
szIoProvName);
}
else
rc = VM_SET_ERROR_U(pUVM, rc, "Configuration error: Querying \"StubType\" failed");
}
else
rc = VM_SET_ERROR_U(pUVM, rc, "Configuration error: Querying \"Provider\" failed");
}
else
rc = VM_SET_ERROR_U(pUVM, rc, "Configuration error: Querying service identifier failed (maybe too long)");
return rc;
}
/**
* Creates the DBGC I/O services from the legacy TCP config.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param pKey The config key.
* @param ppvData Where to store the I/o instance data on success.
*/
static int dbgcIoCreateLegacyTcp(PUVM pUVM, PCFGMNODE pKey, void **ppvData)
{
bool fEnabled;
int rc = CFGMR3QueryBoolDef(pKey, "Enabled", &fEnabled,
#if defined(VBOX_WITH_DEBUGGER) && defined(VBOX_WITH_DEBUGGER_TCP_BY_DEFAULT)
true
#else
false
#endif
);
if (RT_FAILURE(rc))
return VM_SET_ERROR_U(pUVM, rc, "Configuration error: Failed querying \"DBGC/Enabled\"");
if (!fEnabled)
{
LogFlow(("DBGCTcpCreate: returns VINF_SUCCESS (Disabled)\n"));
return VINF_SUCCESS;
}
PDBGCIOINT pDbgcIo = (PDBGCIOINT)RTMemAllocZ(RT_UOFFSETOF_DYN(DBGCIOINT, aSvc[1]));
if (RT_LIKELY(pDbgcIo))
{
pDbgcIo->aSvc[0].pDbgcIo = pDbgcIo;
pDbgcIo->cSvcsCfg = 1;
pDbgcIo->cSvcsRunning = 1;
rc = dbgcIoSvcInitWorker(pUVM, &pDbgcIo->aSvc[0], &g_DbgcIoProvTcp, &g_aStubs[0], pKey, "TCP",
true /*fIgnoreNetAddrInUse*/);
if (RT_SUCCESS(rc))
{
*ppvData = pDbgcIo;
return VINF_SUCCESS;
}
RTMemFree(pDbgcIo);
if (rc == VERR_NET_ADDRESS_IN_USE)
rc = VINF_SUCCESS;
}
else
rc = VERR_NO_MEMORY;
if (RT_FAILURE(rc))
rc = VM_SET_ERROR_U(pUVM, rc, "Cannot start TCP-based debugging console service");
return rc;
}
/**
* Sets up debugger I/O based on the VM config.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param ppvData Where to store a pointer to the instance data.
*/
DBGDECL(int) DBGCIoCreate(PUVM pUVM, void **ppvData)
{
/*
* Check what the configuration says.
*/
PCFGMNODE pKey = CFGMR3GetChild(CFGMR3GetRootU(pUVM), "DBGC");
uint32_t cSvcs = dbgcIoGetSvcCount(pKey);
int rc = VINF_SUCCESS;
/* If no services are configured try the legacy config supporting TCP only. */
if (cSvcs)
{
PDBGCIOINT pDbgcIo = (PDBGCIOINT)RTMemAllocZ(RT_UOFFSETOF_DYN(DBGCIOINT, aSvc[cSvcs]));
if (RT_LIKELY(pDbgcIo))
{
pDbgcIo->cSvcsCfg = 0;
pDbgcIo->cSvcsRunning = 1;
pDbgcIo->fShutdown = false;
for (uint32_t i = 0; i < cSvcs; i++)
pDbgcIo->aSvc[i].hThreadSvc = NIL_RTTHREAD;
PCFGMNODE pSvcCfg = CFGMR3GetFirstChild(pKey);
for (uint32_t i = 0; i < cSvcs && RT_SUCCESS(rc); i++)
{
pDbgcIo->aSvc[i].pDbgcIo = pDbgcIo;
rc = dbgcIoSvcInit(pUVM, &pDbgcIo->aSvc[i], pSvcCfg);
if (RT_SUCCESS(rc))
pDbgcIo->cSvcsCfg++;
else
rc = VM_SET_ERROR_U(pUVM, rc, "Failed to initialize the debugger I/O service");
pSvcCfg = CFGMR3GetNextChild(pSvcCfg);
}
if (RT_SUCCESS(rc))
*ppvData = pDbgcIo;
else
{
if (!ASMAtomicDecU32(&pDbgcIo->cSvcsRunning))
dbgcIoDestroy(pDbgcIo);
}
}
else
rc = VM_SET_ERROR_U(pUVM, VERR_NO_MEMORY, "Failed to allocate memory for the debugger I/O service");
}
else
rc = dbgcIoCreateLegacyTcp(pUVM, pKey, ppvData);
return rc;
}
/**
* Terminates any running debugger services.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle.
* @param pvData The data returned by DBGCIoCreate.
*/
DBGDECL(int) DBGCIoTerminate(PUVM pUVM, void *pvData)
{
RT_NOREF(pUVM);
PDBGCIOINT pDbgcIo = (PDBGCIOINT)pvData;
if (pDbgcIo)
{
ASMAtomicXchgBool(&pDbgcIo->fShutdown, true);
for (uint32_t i = 0; i < pDbgcIo->cSvcsCfg; i++)
{
PDBGCIOSVC pIoSvc = &pDbgcIo->aSvc[i];
if (pIoSvc->hThreadSvc != NIL_RTTHREAD)
pIoSvc->pIoProvReg->pfnWaitInterrupt(pIoSvc->hDbgcIoProv);
}
if (!ASMAtomicDecU32(&pDbgcIo->cSvcsRunning))
dbgcIoDestroy(pDbgcIo);
}
return VINF_SUCCESS;
}
|