Commit Graph

119 Commits

Author SHA1 Message Date
Mitchell Horne
5644850620 ddb: have 'reset' command use normal reboot path
This conditionally gives all registered shutdown handlers a chance to
perform the reboot, with cpu_reset() being the fallback. The '\s'
modifier can be used with the command to get the previous behaviour.

The motivation is that some platforms may not be able do anything
meaningful via cpu_reset(), due to a lack of standardized reset
mechanism and/or firmware shortcomings. However, they may have a
separate device driver attached that normally performs the reboot. Such
is the case for some versions of the Raspberry Pi, where reset via PSCI
fails, but the BCM2835 watchdog driver has a shutdown hook.

Reported by:	bz
Reviewed by:	markj (slightly earlier version)
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D37981
2023-01-23 15:10:24 -04:00
Mark Johnston
c036339ddf ddb: Don't flag breakpoint/watchpoint commands as MEMSAFE
They could potentially be abused to overwrite kernel memory, so
shouldn't be accessible when mac_ddb is loaded.

Reviewed by:	mhorne
Fixes:	bc4ea61d55cb ("ddb: tag core commands with DB_CMD_MEMSAFE")
Sponsored by:	Juniper Networks, Inc.
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D37105
2022-11-02 13:46:24 -04:00
Mitchell Horne
2449b9e5fe mac: kdb/ddb framework hooks
Add three simple hooks to the debugger allowing for a loaded MAC policy
to intervene if desired:
 1. Before invoking the kdb backend
 2. Before ddb command registration
 3. Before ddb command execution

We extend struct db_command with a private pointer and two flag bits
reserved for policy use.

Reviewed by:	markj
Sponsored by:	Juniper Networks, Inc.
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D35370
2022-07-18 22:06:13 +00:00
Mitchell Horne
a305b20ead ddb: tag core commands with DB_CMD_MEMSAFE
Those which are statically defined in db_command.c.

Sponsored by:	Juniper Networks, Inc.
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D35584
2022-07-18 22:06:11 +00:00
Mitchell Horne
c84c5e00ac ddb: annotate some commands with DB_CMD_MEMSAFE
This is not completely exhaustive, but covers a large majority of
commands in the tree.

Reviewed by:	markj
Sponsored by:	Juniper Networks, Inc.
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D35583
2022-07-18 22:06:09 +00:00
Mitchell Horne
8a09948233 db_command.c: use designated initializers
Provide separate helper macros for regular commands and next-level table
commands as they are mutually exclusive. This ensures proper
initialization of each element and allows us to exclude some redundant
fields, such as specifying .more = NULL for every regular command.

Reviewed by:	markj, jhb
MFC after:	3 days
Sponsored by:	Juniper Networks, Inc.
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D35580
2022-07-05 11:51:31 -03:00
Mitchell Horne
4f2ad6243f db_command.c: style
Reviewed by:	jhb
MFC after:	3 days
Sponsored by:	Juniper Networks, Inc.
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D35368
2022-07-05 11:51:31 -03:00
Mitchell Horne
4ef7db5a7e ddb: namespacing of struct command
'command' is too generic for something specific to the kernel debugger;
change this so it is less likely to collide with local variable names.
Also rename struct command_table to struct db_command_table.

Reviewed by:	markj
MFC after:	1 week
Sponsored by:	Juniper Networks, Inc.
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D35367
2022-06-14 13:10:08 -03:00
Ryan Libby
d85c9cef13 ddb: reliably fail with ambiguous commands
db_cmd_match had an even/odd bug, where if a third command was partially
matched (or any odd number greater than one) the search result would be
set back from CMD_AMBIGUOUS to CMD_FOUND, causing the last command in
the list to be executed instead of failing the match.

Reported by:	mlaier
Reviewed by:	markj, mlaier, vangyzen
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D28659
2021-02-24 15:56:16 -08:00
Alex Richardson
fa2528ac64 Use atomic loads/stores when updating td->td_state
KCSAN complains about racy accesses in the locking code. Those races are
fine since they are inside a TD_SET_RUNNING() loop that expects the value
to be changed by another CPU.

Use relaxed atomic stores/loads to indicate that this variable can be
written/read by multiple CPUs at the same time. This will also prevent
the compiler from doing unexpected re-ordering.

Reported by:	GENERIC-KCSAN
Test Plan:	KCSAN no longer complains, kernel still runs fine.
Reviewed By:	markj, mjg (earlier version)
Differential Revision: https://reviews.freebsd.org/D28569
2021-02-18 14:02:48 +00:00
John Baldwin
3e06c7da02 Use kdb_thr_* to iterate over threads consistently in DDB.
The "findstack", "show all trace", and "show active trace" commands
were iterating over allproc to enumerate threads.  This missed threads
executing in exit1() after being removed from allproc.

Reviewed by:	kib
Sponsored by:	Netflix
Differential Revision:	https://reviews.freebsd.org/D27829
2020-12-31 16:01:35 -08:00
John Baldwin
825d234144 Don't check P_INMEM in kdb_thr_*().
Not all debugger operations that enumerate threads require thread
stacks to be resident in memory to be useful.  Instead, push P_INMEM
checks (if needed) into callers.

Reviewed by:	kib
Sponsored by:	Netflix
Differential Revision:	https://reviews.freebsd.org/D27827
2020-12-31 16:01:12 -08:00
Conrad Meyer
e2e050c8ef Extract eventfilter declarations to sys/_eventfilter.h
This allows replacing "sys/eventfilter.h" includes with "sys/_eventfilter.h"
in other header files (e.g., sys/{bus,conf,cpu}.h) and reduces header
pollution substantially.

EVENTHANDLER_DECLARE and EVENTHANDLER_LIST_DECLAREs were moved out of .c
files into appropriate headers (e.g., sys/proc.h, powernv/opal.h).

As a side effect of reduced header pollution, many .c files and headers no
longer contain needed definitions.  The remainder of the patch addresses
adding appropriate includes to fix those files.

LOCK_DEBUG and LOCK_FILE_LINE_ARG are moved to sys/_lock.h, as required by
sys/mutex.h since r326106 (but silently protected by header pollution prior
to this change).

No functional change (intended).  Of course, any out of tree modules that
relied on header pollution for sys/eventhandler.h, sys/lock.h, or
sys/mutex.h inclusion need to be fixed.  __FreeBSD_version has been bumped.
2019-05-20 00:38:23 +00:00
Conrad Meyer
e41793db2c ddb: Enable 'thread <address>'
Currently, the 'thread' command (to switch the debugger to another thread)
only accepts decimal-encoded tids.  Use the same parsing logic as 'show
thread <arg>' to accept hex-encoded thread pointers in addition to
decimal-encoded tids.

Document the 'thread' command in ddb.4 and expand the 'show thread'
documentation to cover the tid usage.

Reported by:	bwidawsk
Reviewed by:	bwidawsk (earlier version), kib (earlier version), markj
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D16962
2018-10-20 20:45:49 +00:00
Warner Losh
3531bbb5ea Restore db_radix on parse error, otherwise we'll silently change it to
10 on a botched trace command.
2018-04-17 15:44:05 +00:00
Pedro F. Giffuni
796df753f4 SPDX: Consider code from Carnegie-Mellon University.
Interesting cases, most likely from CMU Mach sources.
2017-11-30 15:48:35 +00:00
Edward Tomasz Napierala
212ff84f4a Make kdb_reenter() silent when explicitly called from db_error().
This removes the useless backtrace on various ddb(4) user errors.

Reviewed by:	jhb@
Obtained from:	CheriBSD
MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D13212
2017-11-28 12:53:55 +00:00
Gavin Atkinson
c6404e6690 Point people towards our https web site for documentation, http accesses are
just redirected anyway.

MFC after:	1 week
2017-11-09 13:38:24 +00:00
Edward Tomasz Napierala
9990da25ea Improve ddb(4) error reporting a bit.
Obtained from:	CheriBSD
MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
2017-07-06 12:30:39 +00:00
Edward Tomasz Napierala
b5bd6c7383 Make ddb(4) a bit more user-friendly by improving "help".
Obtained from:	CheriBSD
MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
2017-07-06 12:27:14 +00:00
Justin Hibbits
51d025a596 Correct the type of db_cmd_loop_done.
On big endian hardware that uses 1 byte bool a type mismatch of bool vs int will
cause the least signifcant byte of db_cmd_loop_done to be set, but the MSB to be
read, and read as 0.  This causes ddb to stay in an infinite loop.

MFC after:	1 week
2016-09-09 04:16:53 +00:00
Conrad Meyer
7e89a3221f ddb: Add 'show active trace' command
'show active trace', or 'acttrace' for short, prints backtraces from running
threads only.

Reviewed by:	mjg
Differential Revision:	https://reviews.freebsd.org/D7646
2016-08-26 02:46:47 +00:00
Pedro F. Giffuni
9f915a92c7 ddb: for pointers replace 0 with NULL.
Mostly cosmetical, no functional change.

Found with devel/coccinelle.
2016-04-15 17:27:20 +00:00
Pedro F. Giffuni
0a95ab74df ddb: de-register 2015-05-23 14:59:27 +00:00
Pedro F. Giffuni
7fb888588c ddb: Use NULL for pointers
Hinted by:	DragonflyBSD
2015-05-22 19:04:06 +00:00
Pedro F. Giffuni
cd508278c1 ddb: finish converting boolean values.
The replacement started at r283088 was necessarily incomplete without
replacing boolean_t with bool.  This also involved cleaning some type
mismatches and ansifying old C function declarations.

Pointed out by:	bde
Discussed with:	bde, ian, jhb
2015-05-21 15:16:18 +00:00
Pedro F. Giffuni
2b490bc747 ddb: stop boolean screaming.
TRUE --> true
FALSE--> false

Hinted by:	NetBSD
2015-05-18 22:27:46 +00:00
Pedro F. Giffuni
a41dd0319c ddb: ANSI-fy function declarations.
MFC after:	5 days
2014-10-12 18:01:52 +00:00
Pedro F. Giffuni
1549fb22e6 ddb: space/tab fixes.
No functional change.

MFC after:	3 days
2014-10-11 20:25:19 +00:00
John-Mark Gurney
527be4f229 handle longer commands so that lines don't overflow... people who added
commands forgot to check this...
2014-06-02 23:50:19 +00:00
Alfred Perlstein
21d748a957 Small textdump enhancements.
Allow textdumps to be called explicitly from DDB.

If "dump" is called in DDB and textdumps are enabled then abort the
dump and tell the user to turn off textdumps.

Add options TEXTDUMP_PREFERRED to turn textdumps on by default.
Add options TEXTDUMP_VERBOSE to be a bit more verbose while textdumping.

Reviewed by: rwatson

MFC after:	2 weeks
2012-11-01 04:07:08 +00:00
Konstantin Belousov
9ab83ecbcb Add 'findstack' ddb command to search either the thread kernel stack
or cached stack containing the specified kernel virtual address.

Discussed with:	pho
MFC after:	1 week
2011-12-16 11:44:20 +00:00
Marcel Moolenaar
299cceef03 Fix making kernel dumps from the debugger by creating a command
for it. Do not not expect a developer to call doadump(). Calling
doadump does not necessarily work when it's declared static. Nor
does it necessarily do what was intended in the context of text
dumps. The dump command always creates a core dump.

Move printing of error messages from doadump to the dump command,
now that we don't have to worry about being called from DDB.
2011-06-07 01:28:12 +00:00
Attilio Rao
7126ba42f6 - Add the possibility to reuse the already last used timeout when patting
the watchdog, via the watchdog(9) interface.
  For that, the WD_LASTVAL bitwise operation is used. It is mutually
  exclusive with any explicit timout passing to the watchdogs.
  The last timeout can be returned via the wdog_kern_last_timeout()
  KPI.
- Add the possibility to pat the watchdogs installed via the watchdog(9)
  interface from the kernel.
  In order to do that the new KPI wdog_kern_pat() is offered and it does
  accept normalized nanoseconds or WD_LASTVAL.
- Avoid to pass WD_ACTIVE down in the watchdog handlers. All the control
  bit processing should over to the upper layer functions and not passed
  down to the handlers at all.

These changes are intended to be used in order to fix up the watchdog
tripping in situation when the userland is busted, but protection is still
wanted (examples: shutdown syncing / disk dumping).

Sponsored by:	Sandvine Incorporated
Reviewed by:	emaste, des, cognet
MFC after:	2 weeks
2011-04-27 16:43:03 +00:00
Attilio Rao
8b927d7b7e Extend the DDB command "watchdog" with the ability to specify a timeout
value.

The timeout is expressed in the form T(N) = (2^N * nanoseconds) and can
be easilly extracted from the watchdog interface as a WD_TO_* macro.
That new functionality is supposed to fix re-entering the kernel from DDB
re-enabling the watchdog again (previously disabled) and also offer the
possibility to break for deadlocked DDB commands.

Please note that retro-compatibility is retained.

Sponsored by:	Sandvine Incorporated
Approved by:	des
MFC after:	10 days
2011-04-05 14:15:58 +00:00
Matthew D Fleming
3a5d36716f Modify kdb_trap() so that it re-calls the dbbe_trap function as long as
the debugger back-end has changed.  This means that switching from ddb
to gdb no longer requires a "step" which can be dangerous on an
already-crashed kernel.

Also add a capability to get from the gdb back-end back to ddb, by
typing ^C in the console window.

While here, simplify kdb_sysctl_available() by using
sbuf_new_for_sysctl(), and use strlcpy() instead of strncpy() since the
strlcpy semantic is desired.

MFC after:	1 month
2011-02-18 22:25:11 +00:00
Konstantin Belousov
a29af74b70 One more use for _SIG_VALID.
Submitted by:	Garrett Cooper <yanegomi gmail com>
MFC after:	1 week
2010-07-12 10:18:10 +00:00
Bjoern A. Zeeb
0f59fbc3d6 MFp4 @178364:
Implement an optional delay to the ddb reset/reboot command.

This allows textdumps to be run automatically with unattended reboots
after a resonable timeout, while still permitting an administrator to
break into debugger if attached to the console at the time of the
event for further debugging.  Cap the maximum delay at 1 week to avoid
highly accidental results, and default to 15s in case of problems
parsing the timeout value.

Move hex2dec helper function from db_thread.c to db_command.c to make
it generally available and prefix it with a "db_" to avoid namespace
collisions.

Reviewed by:	rwatson
MFC after:	4 weeks
2010-05-24 16:41:05 +00:00
Konstantin Belousov
a3de221dbe Among signal generation syscalls, only sigqueue(2) is allowed by POSIX
to fail due to lack of resources to queue siginfo. Add KSI_SIGQ flag
that allows sigqueue_add() to fail while trying to allocate memory for
new siginfo. When the flag is not set, behaviour is the same as for
KSI_TRAP: if memory cannot be allocated, set bit in sq_kill. KSI_TRAP is
kept to preserve KBI.

Add SI_KERNEL si_code, to be used in siginfo.si_code when signal is
generated by kernel. Deliver siginfo when signal is generated by kill(2)
family of syscalls (SI_USER with properly filled si_uid and si_pid), or
by kernel (SI_KERNEL, mostly job control or SIGIO). Since KSI_SIGQ flag
is not set for the ksi, low memory condition cause old behaviour.

Keep psignal(9) KBI intact, but modify it to generate SI_KERNEL
si_code. Pgsignal(9) and gsignal(9) now take ksi explicitely. Add
pksignal(9) that behaves like psignal but takes ksi, and ddb kill
command implemented as pksignal(..., ksi = NULL) to not do allocation
while in debugger.

While there, remove some register specifiers and use ANSI C prototypes.

Reviewed by:	davidxu
MFC after:	1 month
2009-11-17 11:39:15 +00:00
John Baldwin
93d4804b62 Initialize the DDB command list when initializing DDB so that the basic
commands are available from 'boot -d'.

Suggested by:	dfr
2008-09-25 19:50:14 +00:00
Sam Leffler
39297ba455 Make ddb command registration dynamic so modules can extend
the command set (only so long as the module is present):
o add db_command_register and db_command_unregister to add and remove
  commands, respectively
o replace linker sets with SYSINIT's (and SYSUINIT's) that register
  commands
o expose 3 list heads: db_cmd_table, db_show_table, and db_show_all_table
  for registering top-level commands, show operands, and show all operands,
  respectively

While here also:
o sort command lists
o add DB_ALIAS, DB_SHOW_ALIAS, and DB_SHOW_ALL_ALIAS to add aliases
  for existing commands
o add "show all trace" as an alias for "show alltrace"
o add "show all locks" as an alias for "show alllocks"

Submitted by:	Guillaume Ballet <gballet@gmail.com> (original version)
Reviewed by:	jhb
MFC after:	1 month
2008-09-15 22:45:14 +00:00
Sam Leffler
9e340a6190 enable dynamic addition of "show all" commands
MFC after:	3 weeks
2008-03-25 20:36:32 +00:00
Robert Watson
3755dbd805 When killing a user process from DDB, check that the requested signal is
> 0 rather than >= 0, or we will panic when trying to deliver the signal.

MFC after:	3 days
PR:		100802
Submitted by:	Valerio Daelli <valerio.daelli at gmail.com>
2008-03-07 14:26:30 +00:00
Robert Watson
618c7db30a Add textdump(4) facility, which provides an alternative form of kernel
dump using mechanically generated/extracted debugging output rather than
a simple memory dump.  Current sources of debugging output are:

- DDB output capture buffer, if there is captured output to save
- Kernel message buffer
- Kernel configuration, if included in kernel
- Kernel version string
- Panic message

Textdumps are stored in swap/dump partitions as with regular dumps, but
are laid out as ustar files in order to allow multiple parts to be stored
as a stream of sequentially written blocks.  Blocks are written out in
reverse order, as the size of a textdump isn't known a priori.  As with
regular dumps, they will be extracted using savecore(8).

One new DDB(4) command is added, "textdump", which accepts "set",
"unset", and "status" arguments.  By default, normal kernel dumps are
generated unless "textdump set" is run in order to schedule a textdump.
It can be canceled using "textdump unset" to restore generation of a
normal kernel dump.

Several sysctls exist to configure aspects of textdumps;
debug.ddb.textdump.pending can be set to check whether a textdump is
pending, or set/unset in order to control whether the next kernel dump
will be a textdump from userspace.

While textdumps don't have to be generated as a result of a DDB script
run automatically as part of a kernel panic, this is a particular useful
way to use them, as instead of generating a complete memory dump, a
simple transcript of an automated DDB session can be captured using the
DDB output capture and textdump facilities.  This can be used to
generate quite brief kernel bug reports rich in debugging information
but not dependent on kernel symbol tables or precisely synchronized
source code.  Most textdumps I generate are less than 100k including
the full message buffer.  Using textdumps with an interactive debugging
session is also useful, with capture being enabled/disabled in order to
record some but not all of the DDB session.

MFC after:	3 months
2007-12-26 11:32:33 +00:00
Robert Watson
c9b0cc3b96 Add a simple scripting facility to DDB(4), allowing the user to
define a set of named scripts.  Each script consists of a list of DDB
commands separated by ";"s that will be executed verbatim.  No higher
level language constructs, such as branching, are provided for:
scripts are executed by sequentially injecting commands into the DDB
input buffer.

Four new commands are present in DDB: "run" to run a specific script,
"script" to define or print a script, "scripts" to list currently
defined scripts, and "unscript" to delete a script, modeled on shell
alias commands.  Scripts may also be manipulated using sysctls in the
debug.ddb.scripting MIB space, although users will prefer to use the
soon-to-be-added ddb(8) tool for usability reasons.

Scripts with certain names are automatically executed on various DDB
events, such as entering the debugger via a panic, a witness error,
watchdog, breakpoint, sysctl, serial break, etc, allowing customized
handling.

MFC after:	3 months
2007-12-26 09:33:19 +00:00
Robert Watson
086fec574e Add a new DDB(4) facility, output capture. Input and output from DDB may be
captured to a memory buffer for later inspection using sysctl(8), or in the
future, to a textdump.

A new DDB command, "capture", is added, which accepts arguments "on", "off",
"reset", and "status".

A new DDB sysctl tree, debug.ddb.capture, is added, which can be used to
resize the capture buffer and extract buffer contents.

MFC after:	3 months
2007-12-25 23:06:51 +00:00
Xin LI
f67af5c918 Use FOREACH_PROC_IN_SYSTEM instead of using its unrolled form. 2007-01-17 15:05:52 +00:00
Konstantin Belousov
9641e38966 On trap while inside ddb, the trap handler calls kdb_reenter(), that
longjmp to the default context. As result, "alltrace" command may
be prematurely terminated (without error message). This is happens,
for instance, when system is low on memory and referenced page in
kernel-mode thread stack is swapped out.

Protect "alltrace" against termination on trap by setting temporary
kdb_jmpbuf context.

Submitted by:	Peter Holm
2006-11-02 11:47:38 +00:00
Bruce Evans
e4b732cff1 The powerpc and sparc64 MD `reboot' commands should never have existed
since they just duplicated the MI `reset' command.  Instead of removing
them, make `reboot' an MI alias for `reboot' since this gives a better
way of killing the `r' alias for `reset'.  Remove the `registers' command
that was used to kill the alias.

Turn the powerpc and sparc64 MD `halt' command into an MI command.

A copy of sparc64/db_interface.c grew in sun4v just after I found the
extra reboot commands.  It has not been changed, and is now not
identical.  Duplicated commands come out duplicated in ddb's online
help, but cause large problems when used (e.g., on i386's with 2 halt's
and an hwatch, typing h doesn' give the expected message about an
ambiguous command, but hangs like the halt command or a looping parseri
would).
2006-10-10 07:26:54 +00:00
Bruce Evans
75149ab872 Added some aliases:
- `b' is now an official alias for `break'.  It used to be an unofficial
  alias, but this was broken by adding the `bt' alias for `trace'.
- `t' is now an official alias for `trace'.  It used to be an unofficial
  alias, but this was broken by adding the `thread' command.
- `registers' is now an alias for `show registers'.  This is a hack to
  break the unofficial `r' alias for `reset'.  `r' really means
  `registers' in some debuggers, so I sometimes type it accidentally and
  am annoyed when it resets the system.  A short command shouldn't have
  such a large effect.  Now at least `res' must be typed to disambiguate
  `reset'.
2006-10-08 18:37:00 +00:00