crash —— 如何知道哪些数据结构内嵌了指定的数据结构或者内嵌了指向指定数据结构的指针

发布时间 2023-09-24 11:14:51作者: 摩斯电码

需求

有时我们有这样的需求,比如我们知道了结构体task_struct,然后我们又想知道内核中有哪些数据结构中包含了task_struct,或者包含了指向task_struct的指针。

做法

whatis -m可以做到:

crash> whatis -m task_struct
  SIZE  TYPE
     8  rcuwait
    16  core_thread
    16  io_task_cancel
    16  ns_get_path_task_args
    16  tgid_iter
    24  ldsem_waiter
    24  migration_arg
    24  migration_swap_arg
    24  rt_wake_q_head
    24  swait_queue
    32  default_wait_cb
    32  hwlat_kthread_data
    32  io_tctx_node
    32  io_wq_data
    32  irq_work
    32  msg_sender
...

此外还支持对列出的数据结构的大小进行过滤:

  • 获取大小为32字节的结构
crash> whatis -r 32 -m task_struct
SIZE  TYPE
  32  default_wait_cb
  32  hwlat_kthread_data
  32  io_tctx_node
  32  io_wq_data
  32  irq_work
  32  msg_sender
  32  nfs_referral_count
  32  perf_switch_event
  32  remote_function_call
  32  request_wait
  32  semaphore_waiter
  • 获取大小范围32-48字节的结构
crash> whatis -r 32-48 -m task_struct
SIZE  TYPE
  32  default_wait_cb
  32  hwlat_kthread_data
  32  io_tctx_node
  32  io_wq_data
  32  irq_work
  32  msg_sender
  32  nfs_referral_count
  32  perf_switch_event
  32  remote_function_call
  32  request_wait
  32  semaphore_waiter
  40  bpf_iter_seq_task_file_info
  40  debuggerinfo_struct
  40  ext_wait_queue
  40  klist_waiter
  40  mmap_unlock_irq_work
  40  mutex_waiter
  40  perf_comm_event
  40  to_kill
  48  execute_cb
  48  perf_task_event
  48  rwsem_waiter
  48  send_signal_irq_work

有时遇到slab内存泄漏,比如发现kmalloc-64这个slab占用的内存太大,我们可以用这个命令来获取大致是这个尺寸的结构有哪些:

crash> whatis -r 33-64
SIZE  TYPE
  33  cfg80211_ssid
  33  hdcp2_ake_send_hprime
  33  hdcp2_lc_send_lprime
  33  hdcp2_rep_stream_ready
  33  iso_directory_record
  33  netdev_phys_item_id
  34  acpi_pld_info
  34  bdb_lvds_options
  34  g4x_wm_state
  34  i2c_smbus_data
  34  ptp_header
  34  sky2_stat
  36  acpi_cedt_cfmws
  36  acpi_resource_i2c_serialbus
  36  acpi_resource_pin_function
...