/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2004 Marcel Moolenaar4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/kdb.h>31#include <sys/proc.h>3233#include <machine/pcb.h>3435#include <ddb/ddb.h>36#include <ddb/db_command.h>37#include <ddb/db_sym.h>3839void40db_print_thread(void)41{42pid_t pid;4344pid = -1;45if (kdb_thread->td_proc != NULL)46pid = kdb_thread->td_proc->p_pid;47db_printf("[ thread pid %d tid %ld ]\n", pid, (long)kdb_thread->td_tid);48}4950void51db_set_thread(db_expr_t tid, bool hastid, db_expr_t cnt, char *mod)52{53struct thread *thr;54int err;5556if (hastid) {57thr = db_lookup_thread(tid, false);58if (thr != NULL) {59err = kdb_thr_select(thr);60if (err != 0) {61db_printf("unable to switch to thread %ld\n",62(long)thr->td_tid);63return;64}65db_dot = PC_REGS();66} else {67db_printf("%d: invalid thread\n", (int)tid);68return;69}70}7172db_print_thread();73db_print_loc_and_inst(PC_REGS());74}7576void77db_show_threads(db_expr_t addr, bool hasaddr, db_expr_t cnt, char *mod)78{79jmp_buf jb;80void *prev_jb;81struct thread *thr;8283thr = kdb_thr_first();84while (!db_pager_quit && thr != NULL) {85db_printf(" %6ld (%p) (stack %p) ", (long)thr->td_tid, thr,86(void *)thr->td_kstack);87prev_jb = kdb_jmpbuf(jb);88if (setjmp(jb) == 0) {89if (db_trace_thread(thr, 1) != 0)90db_printf("***\n");91}92kdb_jmpbuf(prev_jb);93thr = kdb_thr_next(thr);94}95}9697/*98* Lookup a thread based on a db expression address. We assume that the99* address was parsed in hexadecimal. We reparse the address in decimal100* first and try to treat it as a thread ID to find an associated thread.101* If that fails and check_pid is true, we treat the decimal value as a102* PID. If that matches a process, we return the first thread in that103* process. Otherwise, we treat the addr as a pointer to a thread.104*/105struct thread *106db_lookup_thread(db_expr_t addr, bool check_pid)107{108struct thread *td;109db_expr_t decaddr;110111/*112* If the parsed address was not a valid decimal expression,113* assume it is a thread pointer.114*/115decaddr = db_hex2dec(addr);116if (decaddr == -1)117return ((struct thread *)addr);118119td = kdb_thr_lookup(decaddr);120if (td != NULL)121return (td);122if (check_pid) {123td = kdb_thr_from_pid(decaddr);124if (td != NULL)125return (td);126}127return ((struct thread *)addr);128}129130/*131* Lookup a process based on a db expression address. We assume that the132* address was parsed in hexadecimal. We reparse the address in decimal133* first and try to treat it as a PID to find an associated process.134* If that fails we treat the addr as a pointer to a process.135*/136struct proc *137db_lookup_proc(db_expr_t addr)138{139db_expr_t decaddr;140struct proc *p;141142decaddr = db_hex2dec(addr);143if (decaddr != -1) {144LIST_FOREACH(p, PIDHASH(decaddr), p_hash) {145if (p->p_pid == decaddr)146return (p);147}148}149return ((struct proc *)addr);150}151152153