Linux Kernel Development -- list all processes in the system

Source: Internet
Author: User
Tags systemtap

1. List all processes in the kernel module:

Traverse the kernel linked list from init_task and output all processes

# Include <Linux/module. h> # Include <Linux/list. h> # Include <Linux/init. h> # Include <Linux/sched. h> Module_license (  "  Dual BSD/GPL  "  );  Static   Int Test_init ( Void  ){ Struct Task_struct * task ,* P;  Struct List_head * Pos;  Int Count = 0  ; Printk (kern_alert  "  Test Module init \ n  "  ); Task = & Init_task; list_for_each (Pos, & Task-> Tasks) {P = List_entry (Pos,Struct  Task_struct, tasks); count ++ ; Printk (kern_alert  "  % S [% d] \ n  " , P-> comm, p-> PID);} printk (kern_alert  "  Total % d Tasks \ n  "  , Count );  Return   0  ;}  Static  Void Test_exit ( Void  ) {Printk (kern_alert  "  Test Module exit! \ N  "  );} Module_init (test_init); module_exit (test_exit ); 

Makefile

Ifneq ($ (kernelrelease),) OBJ-M: =Test. oElseKdir:=/Lib/modules/$ (shell uname-R )/Buildpwd:=$ (Shell PWD)Default: $ (Make)-C $ (kdir) M =$ (PWD) modulesendif

2. Use systemtap to output all processes:

//Process_list.stp%{# Include<Linux/list. h># Include<Linux/sched. h> %} Function process_list ()%{StructTask_struct *P;StructList_head * _ p ,*_ N; for_each_process (p) {_ stp_printf ("%-15 s (%-5d) \ n", P-> comm, p->PID );}%} Probe begin {process_list (); exit ()}

Running Method

# STAP-G process_list.stp Init (  1  ) Kthreadd (  2  ) Migration / 0 ( 3  ) Ksoftirqd / 0 ( 4  ) Migration / 0 ( 5  ) Watchdog /0 ( 6  ) Migration / 1 ( 7  ) Migration / 1 ( 8  ) Ksoftirqd / 1 ( 9  ) Watchdog / 1 ( 10 ) Events / 0 ( 11  ) Events / 1 ( 12  ) Cpuset (  13  ) Khelper (  14  ) Netns (  15  ).... 

3. Use systemtap to print the UTS namespace information of the process

  // Namespace_uts.stp % {# Include <Linux/list. h> # Include <Linux/sched. h> # Include <Linux/nsproxy. h> # Include <Linux/utsname. h> % } Function process_list () % {  Struct Task_struct * P;  Struct List_head * _ p ,* _ N;  Struct Uts_namespace * Uts;  Struct New_utsname * Utsname; for_each_process (p) {Uts = P-> nsproxy-> Uts_ns; utsname = & (UTS-> Name); _ stp_printf (  "  %-15 s (%-5d) %-24 S %-16s \ n  " , P-> comm, p-> PID, utsname-> release, utsname-> Sysname );} % } Probe begin {process_list (); exit ()} 
# STAP-G namespace_uts.stp Init (  1 ) 2.6 . 32 - 220  . El6.x86 _ 64 Linux kthreadd (  2 ) 2.6 . 32 - 220  . El6.x86 _ 64 Linux migration / 0 ( 3 )2.6 . 32 - 220  . El6.x86 _ 64 Linux ksoftirqd / 0 ( 4 ) 2.6 . 32 - 220  . El6.x86 _ 64 Linux migration / 0 ( 5 ) 2.6 .32 - 220  . El6.x86 _ 64 Linux Watchdog / 0 ( 6 ) 2.6 . 32 - 220  . El6.x86 _ 64 Linux migration / 1 ( 7 ) 2.6 . 32 -220  . El6.x86 _ 64 Linux migration / 1 ( 8 ) 2.6 . 32 - 220  . El6.x86 _ 64 Linux .... 

Reference:

Http://blog.csdn.net/lzuzhp06/article/details/6933525

Linux Kernel Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.