Solaris2.4 multi-thread programming guide 6-compilation and debugging

Source: Internet
Author: User
Tags dbx

BBS shuimu Tsinghua station: essence District

Sender: McCartney (coolcat), email zone: Unix
Mailing site: BBS shuimu Tsinghua station (Sun May 17 16:33:41 1998)
 
6. Compilation and debugging
 
This chapter describes how to compile and debug multi-threaded programs.
Compile a multi-threaded Application
Debug a multi-threaded Application
 
6.1 compile a multi-threaded Application
 
6.1.1 use the C Compiler
 
Make sure you have the following software; otherwise, you will not be able to compile and connect to multi-threaded programs.
· Header file: thread. h errno. h
· Standard C Compiler
· Standard Solaris Connector
· Libthread)
· MT-safe Library (libc, libm, libw, libintl, libmalloc,
Libmapmalloc, libnsl, etc)
 
6.1.2 compile with the alternative (_ reentrant) Flag
 
Use the-D _ reentrant flag when compiling a multi-threaded program.
This flag must be used when compiling every module of the application. If this flag does not exist
Use old definitions such as errno and stdio. If you want to compile a single-threaded application, do not use this
.
 
* Be careful when connecting old and new connections
 
Table 6-1 shows how to connect the multi-threaded target code module to the Old Code module with caution.
Table 6-1 use the "-D _ reentrant" flag when compiling a multi-threaded Program
File Type compilation reference return
The old target file (non-thread version) and new target file do not have the "-D _ reentrant" flag. Static storage of traditional errno
The new target file has the "-D _ reentrant" flag. _ Errno. The new binary entry thread defines the errno address.
The Tli programming in libnsl contains the "-D _ reentrant" sign (required ). _ T_errno: a new entry thread defines the address of t_errno.
 
6.1.3 use libthread
 
To use libthread during connection, you must specify
-Lthread, or at the end of the CC command line.
If the application is not connected to libthread, no actual operation is performed on function calls in the library.
Libc defines the libthread as a null process .??? The real process is that the application connects both libc and
Libthread is added when libthread is connected.
If a LD command line contains the following fields:. O's...-LC-lthread..., then the c Function
The behavior of the database is not defined .???
Do not use-lthread in a single-threaded program. This will create a multi-threaded Mechanism During connection
The row is initialized. In this way, not only resources are wasted, but the running results are incorrectly displayed during debugging.
 
6.1.4 use a non-C Compiler
 
The thread library uses the following content in libc:
· System call package (wrappers)
· Call for displaying error information (usually printf)
· Support for runtime connections to parse symbols (because libraries are dynamically connected)
 
You can also write your own system to call the wrapper and your printf function, and when you connect (instead
Is executed at runtime) to eliminate libc dependencies.
If the thread uses the stack provided by the application, the thread library does not use the dynamic memory allocation method.
The thr_create (3 T) function can be used by the application to specify its own stack.
 
6.2 debug multi-threaded applications
 
6.2.1 General omissions
The following lists Common Omissions that can cause multithreading errors:
· Use local or global variables when passing parameters to a new thread
· Access global memory without the protection of synchronization mechanism
· Two threads apply for two resources in different order, resulting in deadlocks (two threads each occupy one resource
Parallel Execution)
· Hidden vulnerabilities in synchronization protection. For example, a synchronization mechanism (such as mutex
Lock) the protected code segment contains a function call that first releases and obtains the synchronization mechanism again. The result is global memory.
Actually not protected.
· The use of hidden, repeated, or recursive large automatic arrays may cause problems because of the heap of multi-thread programs
Stack capacity has more limits than single-threaded programs.
· The specified stack space is insufficient.
· The stack is not specified through the call of the thread library.
Note that multi-threaded programs (especially those with errors) often get different knots with the same input.
Because the thread scheduling order is different.
Generally, multithreading bugs are statistical, not deterministic. During debugging, the tracing method will be compared
It is better to set breakpoints.
 
6.2.2 use ADB
 
If you bind all threads to a multi-threaded program, one thread and one lwp are synchronized. However
Then you can access every thread through the following multi-threaded programming ADB command.
 
Table 6-2 Mt ADB commands
-------------------------------------
PID: A is bound to the process PID, which stops the process and all its lwp
: R and process separation, which will restore the process and all its lwp
$ L display lwp of all activities in (stopped) Processes
N: l switch the focus to lwp n.
$ L display the lwp of the current focus
Num: I ignores signals whose signal code is num.
 
6.2.3 use dbx
 
With DBX, You can debug and execute source programs using C ++, ansi c, Fortran, and Pascal. Dbx
With sparcworks? The same command as the debugger, but the standard terminal (TTY) interface is used. Dbx and
Sparcworks debugger now supports multi-threaded programs.
For more information about DBX and debugger, see sunpro DBX (1) man page and
Debugging a program user guide.
The following DBX options support multithreading.
Table 6-3 DBX options for the MT Program
Cont at line [sig signo id] continues to execute line 1 when the signal signo occurs.
See "continue" in the loop control of DBX command language.
If the ID parameter exists, specify which thread or lwp to continue.
The default value is all.
Lwp displays the current lwp. Switch to the given lwp [lwpid]
Lwps lists all the lwp of the current process
Next... TID specifies the thread for one-step execution. If a function call is skipped,
All lwp start again during this function call ??? Non
The active thread cannot be executed in a single step.
Next... Lid specifies lwp for one-step execution. It is not implicitly restored when a function is skipped.
Repeat all lwp values. The thread on the lwp is active.
Step... TID specifies the thread for one-step execution. If a function call is skipped,
All lwp start again during this function call ??? Non
The active thread cannot be executed in a single step.
Step... Lid specifies lwp for one-step execution. It is not implicitly restored when a function is skipped.
Repeat all lwp values.
Stepi... Lwp specified by Lid
Stepi... TID threads on lwp are active.
The thread displays the current thread. Switch to the thread tid. In the following cases,
An optional TID indicates the current thread.
Thread-info [TID] prints all known conditions of the specified thread.
Thread-locks [TID] print all locks controlled by the specified Thread
Thread-suspend [TID] puts the specified thread in the suspended state.
Thread-continue [TID] causes the specified thread to exit the suspension state.
Thread-hide [TID] hides the specified (or current) thread, in the normal thread list
Will not be displayed
Thread-unhide [TID] unhide the specified Thread
Allthread-unhide unhides all threads
Threads prints the list of known threads
Threads-all print all threads (including zombies which are usually not printed)
All | filterthreads-mode control threads command to print all threads or select a list
Auto | manualthreads-mode enables thread inspector in the GUI)
The thread list is automatically updated.
Threads-mode: displays the current mode. Any of the previous forms
Can be followed by
Thread or lwp ID to get the traceback for the specified entity.
 
--
※Source: · bbs.net.tsinghua.edu.cn · [from: sys11.cic. Tsing]

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.