5

I'm monitoring a process with strace/ltrace in the hope to find and intercept a call that checks, and potentially activates some kind of globally shared lock.

While I've dealt with and read about several forms of interprocess locking on Linux before, I'm drawing a blank on what to calls to look for.

Currently my only suspect is futex() which comes up very early on in the process' execution.

Update0

There is some confusion about what I'm after. I'm monitoring an existing process for calls to persistent interprocess memory or equivalent. I'd like to know what system and library calls to look for. I have no intention call these myself, so naturally futex() will come up, I'm sure many libraries will implement their locking calls in terms of this, etc.

Update1

I'd like a list of function names or a link to documentation, that I should monitor at the ltrace and strace levels (and specifying which). Any other good advice about how to track and locate the global lock in mind would be great.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • You can monitor `futex` and `semop`. Sometimes `pipe` can be used for locking and ordering of processes. Shared Memory based syncronization with atomic operations don't use any syscall. – osgx Feb 17 '10 at 12:14
  • @osgx: Can you describe these atomic operations? – Matt Joiner May 29 '10 at 13:30
  • i'm wrong. not only atomic, but ordinal read/writes to shared memory can be used for syncro. so you can't monitor atomic and read/writes with ltrace/strace. Even futex will not do syscall every time. atomic: the easyest way is pthread_spinlock, and http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html But gcc atomics are inlined – osgx May 30 '10 at 02:20
  • Why can't I start a bounty on this? – Matt Joiner Aug 10 '10 at 07:59

4 Answers4

2

flock is another good one

user262976
  • 1,994
  • 12
  • 7
2

If you can start monitored process in valgrind, then there are two projects:

http://code.google.com/p/data-race-test/wiki/ThreadSanitizer

and Helgrind

http://valgrind.org/docs/manual/hg-manual.html

Helgrind is aware of all the pthread abstractions and tracks their effects as accurately as it can. On x86 and amd64 platforms, it understands and partially handles implicit locking arising from the use of the LOCK instruction prefix.

So, this tools can detect even atomic memory accesses. And they will check pthread usage

osgx
  • 90,338
  • 53
  • 357
  • 513
  • Well I since lost interest, but I now suspect that Helgrind would be the best chance to track these things with the least effort. – Matt Joiner Sep 11 '10 at 14:32
  • Matt Joiner, Thread Sanitizer has some interesting features and trys to monitor a bit more locking types, than Helgrind. It is a product from the Google internals, so it can be better – osgx Sep 13 '10 at 13:03
  • Thanks for the extra tip, I'll check it out if I run into this again. – Matt Joiner Nov 11 '10 at 06:14
0

on systems with glibc ~ >= 2.5 (glibc + nptl) you can use process shared

semaphores (last parameter to sem_init), more precisely, posix unnamed semaphores

posix mutexes (with PTHREAD_PROCESS_SHARED to pthread_mutexattr_setpshared)

posix named semaphores (got from sem_open/sem_unlink)

system v (sysv) semaphores: semget, semop

On older systems with glibc 2.2, 2.3 with linuxthreads or on embedded systems with uClibc you can use ONLY system v (sysv) semaphores for iterprocess communication.

upd1: any IPC and socker must be checked.

osgx
  • 90,338
  • 53
  • 357
  • 513
  • And please, don't use futexes directly, if you want to retain portability (both OS and architectural). – osgx Feb 10 '10 at 18:15
0
  1. There are many system calls can be used for locking: flock, fcntl, and even create.

  2. When you are using pthreads/sem_* locks they may be executed in user space so you'll never see them in strace as futex is called only for pending operations. Like when you actually need to wait.

  3. Some operations can be done in user space only - like spinlocks - you'll never see them unless they do some waits for timer - backoff so you may see only stuff like nanosleep when one lock waits for other.

So there is no "generic" way to trace them.

Artyom
  • 31,019
  • 21
  • 127
  • 215