2

I want to access data that return from "open" function of glibc such as filename or file descriptor

I try

probe process("/lib*/libc.so.*").function("open") { 
   fd = $fd
   filename = user_string($filename)
   printf("%d %d %s %s\n",pid(),ppid(),filename,fd)
}

but it error

semantic error: unresolved target-symbol expression: identifier '$fd' at malloc.stp:3:10 source: fd = $fd ^

Pass 2: analysis failed. [man error::pass2]

SilverIce
  • 75
  • 1
  • 13

1 Answers1

6

The open system call does not take an fd argument, so a .function probe naturally won't find it. If you'd like to see the file descriptor returned from open, then probe the .function("...").return point, and $return.

probe process("/lib*/libc.so.6").function("open").return {
    fd=$return
    path=user_string(@entry(@choose_defined($file,$filename)))
    printf("open %s -> %d\n", path, fd)
}

(Added the @choose_defined() here because some versions of glibc have renamed this parameter.)

fche
  • 2,641
  • 20
  • 28
  • 1
    I have tried your example, but I always receive a "semantic error: unresolved target-symbol expression: identifier '$filename'". Do you know why this happens? – Fernando Sep 27 '22 at 15:01
  • Maybe your installation is lacking access to glibc debuginfo? – fche Sep 27 '22 at 16:40
  • In glibc v2.37, this variable has been renamed: `% stap -L 'process("/lib*/libc.so.6").function("open")'` `process("/usr/lib/libc.so.6").function("__libc_open@../sysdeps/unix/sysv/linux/open.c:31") $file:char const* $oflag:int` `process("/usr/lib64/libc.so.6").function("__libc_open64@../sysdeps/unix/sysv/linux/open64.c:29") $file:char const* $oflag:int` – fche Aug 29 '23 at 00:53