Red Hat Linux Fault location technology detailed and examples (4)

Source: Internet
Author: User
Tags systemtap

Red Hat Linux Fault location technology detailed and examples (4)

On-line fault location is in the event of a fault, the operating system environment is still accessible, fault handlers can be logged into the operating system through console, SSH, etc., in the shell to perform a variety of operation commands or test procedures in the manner of the failure environment to observe, analyze, test, To locate the cause of the failure.

AD:2014WOT Global Software Technology Summit Beijing Station course video release

6. Use Kprobe to observe the execution instance of kernel function

Kprobe is the implementation of SYSTEMTAP kernel functions probing the kernel, because the kernel provides a formal API to use Kprobe, so for many kernel programmers, it might be easier to use kprobe directly than Systemtap. The kernel provides three types of kprobe processing functions, namely Jprobe, Kprobe, Kretprobe, and the following code with these three probe observations in the Arp_process function execution of TCP/IP to Ip_route_input () The return result of the call. This code also shows how to share parameters between entry handler and RET handler in the same function probe. The code is as follows:

  1. ARP_PROBE.C/*
  2. * arp_probe.c, by Qianfeng Zhang ([email protected])
  3. */
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. #include
  11. #include
  12. Module_author ("[email protected]");
  13. Module_description ("A module to track the call results of Ip_route_input () inside arp_process using Jprobe and Kretprobe") ;
  14. Module_license ("GPL");
  15. static int j_arp_process (struct sk_buff *skb)
  16. {
  17. struct Net_device *dev = skb->dev;
  18. struct In_device *in_dev;
  19. int no_addr, RPF;
  20. In_dev = in_dev_get (dev);
  21. No_addr = (in_dev->ifa_list = = NULL);
  22. RPF = In_dev_rpfilter (in_dev);
  23. In_dev_put (In_dev);
  24. PRINTK ("\narp_process () is called with Interface device%s, In_dev (no_addr=%d,rpf=%d) \ n", dev->name, No_  addr, RPF);
  25. Jprobe_return ();
  26. return (0);
  27. };
  28. static int J_fib_validate_source (__be32 src, __be32 DST, U8 tos, int oif,
  29. struct Net_device *dev, __be32 *spec_dst, u32 *itag, u32 Mark)
  30. {
  31. PRINTK ("Fib_validate_source () is called with dst=0x%x, oif=%d \ n", DST, OIF);
  32. Jprobe_return ();
  33. return (0);
  34. };
  35. static struct Jprobe my_jp1 = {
  36. . Entry = j_arp_process,
  37. . Kp.symbol_name = "Arp_process"
  38. };
  39. static struct Jprobe my_jp2 = {
  40. . Entry = J_fib_validate_source,
  41. . Kp.symbol_name = "Fib_validate_source"
  42. };
  43. static int Entry_handler (struct kretprobe_instance *ri, struct pt_regs *regs)
  44. {
  45. PRINTK ("Calling:%s () \ n", ri->rp->kp.symbol_name);
  46. return (0);
  47. };
  48. static int Return_handler (struct kretprobe_instance *ri, struct pt_regs *regs)
  49. {
  50. int eax;
  51. EAX = regs->ax & 0xFFFF;
  52. PRINTK ("Returning:%s () with a return VALUE:0X%LX (64bit) 0x%x (32bit) \ n", ri->rp->kp.symbol_name, regs-  >ax, EAX);
  53. return (0);
  54. };
  55. static int Fib_lookup_entry_handler (struct kretprobe_instance *ri, struct pt_regs *regs)
  56. {
  57. struct Fib_result *resp;
  58. Resp = (struct Fib_result *) regs->dx;
  59. PRINTK ("Calling:%s () \ n", ri->rp->kp.symbol_name);
  60. * (struct Fib_result * *) ri->data) = resp;
  61. return (0);
  62. };
  63. static int Fib_lookup_return_handler (struct kretprobe_instance *ri, struct pt_regs *regs)
  64. {
  65. struct Fib_result *resp;
  66. int eax;
  67. EAX = regs->ax & 0xFFFF;
  68. RESP = * (struct fib_result * *) ri->data);
  69. PRINTK ("Returning:fib_lookup () with a return VALUE:0X%LX (64bit) 0x%x (32bit), result->type:%d\n", regs->  Ax, eax, resp->type);
  70. return (0);
  71. }
  72. static struct Kretprobe MY_RP1 = {
  73. . Handler = Return_handler,
  74. . Entry_handler = Entry_handler,
  75. . Kp.symbol_name = "Ip_route_input_slow"
  76. };
  77. static struct Kretprobe My_rp2 = {
  78. . Handler = Return_handler,
  79. . Entry_handler = Entry_handler,
  80. . Kp.symbol_name = "Fib_validate_source"
  81. };
  82. static struct Kretprobe MY_RP3 = {
  83. . Handler = Fib_lookup_return_handler,
  84. . Entry_handler = Fib_lookup_entry_handler,
  85. . Kp.symbol_name = "Fib_lookup",
  86. . data_size = sizeof (struct Fib_result *)
  87. };
  88. static int __init init_myprobe (void)
  89. {
  90. int ret;
  91. PRINTK ("Rtn_unicast is%d\n", rtn_unicast);
  92. if (ret = register_jprobe (&MY_JP1)) < 0) {
  93. PRINTK ("Register_jprobe%s failed, returned%d\n", My_jp1.kp.symbol_name, ret);
  94. Return (-1);
  95. }
  96. if (ret = register_jprobe (&MY_JP2)) < 0) {
  97. PRINTK ("Register_jprobe%s failed, returned%d\n", My_jp2.kp.symbol_name, ret);
  98. Return (-1);
  99. }
  100. if (ret = register_kretprobe (&MY_RP1)) < 0) {
  101. PRINTK ("Register_kretprobe%s failed, returned%d\n", My_rp1.kp.symbol_name, ret);
  102. Unregister_jprobe (&MY_JP1);
  103. Unregister_jprobe (&MY_JP2);
  104. Return (-1);
  105. }
  106. if (ret = register_kretprobe (&MY_RP2)) < 0) {
  107. PRINTK ("Register_kretprobe%s failed, returned%d\n", My_rp2.kp.symbol_name, ret);
  108. Unregister_jprobe (&MY_JP1);
  109. Unregister_jprobe (&MY_JP2);
  110. Unregister_kretprobe (&MY_RP1);
  111. Return (-1);
  112. }
  113. if (ret = register_kretprobe (&MY_RP3)) < 0) {
  114. PRINTK ("Register_kretprobe%s failed, returned%d\n", My_rp3.kp.symbol_name, ret);
  115. Unregister_jprobe (&MY_JP1);
  116. Unregister_jprobe (&MY_JP2);
  117. Unregister_kretprobe (&MY_RP1);
  118. Unregister_kretprobe (&MY_RP2);
  119. Return (-1);
  120. }
  121. return 0;
  122. }
  123. static void __exit rel_myprobe (void)
  124. {
  125. Unregister_jprobe (&MY_JP1);
  126. Unregister_jprobe (&MY_JP2);
  127. Unregister_kretprobe (&MY_RP1);
  128. Unregister_kretprobe (&MY_RP2);
  129. Unregister_kretprobe (&MY_RP3);
  130. }
  131. Module_init (Init_myprobe);
  132. Module_exit (Rel_myprobe);
  133. Makefile Obj-m + = ARP_PROBE.O
  134. Making #> Make-c/usr/src/kernels/2.6.32-71.el6.x86_64/ m= ' pwd ' modules

Linux Fault location technology details and examples of the content of the introduction, I hope that this article Red Hat Linux fault location technology learning can help you!

Original address: http://beareyes.com.cn/2/lib/201109/27/20110927182_0.htm

Red Hat Linux Fault location technology detailed and examples (4)

Related Article

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.