kernel 调试 API

  1. dump 寄存器 show_regs(regs);
    code : https://elixir.bootlin.com/linux/v4.9.185/source/arch/arm64/kernel/process.c
  void __show_regs(struct pt_regs *regs)
  {
  int i, top_reg;
  u64 lr, sp;

  if (compat_user_mode(regs)) {
  	lr = regs->compat_lr;
  	sp = regs->compat_sp;
  	top_reg = 12;
  } else {
  	lr = regs->regs[30];
  	sp = regs->sp;
  	top_reg = 29;
  }
  show_regs_print_info(KERN_DEFAULT);
  print_symbol("PC is at %s\n", instruction_pointer(regs));
  print_symbol("LR is at %s\n", lr);
  printk("pc : [<%016llx>] lr : [<%016llx>] pstate: %08llx\n",
         regs->pc, lr, regs->pstate);
  printk("sp : %016llx\n", sp);

  i = top_reg;

  while (i >= 0) {
  	printk("x%-2d: %016llx ", i, regs->regs[i]);
  	i--;

  	if (i % 2 == 0) {
  		pr_cont("x%-2d: %016llx ", i, regs->regs[i]);
  		i--;
  	}

  	pr_cont("\n");
  }
  printk("\n");
}

void show_regs(struct pt_regs * regs)
{
  printk("\n");
  __show_regs(regs);
}
  1. dump lock debug_show_held_locks // 只有在 debug lockdep 打开的情形下才有用
    code: https://elixir.bootlin.com/linux/v4.9.185/source/kernel/locking/lockdep.c
/*
 * Careful: only use this function if you are sure that
 * the task cannot run in parallel!
 */
void debug_show_held_locks(struct task_struct *task)
{
	if (unlikely(!debug_locks)) {
		printk("INFO: lockdep is turned off.\n");
		return;
	}
	lockdep_print_held_locks(task);
}
EXPORT_SYMBOL_GPL(debug_show_held_locks);

asmlinkage __visible void lockdep_sys_exit(void)
{
	struct task_struct *curr = current;

	if (unlikely(curr->lockdep_depth)) {
		if (!debug_locks_off())
			return;
		printk("\n");
		printk("================================================\n");
		printk("[ BUG: lock held when returning to user space! ]\n");
		print_kernel_ident();
		printk("------------------------------------------------\n");
		printk("%s/%d is leaving the kernel with locks still held!\n",
				curr->comm, curr->pid);
		lockdep_print_held_locks(curr);
	}
}
  1. find_task_by_vpid, find_get_task_by_vpid 找 根据pid 找到 task
    调用find_task_by_vpid的时候同样需要rcu_read_lock/rcu_read_unlock 的保护,而 find_get_task_by_vpid 内部实现已经包含了rcu相关操作
	rcu_read_lock();
	task = pfind_task_by_vpid(pid);
    rcu_read_unlock();
struct task_struct *find_task_by_vpid(pid_t vnr)
{
	return find_task_by_pid_ns(vnr, task_active_pid_ns(current));
}

struct task_struct *find_get_task_by_vpid(pid_t nr)
{
	struct task_struct *task;

	rcu_read_lock();
	task = find_task_by_vpid(nr);
	if (task)
		get_task_struct(task);
	rcu_read_unlock();

	return task;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值