VirtualBox

source: vbox/trunk/src/VBox/Debugger/DBGCIo.cpp@ 104448

Last change on this file since 104448 was 99739, checked in by vboxsync, 17 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.0 KB
Line 
1/* $Id: DBGCIo.cpp 99739 2023-05-11 01:01:08Z vboxsync $ */
2/** @file
3 * DBGC - Debugger Console, I/O provider handling.
4 */
5
6/*
7 * Copyright (C) 2020-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <VBox/dbg.h>
33#include <VBox/vmm/cfgm.h>
34#include <VBox/err.h>
35
36#include <iprt/mem.h>
37#include <iprt/thread.h>
38#include <VBox/log.h>
39#include <iprt/assert.h>
40
41#include <iprt/string.h>
42
43#include "DBGCIoProvInternal.h"
44#include "DBGCInternal.h"
45
46
47/*********************************************************************************************************************************
48* Structures and Typedefs *
49*********************************************************************************************************************************/
50
51/**
52 * Stub descriptor.
53 */
54typedef struct DBGCSTUB
55{
56 /** Name of the stub. */
57 const char *pszName;
58 /** Flag whether this is an ASCII based protocol which requires some newline handling. */
59 bool fAscii;
60 /**
61 * The runloop callback.
62 *
63 * @returns VBox status code.
64 * @param pUVM The user mode VM handle.
65 * @param pIo Pointer to the I/O callback table.
66 * @param fFlags Flags for the runloop, MBZ for now.
67 */
68 DECLCALLBACKMEMBER(int, pfnRunloop, (PUVM pUVM, PCDBGCIO pIo, unsigned fFlags));
69} DBGCSTUB;
70/** Pointer to a stub descriptor. */
71typedef DBGCSTUB *PDBGCSTUB;
72/** Pointer to a const stub descriptor. */
73typedef const DBGCSTUB *PCDBGCSTUB;
74
75
76/** Pointer to the instance data of the debug console I/O. */
77typedef struct DBGCIOINT *PDBGCIOINT;
78
79
80/**
81 * A single debug console I/O service.
82 */
83typedef struct DBGCIOSVC
84{
85 /** Pointer to the owning structure. */
86 PDBGCIOINT pDbgcIo;
87 /** The user mode VM handle this service belongs to. */
88 PUVM pUVM;
89 /** The I/O provider registration record for this service. */
90 PCDBGCIOPROVREG pIoProvReg;
91 /** The I/O provider instance. */
92 DBGCIOPROV hDbgcIoProv;
93 /** The stub type. */
94 PCDBGCSTUB pStub;
95 /** The thread managing the service. */
96 RTTHREAD hThreadSvc;
97 /** Pointer to the I/O callback table currently being served. */
98 PCDBGCIO pIo;
99 /** The wrapping DBGC I/O callback table for ASCII based protocols. */
100 DBGCIO IoAscii;
101} DBGCIOSVC;
102/** Pointer to a single debug console I/O service. */
103typedef DBGCIOSVC *PDBGCIOSVC;
104/** Poitner to a const single debug console I/O service. */
105typedef const DBGCIOSVC *PCDBGCIOSVC;
106
107
108/**
109 * Debug console I/O instance data.
110 */
111typedef struct DBGCIOINT
112{
113 /** Number of configured I/O service instances. */
114 volatile uint32_t cSvcsCfg;
115 /** Number of running I/O service instances. */
116 volatile uint32_t cSvcsRunning;
117 /** Flag whether the services were asked to shut down. */
118 volatile bool fShutdown;
119 /** Array of active I/O service instances. */
120 RT_FLEXIBLE_ARRAY_EXTENSION
121 DBGCIOSVC aSvc[RT_FLEXIBLE_ARRAY];
122} DBGCIOINT;
123
124
125/*********************************************************************************************************************************
126* Global Variables *
127*********************************************************************************************************************************/
128
129/**
130 * Array of supported I/O providers.
131 */
132static PCDBGCIOPROVREG g_aIoProv[] =
133{
134 &g_DbgcIoProvTcp,
135 &g_DbgcIoProvUdp,
136 &g_DbgcIoProvIpc
137};
138
139
140static DECLCALLBACK(int) dbgcIoNativeStubRunloop(PUVM pUVM, PCDBGCIO pIo, unsigned fFlags);
141
142/**
143 * Array of supported stubs.
144 */
145static const DBGCSTUB g_aStubs[] =
146{
147 /** pszName fAscii pfnRunloop */
148 { "Native", true, dbgcIoNativeStubRunloop },
149 { "Gdb", false, dbgcGdbStubRunloop },
150 { "Kd", false, dbgcKdStubRunloop }
151};
152
153
154/*********************************************************************************************************************************
155* Internal Functions *
156*********************************************************************************************************************************/
157
158
159/**
160 * Destroys all allocated data for the given dbeugger console I/O instance.
161 *
162 * @param pDbgcIo Pointer to the dbeugger console I/O instance data.
163 */
164static void dbgcIoDestroy(PDBGCIOINT pDbgcIo)
165{
166 for (uint32_t i = 0; i < pDbgcIo->cSvcsCfg; i++)
167 {
168 PDBGCIOSVC pIoSvc = &pDbgcIo->aSvc[i];
169
170 if (pIoSvc->hThreadSvc != NIL_RTTHREAD)
171 {
172 int rc = RTThreadWait(pIoSvc->hThreadSvc, RT_MS_10SEC, NULL /*prc*/);
173 AssertRC(rc);
174
175 pIoSvc->hThreadSvc = NIL_RTTHREAD;
176 pIoSvc->pIoProvReg->pfnDestroy(pIoSvc->hDbgcIoProv);
177 }
178 }
179
180 RTMemFree(pDbgcIo);
181}
182
183
184/**
185 * Returns the number of I/O services configured.
186 *
187 * @returns I/O service count.
188 * @param pCfgRoot The root of the config.
189 */
190static uint32_t dbgcIoGetSvcCount(PCFGMNODE pCfgRoot)
191{
192 uint32_t cSvcs = 0;
193 PCFGMNODE pNd = CFGMR3GetFirstChild(pCfgRoot);
194 while (pNd)
195 {
196 cSvcs++;
197 pNd = CFGMR3GetNextChild(pNd);
198 }
199
200 return cSvcs;
201}
202
203
204/**
205 * Returns a pointer to the I/O provider registration record matching the given name.
206 *
207 * @returns Pointer to the registration record or NULL if not found.
208 * @param pszName The name to look for (case insensitive matching).
209 */
210static PCDBGCIOPROVREG dbgcIoProvFindRegByName(const char *pszName)
211{
212 for (uint32_t i = 0; i < RT_ELEMENTS(g_aIoProv); i++)
213 {
214 if (!RTStrICmp(g_aIoProv[i]->pszName, pszName))
215 return g_aIoProv[i];
216 }
217 return NULL;
218}
219
220
221/**
222 * Returns a pointer to the stub record matching the given name.
223 *
224 * @returns Pointer to the stub record or NULL if not found.
225 * @param pszName The name to look for (case insensitive matching).
226 */
227static PCDBGCSTUB dbgcIoFindStubByName(const char *pszName)
228{
229 for (uint32_t i = 0; i < RT_ELEMENTS(g_aStubs); i++)
230 {
231 if (!RTStrICmp(g_aStubs[i].pszName, pszName))
232 return &g_aStubs[i];
233 }
234 return NULL;
235}
236
237
238/**
239 * Wrapper around DBGCCreate() to get it working as a callback.
240 */
241static DECLCALLBACK(int) dbgcIoNativeStubRunloop(PUVM pUVM, PCDBGCIO pIo, unsigned fFlags)
242{
243 return DBGCCreate(pUVM, pIo, fFlags);
244}
245
246
247/**
248 * @interface_method_impl{DBGCIO,pfnDestroy}
249 */
250static DECLCALLBACK(void) dbgcIoAsciiDestroy(PCDBGCIO pIo)
251{
252 PDBGCIOSVC pIoSvc = RT_FROM_MEMBER(pIo, DBGCIOSVC, IoAscii);
253 pIoSvc->pIo->pfnDestroy(pIoSvc->pIo);
254}
255
256
257/**
258 * @interface_method_impl{DBGCIO,pfnInput}
259 */
260static DECLCALLBACK(bool) dbgcIoAsciiInput(PCDBGCIO pIo, uint32_t cMillies)
261{
262 PDBGCIOSVC pIoSvc = RT_FROM_MEMBER(pIo, DBGCIOSVC, IoAscii);
263 return pIoSvc->pIo->pfnInput(pIoSvc->pIo, cMillies);
264}
265
266
267/**
268 * @interface_method_impl{DBGCIO,pfnRead}
269 */
270static DECLCALLBACK(int) dbgcIoAsciiRead(PCDBGCIO pIo, void *pvBuf, size_t cbBuf, size_t *pcbRead)
271{
272 PDBGCIOSVC pIoSvc = RT_FROM_MEMBER(pIo, DBGCIOSVC, IoAscii);
273 return pIoSvc->pIo->pfnRead(pIoSvc->pIo, pvBuf, cbBuf, pcbRead);
274}
275
276
277/**
278 * @interface_method_impl{DBGCIO,pfnWrite}
279 */
280static DECLCALLBACK(int) dbgcIoAsciiWrite(PCDBGCIO pIo, const void *pvBuf, size_t cbBuf, size_t *pcbWritten)
281{
282 PDBGCIOSVC pIoSvc = RT_FROM_MEMBER(pIo, DBGCIOSVC, IoAscii);
283
284 /*
285 * convert '\n' to '\r\n' while writing.
286 */
287 int rc = 0;
288 size_t cbLeft = cbBuf;
289 while (cbLeft)
290 {
291 size_t cb = cbLeft;
292 /* write newlines */
293 if (*(const char *)pvBuf == '\n')
294 {
295 rc = pIoSvc->pIo->pfnWrite(pIoSvc->pIo, "\r\n", 2, NULL);
296 cb = 1;
297 }
298 /* write till next newline */
299 else
300 {
301 const char *pszNL = (const char *)memchr(pvBuf, '\n', cbLeft);
302 if (pszNL)
303 cb = (uintptr_t)pszNL - (uintptr_t)pvBuf;
304 rc = pIoSvc->pIo->pfnWrite(pIoSvc->pIo, pvBuf, cb, NULL);
305 }
306 if (RT_FAILURE(rc))
307 break;
308
309 /* advance */
310 cbLeft -= cb;
311 pvBuf = (const char *)pvBuf + cb;
312 }
313
314 /*
315 * Set returned value and return.
316 */
317 if (pcbWritten)
318 *pcbWritten = cbBuf - cbLeft;
319 return rc;
320}
321
322
323/**
324 * @interface_method_impl{DBGCIO,pfnSetReady}
325 */
326static DECLCALLBACK(void) dbgcIoAsciiSetReady(PCDBGCIO pIo, bool fReady)
327{
328 PDBGCIOSVC pIoSvc = RT_FROM_MEMBER(pIo, DBGCIOSVC, IoAscii);
329 return pIoSvc->pIo->pfnSetReady(pIoSvc->pIo, fReady);
330}
331
332
333/**
334 * The I/O thread handling the service.
335 */
336static DECLCALLBACK(int) dbgcIoSvcThread(RTTHREAD hThreadSelf, void *pvUser)
337{
338 RT_NOREF(hThreadSelf);
339
340 int rc = VINF_SUCCESS;
341 PDBGCIOSVC pIoSvc = (PDBGCIOSVC)pvUser;
342 PDBGCIOINT pDbgcIo = pIoSvc->pDbgcIo;
343 PCDBGCIOPROVREG pIoProvReg = pIoSvc->pIoProvReg;
344
345 while (!ASMAtomicReadBool(&pDbgcIo->fShutdown))
346 {
347 /* Wait until someone connects. */
348 rc = pIoProvReg->pfnWaitForConnect(pIoSvc->hDbgcIoProv, RT_INDEFINITE_WAIT, &pIoSvc->pIo);
349 if (RT_SUCCESS(rc))
350 {
351 PCDBGCIO pIo = pIoSvc->pIo;
352
353 if (pIoSvc->pStub->fAscii)
354 {
355 pIoSvc->IoAscii.pfnDestroy = dbgcIoAsciiDestroy;
356 pIoSvc->IoAscii.pfnInput = dbgcIoAsciiInput;
357 pIoSvc->IoAscii.pfnRead = dbgcIoAsciiRead;
358 pIoSvc->IoAscii.pfnWrite = dbgcIoAsciiWrite;
359 pIoSvc->IoAscii.pfnSetReady = dbgcIoAsciiSetReady;
360 pIo = &pIoSvc->IoAscii;
361 }
362
363 /* call the runloop for the connection. */
364 pIoSvc->pStub->pfnRunloop(pIoSvc->pUVM, pIo, 0 /*fFlags*/);
365
366 pIo->pfnDestroy(pIo);
367 }
368 else if ( rc != VERR_TIMEOUT
369 && rc != VERR_INTERRUPTED)
370 break;
371 }
372
373 if (!ASMAtomicDecU32(&pDbgcIo->cSvcsRunning))
374 dbgcIoDestroy(pDbgcIo);
375
376 return rc;
377}
378
379
380static int dbgcIoSvcInitWorker(PUVM pUVM, PDBGCIOSVC pIoSvc, PCDBGCIOPROVREG pIoProvReg,
381 PCDBGCSTUB pStub, PCFGMNODE pCfg, const char *pszName,
382 bool fIgnoreNetAddrInUse)
383{
384 pIoSvc->pUVM = pUVM;
385 pIoSvc->pIoProvReg = pIoProvReg;
386 pIoSvc->pStub = pStub;
387
388 /* Create the provider instance and spawn the dedicated thread handling that service. */
389 int rc = pIoProvReg->pfnCreate(&pIoSvc->hDbgcIoProv, pCfg);
390 if (RT_SUCCESS(rc))
391 {
392 rc = RTThreadCreateF(&pIoSvc->hThreadSvc, dbgcIoSvcThread, pIoSvc, 0 /*cbStack*/,
393 RTTHREADTYPE_DEBUGGER, RTTHREADFLAGS_WAITABLE, "DbgcThrd-%s", pszName);
394 if (RT_SUCCESS(rc))
395 {
396 ASMAtomicIncU32(&pIoSvc->pDbgcIo->cSvcsRunning);
397 return VINF_SUCCESS;
398 }
399 else
400 rc = VMR3SetError(pUVM, rc, RT_SRC_POS,
401 "Configuration error: Creating an instance of the service \"%s\" failed",
402 pszName);
403
404 pIoProvReg->pfnDestroy(pIoSvc->hDbgcIoProv);
405 }
406 else if ( rc != VERR_NET_ADDRESS_IN_USE
407 || !fIgnoreNetAddrInUse)
408 rc = VMR3SetError(pUVM, rc, RT_SRC_POS,
409 "Configuration error: Creating an instance of the I/O provider \"%s\" failed",
410 pIoProvReg->pszName);
411
412 return rc;
413}
414
415
416/**
417 * Tries to initialize the given I/O service from the given config.
418 *
419 * @returns VBox status code.
420 * @param pUVM The user mode VM handle.
421 * @param pIoSvc The I/O service instance to initialize.
422 * @param pCfg The config for the instance.
423 */
424static int dbgcIoSvcInit(PUVM pUVM, PDBGCIOSVC pIoSvc, PCFGMNODE pCfg)
425{
426 char szName[32 + 1]; RT_ZERO(szName);
427 int rc = CFGMR3GetName(pCfg, &szName[0], sizeof(szName));
428 if (RT_SUCCESS(rc))
429 {
430 char szIoProvName[32 + 1]; RT_ZERO(szIoProvName);
431 rc = CFGMR3QueryString(pCfg, "Provider", &szIoProvName[0], sizeof(szIoProvName));
432 if (RT_SUCCESS(rc))
433 {
434 char szStub[32 + 1]; RT_ZERO(szStub);
435 rc = CFGMR3QueryString(pCfg, "StubType", &szStub[0], sizeof(szStub));
436 if (RT_SUCCESS(rc))
437 {
438 PCDBGCIOPROVREG pIoProvReg = dbgcIoProvFindRegByName(szIoProvName);
439 if (pIoProvReg)
440 {
441 PCDBGCSTUB pStub = dbgcIoFindStubByName(szStub);
442 if (pStub)
443 rc = dbgcIoSvcInitWorker(pUVM, pIoSvc, pIoProvReg, pStub, pCfg, szName,
444 false /*fIgnoreNetAddrInUse*/);
445 else
446 rc = VMR3SetError(pUVM, VERR_NOT_FOUND, RT_SRC_POS, "Configuration error: The stub type \"%s\" could not be found",
447 szStub);
448 }
449 else
450 rc = VMR3SetError(pUVM, VERR_NOT_FOUND, RT_SRC_POS, "Configuration error: The provider \"%s\" could not be found",
451 szIoProvName);
452 }
453 else
454 rc = VM_SET_ERROR_U(pUVM, rc, "Configuration error: Querying \"StubType\" failed");
455 }
456 else
457 rc = VM_SET_ERROR_U(pUVM, rc, "Configuration error: Querying \"Provider\" failed");
458 }
459 else
460 rc = VM_SET_ERROR_U(pUVM, rc, "Configuration error: Querying service identifier failed (maybe too long)");
461
462 return rc;
463}
464
465
466/**
467 * Creates the DBGC I/O services from the legacy TCP config.
468 *
469 * @returns VBox status code.
470 * @param pUVM The user mode VM handle.
471 * @param pKey The config key.
472 * @param ppvData Where to store the I/o instance data on success.
473 */
474static int dbgcIoCreateLegacyTcp(PUVM pUVM, PCFGMNODE pKey, void **ppvData)
475{
476 bool fEnabled;
477 int rc = CFGMR3QueryBoolDef(pKey, "Enabled", &fEnabled,
478#if defined(VBOX_WITH_DEBUGGER) && defined(VBOX_WITH_DEBUGGER_TCP_BY_DEFAULT)
479 true
480#else
481 false
482#endif
483 );
484 if (RT_FAILURE(rc))
485 return VM_SET_ERROR_U(pUVM, rc, "Configuration error: Failed querying \"DBGC/Enabled\"");
486
487 if (!fEnabled)
488 {
489 LogFlow(("DBGCTcpCreate: returns VINF_SUCCESS (Disabled)\n"));
490 return VINF_SUCCESS;
491 }
492
493 PDBGCIOINT pDbgcIo = (PDBGCIOINT)RTMemAllocZ(RT_UOFFSETOF_DYN(DBGCIOINT, aSvc[1]));
494 if (RT_LIKELY(pDbgcIo))
495 {
496 pDbgcIo->aSvc[0].pDbgcIo = pDbgcIo;
497 pDbgcIo->cSvcsCfg = 1;
498 pDbgcIo->cSvcsRunning = 1;
499 rc = dbgcIoSvcInitWorker(pUVM, &pDbgcIo->aSvc[0], &g_DbgcIoProvTcp, &g_aStubs[0], pKey, "TCP",
500 true /*fIgnoreNetAddrInUse*/);
501 if (RT_SUCCESS(rc))
502 {
503 *ppvData = pDbgcIo;
504 return VINF_SUCCESS;
505 }
506
507 RTMemFree(pDbgcIo);
508 if (rc == VERR_NET_ADDRESS_IN_USE)
509 rc = VINF_SUCCESS;
510 }
511 else
512 rc = VERR_NO_MEMORY;
513
514 if (RT_FAILURE(rc))
515 rc = VM_SET_ERROR_U(pUVM, rc, "Cannot start TCP-based debugging console service");
516 return rc;
517}
518
519
520/**
521 * Sets up debugger I/O based on the VM config.
522 *
523 * @returns VBox status code.
524 * @param pUVM The user mode VM handle.
525 * @param ppvData Where to store a pointer to the instance data.
526 */
527DBGDECL(int) DBGCIoCreate(PUVM pUVM, void **ppvData)
528{
529 /*
530 * Check what the configuration says.
531 */
532 PCFGMNODE pKey = CFGMR3GetChild(CFGMR3GetRootU(pUVM), "DBGC");
533 uint32_t cSvcs = dbgcIoGetSvcCount(pKey);
534 int rc = VINF_SUCCESS;
535
536 /* If no services are configured try the legacy config supporting TCP only. */
537 if (cSvcs)
538 {
539 PDBGCIOINT pDbgcIo = (PDBGCIOINT)RTMemAllocZ(RT_UOFFSETOF_DYN(DBGCIOINT, aSvc[cSvcs]));
540 if (RT_LIKELY(pDbgcIo))
541 {
542 pDbgcIo->cSvcsCfg = 0;
543 pDbgcIo->cSvcsRunning = 1;
544 pDbgcIo->fShutdown = false;
545
546 for (uint32_t i = 0; i < cSvcs; i++)
547 pDbgcIo->aSvc[i].hThreadSvc = NIL_RTTHREAD;
548
549 PCFGMNODE pSvcCfg = CFGMR3GetFirstChild(pKey);
550 for (uint32_t i = 0; i < cSvcs && RT_SUCCESS(rc); i++)
551 {
552 pDbgcIo->aSvc[i].pDbgcIo = pDbgcIo;
553
554 rc = dbgcIoSvcInit(pUVM, &pDbgcIo->aSvc[i], pSvcCfg);
555 if (RT_SUCCESS(rc))
556 pDbgcIo->cSvcsCfg++;
557 else
558 rc = VM_SET_ERROR_U(pUVM, rc, "Failed to initialize the debugger I/O service");
559
560 pSvcCfg = CFGMR3GetNextChild(pSvcCfg);
561 }
562
563 if (RT_SUCCESS(rc))
564 *ppvData = pDbgcIo;
565 else
566 {
567 if (!ASMAtomicDecU32(&pDbgcIo->cSvcsRunning))
568 dbgcIoDestroy(pDbgcIo);
569 }
570 }
571 else
572 rc = VM_SET_ERROR_U(pUVM, VERR_NO_MEMORY, "Failed to allocate memory for the debugger I/O service");
573 }
574 else
575 rc = dbgcIoCreateLegacyTcp(pUVM, pKey, ppvData);
576
577 return rc;
578}
579
580
581/**
582 * Terminates any running debugger services.
583 *
584 * @returns VBox status code.
585 * @param pUVM The user mode VM handle.
586 * @param pvData The data returned by DBGCIoCreate.
587 */
588DBGDECL(int) DBGCIoTerminate(PUVM pUVM, void *pvData)
589{
590 RT_NOREF(pUVM);
591 PDBGCIOINT pDbgcIo = (PDBGCIOINT)pvData;
592
593 if (pDbgcIo)
594 {
595 ASMAtomicXchgBool(&pDbgcIo->fShutdown, true);
596
597 for (uint32_t i = 0; i < pDbgcIo->cSvcsCfg; i++)
598 {
599 PDBGCIOSVC pIoSvc = &pDbgcIo->aSvc[i];
600
601 if (pIoSvc->hThreadSvc != NIL_RTTHREAD)
602 pIoSvc->pIoProvReg->pfnWaitInterrupt(pIoSvc->hDbgcIoProv);
603 }
604
605 if (!ASMAtomicDecU32(&pDbgcIo->cSvcsRunning))
606 dbgcIoDestroy(pDbgcIo);
607 }
608
609 return VINF_SUCCESS;
610}
611
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette