Ways to trace threads in Linux: LWP and Strace commands

Source: Internet
Author: User

absrtact: When using multi-threaded programs, there are cases where the program functions abnormally, and this anomaly is not always happening every time, it is difficult to simulate. At this point you need to use the means of tracking threads while the program is running, and the LWP and Strace commands of the Linux system are the technical means. In this paper, the LWP and Strace commands are briefly introduced, and an example is given to illustrate how to use them. In a word, the use of LWP and strace can improve the maintainability of multi-thread routines.


Problem Description:

Let's look at a problem: The program tcp_client simultaneously create multiple threads to send data to the same server, each thread sends different types of data, and after the server receives the data, it can view multiple data refreshes through the interface. After a period of time, the program suddenly appears only one type of data is not refreshed, other data refresh normal, but the server and client programs are not error. What should we do then?


Obviously, the program debugger needs to know that the thread ID of the thread is not flushed and then, by some means, to see why the thread has an exception. The thread ID (LWP number) of the process can be viewed through the Linux ps-elf|grep PID command, but the problem is that if we do not record its thread ID when we create the thread, we still don't know which LWP number we want to track.


Let's take a look at what the LWP number is and how to record each thread's LWP number when the thread is created.


Pthread_create is based on clone and is created as a process, but these processes share a lot of things with the parent process,

The shared stuff is not copied to the subprocess, which saves a lot of overhead, so these sub-processes are also called lightweight processes (Light-weight process) referred to as LWP. Each LWP is bound to a kernel thread so that it can participate in CPU contention as a separate scheduling entity.


LWP is pthread encapsulated, the thread face, it has its own ID, here to distinguish phtread_create

The ID assigned to the LWP, and the process ID assigned to the LWP by the operating system. The two are different, the former used for the flag thread, which can be exposed to

The user, the latter is actually the ID of the process, it is not exposed, it must be used by the system to get a tune.


The way to get the LWP is through the Syscall system call (specific instructions to participate in the Man Manual), below is an example program:

#include <stdio.h> #include <unistd.h> #include <sys/syscall.h> #include <pthread.h>//threading Functions,    Output lwp number void *func (void *argv) {//Method 1 int lwp for Get LWP;    LWP = Syscall (__nr_gettid);        printf ("lwp[%d]\n", LWP);    Method of obtaining LWP 2 int lwp2;    LWP2 = Syscall (Sys_gettid);    printf ("lwp2[%d]\n", LWP2); Sleep (50);}    int main (void) {pthread_t THD; Pthread_create (&THD, NULL, func, NULL);    Create thread sleep (60); return 0;}


After obtaining the LWP number, the next step is to trace the thread through the strace command.


Strace is commonly used to track system calls and received signals when a process executes. In the Linux world, processes cannot directly access hardware devices, and when a process requires access to a hardware device (such as reading a disk file, receiving network data, and so on), it must be switched from user mode to kernel mode and access to the hardware device through system calls. Strace can trace the system calls generated by a process, including parameters, return values, and time spent executing.


In an ideal world, whenever a program fails to perform a function properly, it gives a useful error message that tells you enough clues to correct the error. Unfortunately, we do not live in an ideal world, at least not always in the ideal world. Sometimes a program has a problem and you can't find the reason. This is why the debugger appears. Strace is an essential debugging tool that Strace uses to monitor system calls. Not only can you debug a newly started program, you can also debug a program that is already running (bind the strace to an existing PID).


Strace parameters are many, the common parameters are described as follows:

-o filename writes the output of Strace to the file filename

-p PID tracks the specified process PID.

-T adds time information before each line in the output.

-tt The time information, in microseconds, before each line in the output.

-T displays the elapsed time for each call.

-F tracks the child processes that are generated by the fork call.

-FF If you provide-o filename, the trace results for all processes are output to the corresponding filename.pid, and the PID is the process number of each process.


For example : We make a small change to the Tcp_client program, after creating the thread through Pthread_create, each thread records its LWP number through the Syscall function and records the data type of the LWP number. If the program reappears with the exception that was mentioned at the beginning of the article, we can then pass "Strace-o tcp_client.txt-tt-p to trace the LWP number" so that we can quickly know what system calls are being made by the exception's thread.


Summary: in the design process, be sure to consider the future maintenance issues. For multithreaded programs, debugging can be more difficult by recording the LWP number and the corresponding threading functionality in the creation thread, which provides a great help for future debugging.


Reference article:

1. About Linux processes and threads

http://kenby.iteye.com/blog/1014039

2. Linux strace command:

Http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316692.html

This article is from the "Basket Big God" blog, please make sure to keep this source http://lancaidashen.blog.51cto.com/11086793/1734196

Ways to trace threads in Linux: LWP and Strace commands

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.