Subject: Debugging deadlocks using SV semaphores via PROC FS

From: Steffen Rumler <[email-masked]>
Date: Tue, 27 Apr 2004 09:38:38 +0200

Hi,

I have extended the PROC FS entry in ipc/sem.c for the 2.4.20 DEVEL
in order to debug deadlock problems; see attachment.

For each semaphore array, now also all array elements: (last-pid, sem.-value)
will be traced.

In this way, I can easy answer the question:

     "Which process is holding a distinct semaphore in a permanent way ?"

An subsequent strace of the found process provides additional information.

Steffen

--

--------------------------------------------------------------

Steffen Rumler
ICN CP D NT SW 3
Siemens AG
Hofmannstr. 51                 Email: [email-masked]
D-81359 Munich                 Phone: +49 89 722-44061
Germany                        Fax  : +49 89 722-36703

--------------------------------------------------------------

static int sysvipc_sem_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
{
	off_t pos = 0;
	off_t begin = 0;
	int i, len = 0;
        int j;
        struct sem *sem_base;

	len += sprintf(buffer, "       key      semid perms      nsems   uid   gid  cuid  cgid      otime      ctime\n");
	down(&sem_ids.sem);

	for(i = 0; i <= sem_ids.max_id; i++) {
		struct sem_array *sma;
		sma = sem_lock(i);
		if(sma) {
			len += sprintf(buffer + len, "\n%10d %10d  %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
				sma->sem_perm.key,
				sem_buildid(i,sma->sem_perm.seq),
				sma->sem_perm.mode,
				sma->sem_nsems,
				sma->sem_perm.uid,
				sma->sem_perm.gid,
				sma->sem_perm.cuid,
				sma->sem_perm.cgid,
				sma->sem_otime,
				sma->sem_ctime);

			pos += len;
			if(pos < offset) {
				len = 0;
	    			begin = pos;
			}
			if(pos > offset + length){
                                sem_unlock(i);
				goto done;
                        }

                        /*  S.R.:  print-out (pid, val) pairs for semaphore array
                         */
                        sem_base = sma->sem_base;
                        for(j=0; j < sma->sem_nsems; j++){

                            len += sprintf(buffer + len, 
                                           "               %ld (pid)  %ld (val)\n", 
                                           (sem_base->sempid) & 0xffff, 
                                           sem_base->semval);

                            pos += len;
                            if(pos < offset) {
                                len = 0;
                                begin = pos;
                            }
                            if(pos > offset + length){
                                sem_unlock(i);
                                goto done;
                            }

                            sem_base++;
                        } 
                        sem_unlock(i);
		}
	}
	*eof = 1;
done:
	up(&sem_ids.sem);
	*start = buffer + (offset - begin);
	len -= (offset - begin);
	if(len > length)
		len = length;
	if(len < 0)
		len = 0;
	return len;
}