kdb: Handle process enumeration before procinit()

Make kdb_thr_first() and kdb_thr_next() return sane values if the
allproc list and pidhashtbl haven't been initialized yet. This can
happen if the debugger is entered very early on, for example with the
'-d' boot flag.

This allows remote gdb to attach at such a time, and fixes some ddb
commands like 'show threads'.

Be explicit about the static initialization of these variables. This
part has no functional change.

Reviewed by:	markj, imp (previous version)
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D31495
This commit is contained in:
Mitchell Horne 2021-08-11 14:40:01 -03:00
parent 09319f7d3f
commit 4ccaa87f69
2 changed files with 8 additions and 3 deletions

View File

@ -122,13 +122,13 @@ static void pargs_free(struct pargs *pa);
/*
* Other process lists
*/
struct pidhashhead *pidhashtbl;
struct pidhashhead *pidhashtbl = NULL;
struct sx *pidhashtbl_lock;
u_long pidhash;
u_long pidhashlock;
struct pgrphashhead *pgrphashtbl;
u_long pgrphash;
struct proclist allproc;
struct proclist allproc = LIST_HEAD_INITIALIZER(allproc);
struct sx __exclusive_cache_line allproc_lock;
struct sx __exclusive_cache_line proctree_lock;
struct mtx __exclusive_cache_line ppeers_lock;
@ -185,7 +185,6 @@ procinit(void)
sx_init(&proctree_lock, "proctree");
mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
LIST_INIT(&allproc);
pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
pidhashlock = (pidhash + 1) / 64;
if (pidhashlock > 0)

View File

@ -608,6 +608,10 @@ kdb_thr_first(void)
struct thread *thr;
u_int i;
/* This function may be called early. */
if (pidhashtbl == NULL)
return (&thread0);
for (i = 0; i <= pidhash; i++) {
LIST_FOREACH(p, &pidhashtbl[i], p_hash) {
thr = FIRST_THREAD_IN_PROC(p);
@ -651,6 +655,8 @@ kdb_thr_next(struct thread *thr)
thr = TAILQ_NEXT(thr, td_plist);
if (thr != NULL)
return (thr);
if (pidhashtbl == NULL)
return (NULL);
hash = p->p_pid & pidhash;
for (;;) {
p = LIST_NEXT(p, p_hash);