Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/kernel/debug/kdb/kdb_main.c
29520 views
1
/*
2
* Kernel Debugger Architecture Independent Main Code
3
*
4
* This file is subject to the terms and conditions of the GNU General Public
5
* License. See the file "COPYING" in the main directory of this archive
6
* for more details.
7
*
8
* Copyright (C) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
9
* Copyright (C) 2000 Stephane Eranian <[email protected]>
10
* Xscale (R) modifications copyright (C) 2003 Intel Corporation.
11
* Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
12
*/
13
14
#include <linux/ctype.h>
15
#include <linux/types.h>
16
#include <linux/string.h>
17
#include <linux/kernel.h>
18
#include <linux/kmsg_dump.h>
19
#include <linux/reboot.h>
20
#include <linux/sched.h>
21
#include <linux/sched/loadavg.h>
22
#include <linux/sched/stat.h>
23
#include <linux/sched/debug.h>
24
#include <linux/sysrq.h>
25
#include <linux/smp.h>
26
#include <linux/utsname.h>
27
#include <linux/vmalloc.h>
28
#include <linux/moduleparam.h>
29
#include <linux/mm.h>
30
#include <linux/init.h>
31
#include <linux/kallsyms.h>
32
#include <linux/kgdb.h>
33
#include <linux/kdb.h>
34
#include <linux/notifier.h>
35
#include <linux/interrupt.h>
36
#include <linux/delay.h>
37
#include <linux/nmi.h>
38
#include <linux/time.h>
39
#include <linux/ptrace.h>
40
#include <linux/sysctl.h>
41
#include <linux/cpu.h>
42
#include <linux/kdebug.h>
43
#include <linux/proc_fs.h>
44
#include <linux/uaccess.h>
45
#include <linux/slab.h>
46
#include <linux/security.h>
47
#include "kdb_private.h"
48
49
#undef MODULE_PARAM_PREFIX
50
#define MODULE_PARAM_PREFIX "kdb."
51
52
static int kdb_cmd_enabled = CONFIG_KDB_DEFAULT_ENABLE;
53
module_param_named(cmd_enable, kdb_cmd_enabled, int, 0600);
54
55
char kdb_grep_string[KDB_GREP_STRLEN];
56
int kdb_grepping_flag;
57
EXPORT_SYMBOL(kdb_grepping_flag);
58
int kdb_grep_leading;
59
int kdb_grep_trailing;
60
61
/*
62
* Kernel debugger state flags
63
*/
64
unsigned int kdb_flags;
65
66
/*
67
* kdb_lock protects updates to kdb_initial_cpu. Used to
68
* single thread processors through the kernel debugger.
69
*/
70
int kdb_initial_cpu = -1; /* cpu number that owns kdb */
71
int kdb_nextline = 1;
72
int kdb_state; /* General KDB state */
73
74
struct task_struct *kdb_current_task;
75
struct pt_regs *kdb_current_regs;
76
77
const char *kdb_diemsg;
78
static int kdb_go_count;
79
#ifdef CONFIG_KDB_CONTINUE_CATASTROPHIC
80
static unsigned int kdb_continue_catastrophic =
81
CONFIG_KDB_CONTINUE_CATASTROPHIC;
82
#else
83
static unsigned int kdb_continue_catastrophic;
84
#endif
85
86
/* kdb_cmds_head describes the available commands. */
87
static LIST_HEAD(kdb_cmds_head);
88
89
typedef struct _kdbmsg {
90
int km_diag; /* kdb diagnostic */
91
char *km_msg; /* Corresponding message text */
92
} kdbmsg_t;
93
94
#define KDBMSG(msgnum, text) \
95
{ KDB_##msgnum, text }
96
97
static kdbmsg_t kdbmsgs[] = {
98
KDBMSG(NOTFOUND, "Command Not Found"),
99
KDBMSG(ARGCOUNT, "Improper argument count, see usage."),
100
KDBMSG(BADWIDTH, "Illegal value for BYTESPERWORD use 1, 2, 4 or 8, "
101
"8 is only allowed on 64 bit systems"),
102
KDBMSG(BADRADIX, "Illegal value for RADIX use 8, 10 or 16"),
103
KDBMSG(NOTENV, "Cannot find environment variable"),
104
KDBMSG(NOENVVALUE, "Environment variable should have value"),
105
KDBMSG(NOTIMP, "Command not implemented"),
106
KDBMSG(ENVFULL, "Environment full"),
107
KDBMSG(KMALLOCFAILED, "Failed to allocate memory"),
108
KDBMSG(TOOMANYBPT, "Too many breakpoints defined"),
109
#ifdef CONFIG_CPU_XSCALE
110
KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"),
111
#else
112
KDBMSG(TOOMANYDBREGS, "More breakpoints than db registers defined"),
113
#endif
114
KDBMSG(DUPBPT, "Duplicate breakpoint address"),
115
KDBMSG(BPTNOTFOUND, "Breakpoint not found"),
116
KDBMSG(BADMODE, "Invalid IDMODE"),
117
KDBMSG(BADINT, "Illegal numeric value"),
118
KDBMSG(INVADDRFMT, "Invalid symbolic address format"),
119
KDBMSG(BADREG, "Invalid register name"),
120
KDBMSG(BADCPUNUM, "Invalid cpu number"),
121
KDBMSG(BADLENGTH, "Invalid length field"),
122
KDBMSG(NOBP, "No Breakpoint exists"),
123
KDBMSG(BADADDR, "Invalid address"),
124
KDBMSG(NOPERM, "Permission denied"),
125
};
126
#undef KDBMSG
127
128
static const int __nkdb_err = ARRAY_SIZE(kdbmsgs);
129
130
131
/*
132
* Initial environment. This is all kept static and local to this file.
133
* The entire environment is limited to a fixed number of entries
134
* (add more to __env[] if required)
135
*/
136
137
static char *__env[31] = {
138
#if defined(CONFIG_SMP)
139
"PROMPT=[%d]kdb> ",
140
#else
141
"PROMPT=kdb> ",
142
#endif
143
"MOREPROMPT=more> ",
144
"RADIX=16",
145
"MDCOUNT=8", /* lines of md output */
146
KDB_PLATFORM_ENV,
147
"DTABCOUNT=30",
148
"NOSECT=1",
149
};
150
151
static const int __nenv = ARRAY_SIZE(__env);
152
153
/*
154
* Update the permissions flags (kdb_cmd_enabled) to match the
155
* current lockdown state.
156
*
157
* Within this function the calls to security_locked_down() are "lazy". We
158
* avoid calling them if the current value of kdb_cmd_enabled already excludes
159
* flags that might be subject to lockdown. Additionally we deliberately check
160
* the lockdown flags independently (even though read lockdown implies write
161
* lockdown) since that results in both simpler code and clearer messages to
162
* the user on first-time debugger entry.
163
*
164
* The permission masks during a read+write lockdown permits the following
165
* flags: INSPECT, SIGNAL, REBOOT (and ALWAYS_SAFE).
166
*
167
* The INSPECT commands are not blocked during lockdown because they are
168
* not arbitrary memory reads. INSPECT covers the backtrace family (sometimes
169
* forcing them to have no arguments) and lsmod. These commands do expose
170
* some kernel state but do not allow the developer seated at the console to
171
* choose what state is reported. SIGNAL and REBOOT should not be controversial,
172
* given these are allowed for root during lockdown already.
173
*/
174
static void kdb_check_for_lockdown(void)
175
{
176
const int write_flags = KDB_ENABLE_MEM_WRITE |
177
KDB_ENABLE_REG_WRITE |
178
KDB_ENABLE_FLOW_CTRL;
179
const int read_flags = KDB_ENABLE_MEM_READ |
180
KDB_ENABLE_REG_READ;
181
182
bool need_to_lockdown_write = false;
183
bool need_to_lockdown_read = false;
184
185
if (kdb_cmd_enabled & (KDB_ENABLE_ALL | write_flags))
186
need_to_lockdown_write =
187
security_locked_down(LOCKDOWN_DBG_WRITE_KERNEL);
188
189
if (kdb_cmd_enabled & (KDB_ENABLE_ALL | read_flags))
190
need_to_lockdown_read =
191
security_locked_down(LOCKDOWN_DBG_READ_KERNEL);
192
193
/* De-compose KDB_ENABLE_ALL if required */
194
if (need_to_lockdown_write || need_to_lockdown_read)
195
if (kdb_cmd_enabled & KDB_ENABLE_ALL)
196
kdb_cmd_enabled = KDB_ENABLE_MASK & ~KDB_ENABLE_ALL;
197
198
if (need_to_lockdown_write)
199
kdb_cmd_enabled &= ~write_flags;
200
201
if (need_to_lockdown_read)
202
kdb_cmd_enabled &= ~read_flags;
203
}
204
205
/*
206
* Check whether the flags of the current command, the permissions of the kdb
207
* console and the lockdown state allow a command to be run.
208
*/
209
static bool kdb_check_flags(kdb_cmdflags_t flags, int permissions,
210
bool no_args)
211
{
212
/* permissions comes from userspace so needs massaging slightly */
213
permissions &= KDB_ENABLE_MASK;
214
permissions |= KDB_ENABLE_ALWAYS_SAFE;
215
216
/* some commands change group when launched with no arguments */
217
if (no_args)
218
permissions |= permissions << KDB_ENABLE_NO_ARGS_SHIFT;
219
220
flags |= KDB_ENABLE_ALL;
221
222
return permissions & flags;
223
}
224
225
/*
226
* kdbgetenv - This function will return the character string value of
227
* an environment variable.
228
* Parameters:
229
* match A character string representing an environment variable.
230
* Returns:
231
* NULL No environment variable matches 'match'
232
* char* Pointer to string value of environment variable.
233
*/
234
char *kdbgetenv(const char *match)
235
{
236
char **ep = __env;
237
int matchlen = strlen(match);
238
int i;
239
240
for (i = 0; i < __nenv; i++) {
241
char *e = *ep++;
242
243
if (!e)
244
continue;
245
246
if ((strncmp(match, e, matchlen) == 0)
247
&& ((e[matchlen] == '\0')
248
|| (e[matchlen] == '='))) {
249
char *cp = strchr(e, '=');
250
return cp ? ++cp : "";
251
}
252
}
253
return NULL;
254
}
255
256
/*
257
* kdbgetulenv - This function will return the value of an unsigned
258
* long-valued environment variable.
259
* Parameters:
260
* match A character string representing a numeric value
261
* Outputs:
262
* *value the unsigned long representation of the env variable 'match'
263
* Returns:
264
* Zero on success, a kdb diagnostic on failure.
265
*/
266
static int kdbgetulenv(const char *match, unsigned long *value)
267
{
268
char *ep;
269
270
ep = kdbgetenv(match);
271
if (!ep)
272
return KDB_NOTENV;
273
if (strlen(ep) == 0)
274
return KDB_NOENVVALUE;
275
if (kstrtoul(ep, 0, value))
276
return KDB_BADINT;
277
278
return 0;
279
}
280
281
/*
282
* kdbgetintenv - This function will return the value of an
283
* integer-valued environment variable.
284
* Parameters:
285
* match A character string representing an integer-valued env variable
286
* Outputs:
287
* *value the integer representation of the environment variable 'match'
288
* Returns:
289
* Zero on success, a kdb diagnostic on failure.
290
*/
291
int kdbgetintenv(const char *match, int *value)
292
{
293
unsigned long val;
294
int diag;
295
296
diag = kdbgetulenv(match, &val);
297
if (!diag)
298
*value = (int) val;
299
return diag;
300
}
301
302
/*
303
* kdb_setenv() - Alter an existing environment variable or create a new one.
304
* @var: Name of the variable
305
* @val: Value of the variable
306
*
307
* Return: Zero on success, a kdb diagnostic on failure.
308
*/
309
static int kdb_setenv(const char *var, const char *val)
310
{
311
int i;
312
char *ep;
313
size_t varlen, vallen;
314
315
varlen = strlen(var);
316
vallen = strlen(val);
317
ep = kmalloc(varlen + vallen + 2, GFP_KDB);
318
if (!ep)
319
return KDB_KMALLOCFAILED;
320
321
sprintf(ep, "%s=%s", var, val);
322
323
for (i = 0; i < __nenv; i++) {
324
if (__env[i]
325
&& ((strncmp(__env[i], var, varlen) == 0)
326
&& ((__env[i][varlen] == '\0')
327
|| (__env[i][varlen] == '=')))) {
328
kfree_const(__env[i]);
329
__env[i] = ep;
330
return 0;
331
}
332
}
333
334
/*
335
* Wasn't existing variable. Fit into slot.
336
*/
337
for (i = 0; i < __nenv-1; i++) {
338
if (__env[i] == (char *)0) {
339
__env[i] = ep;
340
return 0;
341
}
342
}
343
344
return KDB_ENVFULL;
345
}
346
347
/*
348
* kdb_printenv() - Display the current environment variables.
349
*/
350
static void kdb_printenv(void)
351
{
352
int i;
353
354
for (i = 0; i < __nenv; i++) {
355
if (__env[i])
356
kdb_printf("%s\n", __env[i]);
357
}
358
}
359
360
/*
361
* kdbgetularg - This function will convert a numeric string into an
362
* unsigned long value.
363
* Parameters:
364
* arg A character string representing a numeric value
365
* Outputs:
366
* *value the unsigned long representation of arg.
367
* Returns:
368
* Zero on success, a kdb diagnostic on failure.
369
*/
370
int kdbgetularg(const char *arg, unsigned long *value)
371
{
372
if (kstrtoul(arg, 0, value))
373
return KDB_BADINT;
374
return 0;
375
}
376
377
int kdbgetu64arg(const char *arg, u64 *value)
378
{
379
if (kstrtou64(arg, 0, value))
380
return KDB_BADINT;
381
return 0;
382
}
383
384
/*
385
* kdb_set - This function implements the 'set' command. Alter an
386
* existing environment variable or create a new one.
387
*/
388
int kdb_set(int argc, const char **argv)
389
{
390
/*
391
* we can be invoked two ways:
392
* set var=value argv[1]="var", argv[2]="value"
393
* set var = value argv[1]="var", argv[2]="=", argv[3]="value"
394
* - if the latter, shift 'em down.
395
*/
396
if (argc == 3) {
397
argv[2] = argv[3];
398
argc--;
399
}
400
401
if (argc != 2)
402
return KDB_ARGCOUNT;
403
404
/*
405
* Censor sensitive variables
406
*/
407
if (strcmp(argv[1], "PROMPT") == 0 &&
408
!kdb_check_flags(KDB_ENABLE_MEM_READ, kdb_cmd_enabled, false))
409
return KDB_NOPERM;
410
411
/*
412
* Check for internal variables
413
*/
414
if (strcmp(argv[1], "KDBDEBUG") == 0) {
415
unsigned int debugflags;
416
int ret;
417
418
ret = kstrtouint(argv[2], 0, &debugflags);
419
if (ret || debugflags & ~KDB_DEBUG_FLAG_MASK) {
420
kdb_printf("kdb: illegal debug flags '%s'\n",
421
argv[2]);
422
return 0;
423
}
424
kdb_flags = (kdb_flags & ~KDB_DEBUG(MASK))
425
| (debugflags << KDB_DEBUG_FLAG_SHIFT);
426
427
return 0;
428
}
429
430
/*
431
* Tokenizer squashed the '=' sign. argv[1] is variable
432
* name, argv[2] = value.
433
*/
434
return kdb_setenv(argv[1], argv[2]);
435
}
436
437
static int kdb_check_regs(void)
438
{
439
if (!kdb_current_regs) {
440
kdb_printf("No current kdb registers."
441
" You may need to select another task\n");
442
return KDB_BADREG;
443
}
444
return 0;
445
}
446
447
/*
448
* kdbgetaddrarg - This function is responsible for parsing an
449
* address-expression and returning the value of the expression,
450
* symbol name, and offset to the caller.
451
*
452
* The argument may consist of a numeric value (decimal or
453
* hexadecimal), a symbol name, a register name (preceded by the
454
* percent sign), an environment variable with a numeric value
455
* (preceded by a dollar sign) or a simple arithmetic expression
456
* consisting of a symbol name, +/-, and a numeric constant value
457
* (offset).
458
* Parameters:
459
* argc - count of arguments in argv
460
* argv - argument vector
461
* *nextarg - index to next unparsed argument in argv[]
462
* regs - Register state at time of KDB entry
463
* Outputs:
464
* *value - receives the value of the address-expression
465
* *offset - receives the offset specified, if any
466
* *name - receives the symbol name, if any
467
* *nextarg - index to next unparsed argument in argv[]
468
* Returns:
469
* zero is returned on success, a kdb diagnostic code is
470
* returned on error.
471
*/
472
int kdbgetaddrarg(int argc, const char **argv, int *nextarg,
473
unsigned long *value, long *offset,
474
char **name)
475
{
476
unsigned long addr;
477
unsigned long off = 0;
478
int positive;
479
int diag;
480
int found = 0;
481
char *symname;
482
char symbol = '\0';
483
char *cp;
484
kdb_symtab_t symtab;
485
486
/*
487
* If the enable flags prohibit both arbitrary memory access
488
* and flow control then there are no reasonable grounds to
489
* provide symbol lookup.
490
*/
491
if (!kdb_check_flags(KDB_ENABLE_MEM_READ | KDB_ENABLE_FLOW_CTRL,
492
kdb_cmd_enabled, false))
493
return KDB_NOPERM;
494
495
/*
496
* Process arguments which follow the following syntax:
497
*
498
* symbol | numeric-address [+/- numeric-offset]
499
* %register
500
* $environment-variable
501
*/
502
503
if (*nextarg > argc)
504
return KDB_ARGCOUNT;
505
506
symname = (char *)argv[*nextarg];
507
508
/*
509
* If there is no whitespace between the symbol
510
* or address and the '+' or '-' symbols, we
511
* remember the character and replace it with a
512
* null so the symbol/value can be properly parsed
513
*/
514
cp = strpbrk(symname, "+-");
515
if (cp != NULL) {
516
symbol = *cp;
517
*cp++ = '\0';
518
}
519
520
if (symname[0] == '$') {
521
diag = kdbgetulenv(&symname[1], &addr);
522
if (diag)
523
return diag;
524
} else if (symname[0] == '%') {
525
diag = kdb_check_regs();
526
if (diag)
527
return diag;
528
/* Implement register values with % at a later time as it is
529
* arch optional.
530
*/
531
return KDB_NOTIMP;
532
} else {
533
found = kdbgetsymval(symname, &symtab);
534
if (found) {
535
addr = symtab.sym_start;
536
} else {
537
diag = kdbgetularg(argv[*nextarg], &addr);
538
if (diag)
539
return diag;
540
}
541
}
542
543
if (!found)
544
found = kdbnearsym(addr, &symtab);
545
546
(*nextarg)++;
547
548
if (name)
549
*name = symname;
550
if (value)
551
*value = addr;
552
if (offset && name && *name)
553
*offset = addr - symtab.sym_start;
554
555
if ((*nextarg > argc)
556
&& (symbol == '\0'))
557
return 0;
558
559
/*
560
* check for +/- and offset
561
*/
562
563
if (symbol == '\0') {
564
if ((argv[*nextarg][0] != '+')
565
&& (argv[*nextarg][0] != '-')) {
566
/*
567
* Not our argument. Return.
568
*/
569
return 0;
570
} else {
571
positive = (argv[*nextarg][0] == '+');
572
(*nextarg)++;
573
}
574
} else
575
positive = (symbol == '+');
576
577
/*
578
* Now there must be an offset!
579
*/
580
if ((*nextarg > argc)
581
&& (symbol == '\0')) {
582
return KDB_INVADDRFMT;
583
}
584
585
if (!symbol) {
586
cp = (char *)argv[*nextarg];
587
(*nextarg)++;
588
}
589
590
diag = kdbgetularg(cp, &off);
591
if (diag)
592
return diag;
593
594
if (!positive)
595
off = -off;
596
597
if (offset)
598
*offset += off;
599
600
if (value)
601
*value += off;
602
603
return 0;
604
}
605
606
static void kdb_cmderror(int diag)
607
{
608
int i;
609
610
if (diag >= 0) {
611
kdb_printf("no error detected (diagnostic is %d)\n", diag);
612
return;
613
}
614
615
for (i = 0; i < __nkdb_err; i++) {
616
if (kdbmsgs[i].km_diag == diag) {
617
kdb_printf("diag: %d: %s\n", diag, kdbmsgs[i].km_msg);
618
return;
619
}
620
}
621
622
kdb_printf("Unknown diag %d\n", -diag);
623
}
624
625
/*
626
* kdb_defcmd, kdb_defcmd2 - This function implements the 'defcmd'
627
* command which defines one command as a set of other commands,
628
* terminated by endefcmd. kdb_defcmd processes the initial
629
* 'defcmd' command, kdb_defcmd2 is invoked from kdb_parse for
630
* the following commands until 'endefcmd'.
631
* Inputs:
632
* argc argument count
633
* argv argument vector
634
* Returns:
635
* zero for success, a kdb diagnostic if error
636
*/
637
struct kdb_macro {
638
kdbtab_t cmd; /* Macro command */
639
struct list_head statements; /* Associated statement list */
640
};
641
642
struct kdb_macro_statement {
643
char *statement; /* Statement text */
644
struct list_head list_node; /* Statement list node */
645
};
646
647
static struct kdb_macro *kdb_macro;
648
static bool defcmd_in_progress;
649
650
/* Forward references */
651
static int kdb_exec_defcmd(int argc, const char **argv);
652
653
static int kdb_defcmd2(const char *cmdstr, const char *argv0)
654
{
655
struct kdb_macro_statement *kms;
656
657
if (!kdb_macro)
658
return KDB_NOTIMP;
659
660
if (strcmp(argv0, "endefcmd") == 0) {
661
defcmd_in_progress = false;
662
if (!list_empty(&kdb_macro->statements))
663
kdb_register(&kdb_macro->cmd);
664
return 0;
665
}
666
667
kms = kmalloc(sizeof(*kms), GFP_KDB);
668
if (!kms) {
669
kdb_printf("Could not allocate new kdb macro command: %s\n",
670
cmdstr);
671
return KDB_NOTIMP;
672
}
673
674
kms->statement = kdb_strdup(cmdstr, GFP_KDB);
675
list_add_tail(&kms->list_node, &kdb_macro->statements);
676
677
return 0;
678
}
679
680
static int kdb_defcmd(int argc, const char **argv)
681
{
682
kdbtab_t *mp;
683
684
if (defcmd_in_progress) {
685
kdb_printf("kdb: nested defcmd detected, assuming missing "
686
"endefcmd\n");
687
kdb_defcmd2("endefcmd", "endefcmd");
688
}
689
if (argc == 0) {
690
kdbtab_t *kp;
691
struct kdb_macro *kmp;
692
struct kdb_macro_statement *kms;
693
694
list_for_each_entry(kp, &kdb_cmds_head, list_node) {
695
if (kp->func == kdb_exec_defcmd) {
696
kdb_printf("defcmd %s \"%s\" \"%s\"\n",
697
kp->name, kp->usage, kp->help);
698
kmp = container_of(kp, struct kdb_macro, cmd);
699
list_for_each_entry(kms, &kmp->statements,
700
list_node)
701
kdb_printf("%s", kms->statement);
702
kdb_printf("endefcmd\n");
703
}
704
}
705
return 0;
706
}
707
if (argc != 3)
708
return KDB_ARGCOUNT;
709
if (in_dbg_master()) {
710
kdb_printf("Command only available during kdb_init()\n");
711
return KDB_NOTIMP;
712
}
713
kdb_macro = kzalloc(sizeof(*kdb_macro), GFP_KDB);
714
if (!kdb_macro)
715
goto fail_defcmd;
716
717
mp = &kdb_macro->cmd;
718
mp->func = kdb_exec_defcmd;
719
mp->minlen = 0;
720
mp->flags = KDB_ENABLE_ALWAYS_SAFE;
721
mp->name = kdb_strdup(argv[1], GFP_KDB);
722
if (!mp->name)
723
goto fail_name;
724
mp->usage = kdb_strdup_dequote(argv[2], GFP_KDB);
725
if (!mp->usage)
726
goto fail_usage;
727
mp->help = kdb_strdup_dequote(argv[3], GFP_KDB);
728
if (!mp->help)
729
goto fail_help;
730
731
INIT_LIST_HEAD(&kdb_macro->statements);
732
defcmd_in_progress = true;
733
return 0;
734
fail_help:
735
kfree(mp->usage);
736
fail_usage:
737
kfree(mp->name);
738
fail_name:
739
kfree(kdb_macro);
740
fail_defcmd:
741
kdb_printf("Could not allocate new kdb_macro entry for %s\n", argv[1]);
742
return KDB_NOTIMP;
743
}
744
745
/*
746
* kdb_exec_defcmd - Execute the set of commands associated with this
747
* defcmd name.
748
* Inputs:
749
* argc argument count
750
* argv argument vector
751
* Returns:
752
* zero for success, a kdb diagnostic if error
753
*/
754
static int kdb_exec_defcmd(int argc, const char **argv)
755
{
756
int ret;
757
kdbtab_t *kp;
758
struct kdb_macro *kmp;
759
struct kdb_macro_statement *kms;
760
761
if (argc != 0)
762
return KDB_ARGCOUNT;
763
764
list_for_each_entry(kp, &kdb_cmds_head, list_node) {
765
if (strcmp(kp->name, argv[0]) == 0)
766
break;
767
}
768
if (list_entry_is_head(kp, &kdb_cmds_head, list_node)) {
769
kdb_printf("kdb_exec_defcmd: could not find commands for %s\n",
770
argv[0]);
771
return KDB_NOTIMP;
772
}
773
kmp = container_of(kp, struct kdb_macro, cmd);
774
list_for_each_entry(kms, &kmp->statements, list_node) {
775
/*
776
* Recursive use of kdb_parse, do not use argv after this point.
777
*/
778
argv = NULL;
779
kdb_printf("[%s]kdb> %s\n", kmp->cmd.name, kms->statement);
780
ret = kdb_parse(kms->statement);
781
if (ret)
782
return ret;
783
}
784
return 0;
785
}
786
787
/* Command history */
788
#define KDB_CMD_HISTORY_COUNT 32
789
#define CMD_BUFLEN 200 /* kdb_printf: max printline
790
* size == 256 */
791
static unsigned int cmd_head, cmd_tail;
792
static unsigned int cmdptr;
793
static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN];
794
static char cmd_cur[CMD_BUFLEN];
795
796
/*
797
* The "str" argument may point to something like | grep xyz
798
*/
799
static void parse_grep(const char *str)
800
{
801
int len;
802
char *cp = (char *)str, *cp2;
803
804
/* sanity check: we should have been called with the \ first */
805
if (*cp != '|')
806
return;
807
cp++;
808
while (isspace(*cp))
809
cp++;
810
if (!str_has_prefix(cp, "grep ")) {
811
kdb_printf("invalid 'pipe', see grephelp\n");
812
return;
813
}
814
cp += 5;
815
while (isspace(*cp))
816
cp++;
817
cp2 = strchr(cp, '\n');
818
if (cp2)
819
*cp2 = '\0'; /* remove the trailing newline */
820
len = strlen(cp);
821
if (len == 0) {
822
kdb_printf("invalid 'pipe', see grephelp\n");
823
return;
824
}
825
/* now cp points to a nonzero length search string */
826
if (*cp == '"') {
827
/* allow it be "x y z" by removing the "'s - there must
828
be two of them */
829
cp++;
830
cp2 = strchr(cp, '"');
831
if (!cp2) {
832
kdb_printf("invalid quoted string, see grephelp\n");
833
return;
834
}
835
*cp2 = '\0'; /* end the string where the 2nd " was */
836
}
837
kdb_grep_leading = 0;
838
if (*cp == '^') {
839
kdb_grep_leading = 1;
840
cp++;
841
}
842
len = strlen(cp);
843
kdb_grep_trailing = 0;
844
if (*(cp+len-1) == '$') {
845
kdb_grep_trailing = 1;
846
*(cp+len-1) = '\0';
847
}
848
len = strlen(cp);
849
if (!len)
850
return;
851
if (len >= KDB_GREP_STRLEN) {
852
kdb_printf("search string too long\n");
853
return;
854
}
855
memcpy(kdb_grep_string, cp, len + 1);
856
kdb_grepping_flag++;
857
return;
858
}
859
860
/*
861
* kdb_parse - Parse the command line, search the command table for a
862
* matching command and invoke the command function. This
863
* function may be called recursively, if it is, the second call
864
* will overwrite argv and cbuf. It is the caller's
865
* responsibility to save their argv if they recursively call
866
* kdb_parse().
867
* Parameters:
868
* cmdstr The input command line to be parsed.
869
* regs The registers at the time kdb was entered.
870
* Returns:
871
* Zero for success, a kdb diagnostic if failure.
872
* Remarks:
873
* Limited to 20 tokens.
874
*
875
* Real rudimentary tokenization. Basically only whitespace
876
* is considered a token delimiter (but special consideration
877
* is taken of the '=' sign as used by the 'set' command).
878
*
879
* The algorithm used to tokenize the input string relies on
880
* there being at least one whitespace (or otherwise useless)
881
* character between tokens as the character immediately following
882
* the token is altered in-place to a null-byte to terminate the
883
* token string.
884
*/
885
886
#define MAXARGC 20
887
888
int kdb_parse(const char *cmdstr)
889
{
890
static char *argv[MAXARGC];
891
static int argc;
892
static char cbuf[CMD_BUFLEN+2];
893
char *cp;
894
char *cpp, quoted;
895
kdbtab_t *tp;
896
int escaped, ignore_errors = 0, check_grep = 0;
897
898
/*
899
* First tokenize the command string.
900
*/
901
cp = (char *)cmdstr;
902
903
if (KDB_FLAG(CMD_INTERRUPT)) {
904
/* Previous command was interrupted, newline must not
905
* repeat the command */
906
KDB_FLAG_CLEAR(CMD_INTERRUPT);
907
KDB_STATE_SET(PAGER);
908
argc = 0; /* no repeat */
909
}
910
911
if (*cp != '\n' && *cp != '\0') {
912
argc = 0;
913
cpp = cbuf;
914
while (*cp) {
915
/* skip whitespace */
916
while (isspace(*cp))
917
cp++;
918
if ((*cp == '\0') || (*cp == '\n') ||
919
(*cp == '#' && !defcmd_in_progress))
920
break;
921
/* special case: check for | grep pattern */
922
if (*cp == '|') {
923
check_grep++;
924
break;
925
}
926
if (cpp >= cbuf + CMD_BUFLEN) {
927
kdb_printf("kdb_parse: command buffer "
928
"overflow, command ignored\n%s\n",
929
cmdstr);
930
return KDB_NOTFOUND;
931
}
932
if (argc >= MAXARGC - 1) {
933
kdb_printf("kdb_parse: too many arguments, "
934
"command ignored\n%s\n", cmdstr);
935
return KDB_NOTFOUND;
936
}
937
argv[argc++] = cpp;
938
escaped = 0;
939
quoted = '\0';
940
/* Copy to next unquoted and unescaped
941
* whitespace or '=' */
942
while (*cp && *cp != '\n' &&
943
(escaped || quoted || !isspace(*cp))) {
944
if (cpp >= cbuf + CMD_BUFLEN)
945
break;
946
if (escaped) {
947
escaped = 0;
948
*cpp++ = *cp++;
949
continue;
950
}
951
if (*cp == '\\') {
952
escaped = 1;
953
++cp;
954
continue;
955
}
956
if (*cp == quoted)
957
quoted = '\0';
958
else if (*cp == '\'' || *cp == '"')
959
quoted = *cp;
960
*cpp = *cp++;
961
if (*cpp == '=' && !quoted)
962
break;
963
++cpp;
964
}
965
*cpp++ = '\0'; /* Squash a ws or '=' character */
966
}
967
}
968
if (!argc)
969
return 0;
970
if (check_grep)
971
parse_grep(cp);
972
if (defcmd_in_progress) {
973
int result = kdb_defcmd2(cmdstr, argv[0]);
974
if (!defcmd_in_progress) {
975
argc = 0; /* avoid repeat on endefcmd */
976
*(argv[0]) = '\0';
977
}
978
return result;
979
}
980
if (argv[0][0] == '-' && argv[0][1] &&
981
(argv[0][1] < '0' || argv[0][1] > '9')) {
982
ignore_errors = 1;
983
++argv[0];
984
}
985
986
list_for_each_entry(tp, &kdb_cmds_head, list_node) {
987
/*
988
* If this command is allowed to be abbreviated,
989
* check to see if this is it.
990
*/
991
if (tp->minlen && (strlen(argv[0]) <= tp->minlen) &&
992
(strncmp(argv[0], tp->name, tp->minlen) == 0))
993
break;
994
995
if (strcmp(argv[0], tp->name) == 0)
996
break;
997
}
998
999
/*
1000
* If we don't find a command by this name, see if the first
1001
* few characters of this match any of the known commands.
1002
* e.g., md1c20 should match md.
1003
*/
1004
if (list_entry_is_head(tp, &kdb_cmds_head, list_node)) {
1005
list_for_each_entry(tp, &kdb_cmds_head, list_node) {
1006
if (strncmp(argv[0], tp->name, strlen(tp->name)) == 0)
1007
break;
1008
}
1009
}
1010
1011
if (!list_entry_is_head(tp, &kdb_cmds_head, list_node)) {
1012
int result;
1013
1014
if (!kdb_check_flags(tp->flags, kdb_cmd_enabled, argc <= 1))
1015
return KDB_NOPERM;
1016
1017
KDB_STATE_SET(CMD);
1018
result = (*tp->func)(argc-1, (const char **)argv);
1019
if (result && ignore_errors && result > KDB_CMD_GO)
1020
result = 0;
1021
KDB_STATE_CLEAR(CMD);
1022
1023
if (tp->flags & KDB_REPEAT_WITH_ARGS)
1024
return result;
1025
1026
argc = tp->flags & KDB_REPEAT_NO_ARGS ? 1 : 0;
1027
if (argv[argc])
1028
*(argv[argc]) = '\0';
1029
return result;
1030
}
1031
1032
/*
1033
* If the input with which we were presented does not
1034
* map to an existing command, attempt to parse it as an
1035
* address argument and display the result. Useful for
1036
* obtaining the address of a variable, or the nearest symbol
1037
* to an address contained in a register.
1038
*/
1039
{
1040
unsigned long value;
1041
char *name = NULL;
1042
long offset;
1043
int nextarg = 0;
1044
1045
if (kdbgetaddrarg(0, (const char **)argv, &nextarg,
1046
&value, &offset, &name)) {
1047
return KDB_NOTFOUND;
1048
}
1049
1050
kdb_printf("%s = ", argv[0]);
1051
kdb_symbol_print(value, NULL, KDB_SP_DEFAULT);
1052
kdb_printf("\n");
1053
return 0;
1054
}
1055
}
1056
1057
1058
static int handle_ctrl_cmd(char *cmd)
1059
{
1060
#define CTRL_P 16
1061
#define CTRL_N 14
1062
1063
/* initial situation */
1064
if (cmd_head == cmd_tail)
1065
return 0;
1066
switch (*cmd) {
1067
case CTRL_P:
1068
if (cmdptr != cmd_tail)
1069
cmdptr = (cmdptr + KDB_CMD_HISTORY_COUNT - 1) %
1070
KDB_CMD_HISTORY_COUNT;
1071
strscpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
1072
return 1;
1073
case CTRL_N:
1074
if (cmdptr != cmd_head)
1075
cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT;
1076
strscpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
1077
return 1;
1078
}
1079
return 0;
1080
}
1081
1082
/*
1083
* kdb_reboot - This function implements the 'reboot' command. Reboot
1084
* the system immediately, or loop for ever on failure.
1085
*/
1086
static int kdb_reboot(int argc, const char **argv)
1087
{
1088
emergency_restart();
1089
kdb_printf("Hmm, kdb_reboot did not reboot, spinning here\n");
1090
while (1)
1091
cpu_relax();
1092
/* NOTREACHED */
1093
return 0;
1094
}
1095
1096
static void kdb_dumpregs(struct pt_regs *regs)
1097
{
1098
int old_lvl = console_loglevel;
1099
console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
1100
kdb_trap_printk++;
1101
show_regs(regs);
1102
kdb_trap_printk--;
1103
kdb_printf("\n");
1104
console_loglevel = old_lvl;
1105
}
1106
1107
static void kdb_set_current_task(struct task_struct *p)
1108
{
1109
kdb_current_task = p;
1110
1111
if (kdb_task_has_cpu(p)) {
1112
kdb_current_regs = KDB_TSKREGS(kdb_process_cpu(p));
1113
return;
1114
}
1115
kdb_current_regs = NULL;
1116
}
1117
1118
static void drop_newline(char *buf)
1119
{
1120
size_t len = strlen(buf);
1121
1122
if (len == 0)
1123
return;
1124
if (*(buf + len - 1) == '\n')
1125
*(buf + len - 1) = '\0';
1126
}
1127
1128
/*
1129
* kdb_local - The main code for kdb. This routine is invoked on a
1130
* specific processor, it is not global. The main kdb() routine
1131
* ensures that only one processor at a time is in this routine.
1132
* This code is called with the real reason code on the first
1133
* entry to a kdb session, thereafter it is called with reason
1134
* SWITCH, even if the user goes back to the original cpu.
1135
* Inputs:
1136
* reason The reason KDB was invoked
1137
* error The hardware-defined error code
1138
* regs The exception frame at time of fault/breakpoint.
1139
* db_result Result code from the break or debug point.
1140
* Returns:
1141
* 0 KDB was invoked for an event which it wasn't responsible
1142
* 1 KDB handled the event for which it was invoked.
1143
* KDB_CMD_GO User typed 'go'.
1144
* KDB_CMD_CPU User switched to another cpu.
1145
* KDB_CMD_SS Single step.
1146
*/
1147
static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
1148
kdb_dbtrap_t db_result)
1149
{
1150
char *cmdbuf;
1151
int diag;
1152
struct task_struct *kdb_current =
1153
curr_task(raw_smp_processor_id());
1154
1155
KDB_DEBUG_STATE("kdb_local 1", reason);
1156
1157
kdb_check_for_lockdown();
1158
1159
kdb_go_count = 0;
1160
if (reason == KDB_REASON_DEBUG) {
1161
/* special case below */
1162
} else {
1163
kdb_printf("\nEntering kdb (current=0x%px, pid %d) ",
1164
kdb_current, kdb_current ? kdb_current->pid : 0);
1165
#if defined(CONFIG_SMP)
1166
kdb_printf("on processor %d ", raw_smp_processor_id());
1167
#endif
1168
}
1169
1170
switch (reason) {
1171
case KDB_REASON_DEBUG:
1172
{
1173
/*
1174
* If re-entering kdb after a single step
1175
* command, don't print the message.
1176
*/
1177
switch (db_result) {
1178
case KDB_DB_BPT:
1179
kdb_printf("\nEntering kdb (0x%px, pid %d) ",
1180
kdb_current, kdb_current->pid);
1181
#if defined(CONFIG_SMP)
1182
kdb_printf("on processor %d ", raw_smp_processor_id());
1183
#endif
1184
kdb_printf("due to Debug @ " kdb_machreg_fmt "\n",
1185
instruction_pointer(regs));
1186
break;
1187
case KDB_DB_SS:
1188
break;
1189
case KDB_DB_SSBPT:
1190
KDB_DEBUG_STATE("kdb_local 4", reason);
1191
return 1; /* kdba_db_trap did the work */
1192
default:
1193
kdb_printf("kdb: Bad result from kdba_db_trap: %d\n",
1194
db_result);
1195
break;
1196
}
1197
1198
}
1199
break;
1200
case KDB_REASON_ENTER:
1201
if (KDB_STATE(KEYBOARD))
1202
kdb_printf("due to Keyboard Entry\n");
1203
else
1204
kdb_printf("due to KDB_ENTER()\n");
1205
break;
1206
case KDB_REASON_KEYBOARD:
1207
KDB_STATE_SET(KEYBOARD);
1208
kdb_printf("due to Keyboard Entry\n");
1209
break;
1210
case KDB_REASON_ENTER_SLAVE:
1211
/* drop through, slaves only get released via cpu switch */
1212
case KDB_REASON_SWITCH:
1213
kdb_printf("due to cpu switch\n");
1214
break;
1215
case KDB_REASON_OOPS:
1216
kdb_printf("Oops: %s\n", kdb_diemsg);
1217
kdb_printf("due to oops @ " kdb_machreg_fmt "\n",
1218
instruction_pointer(regs));
1219
kdb_dumpregs(regs);
1220
break;
1221
case KDB_REASON_SYSTEM_NMI:
1222
kdb_printf("due to System NonMaskable Interrupt\n");
1223
break;
1224
case KDB_REASON_NMI:
1225
kdb_printf("due to NonMaskable Interrupt @ "
1226
kdb_machreg_fmt "\n",
1227
instruction_pointer(regs));
1228
break;
1229
case KDB_REASON_SSTEP:
1230
case KDB_REASON_BREAK:
1231
kdb_printf("due to %s @ " kdb_machreg_fmt "\n",
1232
reason == KDB_REASON_BREAK ?
1233
"Breakpoint" : "SS trap", instruction_pointer(regs));
1234
/*
1235
* Determine if this breakpoint is one that we
1236
* are interested in.
1237
*/
1238
if (db_result != KDB_DB_BPT) {
1239
kdb_printf("kdb: error return from kdba_bp_trap: %d\n",
1240
db_result);
1241
KDB_DEBUG_STATE("kdb_local 6", reason);
1242
return 0; /* Not for us, dismiss it */
1243
}
1244
break;
1245
case KDB_REASON_RECURSE:
1246
kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n",
1247
instruction_pointer(regs));
1248
break;
1249
default:
1250
kdb_printf("kdb: unexpected reason code: %d\n", reason);
1251
KDB_DEBUG_STATE("kdb_local 8", reason);
1252
return 0; /* Not for us, dismiss it */
1253
}
1254
1255
while (1) {
1256
/*
1257
* Initialize pager context.
1258
*/
1259
kdb_nextline = 1;
1260
KDB_STATE_CLEAR(SUPPRESS);
1261
kdb_grepping_flag = 0;
1262
/* ensure the old search does not leak into '/' commands */
1263
kdb_grep_string[0] = '\0';
1264
1265
cmdbuf = cmd_cur;
1266
*cmdbuf = '\0';
1267
*(cmd_hist[cmd_head]) = '\0';
1268
1269
do_full_getstr:
1270
/* PROMPT can only be set if we have MEM_READ permission. */
1271
snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
1272
raw_smp_processor_id());
1273
1274
/*
1275
* Fetch command from keyboard
1276
*/
1277
cmdbuf = kdb_getstr(cmdbuf, CMD_BUFLEN, kdb_prompt_str);
1278
if (*cmdbuf != '\n') {
1279
if (*cmdbuf < 32) {
1280
if (cmdptr == cmd_head) {
1281
strscpy(cmd_hist[cmd_head], cmd_cur,
1282
CMD_BUFLEN);
1283
*(cmd_hist[cmd_head] +
1284
strlen(cmd_hist[cmd_head])-1) = '\0';
1285
}
1286
if (!handle_ctrl_cmd(cmdbuf))
1287
*(cmd_cur+strlen(cmd_cur)-1) = '\0';
1288
cmdbuf = cmd_cur;
1289
goto do_full_getstr;
1290
} else {
1291
strscpy(cmd_hist[cmd_head], cmd_cur,
1292
CMD_BUFLEN);
1293
}
1294
1295
cmd_head = (cmd_head+1) % KDB_CMD_HISTORY_COUNT;
1296
if (cmd_head == cmd_tail)
1297
cmd_tail = (cmd_tail+1) % KDB_CMD_HISTORY_COUNT;
1298
}
1299
1300
cmdptr = cmd_head;
1301
diag = kdb_parse(cmdbuf);
1302
if (diag == KDB_NOTFOUND) {
1303
drop_newline(cmdbuf);
1304
kdb_printf("Unknown kdb command: '%s'\n", cmdbuf);
1305
diag = 0;
1306
}
1307
if (diag == KDB_CMD_GO
1308
|| diag == KDB_CMD_CPU
1309
|| diag == KDB_CMD_SS
1310
|| diag == KDB_CMD_KGDB)
1311
break;
1312
1313
if (diag)
1314
kdb_cmderror(diag);
1315
}
1316
KDB_DEBUG_STATE("kdb_local 9", diag);
1317
return diag;
1318
}
1319
1320
1321
/*
1322
* kdb_print_state - Print the state data for the current processor
1323
* for debugging.
1324
* Inputs:
1325
* text Identifies the debug point
1326
* value Any integer value to be printed, e.g. reason code.
1327
*/
1328
void kdb_print_state(const char *text, int value)
1329
{
1330
kdb_printf("state: %s cpu %d value %d initial %d state %x\n",
1331
text, raw_smp_processor_id(), value, kdb_initial_cpu,
1332
kdb_state);
1333
}
1334
1335
/*
1336
* kdb_main_loop - After initial setup and assignment of the
1337
* controlling cpu, all cpus are in this loop. One cpu is in
1338
* control and will issue the kdb prompt, the others will spin
1339
* until 'go' or cpu switch.
1340
*
1341
* To get a consistent view of the kernel stacks for all
1342
* processes, this routine is invoked from the main kdb code via
1343
* an architecture specific routine. kdba_main_loop is
1344
* responsible for making the kernel stacks consistent for all
1345
* processes, there should be no difference between a blocked
1346
* process and a running process as far as kdb is concerned.
1347
* Inputs:
1348
* reason The reason KDB was invoked
1349
* error The hardware-defined error code
1350
* reason2 kdb's current reason code.
1351
* Initially error but can change
1352
* according to kdb state.
1353
* db_result Result code from break or debug point.
1354
* regs The exception frame at time of fault/breakpoint.
1355
* should always be valid.
1356
* Returns:
1357
* 0 KDB was invoked for an event which it wasn't responsible
1358
* 1 KDB handled the event for which it was invoked.
1359
*/
1360
int kdb_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
1361
kdb_dbtrap_t db_result, struct pt_regs *regs)
1362
{
1363
int result = 1;
1364
/* Stay in kdb() until 'go', 'ss[b]' or an error */
1365
while (1) {
1366
/*
1367
* All processors except the one that is in control
1368
* will spin here.
1369
*/
1370
KDB_DEBUG_STATE("kdb_main_loop 1", reason);
1371
while (KDB_STATE(HOLD_CPU)) {
1372
/* state KDB is turned off by kdb_cpu to see if the
1373
* other cpus are still live, each cpu in this loop
1374
* turns it back on.
1375
*/
1376
if (!KDB_STATE(KDB))
1377
KDB_STATE_SET(KDB);
1378
}
1379
1380
KDB_STATE_CLEAR(SUPPRESS);
1381
KDB_DEBUG_STATE("kdb_main_loop 2", reason);
1382
if (KDB_STATE(LEAVING))
1383
break; /* Another cpu said 'go' */
1384
/* Still using kdb, this processor is in control */
1385
result = kdb_local(reason2, error, regs, db_result);
1386
KDB_DEBUG_STATE("kdb_main_loop 3", result);
1387
1388
if (result == KDB_CMD_CPU)
1389
break;
1390
1391
if (result == KDB_CMD_SS) {
1392
KDB_STATE_SET(DOING_SS);
1393
break;
1394
}
1395
1396
if (result == KDB_CMD_KGDB) {
1397
if (!KDB_STATE(DOING_KGDB))
1398
kdb_printf("Entering please attach debugger "
1399
"or use $D#44+ or $3#33\n");
1400
break;
1401
}
1402
if (result && result != 1 && result != KDB_CMD_GO)
1403
kdb_printf("\nUnexpected kdb_local return code %d\n",
1404
result);
1405
KDB_DEBUG_STATE("kdb_main_loop 4", reason);
1406
break;
1407
}
1408
if (KDB_STATE(DOING_SS))
1409
KDB_STATE_CLEAR(SSBPT);
1410
1411
/* Clean up any keyboard devices before leaving */
1412
kdb_kbd_cleanup_state();
1413
1414
return result;
1415
}
1416
1417
/*
1418
* kdb_mdr - This function implements the guts of the 'mdr', memory
1419
* read command.
1420
* mdr <addr arg>,<byte count>
1421
* Inputs:
1422
* addr Start address
1423
* count Number of bytes
1424
* Returns:
1425
* Always 0. Any errors are detected and printed by kdb_getarea.
1426
*/
1427
static int kdb_mdr(unsigned long addr, unsigned int count)
1428
{
1429
unsigned char c;
1430
while (count--) {
1431
if (kdb_getarea(c, addr))
1432
return 0;
1433
kdb_printf("%02x", c);
1434
addr++;
1435
}
1436
kdb_printf("\n");
1437
return 0;
1438
}
1439
1440
/*
1441
* kdb_md - This function implements the 'md', 'md1', 'md2', 'md4',
1442
* 'md8' 'mdr' and 'mds' commands.
1443
*
1444
* md|mds [<addr arg> [<line count> [<radix>]]]
1445
* mdWcN [<addr arg> [<line count> [<radix>]]]
1446
* where W = is the width (1, 2, 4 or 8) and N is the count.
1447
* for eg., md1c20 reads 20 bytes, 1 at a time.
1448
* mdr <addr arg>,<byte count>
1449
*/
1450
static void kdb_md_line(const char *fmtstr, unsigned long addr,
1451
int symbolic, int nosect, int bytesperword,
1452
int num, int repeat, int phys)
1453
{
1454
/* print just one line of data */
1455
kdb_symtab_t symtab;
1456
char cbuf[32];
1457
char *c = cbuf;
1458
int i;
1459
int j;
1460
unsigned long word;
1461
1462
memset(cbuf, '\0', sizeof(cbuf));
1463
if (phys)
1464
kdb_printf("phys " kdb_machreg_fmt0 " ", addr);
1465
else
1466
kdb_printf(kdb_machreg_fmt0 " ", addr);
1467
1468
for (i = 0; i < num && repeat--; i++) {
1469
if (phys) {
1470
if (kdb_getphysword(&word, addr, bytesperword))
1471
break;
1472
} else if (kdb_getword(&word, addr, bytesperword))
1473
break;
1474
kdb_printf(fmtstr, word);
1475
if (symbolic)
1476
kdbnearsym(word, &symtab);
1477
else
1478
memset(&symtab, 0, sizeof(symtab));
1479
if (symtab.sym_name) {
1480
kdb_symbol_print(word, &symtab, 0);
1481
if (!nosect) {
1482
kdb_printf("\n");
1483
kdb_printf(" %s %s "
1484
kdb_machreg_fmt " "
1485
kdb_machreg_fmt " "
1486
kdb_machreg_fmt, symtab.mod_name,
1487
symtab.sec_name, symtab.sec_start,
1488
symtab.sym_start, symtab.sym_end);
1489
}
1490
addr += bytesperword;
1491
} else {
1492
union {
1493
u64 word;
1494
unsigned char c[8];
1495
} wc;
1496
unsigned char *cp;
1497
#ifdef __BIG_ENDIAN
1498
cp = wc.c + 8 - bytesperword;
1499
#else
1500
cp = wc.c;
1501
#endif
1502
wc.word = word;
1503
#define printable_char(c) \
1504
({unsigned char __c = c; isascii(__c) && isprint(__c) ? __c : '.'; })
1505
for (j = 0; j < bytesperword; j++)
1506
*c++ = printable_char(*cp++);
1507
addr += bytesperword;
1508
#undef printable_char
1509
}
1510
}
1511
kdb_printf("%*s %s\n", (int)((num-i)*(2*bytesperword + 1)+1),
1512
" ", cbuf);
1513
}
1514
1515
static int kdb_md(int argc, const char **argv)
1516
{
1517
static unsigned long last_addr;
1518
static int last_radix, last_bytesperword, last_repeat;
1519
int radix = 16, mdcount = 8, bytesperword = KDB_WORD_SIZE, repeat;
1520
int nosect = 0;
1521
char fmtchar, fmtstr[64];
1522
unsigned long addr;
1523
unsigned long word;
1524
long offset = 0;
1525
int symbolic = 0;
1526
int valid = 0;
1527
int phys = 0;
1528
int raw = 0;
1529
1530
kdbgetintenv("MDCOUNT", &mdcount);
1531
kdbgetintenv("RADIX", &radix);
1532
kdbgetintenv("BYTESPERWORD", &bytesperword);
1533
1534
/* Assume 'md <addr>' and start with environment values */
1535
repeat = mdcount * 16 / bytesperword;
1536
1537
if (strcmp(argv[0], "mdr") == 0) {
1538
if (argc == 2 || (argc == 0 && last_addr != 0))
1539
valid = raw = 1;
1540
else
1541
return KDB_ARGCOUNT;
1542
} else if (isdigit(argv[0][2])) {
1543
bytesperword = (int)(argv[0][2] - '0');
1544
if (bytesperword == 0) {
1545
bytesperword = last_bytesperword;
1546
if (bytesperword == 0)
1547
bytesperword = 4;
1548
}
1549
last_bytesperword = bytesperword;
1550
repeat = mdcount * 16 / bytesperword;
1551
if (!argv[0][3])
1552
valid = 1;
1553
else if (argv[0][3] == 'c' && argv[0][4]) {
1554
if (kstrtouint(argv[0] + 4, 10, &repeat))
1555
return KDB_BADINT;
1556
mdcount = ((repeat * bytesperword) + 15) / 16;
1557
valid = 1;
1558
}
1559
last_repeat = repeat;
1560
} else if (strcmp(argv[0], "md") == 0)
1561
valid = 1;
1562
else if (strcmp(argv[0], "mds") == 0)
1563
valid = 1;
1564
else if (strcmp(argv[0], "mdp") == 0) {
1565
phys = valid = 1;
1566
}
1567
if (!valid)
1568
return KDB_NOTFOUND;
1569
1570
if (argc == 0) {
1571
if (last_addr == 0)
1572
return KDB_ARGCOUNT;
1573
addr = last_addr;
1574
radix = last_radix;
1575
bytesperword = last_bytesperword;
1576
repeat = last_repeat;
1577
if (raw)
1578
mdcount = repeat;
1579
else
1580
mdcount = ((repeat * bytesperword) + 15) / 16;
1581
}
1582
1583
if (argc) {
1584
unsigned long val;
1585
int diag, nextarg = 1;
1586
diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
1587
&offset, NULL);
1588
if (diag)
1589
return diag;
1590
if (argc > nextarg+2)
1591
return KDB_ARGCOUNT;
1592
1593
if (argc >= nextarg) {
1594
diag = kdbgetularg(argv[nextarg], &val);
1595
if (!diag) {
1596
mdcount = (int) val;
1597
if (raw)
1598
repeat = mdcount;
1599
else
1600
repeat = mdcount * 16 / bytesperword;
1601
}
1602
}
1603
if (argc >= nextarg+1) {
1604
diag = kdbgetularg(argv[nextarg+1], &val);
1605
if (!diag)
1606
radix = (int) val;
1607
}
1608
}
1609
1610
if (strcmp(argv[0], "mdr") == 0) {
1611
int ret;
1612
last_addr = addr;
1613
ret = kdb_mdr(addr, mdcount);
1614
last_addr += mdcount;
1615
last_repeat = mdcount;
1616
last_bytesperword = bytesperword; // to make REPEAT happy
1617
return ret;
1618
}
1619
1620
switch (radix) {
1621
case 10:
1622
fmtchar = 'd';
1623
break;
1624
case 16:
1625
fmtchar = 'x';
1626
break;
1627
case 8:
1628
fmtchar = 'o';
1629
break;
1630
default:
1631
return KDB_BADRADIX;
1632
}
1633
1634
last_radix = radix;
1635
1636
if (bytesperword > KDB_WORD_SIZE)
1637
return KDB_BADWIDTH;
1638
1639
switch (bytesperword) {
1640
case 8:
1641
sprintf(fmtstr, "%%16.16l%c ", fmtchar);
1642
break;
1643
case 4:
1644
sprintf(fmtstr, "%%8.8l%c ", fmtchar);
1645
break;
1646
case 2:
1647
sprintf(fmtstr, "%%4.4l%c ", fmtchar);
1648
break;
1649
case 1:
1650
sprintf(fmtstr, "%%2.2l%c ", fmtchar);
1651
break;
1652
default:
1653
return KDB_BADWIDTH;
1654
}
1655
1656
last_repeat = repeat;
1657
last_bytesperword = bytesperword;
1658
1659
if (strcmp(argv[0], "mds") == 0) {
1660
symbolic = 1;
1661
/* Do not save these changes as last_*, they are temporary mds
1662
* overrides.
1663
*/
1664
bytesperword = KDB_WORD_SIZE;
1665
repeat = mdcount;
1666
kdbgetintenv("NOSECT", &nosect);
1667
}
1668
1669
/* Round address down modulo BYTESPERWORD */
1670
1671
addr &= ~(bytesperword-1);
1672
1673
while (repeat > 0) {
1674
unsigned long a;
1675
int n, z, num = (symbolic ? 1 : (16 / bytesperword));
1676
1677
if (KDB_FLAG(CMD_INTERRUPT))
1678
return 0;
1679
for (a = addr, z = 0; z < repeat; a += bytesperword, ++z) {
1680
if (phys) {
1681
if (kdb_getphysword(&word, a, bytesperword)
1682
|| word)
1683
break;
1684
} else if (kdb_getword(&word, a, bytesperword) || word)
1685
break;
1686
}
1687
n = min(num, repeat);
1688
kdb_md_line(fmtstr, addr, symbolic, nosect, bytesperword,
1689
num, repeat, phys);
1690
addr += bytesperword * n;
1691
repeat -= n;
1692
z = (z + num - 1) / num;
1693
if (z > 2) {
1694
int s = num * (z-2);
1695
kdb_printf(kdb_machreg_fmt0 "-" kdb_machreg_fmt0
1696
" zero suppressed\n",
1697
addr, addr + bytesperword * s - 1);
1698
addr += bytesperword * s;
1699
repeat -= s;
1700
}
1701
}
1702
last_addr = addr;
1703
1704
return 0;
1705
}
1706
1707
/*
1708
* kdb_mm - This function implements the 'mm' command.
1709
* mm address-expression new-value
1710
* Remarks:
1711
* mm works on machine words, mmW works on bytes.
1712
*/
1713
static int kdb_mm(int argc, const char **argv)
1714
{
1715
int diag;
1716
unsigned long addr;
1717
long offset = 0;
1718
unsigned long contents;
1719
int nextarg;
1720
int width;
1721
1722
if (argv[0][2] && !isdigit(argv[0][2]))
1723
return KDB_NOTFOUND;
1724
1725
if (argc < 2)
1726
return KDB_ARGCOUNT;
1727
1728
nextarg = 1;
1729
diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
1730
if (diag)
1731
return diag;
1732
1733
if (nextarg > argc)
1734
return KDB_ARGCOUNT;
1735
diag = kdbgetaddrarg(argc, argv, &nextarg, &contents, NULL, NULL);
1736
if (diag)
1737
return diag;
1738
1739
if (nextarg != argc + 1)
1740
return KDB_ARGCOUNT;
1741
1742
width = argv[0][2] ? (argv[0][2] - '0') : (KDB_WORD_SIZE);
1743
diag = kdb_putword(addr, contents, width);
1744
if (diag)
1745
return diag;
1746
1747
kdb_printf(kdb_machreg_fmt " = " kdb_machreg_fmt "\n", addr, contents);
1748
1749
return 0;
1750
}
1751
1752
/*
1753
* kdb_go - This function implements the 'go' command.
1754
* go [address-expression]
1755
*/
1756
static int kdb_go(int argc, const char **argv)
1757
{
1758
unsigned long addr;
1759
int diag;
1760
int nextarg;
1761
long offset;
1762
1763
if (raw_smp_processor_id() != kdb_initial_cpu) {
1764
kdb_printf("go must execute on the entry cpu, "
1765
"please use \"cpu %d\" and then execute go\n",
1766
kdb_initial_cpu);
1767
return KDB_BADCPUNUM;
1768
}
1769
if (argc == 1) {
1770
nextarg = 1;
1771
diag = kdbgetaddrarg(argc, argv, &nextarg,
1772
&addr, &offset, NULL);
1773
if (diag)
1774
return diag;
1775
} else if (argc) {
1776
return KDB_ARGCOUNT;
1777
}
1778
1779
diag = KDB_CMD_GO;
1780
if (KDB_FLAG(CATASTROPHIC)) {
1781
kdb_printf("Catastrophic error detected\n");
1782
kdb_printf("kdb_continue_catastrophic=%d, ",
1783
kdb_continue_catastrophic);
1784
if (kdb_continue_catastrophic == 0 && kdb_go_count++ == 0) {
1785
kdb_printf("type go a second time if you really want "
1786
"to continue\n");
1787
return 0;
1788
}
1789
if (kdb_continue_catastrophic == 2) {
1790
kdb_printf("forcing reboot\n");
1791
kdb_reboot(0, NULL);
1792
}
1793
kdb_printf("attempting to continue\n");
1794
}
1795
return diag;
1796
}
1797
1798
/*
1799
* kdb_rd - This function implements the 'rd' command.
1800
*/
1801
static int kdb_rd(int argc, const char **argv)
1802
{
1803
int len = kdb_check_regs();
1804
#if DBG_MAX_REG_NUM > 0
1805
int i;
1806
char *rname;
1807
int rsize;
1808
u64 reg64;
1809
u32 reg32;
1810
u16 reg16;
1811
u8 reg8;
1812
1813
if (len)
1814
return len;
1815
1816
for (i = 0; i < DBG_MAX_REG_NUM; i++) {
1817
rsize = dbg_reg_def[i].size * 2;
1818
if (rsize > 16)
1819
rsize = 2;
1820
if (len + strlen(dbg_reg_def[i].name) + 4 + rsize > 80) {
1821
len = 0;
1822
kdb_printf("\n");
1823
}
1824
if (len)
1825
len += kdb_printf(" ");
1826
switch(dbg_reg_def[i].size * 8) {
1827
case 8:
1828
rname = dbg_get_reg(i, &reg8, kdb_current_regs);
1829
if (!rname)
1830
break;
1831
len += kdb_printf("%s: %02x", rname, reg8);
1832
break;
1833
case 16:
1834
rname = dbg_get_reg(i, &reg16, kdb_current_regs);
1835
if (!rname)
1836
break;
1837
len += kdb_printf("%s: %04x", rname, reg16);
1838
break;
1839
case 32:
1840
rname = dbg_get_reg(i, &reg32, kdb_current_regs);
1841
if (!rname)
1842
break;
1843
len += kdb_printf("%s: %08x", rname, reg32);
1844
break;
1845
case 64:
1846
rname = dbg_get_reg(i, &reg64, kdb_current_regs);
1847
if (!rname)
1848
break;
1849
len += kdb_printf("%s: %016llx", rname, reg64);
1850
break;
1851
default:
1852
len += kdb_printf("%s: ??", dbg_reg_def[i].name);
1853
}
1854
}
1855
kdb_printf("\n");
1856
#else
1857
if (len)
1858
return len;
1859
1860
kdb_dumpregs(kdb_current_regs);
1861
#endif
1862
return 0;
1863
}
1864
1865
/*
1866
* kdb_rm - This function implements the 'rm' (register modify) command.
1867
* rm register-name new-contents
1868
* Remarks:
1869
* Allows register modification with the same restrictions as gdb
1870
*/
1871
static int kdb_rm(int argc, const char **argv)
1872
{
1873
#if DBG_MAX_REG_NUM > 0
1874
int diag;
1875
const char *rname;
1876
int i;
1877
u64 reg64;
1878
u32 reg32;
1879
u16 reg16;
1880
u8 reg8;
1881
1882
if (argc != 2)
1883
return KDB_ARGCOUNT;
1884
/*
1885
* Allow presence or absence of leading '%' symbol.
1886
*/
1887
rname = argv[1];
1888
if (*rname == '%')
1889
rname++;
1890
1891
diag = kdbgetu64arg(argv[2], &reg64);
1892
if (diag)
1893
return diag;
1894
1895
diag = kdb_check_regs();
1896
if (diag)
1897
return diag;
1898
1899
diag = KDB_BADREG;
1900
for (i = 0; i < DBG_MAX_REG_NUM; i++) {
1901
if (strcmp(rname, dbg_reg_def[i].name) == 0) {
1902
diag = 0;
1903
break;
1904
}
1905
}
1906
if (!diag) {
1907
switch(dbg_reg_def[i].size * 8) {
1908
case 8:
1909
reg8 = reg64;
1910
dbg_set_reg(i, &reg8, kdb_current_regs);
1911
break;
1912
case 16:
1913
reg16 = reg64;
1914
dbg_set_reg(i, &reg16, kdb_current_regs);
1915
break;
1916
case 32:
1917
reg32 = reg64;
1918
dbg_set_reg(i, &reg32, kdb_current_regs);
1919
break;
1920
case 64:
1921
dbg_set_reg(i, &reg64, kdb_current_regs);
1922
break;
1923
}
1924
}
1925
return diag;
1926
#else
1927
kdb_printf("ERROR: Register set currently not implemented\n");
1928
return 0;
1929
#endif
1930
}
1931
1932
#if defined(CONFIG_MAGIC_SYSRQ)
1933
/*
1934
* kdb_sr - This function implements the 'sr' (SYSRQ key) command
1935
* which interfaces to the soi-disant MAGIC SYSRQ functionality.
1936
* sr <magic-sysrq-code>
1937
*/
1938
static int kdb_sr(int argc, const char **argv)
1939
{
1940
bool check_mask =
1941
!kdb_check_flags(KDB_ENABLE_ALL, kdb_cmd_enabled, false);
1942
1943
if (argc != 1)
1944
return KDB_ARGCOUNT;
1945
1946
kdb_trap_printk++;
1947
__handle_sysrq(*argv[1], check_mask);
1948
kdb_trap_printk--;
1949
1950
return 0;
1951
}
1952
#endif /* CONFIG_MAGIC_SYSRQ */
1953
1954
/*
1955
* kdb_ef - This function implements the 'regs' (display exception
1956
* frame) command. This command takes an address and expects to
1957
* find an exception frame at that address, formats and prints
1958
* it.
1959
* regs address-expression
1960
* Remarks:
1961
* Not done yet.
1962
*/
1963
static int kdb_ef(int argc, const char **argv)
1964
{
1965
int diag;
1966
unsigned long addr;
1967
long offset;
1968
int nextarg;
1969
1970
if (argc != 1)
1971
return KDB_ARGCOUNT;
1972
1973
nextarg = 1;
1974
diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
1975
if (diag)
1976
return diag;
1977
show_regs((struct pt_regs *)addr);
1978
return 0;
1979
}
1980
1981
/*
1982
* kdb_env - This function implements the 'env' command. Display the
1983
* current environment variables.
1984
*/
1985
1986
static int kdb_env(int argc, const char **argv)
1987
{
1988
kdb_printenv();
1989
1990
if (KDB_DEBUG(MASK))
1991
kdb_printf("KDBDEBUG=0x%x\n",
1992
(kdb_flags & KDB_DEBUG(MASK)) >> KDB_DEBUG_FLAG_SHIFT);
1993
1994
return 0;
1995
}
1996
1997
#ifdef CONFIG_PRINTK
1998
/*
1999
* kdb_dmesg - This function implements the 'dmesg' command to display
2000
* the contents of the syslog buffer.
2001
* dmesg [lines] [adjust]
2002
*/
2003
static int kdb_dmesg(int argc, const char **argv)
2004
{
2005
int diag;
2006
int logging;
2007
int lines = 0;
2008
int adjust = 0;
2009
int n = 0;
2010
int skip = 0;
2011
struct kmsg_dump_iter iter;
2012
size_t len;
2013
char buf[201];
2014
2015
if (argc > 2)
2016
return KDB_ARGCOUNT;
2017
if (argc) {
2018
if (kstrtoint(argv[1], 0, &lines))
2019
lines = 0;
2020
if (argc > 1 && (kstrtoint(argv[2], 0, &adjust) || adjust < 0))
2021
adjust = 0;
2022
}
2023
2024
/* disable LOGGING if set */
2025
diag = kdbgetintenv("LOGGING", &logging);
2026
if (!diag && logging) {
2027
const char *setargs[] = { "set", "LOGGING", "0" };
2028
kdb_set(2, setargs);
2029
}
2030
2031
kmsg_dump_rewind(&iter);
2032
while (kmsg_dump_get_line(&iter, 1, NULL, 0, NULL))
2033
n++;
2034
2035
if (lines < 0) {
2036
if (adjust >= n)
2037
kdb_printf("buffer only contains %d lines, nothing "
2038
"printed\n", n);
2039
else if (adjust - lines >= n)
2040
kdb_printf("buffer only contains %d lines, last %d "
2041
"lines printed\n", n, n - adjust);
2042
skip = adjust;
2043
lines = abs(lines);
2044
} else if (lines > 0) {
2045
skip = n - lines - adjust;
2046
lines = abs(lines);
2047
if (adjust >= n) {
2048
kdb_printf("buffer only contains %d lines, "
2049
"nothing printed\n", n);
2050
skip = n;
2051
} else if (skip < 0) {
2052
lines += skip;
2053
skip = 0;
2054
kdb_printf("buffer only contains %d lines, first "
2055
"%d lines printed\n", n, lines);
2056
}
2057
} else {
2058
lines = n;
2059
}
2060
2061
if (skip >= n || skip < 0)
2062
return 0;
2063
2064
kmsg_dump_rewind(&iter);
2065
while (kmsg_dump_get_line(&iter, 1, buf, sizeof(buf), &len)) {
2066
if (skip) {
2067
skip--;
2068
continue;
2069
}
2070
if (!lines--)
2071
break;
2072
if (KDB_FLAG(CMD_INTERRUPT))
2073
return 0;
2074
2075
kdb_printf("%.*s\n", (int)len - 1, buf);
2076
}
2077
2078
return 0;
2079
}
2080
#endif /* CONFIG_PRINTK */
2081
/*
2082
* kdb_cpu - This function implements the 'cpu' command.
2083
* cpu [<cpunum>]
2084
* Returns:
2085
* KDB_CMD_CPU for success, a kdb diagnostic if error
2086
*/
2087
static void kdb_cpu_status(void)
2088
{
2089
int i, start_cpu, first_print = 1;
2090
char state, prev_state = '?';
2091
2092
kdb_printf("Currently on cpu %d\n", raw_smp_processor_id());
2093
kdb_printf("Available cpus: ");
2094
for (start_cpu = -1, i = 0; i < NR_CPUS; i++) {
2095
if (!cpu_online(i)) {
2096
state = 'F'; /* cpu is offline */
2097
} else if (!kgdb_info[i].enter_kgdb) {
2098
state = 'D'; /* cpu is online but unresponsive */
2099
} else {
2100
state = ' '; /* cpu is responding to kdb */
2101
if (kdb_task_state_char(KDB_TSK(i)) == '-')
2102
state = '-'; /* idle task */
2103
}
2104
if (state != prev_state) {
2105
if (prev_state != '?') {
2106
if (!first_print)
2107
kdb_printf(", ");
2108
first_print = 0;
2109
kdb_printf("%d", start_cpu);
2110
if (start_cpu < i-1)
2111
kdb_printf("-%d", i-1);
2112
if (prev_state != ' ')
2113
kdb_printf("(%c)", prev_state);
2114
}
2115
prev_state = state;
2116
start_cpu = i;
2117
}
2118
}
2119
/* print the trailing cpus, ignoring them if they are all offline */
2120
if (prev_state != 'F') {
2121
if (!first_print)
2122
kdb_printf(", ");
2123
kdb_printf("%d", start_cpu);
2124
if (start_cpu < i-1)
2125
kdb_printf("-%d", i-1);
2126
if (prev_state != ' ')
2127
kdb_printf("(%c)", prev_state);
2128
}
2129
kdb_printf("\n");
2130
}
2131
2132
static int kdb_cpu(int argc, const char **argv)
2133
{
2134
unsigned long cpunum;
2135
int diag;
2136
2137
if (argc == 0) {
2138
kdb_cpu_status();
2139
return 0;
2140
}
2141
2142
if (argc != 1)
2143
return KDB_ARGCOUNT;
2144
2145
diag = kdbgetularg(argv[1], &cpunum);
2146
if (diag)
2147
return diag;
2148
2149
/*
2150
* Validate cpunum
2151
*/
2152
if ((cpunum >= CONFIG_NR_CPUS) || !kgdb_info[cpunum].enter_kgdb)
2153
return KDB_BADCPUNUM;
2154
2155
dbg_switch_cpu = cpunum;
2156
2157
/*
2158
* Switch to other cpu
2159
*/
2160
return KDB_CMD_CPU;
2161
}
2162
2163
/* The user may not realize that ps/bta with no parameters does not print idle
2164
* or sleeping system daemon processes, so tell them how many were suppressed.
2165
*/
2166
void kdb_ps_suppressed(void)
2167
{
2168
int idle = 0, daemon = 0;
2169
unsigned long cpu;
2170
const struct task_struct *p, *g;
2171
for_each_online_cpu(cpu) {
2172
p = curr_task(cpu);
2173
if (kdb_task_state(p, "-"))
2174
++idle;
2175
}
2176
for_each_process_thread(g, p) {
2177
if (kdb_task_state(p, "ims"))
2178
++daemon;
2179
}
2180
if (idle || daemon) {
2181
if (idle)
2182
kdb_printf("%d idle process%s (state -)%s\n",
2183
idle, idle == 1 ? "" : "es",
2184
daemon ? " and " : "");
2185
if (daemon)
2186
kdb_printf("%d sleeping system daemon (state [ims]) "
2187
"process%s", daemon,
2188
daemon == 1 ? "" : "es");
2189
kdb_printf(" suppressed,\nuse 'ps A' to see all.\n");
2190
}
2191
}
2192
2193
void kdb_ps1(const struct task_struct *p)
2194
{
2195
int cpu;
2196
unsigned long tmp;
2197
2198
if (!p ||
2199
copy_from_kernel_nofault(&tmp, (char *)p, sizeof(unsigned long)))
2200
return;
2201
2202
cpu = kdb_process_cpu(p);
2203
kdb_printf("0x%px %8d %8d %d %4d %c 0x%px %c%s\n",
2204
(void *)p, p->pid, p->parent->pid,
2205
kdb_task_has_cpu(p), kdb_process_cpu(p),
2206
kdb_task_state_char(p),
2207
(void *)(&p->thread),
2208
p == curr_task(raw_smp_processor_id()) ? '*' : ' ',
2209
p->comm);
2210
if (kdb_task_has_cpu(p)) {
2211
if (!KDB_TSK(cpu)) {
2212
kdb_printf(" Error: no saved data for this cpu\n");
2213
} else {
2214
if (KDB_TSK(cpu) != p)
2215
kdb_printf(" Error: does not match running "
2216
"process table (0x%px)\n", KDB_TSK(cpu));
2217
}
2218
}
2219
}
2220
2221
/*
2222
* kdb_ps - This function implements the 'ps' command which shows a
2223
* list of the active processes.
2224
*
2225
* ps [<state_chars>] Show processes, optionally selecting only those whose
2226
* state character is found in <state_chars>.
2227
*/
2228
static int kdb_ps(int argc, const char **argv)
2229
{
2230
struct task_struct *g, *p;
2231
const char *mask;
2232
unsigned long cpu;
2233
2234
if (argc == 0)
2235
kdb_ps_suppressed();
2236
kdb_printf("%-*s Pid Parent [*] cpu State %-*s Command\n",
2237
(int)(2*sizeof(void *))+2, "Task Addr",
2238
(int)(2*sizeof(void *))+2, "Thread");
2239
mask = argc ? argv[1] : kdbgetenv("PS");
2240
/* Run the active tasks first */
2241
for_each_online_cpu(cpu) {
2242
if (KDB_FLAG(CMD_INTERRUPT))
2243
return 0;
2244
p = curr_task(cpu);
2245
if (kdb_task_state(p, mask))
2246
kdb_ps1(p);
2247
}
2248
kdb_printf("\n");
2249
/* Now the real tasks */
2250
for_each_process_thread(g, p) {
2251
if (KDB_FLAG(CMD_INTERRUPT))
2252
return 0;
2253
if (kdb_task_state(p, mask))
2254
kdb_ps1(p);
2255
}
2256
2257
return 0;
2258
}
2259
2260
/*
2261
* kdb_pid - This function implements the 'pid' command which switches
2262
* the currently active process.
2263
* pid [<pid> | R]
2264
*/
2265
static int kdb_pid(int argc, const char **argv)
2266
{
2267
struct task_struct *p;
2268
unsigned long val;
2269
int diag;
2270
2271
if (argc > 1)
2272
return KDB_ARGCOUNT;
2273
2274
if (argc) {
2275
if (strcmp(argv[1], "R") == 0) {
2276
p = KDB_TSK(kdb_initial_cpu);
2277
} else {
2278
diag = kdbgetularg(argv[1], &val);
2279
if (diag)
2280
return KDB_BADINT;
2281
2282
p = find_task_by_pid_ns((pid_t)val, &init_pid_ns);
2283
if (!p) {
2284
kdb_printf("No task with pid=%d\n", (pid_t)val);
2285
return 0;
2286
}
2287
}
2288
kdb_set_current_task(p);
2289
}
2290
kdb_printf("KDB current process is %s(pid=%d)\n",
2291
kdb_current_task->comm,
2292
kdb_current_task->pid);
2293
2294
return 0;
2295
}
2296
2297
static int kdb_kgdb(int argc, const char **argv)
2298
{
2299
return KDB_CMD_KGDB;
2300
}
2301
2302
/*
2303
* kdb_help - This function implements the 'help' and '?' commands.
2304
*/
2305
static int kdb_help(int argc, const char **argv)
2306
{
2307
kdbtab_t *kt;
2308
2309
kdb_printf("%-15.15s %-20.20s %s\n", "Command", "Usage", "Description");
2310
kdb_printf("-----------------------------"
2311
"-----------------------------\n");
2312
list_for_each_entry(kt, &kdb_cmds_head, list_node) {
2313
char *space = "";
2314
if (KDB_FLAG(CMD_INTERRUPT))
2315
return 0;
2316
if (!kdb_check_flags(kt->flags, kdb_cmd_enabled, true))
2317
continue;
2318
if (strlen(kt->usage) > 20)
2319
space = "\n ";
2320
kdb_printf("%-15.15s %-20s%s%s\n", kt->name,
2321
kt->usage, space, kt->help);
2322
}
2323
return 0;
2324
}
2325
2326
/*
2327
* kdb_kill - This function implements the 'kill' commands.
2328
*/
2329
static int kdb_kill(int argc, const char **argv)
2330
{
2331
long sig, pid;
2332
struct task_struct *p;
2333
2334
if (argc != 2)
2335
return KDB_ARGCOUNT;
2336
2337
if (kstrtol(argv[1], 0, &sig))
2338
return KDB_BADINT;
2339
if ((sig >= 0) || !valid_signal(-sig)) {
2340
kdb_printf("Invalid signal parameter.<-signal>\n");
2341
return 0;
2342
}
2343
sig = -sig;
2344
2345
if (kstrtol(argv[2], 0, &pid))
2346
return KDB_BADINT;
2347
if (pid <= 0) {
2348
kdb_printf("Process ID must be large than 0.\n");
2349
return 0;
2350
}
2351
2352
/* Find the process. */
2353
p = find_task_by_pid_ns(pid, &init_pid_ns);
2354
if (!p) {
2355
kdb_printf("The specified process isn't found.\n");
2356
return 0;
2357
}
2358
p = p->group_leader;
2359
kdb_send_sig(p, sig);
2360
return 0;
2361
}
2362
2363
/*
2364
* Most of this code has been lifted from kernel/timer.c::sys_sysinfo().
2365
* I cannot call that code directly from kdb, it has an unconditional
2366
* cli()/sti() and calls routines that take locks which can stop the debugger.
2367
*/
2368
static void kdb_sysinfo(struct sysinfo *val)
2369
{
2370
u64 uptime = ktime_get_mono_fast_ns();
2371
2372
memset(val, 0, sizeof(*val));
2373
val->uptime = div_u64(uptime, NSEC_PER_SEC);
2374
val->loads[0] = avenrun[0];
2375
val->loads[1] = avenrun[1];
2376
val->loads[2] = avenrun[2];
2377
val->procs = nr_threads-1;
2378
si_meminfo(val);
2379
2380
return;
2381
}
2382
2383
/*
2384
* kdb_summary - This function implements the 'summary' command.
2385
*/
2386
static int kdb_summary(int argc, const char **argv)
2387
{
2388
time64_t now;
2389
struct sysinfo val;
2390
2391
if (argc)
2392
return KDB_ARGCOUNT;
2393
2394
kdb_printf("sysname %s\n", init_uts_ns.name.sysname);
2395
kdb_printf("release %s\n", init_uts_ns.name.release);
2396
kdb_printf("version %s\n", init_uts_ns.name.version);
2397
kdb_printf("machine %s\n", init_uts_ns.name.machine);
2398
kdb_printf("nodename %s\n", init_uts_ns.name.nodename);
2399
kdb_printf("domainname %s\n", init_uts_ns.name.domainname);
2400
2401
now = __ktime_get_real_seconds();
2402
kdb_printf("date %ptTs tz_minuteswest %d\n", &now, sys_tz.tz_minuteswest);
2403
kdb_sysinfo(&val);
2404
kdb_printf("uptime ");
2405
if (val.uptime > (24*60*60)) {
2406
int days = val.uptime / (24*60*60);
2407
val.uptime %= (24*60*60);
2408
kdb_printf("%d day%s ", days, str_plural(days));
2409
}
2410
kdb_printf("%02ld:%02ld\n", val.uptime/(60*60), (val.uptime/60)%60);
2411
2412
kdb_printf("load avg %ld.%02ld %ld.%02ld %ld.%02ld\n",
2413
LOAD_INT(val.loads[0]), LOAD_FRAC(val.loads[0]),
2414
LOAD_INT(val.loads[1]), LOAD_FRAC(val.loads[1]),
2415
LOAD_INT(val.loads[2]), LOAD_FRAC(val.loads[2]));
2416
2417
/* Display in kilobytes */
2418
#define K(x) ((x) << (PAGE_SHIFT - 10))
2419
kdb_printf("\nMemTotal: %8lu kB\nMemFree: %8lu kB\n"
2420
"Buffers: %8lu kB\n",
2421
K(val.totalram), K(val.freeram), K(val.bufferram));
2422
return 0;
2423
}
2424
2425
/*
2426
* kdb_per_cpu - This function implements the 'per_cpu' command.
2427
*/
2428
static int kdb_per_cpu(int argc, const char **argv)
2429
{
2430
char fmtstr[64];
2431
int cpu, diag, nextarg = 1;
2432
unsigned long addr, symaddr, val, bytesperword = 0, whichcpu = ~0UL;
2433
2434
if (argc < 1 || argc > 3)
2435
return KDB_ARGCOUNT;
2436
2437
diag = kdbgetaddrarg(argc, argv, &nextarg, &symaddr, NULL, NULL);
2438
if (diag)
2439
return diag;
2440
2441
if (argc >= 2) {
2442
diag = kdbgetularg(argv[2], &bytesperword);
2443
if (diag)
2444
return diag;
2445
}
2446
if (!bytesperword)
2447
bytesperword = KDB_WORD_SIZE;
2448
else if (bytesperword > KDB_WORD_SIZE)
2449
return KDB_BADWIDTH;
2450
sprintf(fmtstr, "%%0%dlx ", (int)(2*bytesperword));
2451
if (argc >= 3) {
2452
diag = kdbgetularg(argv[3], &whichcpu);
2453
if (diag)
2454
return diag;
2455
if (whichcpu >= nr_cpu_ids || !cpu_online(whichcpu)) {
2456
kdb_printf("cpu %ld is not online\n", whichcpu);
2457
return KDB_BADCPUNUM;
2458
}
2459
}
2460
2461
/* Most architectures use __per_cpu_offset[cpu], some use
2462
* __per_cpu_offset(cpu), smp has no __per_cpu_offset.
2463
*/
2464
#ifdef __per_cpu_offset
2465
#define KDB_PCU(cpu) __per_cpu_offset(cpu)
2466
#else
2467
#ifdef CONFIG_SMP
2468
#define KDB_PCU(cpu) __per_cpu_offset[cpu]
2469
#else
2470
#define KDB_PCU(cpu) 0
2471
#endif
2472
#endif
2473
for_each_online_cpu(cpu) {
2474
if (KDB_FLAG(CMD_INTERRUPT))
2475
return 0;
2476
2477
if (whichcpu != ~0UL && whichcpu != cpu)
2478
continue;
2479
addr = symaddr + KDB_PCU(cpu);
2480
diag = kdb_getword(&val, addr, bytesperword);
2481
if (diag) {
2482
kdb_printf("%5d " kdb_bfd_vma_fmt0 " - unable to "
2483
"read, diag=%d\n", cpu, addr, diag);
2484
continue;
2485
}
2486
kdb_printf("%5d ", cpu);
2487
kdb_md_line(fmtstr, addr,
2488
bytesperword == KDB_WORD_SIZE,
2489
1, bytesperword, 1, 1, 0);
2490
}
2491
#undef KDB_PCU
2492
return 0;
2493
}
2494
2495
/*
2496
* display help for the use of cmd | grep pattern
2497
*/
2498
static int kdb_grep_help(int argc, const char **argv)
2499
{
2500
kdb_printf("Usage of cmd args | grep pattern:\n");
2501
kdb_printf(" Any command's output may be filtered through an ");
2502
kdb_printf("emulated 'pipe'.\n");
2503
kdb_printf(" 'grep' is just a key word.\n");
2504
kdb_printf(" The pattern may include a very limited set of "
2505
"metacharacters:\n");
2506
kdb_printf(" pattern or ^pattern or pattern$ or ^pattern$\n");
2507
kdb_printf(" And if there are spaces in the pattern, you may "
2508
"quote it:\n");
2509
kdb_printf(" \"pat tern\" or \"^pat tern\" or \"pat tern$\""
2510
" or \"^pat tern$\"\n");
2511
return 0;
2512
}
2513
2514
/**
2515
* kdb_register() - This function is used to register a kernel debugger
2516
* command.
2517
* @cmd: pointer to kdb command
2518
*
2519
* Note that it's the job of the caller to keep the memory for the cmd
2520
* allocated until unregister is called.
2521
*/
2522
int kdb_register(kdbtab_t *cmd)
2523
{
2524
kdbtab_t *kp;
2525
2526
list_for_each_entry(kp, &kdb_cmds_head, list_node) {
2527
if (strcmp(kp->name, cmd->name) == 0) {
2528
kdb_printf("Duplicate kdb cmd: %s, func %p help %s\n",
2529
cmd->name, cmd->func, cmd->help);
2530
return 1;
2531
}
2532
}
2533
2534
list_add_tail(&cmd->list_node, &kdb_cmds_head);
2535
return 0;
2536
}
2537
EXPORT_SYMBOL_GPL(kdb_register);
2538
2539
/**
2540
* kdb_register_table() - This function is used to register a kdb command
2541
* table.
2542
* @kp: pointer to kdb command table
2543
* @len: length of kdb command table
2544
*/
2545
void kdb_register_table(kdbtab_t *kp, size_t len)
2546
{
2547
while (len--) {
2548
list_add_tail(&kp->list_node, &kdb_cmds_head);
2549
kp++;
2550
}
2551
}
2552
2553
/**
2554
* kdb_unregister() - This function is used to unregister a kernel debugger
2555
* command. It is generally called when a module which
2556
* implements kdb command is unloaded.
2557
* @cmd: pointer to kdb command
2558
*/
2559
void kdb_unregister(kdbtab_t *cmd)
2560
{
2561
list_del(&cmd->list_node);
2562
}
2563
EXPORT_SYMBOL_GPL(kdb_unregister);
2564
2565
static kdbtab_t maintab[] = {
2566
{ .name = "md",
2567
.func = kdb_md,
2568
.usage = "<vaddr>",
2569
.help = "Display Memory Contents, also mdWcN, e.g. md8c1",
2570
.minlen = 1,
2571
.flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS,
2572
},
2573
{ .name = "mdr",
2574
.func = kdb_md,
2575
.usage = "<vaddr> <bytes>",
2576
.help = "Display Raw Memory",
2577
.flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS,
2578
},
2579
{ .name = "mdp",
2580
.func = kdb_md,
2581
.usage = "<paddr> <bytes>",
2582
.help = "Display Physical Memory",
2583
.flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS,
2584
},
2585
{ .name = "mds",
2586
.func = kdb_md,
2587
.usage = "<vaddr>",
2588
.help = "Display Memory Symbolically",
2589
.flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS,
2590
},
2591
{ .name = "mm",
2592
.func = kdb_mm,
2593
.usage = "<vaddr> <contents>",
2594
.help = "Modify Memory Contents",
2595
.flags = KDB_ENABLE_MEM_WRITE | KDB_REPEAT_NO_ARGS,
2596
},
2597
{ .name = "go",
2598
.func = kdb_go,
2599
.usage = "[<vaddr>]",
2600
.help = "Continue Execution",
2601
.minlen = 1,
2602
.flags = KDB_ENABLE_REG_WRITE |
2603
KDB_ENABLE_ALWAYS_SAFE_NO_ARGS,
2604
},
2605
{ .name = "rd",
2606
.func = kdb_rd,
2607
.usage = "",
2608
.help = "Display Registers",
2609
.flags = KDB_ENABLE_REG_READ,
2610
},
2611
{ .name = "rm",
2612
.func = kdb_rm,
2613
.usage = "<reg> <contents>",
2614
.help = "Modify Registers",
2615
.flags = KDB_ENABLE_REG_WRITE,
2616
},
2617
{ .name = "ef",
2618
.func = kdb_ef,
2619
.usage = "<vaddr>",
2620
.help = "Display exception frame",
2621
.flags = KDB_ENABLE_MEM_READ,
2622
},
2623
{ .name = "bt",
2624
.func = kdb_bt,
2625
.usage = "[<vaddr>]",
2626
.help = "Stack traceback",
2627
.minlen = 1,
2628
.flags = KDB_ENABLE_MEM_READ | KDB_ENABLE_INSPECT_NO_ARGS,
2629
},
2630
{ .name = "btp",
2631
.func = kdb_bt,
2632
.usage = "<pid>",
2633
.help = "Display stack for process <pid>",
2634
.flags = KDB_ENABLE_INSPECT,
2635
},
2636
{ .name = "bta",
2637
.func = kdb_bt,
2638
.usage = "[<state_chars>|A]",
2639
.help = "Backtrace all processes whose state matches",
2640
.flags = KDB_ENABLE_INSPECT,
2641
},
2642
{ .name = "btc",
2643
.func = kdb_bt,
2644
.usage = "",
2645
.help = "Backtrace current process on each cpu",
2646
.flags = KDB_ENABLE_INSPECT,
2647
},
2648
{ .name = "btt",
2649
.func = kdb_bt,
2650
.usage = "<vaddr>",
2651
.help = "Backtrace process given its struct task address",
2652
.flags = KDB_ENABLE_MEM_READ | KDB_ENABLE_INSPECT_NO_ARGS,
2653
},
2654
{ .name = "env",
2655
.func = kdb_env,
2656
.usage = "",
2657
.help = "Show environment variables",
2658
.flags = KDB_ENABLE_ALWAYS_SAFE,
2659
},
2660
{ .name = "set",
2661
.func = kdb_set,
2662
.usage = "",
2663
.help = "Set environment variables",
2664
.flags = KDB_ENABLE_ALWAYS_SAFE,
2665
},
2666
{ .name = "help",
2667
.func = kdb_help,
2668
.usage = "",
2669
.help = "Display Help Message",
2670
.minlen = 1,
2671
.flags = KDB_ENABLE_ALWAYS_SAFE,
2672
},
2673
{ .name = "?",
2674
.func = kdb_help,
2675
.usage = "",
2676
.help = "Display Help Message",
2677
.flags = KDB_ENABLE_ALWAYS_SAFE,
2678
},
2679
{ .name = "cpu",
2680
.func = kdb_cpu,
2681
.usage = "<cpunum>",
2682
.help = "Switch to new cpu",
2683
.flags = KDB_ENABLE_ALWAYS_SAFE_NO_ARGS,
2684
},
2685
{ .name = "kgdb",
2686
.func = kdb_kgdb,
2687
.usage = "",
2688
.help = "Enter kgdb mode",
2689
.flags = 0,
2690
},
2691
{ .name = "ps",
2692
.func = kdb_ps,
2693
.usage = "[<state_chars>|A]",
2694
.help = "Display active task list",
2695
.flags = KDB_ENABLE_INSPECT,
2696
},
2697
{ .name = "pid",
2698
.func = kdb_pid,
2699
.usage = "<pidnum>",
2700
.help = "Switch to another task",
2701
.flags = KDB_ENABLE_INSPECT,
2702
},
2703
{ .name = "reboot",
2704
.func = kdb_reboot,
2705
.usage = "",
2706
.help = "Reboot the machine immediately",
2707
.flags = KDB_ENABLE_REBOOT,
2708
},
2709
#if defined(CONFIG_MODULES)
2710
{ .name = "lsmod",
2711
.func = kdb_lsmod,
2712
.usage = "",
2713
.help = "List loaded kernel modules",
2714
.flags = KDB_ENABLE_INSPECT,
2715
},
2716
#endif
2717
#if defined(CONFIG_MAGIC_SYSRQ)
2718
{ .name = "sr",
2719
.func = kdb_sr,
2720
.usage = "<key>",
2721
.help = "Magic SysRq key",
2722
.flags = KDB_ENABLE_ALWAYS_SAFE,
2723
},
2724
#endif
2725
#if defined(CONFIG_PRINTK)
2726
{ .name = "dmesg",
2727
.func = kdb_dmesg,
2728
.usage = "[lines]",
2729
.help = "Display syslog buffer",
2730
.flags = KDB_ENABLE_ALWAYS_SAFE,
2731
},
2732
#endif
2733
{ .name = "defcmd",
2734
.func = kdb_defcmd,
2735
.usage = "name \"usage\" \"help\"",
2736
.help = "Define a set of commands, down to endefcmd",
2737
/*
2738
* Macros are always safe because when executed each
2739
* internal command re-enters kdb_parse() and is safety
2740
* checked individually.
2741
*/
2742
.flags = KDB_ENABLE_ALWAYS_SAFE,
2743
},
2744
{ .name = "kill",
2745
.func = kdb_kill,
2746
.usage = "<-signal> <pid>",
2747
.help = "Send a signal to a process",
2748
.flags = KDB_ENABLE_SIGNAL,
2749
},
2750
{ .name = "summary",
2751
.func = kdb_summary,
2752
.usage = "",
2753
.help = "Summarize the system",
2754
.minlen = 4,
2755
.flags = KDB_ENABLE_ALWAYS_SAFE,
2756
},
2757
{ .name = "per_cpu",
2758
.func = kdb_per_cpu,
2759
.usage = "<sym> [<bytes>] [<cpu>]",
2760
.help = "Display per_cpu variables",
2761
.minlen = 3,
2762
.flags = KDB_ENABLE_MEM_READ,
2763
},
2764
{ .name = "grephelp",
2765
.func = kdb_grep_help,
2766
.usage = "",
2767
.help = "Display help on | grep",
2768
.flags = KDB_ENABLE_ALWAYS_SAFE,
2769
},
2770
};
2771
2772
/* Initialize the kdb command table. */
2773
static void __init kdb_inittab(void)
2774
{
2775
kdb_register_table(maintab, ARRAY_SIZE(maintab));
2776
}
2777
2778
/* Execute any commands defined in kdb_cmds. */
2779
static void __init kdb_cmd_init(void)
2780
{
2781
int i, diag;
2782
for (i = 0; kdb_cmds[i]; ++i) {
2783
diag = kdb_parse(kdb_cmds[i]);
2784
if (diag)
2785
kdb_printf("kdb command %s failed, kdb diag %d\n",
2786
kdb_cmds[i], diag);
2787
}
2788
if (defcmd_in_progress) {
2789
kdb_printf("Incomplete 'defcmd' set, forcing endefcmd\n");
2790
kdb_parse("endefcmd");
2791
}
2792
}
2793
2794
/* Initialize kdb_printf, breakpoint tables and kdb state */
2795
void __init kdb_init(int lvl)
2796
{
2797
static int kdb_init_lvl = KDB_NOT_INITIALIZED;
2798
int i;
2799
2800
if (kdb_init_lvl == KDB_INIT_FULL || lvl <= kdb_init_lvl)
2801
return;
2802
for (i = kdb_init_lvl; i < lvl; i++) {
2803
switch (i) {
2804
case KDB_NOT_INITIALIZED:
2805
kdb_inittab(); /* Initialize Command Table */
2806
kdb_initbptab(); /* Initialize Breakpoints */
2807
break;
2808
case KDB_INIT_EARLY:
2809
kdb_cmd_init(); /* Build kdb_cmds tables */
2810
break;
2811
}
2812
}
2813
kdb_init_lvl = lvl;
2814
}
2815
2816