Skip to content

Commit 8a99117

Browse files
committed
Merge tag 'kgdb-5.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt/linux
Pull kgdb updates from Daniel Thompson: "The major change here is the work from Douglas Anderson that reworks the way kdb stack traces are handled on SMP systems. The effect is to allow all CPUs to issue their stack trace which reduced the need for architecture specific code to support stack tracing. Also included are general of clean ups from Doug and myself: - Remove some unused variables or arguments. - Tidy up the kdb escape handling code and fix a couple of odd corner cases. - Better ignore escape characters that do not form part of an escape sequence. This mostly benefits vi users since they are most likely to press escape as a nervous habit but it won't harm anyone else" * tag 'kgdb-5.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt/linux: kdb: Tweak escape handling for vi users kdb: Improve handling of characters from different input sources kdb: Remove special case logic from kdb_read() kdb: Simplify code to fetch characters from console kdb: Tidy up code to handle escape sequences kdb: Avoid array subscript warnings on non-SMP builds kdb: Fix stack crawling on 'running' CPUs that aren't the master kdb: Fix "btc <cpu>" crash if the CPU didn't round up kdb: Remove unused "argcount" param from kdb_bt1(); make btaprompt bool kgdb: Remove unused DCPU_SSTEP definition
2 parents 0dd0c8f + c58ff64 commit 8a99117

File tree

5 files changed

+208
-177
lines changed

5 files changed

+208
-177
lines changed

kernel/debug/debug_core.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,37 @@ int dbg_remove_all_break(void)
441441
return 0;
442442
}
443443

444+
#ifdef CONFIG_KGDB_KDB
445+
void kdb_dump_stack_on_cpu(int cpu)
446+
{
447+
if (cpu == raw_smp_processor_id() || !IS_ENABLED(CONFIG_SMP)) {
448+
dump_stack();
449+
return;
450+
}
451+
452+
if (!(kgdb_info[cpu].exception_state & DCPU_IS_SLAVE)) {
453+
kdb_printf("ERROR: Task on cpu %d didn't stop in the debugger\n",
454+
cpu);
455+
return;
456+
}
457+
458+
/*
459+
* In general, architectures don't support dumping the stack of a
460+
* "running" process that's not the current one. From the point of
461+
* view of the Linux, kernel processes that are looping in the kgdb
462+
* slave loop are still "running". There's also no API (that actually
463+
* works across all architectures) that can do a stack crawl based
464+
* on registers passed as a parameter.
465+
*
466+
* Solve this conundrum by asking slave CPUs to do the backtrace
467+
* themselves.
468+
*/
469+
kgdb_info[cpu].exception_state |= DCPU_WANT_BT;
470+
while (kgdb_info[cpu].exception_state & DCPU_WANT_BT)
471+
cpu_relax();
472+
}
473+
#endif
474+
444475
/*
445476
* Return true if there is a valid kgdb I/O module. Also if no
446477
* debugger is attached a message can be printed to the console about
@@ -580,6 +611,9 @@ static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
580611
atomic_xchg(&kgdb_active, cpu);
581612
break;
582613
}
614+
} else if (kgdb_info[cpu].exception_state & DCPU_WANT_BT) {
615+
dump_stack();
616+
kgdb_info[cpu].exception_state &= ~DCPU_WANT_BT;
583617
} else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
584618
if (!raw_spin_is_locked(&dbg_slave_lock))
585619
goto return_normal;

kernel/debug/debug_core.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct kgdb_state {
3333
#define DCPU_WANT_MASTER 0x1 /* Waiting to become a master kgdb cpu */
3434
#define DCPU_NEXT_MASTER 0x2 /* Transition from one master cpu to another */
3535
#define DCPU_IS_SLAVE 0x4 /* Slave cpu enter exception */
36-
#define DCPU_SSTEP 0x8 /* CPU is single stepping */
36+
#define DCPU_WANT_BT 0x8 /* Slave cpu should backtrace then clear flag */
3737

3838
struct debuggerinfo_struct {
3939
void *debuggerinfo;
@@ -76,6 +76,7 @@ extern int kdb_stub(struct kgdb_state *ks);
7676
extern int kdb_parse(const char *cmdstr);
7777
extern int kdb_common_init_state(struct kgdb_state *ks);
7878
extern int kdb_common_deinit_state(void);
79+
extern void kdb_dump_stack_on_cpu(int cpu);
7980
#else /* ! CONFIG_KGDB_KDB */
8081
static inline int kdb_stub(struct kgdb_state *ks)
8182
{

kernel/debug/kdb/kdb_bt.c

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,15 @@
2222
static void kdb_show_stack(struct task_struct *p, void *addr)
2323
{
2424
int old_lvl = console_loglevel;
25+
2526
console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
2627
kdb_trap_printk++;
27-
kdb_set_current_task(p);
28-
if (addr) {
29-
show_stack((struct task_struct *)p, addr);
30-
} else if (kdb_current_regs) {
31-
#ifdef CONFIG_X86
32-
show_stack(p, &kdb_current_regs->sp);
33-
#else
34-
show_stack(p, NULL);
35-
#endif
36-
} else {
37-
show_stack(p, NULL);
38-
}
28+
29+
if (!addr && kdb_task_has_cpu(p))
30+
kdb_dump_stack_on_cpu(kdb_process_cpu(p));
31+
else
32+
show_stack(p, addr);
33+
3934
console_loglevel = old_lvl;
4035
kdb_trap_printk--;
4136
}
@@ -78,35 +73,60 @@ static void kdb_show_stack(struct task_struct *p, void *addr)
7873
*/
7974

8075
static int
81-
kdb_bt1(struct task_struct *p, unsigned long mask,
82-
int argcount, int btaprompt)
76+
kdb_bt1(struct task_struct *p, unsigned long mask, bool btaprompt)
8377
{
84-
char buffer[2];
85-
if (kdb_getarea(buffer[0], (unsigned long)p) ||
86-
kdb_getarea(buffer[0], (unsigned long)(p+1)-1))
78+
char ch;
79+
80+
if (kdb_getarea(ch, (unsigned long)p) ||
81+
kdb_getarea(ch, (unsigned long)(p+1)-1))
8782
return KDB_BADADDR;
8883
if (!kdb_task_state(p, mask))
8984
return 0;
9085
kdb_printf("Stack traceback for pid %d\n", p->pid);
9186
kdb_ps1(p);
9287
kdb_show_stack(p, NULL);
9388
if (btaprompt) {
94-
kdb_getstr(buffer, sizeof(buffer),
95-
"Enter <q> to end, <cr> to continue:");
96-
if (buffer[0] == 'q') {
97-
kdb_printf("\n");
89+
kdb_printf("Enter <q> to end, <cr> or <space> to continue:");
90+
do {
91+
ch = kdb_getchar();
92+
} while (!strchr("\r\n q", ch));
93+
kdb_printf("\n");
94+
95+
/* reset the pager */
96+
kdb_nextline = 1;
97+
98+
if (ch == 'q')
9899
return 1;
99-
}
100100
}
101101
touch_nmi_watchdog();
102102
return 0;
103103
}
104104

105+
static void
106+
kdb_bt_cpu(unsigned long cpu)
107+
{
108+
struct task_struct *kdb_tsk;
109+
110+
if (cpu >= num_possible_cpus() || !cpu_online(cpu)) {
111+
kdb_printf("WARNING: no process for cpu %ld\n", cpu);
112+
return;
113+
}
114+
115+
/* If a CPU failed to round up we could be here */
116+
kdb_tsk = KDB_TSK(cpu);
117+
if (!kdb_tsk) {
118+
kdb_printf("WARNING: no task for cpu %ld\n", cpu);
119+
return;
120+
}
121+
122+
kdb_set_current_task(kdb_tsk);
123+
kdb_bt1(kdb_tsk, ~0UL, false);
124+
}
125+
105126
int
106127
kdb_bt(int argc, const char **argv)
107128
{
108129
int diag;
109-
int argcount = 5;
110130
int btaprompt = 1;
111131
int nextarg;
112132
unsigned long addr;
@@ -125,7 +145,7 @@ kdb_bt(int argc, const char **argv)
125145
/* Run the active tasks first */
126146
for_each_online_cpu(cpu) {
127147
p = kdb_curr_task(cpu);
128-
if (kdb_bt1(p, mask, argcount, btaprompt))
148+
if (kdb_bt1(p, mask, btaprompt))
129149
return 0;
130150
}
131151
/* Now the inactive tasks */
@@ -134,7 +154,7 @@ kdb_bt(int argc, const char **argv)
134154
return 0;
135155
if (task_curr(p))
136156
continue;
137-
if (kdb_bt1(p, mask, argcount, btaprompt))
157+
if (kdb_bt1(p, mask, btaprompt))
138158
return 0;
139159
} kdb_while_each_thread(g, p);
140160
} else if (strcmp(argv[0], "btp") == 0) {
@@ -148,7 +168,7 @@ kdb_bt(int argc, const char **argv)
148168
p = find_task_by_pid_ns(pid, &init_pid_ns);
149169
if (p) {
150170
kdb_set_current_task(p);
151-
return kdb_bt1(p, ~0UL, argcount, 0);
171+
return kdb_bt1(p, ~0UL, false);
152172
}
153173
kdb_printf("No process with pid == %ld found\n", pid);
154174
return 0;
@@ -159,47 +179,33 @@ kdb_bt(int argc, const char **argv)
159179
if (diag)
160180
return diag;
161181
kdb_set_current_task((struct task_struct *)addr);
162-
return kdb_bt1((struct task_struct *)addr, ~0UL, argcount, 0);
182+
return kdb_bt1((struct task_struct *)addr, ~0UL, false);
163183
} else if (strcmp(argv[0], "btc") == 0) {
164184
unsigned long cpu = ~0;
165185
struct task_struct *save_current_task = kdb_current_task;
166-
char buf[80];
167186
if (argc > 1)
168187
return KDB_ARGCOUNT;
169188
if (argc == 1) {
170189
diag = kdbgetularg((char *)argv[1], &cpu);
171190
if (diag)
172191
return diag;
173192
}
174-
/* Recursive use of kdb_parse, do not use argv after
175-
* this point */
176-
argv = NULL;
177193
if (cpu != ~0) {
178-
if (cpu >= num_possible_cpus() || !cpu_online(cpu)) {
179-
kdb_printf("no process for cpu %ld\n", cpu);
180-
return 0;
181-
}
182-
sprintf(buf, "btt 0x%px\n", KDB_TSK(cpu));
183-
kdb_parse(buf);
184-
return 0;
185-
}
186-
kdb_printf("btc: cpu status: ");
187-
kdb_parse("cpu\n");
188-
for_each_online_cpu(cpu) {
189-
void *kdb_tsk = KDB_TSK(cpu);
190-
191-
/* If a CPU failed to round up we could be here */
192-
if (!kdb_tsk) {
193-
kdb_printf("WARNING: no task for cpu %ld\n",
194-
cpu);
195-
continue;
194+
kdb_bt_cpu(cpu);
195+
} else {
196+
/*
197+
* Recursive use of kdb_parse, do not use argv after
198+
* this point.
199+
*/
200+
argv = NULL;
201+
kdb_printf("btc: cpu status: ");
202+
kdb_parse("cpu\n");
203+
for_each_online_cpu(cpu) {
204+
kdb_bt_cpu(cpu);
205+
touch_nmi_watchdog();
196206
}
197-
198-
sprintf(buf, "btt 0x%px\n", kdb_tsk);
199-
kdb_parse(buf);
200-
touch_nmi_watchdog();
207+
kdb_set_current_task(save_current_task);
201208
}
202-
kdb_set_current_task(save_current_task);
203209
return 0;
204210
} else {
205211
if (argc) {
@@ -211,7 +217,7 @@ kdb_bt(int argc, const char **argv)
211217
kdb_show_stack(kdb_current_task, (void *)addr);
212218
return 0;
213219
} else {
214-
return kdb_bt1(kdb_current_task, ~0UL, argcount, 0);
220+
return kdb_bt1(kdb_current_task, ~0UL, false);
215221
}
216222
}
217223

0 commit comments

Comments
 (0)