打印堆栈分为内核态和用户态
1 内核态:dump_stack
参考博客:
http://kernel.meizu.com/2017/03/18-40-19-dump_stack.html
https://blog.csdn.net/xj178926426/article/details/79235952
作用:
打印进程的栈回溯信息。
前提:
内核配置勾选上;make menuconfig -> kernel hacking--> kernel debug
什么情况使用:
1、内核发生panic、内核主动调用dump_stack,打印call trace
2、代码调试,主动调用dump_stack函数。
主要信息内容:
源码分析:
1 /** 2 * dump_stack - dump the current task information and its stack trace 3 * 4 * Architectures can override this implementation by implementing its own. 5 */ 6 #ifdef CONFIG_SMP 7 static atomic_t dump_lock = ATOMIC_INIT(-1); 8 9 asmlinkage __visible void dump_stack(void) 10 { 11 unsigned long flags; 12 int was_locked; 13 int old; 14 int cpu; 15 16 /* 17 * Permit this cpu to perform nested stack dumps while serialising 18 * against other CPUs 19 */ 20 retry: 21 local_irq_save(flags); 22 cpu = smp_processor_id(); 23 old = atomic_cmpxchg(&dump_lock, -1, cpu); 24 if (old == -1) { 25 was_locked = 0; 26 } else if (old == cpu) { 27 was_locked = 1; 28 } else { 29 local_irq_restore(flags); 30 /* 31 * Wait for the lock to release before jumping to 32 * atomic_cmpxchg() in order to mitigate the thundering herd 33 * problem. 34 */ 35 do { cpu_relax(); } while (atomic_read(&dump_lock) != -1); 36 goto retry; 37 } 38 39 __dump_stack(); 40 41 if (!was_locked) 42 atomic_set(&dump_lock, -1); 43 44 local_irq_restore(flags); 45 } 46 #else 47 asmlinkage __visible void dump_stack(void) 48 { 49 __dump_stack(); 50 } 51 #endif