Analyzing Program Performance With Sun WorkShop

Lock Analysis Tool

Many C developers thread their applications by programming directly against Solaris user-level threads via the libthread library. With Sun WorkShop applications, you can run the multithreaded extension to the debugger for debugging programs that use Solaris user-level threads. Use LockLint to statically check your program for consistent use of locks and potential race conditions.

In multithreaded applications, threads must acquire and release locks associated with the data they share. When threads fail to acquire and release locks appropriately, they can cause your program to produce different results over different executions with the same input. This condition is known as a a data race. Data races are easy problems to introduce and difficult ones to find.

A program can also deadlock when various threads are waiting for a lock that will not be releases. For a definition of deadlock, see "Basic Concepts".

Sun WorkShop provides a command-line utility for analyzing locks and how they are used. LockLint looks for inconsistent use of mutex and multiple readers-single writer locks. It detects a common cause of data races: failure to hold the appropriate lock while accessing a variable. LockLint also detects common causes for deadlocks.

To gather the information used by LockLint you specify the -Zll option to the C compiler, which then generates an .ll file for each .c source code file. The .ll file contains information about the flow of control in each function and about each access to a variable or operation on a mutex or readers-writer lock.

LockLint is described in Chapter 5, Lock Analysis Tool.