| 1 |
# pfiles.stp |
| 2 |
# Copyright (C) 2007-2010, 2012, 2015-2019 Red Hat, Inc., Eugene Teo |
| 3 |
# <[email protected]> |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
# Example: |
| 10 |
# $ stap -g pfiles.stp `pgrep udevd` |
| 11 |
# OR |
| 12 |
# $ stap -g pfiles.stp -x `pgrep udevd` |
| 13 |
|
| 14 |
# |
| 15 |
# pfiles.stp report information for all open files by the process id. |
| 16 |
# |
| 17 |
# pfiles.stp is not a port of Solaris' pfiles tool. It was written based |
| 18 |
# on the example outputs in: |
| 19 |
# - https://bugzilla.redhat.com/show_bug.cgi?id=223489 |
| 20 |
# - http://blogs.sun.com/peteh/entry/pfiles_1_locked_files_and |
| 21 |
# - http://lists.samba.org/archive/samba-technical/2001-May/014293.html |
| 22 |
# - http://mail.gnome.org/archives/gconf-list/2004-December/msg00005.html |
| 23 |
# |
| 24 |
# pfiles.stp is able to: |
| 25 |
# - report locked open files |
| 26 |
# - report pathname information |
| 27 |
# - report socket information (thanks Luis Henriques) |
| 28 |
# - report sk_userlocks (thanks Arnaldo Carvalho de Melo) |
| 29 |
# - report socket options |
| 30 |
# |
| 31 |
# Example: |
| 32 |
# $ stap -g pfiles.stp `pgrep udevd` |
| 33 |
# 787: udevd |
| 34 |
# Current rlimit: 32 file descriptors |
| 35 |
# 0: S_IFCHR mode:0666 dev:0,15 ino:396 uid:0 gid:0 rdev:1,3 |
| 36 |
# O_RDWR|O_LARGEFILE |
| 37 |
# /dev/null |
| 38 |
# 1: S_IFCHR mode:0666 dev:0,15 ino:396 uid:0 gid:0 rdev:1,3 |
| 39 |
# O_RDWR|O_LARGEFILE |
| 40 |
# /dev/null |
| 41 |
# 2: S_IFCHR mode:0666 dev:0,15 ino:396 uid:0 gid:0 rdev:1,3 |
| 42 |
# O_RDWR|O_LARGEFILE |
| 43 |
# /dev/null |
| 44 |
# 3: S_IFDIR mode:0600 dev:0,9 ino:1 uid:0 gid:0 rdev:0,0 |
| 45 |
# O_RDONLY |
| 46 |
# inotify |
| 47 |
# 4: S_IFSOCK mode:0777 dev:0,4 ino:2353 uid:0 gid:0 rdev:0,0 |
| 48 |
# O_RDWR |
| 49 |
# socket:[2353] |
| 50 |
# SO_PASSCRED,SO_TYPE(2),SO_SNDBUF(111616),SO_RCVBUF(111616) |
| 51 |
# sockname: AF_UNIX |
| 52 |
# 5: S_IFSOCK mode:0777 dev:0,4 ino:2354 uid:0 gid:0 rdev:0,0 |
| 53 |
# O_RDWR |
| 54 |
# socket:[2354] |
| 55 |
# SO_TYPE(2),SO_SNDBUF(111616),SO_RCVBUF(33554432) |
| 56 |
# ulocks: rcv |
| 57 |
# 6: S_IFIFO mode:0600 dev:0,6 ino:2355 uid:0 gid:0 rdev:0,0 |
| 58 |
# O_RDONLY|O_NONBLOCK |
| 59 |
# pipe:[2355] |
| 60 |
# 7: S_IFIFO mode:0600 dev:0,6 ino:2355 uid:0 gid:0 rdev:0,0 |
| 61 |
# O_WRONLY|O_NONBLOCK |
| 62 |
# pipe:[2355] |
| 63 |
# |
| 64 |
# I used Andrew Tridgell's locktst.c to test the advisory lock code. |
| 65 |
# You can download it at http://samba.org/ftp/unpacked/junkcode/locktst.c |
| 66 |
# |
| 67 |
# $ ./locktst file & |
| 68 |
# fcntl_lock 3 6 1 1 1 |
| 69 |
# $ pfiles 10126 | grep advisory -A1 -B2 |
| 70 |
# 3: S_IFREG mode:0644 dev:253,0 ino:15237159 uid:500 gid:500 rdev:0,0 |
| 71 |
# O_RDWR |
| 72 |
# advisory write lock set by process 10126 |
| 73 |
# /home/eteo/pfiles/file |
| 74 |
# |
| 75 |
|
| 76 |
%{ |
| 77 |
#include <linux/file.h> |
| 78 |
#include <net/sock.h> |
| 79 |
#include <linux/un.h> |
| 80 |
#include <linux/tcp.h> |
| 81 |
#ifdef CONFIG_USER_NS |
| 82 |
#include <linux/user_namespace.h> |
| 83 |
#endif |
| 84 |
#ifdef STAPCONF_LINUX_UIDGID_H |
| 85 |
#include <linux/uidgid.h> |
| 86 |
#endif |
| 87 |
%} |
| 88 |
|
| 89 |
# XXX: For the time being, mark this example as not working on PREEMPT_RT kernels: |
| 90 |
%{ |
| 91 |
#if defined(CONFIG_PREEMPT_RT_FULL) || defined(CONFIG_PREEMPT_RT) |
| 92 |
#error "pfiles.stp not supported on PREEMPT_RT kernels" |
| 93 |
#endif |
| 94 |
%} |
| 95 |
|
| 96 |
function task_valid_file_handle:long (task:long, fd:long) %{ /* pure */ |
| 97 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 98 |
struct files_struct *files; |
| 99 |
struct file *filp; |
| 100 |
|
| 101 |
rcu_read_lock(); |
| 102 |
if ((files = kread(&p->files))) { |
| 103 |
filp = fcheck_files(files, STAP_ARG_fd); |
| 104 |
STAP_RETVALUE = !!filp; |
| 105 |
} |
| 106 |
|
| 107 |
CATCH_DEREF_FAULT(); |
| 108 |
rcu_read_unlock(); |
| 109 |
%} |
| 110 |
|
| 111 |
function i_mode2str:string (i_mode:long) { |
| 112 |
if (%{ S_ISLNK(STAP_ARG_i_mode) %}) |
| 113 |
return "S_IFLNK" |
| 114 |
if (%{ S_ISREG(STAP_ARG_i_mode) %}) |
| 115 |
return "S_IFREG" |
| 116 |
if (%{ S_ISDIR(STAP_ARG_i_mode) %}) |
| 117 |
return "S_IFDIR" |
| 118 |
if (%{ S_ISCHR(STAP_ARG_i_mode) %}) |
| 119 |
return "S_IFCHR" |
| 120 |
if (%{ S_ISBLK(STAP_ARG_i_mode) %}) |
| 121 |
return "S_IFBLK" |
| 122 |
if (%{ S_ISFIFO(STAP_ARG_i_mode) %}) |
| 123 |
return "S_IFIFO" |
| 124 |
if (%{ S_ISSOCK(STAP_ARG_i_mode) %}) |
| 125 |
return "S_IFSOCK" |
| 126 |
} |
| 127 |
|
| 128 |
function task_file_handle_i_mode:long (task:long, fd:long) %{ /* pure */ |
| 129 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 130 |
struct files_struct *files; |
| 131 |
struct file *filp; |
| 132 |
struct dentry *dentry; |
| 133 |
struct inode *inode; |
| 134 |
|
| 135 |
rcu_read_lock(); |
| 136 |
if ((files = kread(&p->files)) && |
| 137 |
(filp = fcheck_files(files, STAP_ARG_fd)) && |
| 138 |
#ifdef STAPCONF_DPATH_PATH |
| 139 |
(dentry = kread(&filp->f_path.dentry)) && |
| 140 |
#else |
| 141 |
(dentry = kread(&filp->f_dentry)) && |
| 142 |
#endif |
| 143 |
(inode = kread(&dentry->d_inode))) { |
| 144 |
STAP_RETVALUE = kread(&inode->i_mode); |
| 145 |
} |
| 146 |
|
| 147 |
CATCH_DEREF_FAULT(); |
| 148 |
rcu_read_unlock(); |
| 149 |
%} |
| 150 |
|
| 151 |
function task_file_handle_majmin_dev:string (task:long, fd:long) %{ /* pure */ |
| 152 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 153 |
struct files_struct *files; |
| 154 |
struct file *filp; |
| 155 |
struct dentry *dentry; |
| 156 |
struct inode *inode; |
| 157 |
struct super_block *sb; |
| 158 |
int dev_nr; |
| 159 |
|
| 160 |
rcu_read_lock(); |
| 161 |
if ((files = kread(&p->files)) && |
| 162 |
(filp = fcheck_files(files, STAP_ARG_fd)) && |
| 163 |
#ifdef STAPCONF_DPATH_PATH |
| 164 |
(dentry = kread(&filp->f_path.dentry)) && |
| 165 |
#else |
| 166 |
(dentry = kread(&filp->f_dentry)) && |
| 167 |
#endif |
| 168 |
(inode = kread(&dentry->d_inode)) && |
| 169 |
(sb = kread(&inode->i_sb))) { |
| 170 |
dev_nr = kread(&sb->s_dev); |
| 171 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 172 |
"%d,%d", MAJOR(dev_nr), MINOR(dev_nr)); |
| 173 |
} |
| 174 |
|
| 175 |
CATCH_DEREF_FAULT(); |
| 176 |
rcu_read_unlock(); |
| 177 |
%} |
| 178 |
|
| 179 |
function task_file_handle_majmin_rdev:string (task:long, fd:long) %{ /* pure */ |
| 180 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 181 |
struct files_struct *files; |
| 182 |
struct file *filp; |
| 183 |
struct dentry *dentry; |
| 184 |
struct inode *inode; |
| 185 |
int rdev_nr; |
| 186 |
|
| 187 |
rcu_read_lock(); |
| 188 |
if ((files = kread(&p->files)) && |
| 189 |
(filp = fcheck_files(files, STAP_ARG_fd)) && |
| 190 |
#ifdef STAPCONF_DPATH_PATH |
| 191 |
(dentry = kread(&filp->f_path.dentry)) && |
| 192 |
#else |
| 193 |
(dentry = kread(&filp->f_dentry)) && |
| 194 |
#endif |
| 195 |
(inode = kread(&dentry->d_inode))) { |
| 196 |
rdev_nr = kread(&inode->i_rdev); |
| 197 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 198 |
"%d,%d", MAJOR(rdev_nr), MINOR(rdev_nr)); |
| 199 |
} |
| 200 |
|
| 201 |
CATCH_DEREF_FAULT(); |
| 202 |
rcu_read_unlock(); |
| 203 |
%} |
| 204 |
|
| 205 |
function task_file_handle_i_node:long (task:long, fd:long) %{ /* pure */ |
| 206 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 207 |
struct files_struct *files; |
| 208 |
struct file *filp; |
| 209 |
struct dentry *dentry; |
| 210 |
struct inode *inode; |
| 211 |
|
| 212 |
rcu_read_lock(); |
| 213 |
if ((files = kread(&p->files)) && |
| 214 |
(filp = fcheck_files(files, STAP_ARG_fd)) && |
| 215 |
#ifdef STAPCONF_DPATH_PATH |
| 216 |
(dentry = kread(&filp->f_path.dentry)) && |
| 217 |
#else |
| 218 |
(dentry = kread(&filp->f_dentry)) && |
| 219 |
#endif |
| 220 |
(inode = kread(&dentry->d_inode))) { |
| 221 |
STAP_RETVALUE = kread(&inode->i_ino); |
| 222 |
} |
| 223 |
|
| 224 |
CATCH_DEREF_FAULT(); |
| 225 |
rcu_read_unlock(); |
| 226 |
%} |
| 227 |
|
| 228 |
function task_file_handle_uid:long (task:long, fd:long) %{ /* pure */ |
| 229 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 230 |
struct files_struct *files; |
| 231 |
struct file *filp; |
| 232 |
#ifdef CONFIG_USER_NS |
| 233 |
struct user_namespace *ns = NULL; |
| 234 |
#endif |
| 235 |
|
| 236 |
rcu_read_lock(); |
| 237 |
if ((files = kread(&p->files)) && |
| 238 |
(filp = fcheck_files(files, STAP_ARG_fd))) { |
| 239 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29) |
| 240 |
/* git commit d76b0d9b */ |
| 241 |
const struct cred *cred; |
| 242 |
if ((cred = kread(&filp->f_cred))) { |
| 243 |
#ifdef CONFIG_USER_NS |
| 244 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) |
| 245 |
ns = p->nsproxy->user_ns; |
| 246 |
#else |
| 247 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39) |
| 248 |
ns = (task_cred_xxx(p, user))->user_ns; |
| 249 |
#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39) */ |
| 250 |
ns = task_cred_xxx(p, user_ns); |
| 251 |
#endif |
| 252 |
#endif |
| 253 |
if (ns) { |
| 254 |
get_user_ns(ns); |
| 255 |
// We call kderef_buffer() here to |
| 256 |
// ensure the memory at the kuid_t |
| 257 |
// location is valid to read. We can't |
| 258 |
// use kderef()/kread(), since they |
| 259 |
// only handle data with a size of 1, |
| 260 |
// 2, 4, or 8 bytes. |
| 261 |
kderef_buffer(NULL, &cred->fsuid, |
| 262 |
sizeof(cred->fsuid)); |
| 263 |
STAP_RETVALUE = from_kuid_munged(ns, |
| 264 |
cred->fsuid); |
| 265 |
} |
| 266 |
else |
| 267 |
STAP_RETVALUE = -1; |
| 268 |
|
| 269 |
#else /* ! CONFIG_USER_NS */ |
| 270 |
STAP_RETVALUE = kread(&cred->fsuid); |
| 271 |
#endif /* ! CONFIG_USER_NS */ |
| 272 |
} |
| 273 |
#else |
| 274 |
STAP_RETVALUE = kread(&filp->f_uid); |
| 275 |
#endif |
| 276 |
} |
| 277 |
|
| 278 |
CATCH_DEREF_FAULT(); |
| 279 |
#ifdef CONFIG_USER_NS |
| 280 |
if (ns) |
| 281 |
put_user_ns(ns); |
| 282 |
#endif |
| 283 |
rcu_read_unlock(); |
| 284 |
%} |
| 285 |
|
| 286 |
function task_file_handle_gid:long (task:long, fd:long) %{ /* pure */ |
| 287 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 288 |
struct files_struct *files; |
| 289 |
struct file *filp; |
| 290 |
#ifdef CONFIG_USER_NS |
| 291 |
struct user_namespace *ns = NULL; |
| 292 |
#endif |
| 293 |
|
| 294 |
rcu_read_lock(); |
| 295 |
if ((files = kread(&p->files)) && |
| 296 |
(filp = fcheck_files(files, STAP_ARG_fd))) { |
| 297 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29) |
| 298 |
/* git commit d76b0d9b */ |
| 299 |
const struct cred *cred; |
| 300 |
if ((cred = kread(&filp->f_cred))) { |
| 301 |
#ifdef CONFIG_USER_NS |
| 302 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) |
| 303 |
ns = p->nsproxy->user_ns; |
| 304 |
#else |
| 305 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39) |
| 306 |
ns = (task_cred_xxx(p, user))->user_ns; |
| 307 |
#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39) */ |
| 308 |
ns = task_cred_xxx(p, user_ns); |
| 309 |
#endif |
| 310 |
#endif |
| 311 |
if (ns) { |
| 312 |
get_user_ns(ns); |
| 313 |
// We call kderef_buffer() here to |
| 314 |
// ensure the memory at the kgid_t |
| 315 |
// location is valid to read. We can't |
| 316 |
// use kderef()/kread(), since they |
| 317 |
// only handle data with a size of 1, |
| 318 |
// 2, 4, or 8 bytes. |
| 319 |
kderef_buffer(NULL, &cred->fsgid, |
| 320 |
sizeof(cred->fsgid)); |
| 321 |
STAP_RETVALUE = from_kgid_munged(ns, |
| 322 |
cred->fsgid); |
| 323 |
} |
| 324 |
else |
| 325 |
STAP_RETVALUE = -1; |
| 326 |
|
| 327 |
#else /* ! CONFIG_USER_NS */ |
| 328 |
STAP_RETVALUE = kread(&cred->fsgid); |
| 329 |
#endif /* ! CONFIG_USER_NS */ |
| 330 |
} |
| 331 |
#else |
| 332 |
STAP_RETVALUE = kread(&filp->f_gid); |
| 333 |
#endif |
| 334 |
} |
| 335 |
|
| 336 |
CATCH_DEREF_FAULT(); |
| 337 |
#ifdef CONFIG_USER_NS |
| 338 |
if (ns) |
| 339 |
put_user_ns(ns); |
| 340 |
#endif |
| 341 |
rcu_read_unlock(); |
| 342 |
%} |
| 343 |
|
| 344 |
function task_file_handle_f_flags:long (task:long, fd:long) %{ /* pure */ |
| 345 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 346 |
struct files_struct *files; |
| 347 |
struct file *filp; |
| 348 |
|
| 349 |
rcu_read_lock(); |
| 350 |
if ((files = kread(&p->files)) && |
| 351 |
(filp = fcheck_files(files, STAP_ARG_fd))) { |
| 352 |
STAP_RETVALUE = kread(&filp->f_flags); |
| 353 |
} |
| 354 |
|
| 355 |
CATCH_DEREF_FAULT(); |
| 356 |
rcu_read_unlock(); |
| 357 |
%} |
| 358 |
|
| 359 |
function task_file_handle_fd_flags:string (task:long, fd:long) %{ /* pure */ |
| 360 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 361 |
struct files_struct *files; |
| 362 |
struct fdtable *fdt; |
| 363 |
int gcoe; |
| 364 |
|
| 365 |
rcu_read_lock(); |
| 366 |
if ((files = kread(&p->files)) && |
| 367 |
(fdt = files_fdtable(files))) { |
| 368 |
/* Use test_bit directly instead of FD_ISSET which might |
| 369 |
not be defined on some kernels. */ |
| 370 |
unsigned long *bs = (unsigned long *)kread(&fdt->close_on_exec); |
| 371 |
gcoe = test_bit(STAP_ARG_fd, bs); |
| 372 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 373 |
"%s", gcoe ? "FD_CLOEXEC" : ""); |
| 374 |
} |
| 375 |
|
| 376 |
CATCH_DEREF_FAULT(); |
| 377 |
rcu_read_unlock(); |
| 378 |
%} |
| 379 |
|
| 380 |
function task_file_handle_flock:string (task:long, fd:long) %{ /* pure */ |
| 381 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 382 |
struct files_struct *files; |
| 383 |
struct file_lock *flock; |
| 384 |
int fl_type = -1; |
| 385 |
int fl_pid = -1; |
| 386 |
struct file *filp; |
| 387 |
struct dentry *dentry; |
| 388 |
struct inode *inode; |
| 389 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,0,0) |
| 390 |
struct file_lock_context *ctx; |
| 391 |
int flc_lock_set = 0; |
| 392 |
#endif |
| 393 |
|
| 394 |
rcu_read_lock(); |
| 395 |
if ((files = kread(&p->files)) && |
| 396 |
(filp = fcheck_files(files, STAP_ARG_fd)) && |
| 397 |
#ifdef STAPCONF_DPATH_PATH |
| 398 |
(dentry = kread(&filp->f_path.dentry)) && |
| 399 |
#else |
| 400 |
(dentry = kread(&filp->f_dentry)) && |
| 401 |
#endif |
| 402 |
(inode = kread(&dentry->d_inode))) { |
| 403 |
// In kernels >= 4.0, we've now got a list of file_lock structures. |
| 404 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,0,0) |
| 405 |
if ((ctx = kread(&inode->i_flctx))) { |
| 406 |
struct file_lock *lock; |
| 407 |
|
| 408 |
// Make sure the file_lock_context struct is valid. |
| 409 |
kderef_buffer(NULL, ctx, |
| 410 |
sizeof(struct file_lock_context)); |
| 411 |
|
| 412 |
// We'll just look at the first lock. |
| 413 |
spin_lock(&ctx->flc_lock); |
| 414 |
flc_lock_set = 1; |
| 415 |
list_for_each_entry(lock, &ctx->flc_flock, fl_list) { |
| 416 |
fl_type = kread(&lock->fl_type); |
| 417 |
fl_pid = kread(&lock->fl_pid); |
| 418 |
break; |
| 419 |
} |
| 420 |
spin_unlock(&ctx->flc_lock); |
| 421 |
flc_lock_set = 0; |
| 422 |
} |
| 423 |
#else |
| 424 |
if ((flock = kread(&inode->i_flock))) { |
| 425 |
fl_type = kread(&flock->fl_type); |
| 426 |
fl_pid = kread(&flock->fl_pid); |
| 427 |
} |
| 428 |
#endif |
| 429 |
|
| 430 |
if (fl_type != -1) { /* !flock */ |
| 431 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 432 |
" advisory %s lock set by process %d", |
| 433 |
fl_type ? "write" : "read", fl_pid); |
| 434 |
} else { |
| 435 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, "NULL"); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
CATCH_DEREF_FAULT(); |
| 440 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,0,0) |
| 441 |
if (flc_lock_set) |
| 442 |
spin_unlock(&ctx->flc_lock); |
| 443 |
#endif |
| 444 |
rcu_read_unlock(); |
| 445 |
%} |
| 446 |
|
| 447 |
function task_file_handle_d_path:string (task:long, fd:long) %{ /* pure */ |
| 448 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 449 |
struct files_struct *files; |
| 450 |
char *page = NULL; |
| 451 |
struct file *filp; |
| 452 |
struct dentry *dentry; |
| 453 |
struct vfsmount *vfsmnt; |
| 454 |
char *path = NULL; |
| 455 |
|
| 456 |
rcu_read_lock(); |
| 457 |
if ((files = kread(&p->files)) && |
| 458 |
// We need GFP_ATOMIC since we're inside a lock so we |
| 459 |
// can't sleep. |
| 460 |
(page = (char *)__get_free_page(GFP_ATOMIC)) && |
| 461 |
(filp = fcheck_files(files, STAP_ARG_fd))) { |
| 462 |
|
| 463 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26) |
| 464 |
/* git commit 9d1bc601 */ |
| 465 |
path = d_path(&filp->f_path, page, PAGE_SIZE); |
| 466 |
#else |
| 467 |
dentry = kread(&filp->f_dentry); |
| 468 |
vfsmnt = kread(&filp->f_vfsmnt); |
| 469 |
|
| 470 |
if (dentry && vfsmnt) { |
| 471 |
path = d_path(dentry, vfsmnt, page, PAGE_SIZE); |
| 472 |
} |
| 473 |
#endif |
| 474 |
if (path && !IS_ERR(path)) { |
| 475 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, "%s", path); |
| 476 |
} |
| 477 |
} |
| 478 |
CATCH_DEREF_FAULT(); |
| 479 |
|
| 480 |
if (page) free_page((unsigned long)page); |
| 481 |
|
| 482 |
rcu_read_unlock(); |
| 483 |
%} |
| 484 |
|
| 485 |
function task_file_handle_socket:long (task:long, fd:long) %{ /* pure */ |
| 486 |
struct task_struct *p = (struct task_struct *)((long)STAP_ARG_task); |
| 487 |
struct files_struct *files; |
| 488 |
struct file *filp; |
| 489 |
struct dentry *dentry; |
| 490 |
struct inode *inode; |
| 491 |
|
| 492 |
rcu_read_lock(); |
| 493 |
if ((files = kread(&p->files)) && |
| 494 |
(filp = fcheck_files(files, STAP_ARG_fd)) && |
| 495 |
#ifdef STAPCONF_DPATH_PATH |
| 496 |
(dentry = kread(&filp->f_path.dentry)) && |
| 497 |
#else |
| 498 |
(dentry = kread(&filp->f_dentry)) && |
| 499 |
#endif |
| 500 |
(inode = kread(&dentry->d_inode))) { |
| 501 |
if (S_ISSOCK(kread(&inode->i_mode))) |
| 502 |
STAP_RETVALUE = (long)SOCKET_I(inode); |
| 503 |
} |
| 504 |
|
| 505 |
CATCH_DEREF_FAULT(); |
| 506 |
rcu_read_unlock(); |
| 507 |
%} |
| 508 |
|
| 509 |
function socket_userlocks:string (sock:long) %{ /* pure */ |
| 510 |
struct socket *sock = (struct socket *)((long)STAP_ARG_sock); |
| 511 |
struct sock *sk = (struct sock *)kread(&sock->sk); |
| 512 |
unsigned char locks = sk->sk_userlocks; |
| 513 |
int printed = 0; |
| 514 |
|
| 515 |
STAP_RETVALUE[0] = '\0'; |
| 516 |
|
| 517 |
if (locks & SOCK_RCVBUF_LOCK) |
| 518 |
printed = snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 519 |
"ulocks: rcv"); |
| 520 |
|
| 521 |
if (locks & SOCK_SNDBUF_LOCK) |
| 522 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 523 |
"ulocks: %ssnd", |
| 524 |
printed ? "rcv, " : ""); |
| 525 |
|
| 526 |
CATCH_DEREF_FAULT(); |
| 527 |
%} |
| 528 |
|
| 529 |
function socket_optname:string (sock:long) %{ /* pure */ |
| 530 |
struct socket *sock = (struct socket *)((long)STAP_ARG_sock); |
| 531 |
int optval; |
| 532 |
int optlen; |
| 533 |
int ret, a; |
| 534 |
char str[30]; |
| 535 |
struct linger ling; |
| 536 |
struct optname_item { |
| 537 |
int optfam; |
| 538 |
int optnum; |
| 539 |
const char *optname; |
| 540 |
int show_val; |
| 541 |
} const *p; |
| 542 |
static const struct optname_item optname_entries[] = { |
| 543 |
{ SOL_SOCKET, SO_DEBUG, "SO_DEBUG", 0 }, |
| 544 |
{ SOL_SOCKET, SO_REUSEADDR, "SO_REUSEADDR", 0 }, |
| 545 |
{ SOL_SOCKET, SO_DONTROUTE, "SO_DONTROUTE", 0 }, |
| 546 |
{ SOL_SOCKET, SO_BROADCAST, "SO_BROADCAST", 0 }, |
| 547 |
{ SOL_SOCKET, SO_KEEPALIVE, "SO_KEEPALIVE", 0 }, |
| 548 |
{ SOL_SOCKET, SO_OOBINLINE, "SO_OOBINLINE", 0 }, |
| 549 |
{ SOL_SOCKET, SO_PASSCRED, "SO_PASSCRED", 0 }, |
| 550 |
/* { SOL_SOCKET, SO_SNDLOWAT, "SO_SNDLOWAT", 0 },*/ |
| 551 |
{ SOL_SOCKET, SO_TYPE, "SO_TYPE", 1 }, |
| 552 |
{ SOL_SOCKET, SO_ERROR, "SO_ERROR", 1 }, |
| 553 |
{ SOL_SOCKET, SO_SNDBUF, "SO_SNDBUF", 1 }, |
| 554 |
{ SOL_SOCKET, SO_RCVBUF, "SO_RCVBUF", 1 }, |
| 555 |
{ SOL_SOCKET, SO_NO_CHECK, "SO_NO_CHECK", 1 }, |
| 556 |
{ SOL_TCP, TCP_NODELAY, "TCP_NODELAY", 0 }, |
| 557 |
{ 0, 0, NULL, 0 }, |
| 558 |
}; |
| 559 |
|
| 560 |
for (p = optname_entries; p->optname; ++p) { |
| 561 |
optlen = sizeof(optval); |
| 562 |
ret = kernel_getsockopt(sock, p->optfam, p->optnum, (char *)&optval, &optlen); |
| 563 |
if (ret == 0 && optval != 0) { |
| 564 |
if (!p->show_val) |
| 565 |
snprintf(str, MAXSTRINGLEN, "%s,", p->optname); |
| 566 |
else |
| 567 |
snprintf(str, MAXSTRINGLEN, "%s(%d),", p->optname, optval); |
| 568 |
strcat(STAP_RETVALUE, str); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
optlen = sizeof(ling); |
| 573 |
ret = kernel_getsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&ling, &optlen); |
| 574 |
if (ret == 0 && ling.l_onoff != 0) { |
| 575 |
snprintf(str, MAXSTRINGLEN, "SO_LINGER(%d),", ling.l_linger); |
| 576 |
strcat(STAP_RETVALUE, str); |
| 577 |
} |
| 578 |
|
| 579 |
STAP_RETVALUE[strlen(STAP_RETVALUE) - 1] = '\0'; |
| 580 |
%} |
| 581 |
|
| 582 |
function socket_family:long (sock:long) %{ /* pure */ |
| 583 |
struct socket *sock = (struct socket *)((long)STAP_ARG_sock); |
| 584 |
const struct proto_ops *ops = kread(&sock->ops); |
| 585 |
STAP_RETVALUE = (long)kread(&ops->family); |
| 586 |
CATCH_DEREF_FAULT(); |
| 587 |
%} |
| 588 |
|
| 589 |
function socket_unix_sockname:string (sock:long) %{ /* pure */ |
| 590 |
struct socket *sock = (struct socket *)((long)STAP_ARG_sock); |
| 591 |
const struct proto_ops *ops = kread(&sock->ops); |
| 592 |
struct sockaddr_un un_addr; |
| 593 |
int err, len; |
| 594 |
|
| 595 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0) |
| 596 |
err = ops->getname(sock, (struct sockaddr *)(&un_addr), 0); |
| 597 |
#else |
| 598 |
err = ops->getname(sock, (struct sockaddr *)(&un_addr), &len, 0); |
| 599 |
#endif |
| 600 |
// err = kernel_getsockname(kread(&sock), (struct sockaddr *)(&un_addr), &len); |
| 601 |
if (!(err < 0)) { |
| 602 |
if (kread(&un_addr.sun_path[0]) != 0) |
| 603 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 604 |
" sockname: AF_UNIX %s", un_addr.sun_path); |
| 605 |
else |
| 606 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, " sockname: AF_UNIX"); |
| 607 |
} |
| 608 |
CATCH_DEREF_FAULT(); |
| 609 |
%} |
| 610 |
|
| 611 |
function socket_unix_peername:string (sock:long) %{ /* pure */ |
| 612 |
struct socket *sock = (struct socket *)((long)STAP_ARG_sock); |
| 613 |
const struct proto_ops *ops = kread(&sock->ops); |
| 614 |
struct sockaddr_un un_addr; |
| 615 |
int err, len; |
| 616 |
|
| 617 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0) |
| 618 |
err = ops->getname(sock, (struct sockaddr *)(&un_addr), 1); |
| 619 |
#else |
| 620 |
err = ops->getname(sock, (struct sockaddr *)(&un_addr), &len, 1); |
| 621 |
#endif |
| 622 |
// err = kernel_getpeername(kread(&sock), (struct sockaddr *)(&un_addr), &len); |
| 623 |
if (!(err < 0)) { |
| 624 |
if (un_addr.sun_path[0] != 0) |
| 625 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 626 |
" peername: AF_UNIX %s", un_addr.sun_path); |
| 627 |
else |
| 628 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, " peername: AF_UNIX"); |
| 629 |
} |
| 630 |
CATCH_DEREF_FAULT(); |
| 631 |
%} |
| 632 |
|
| 633 |
%{ |
| 634 |
#ifndef NIPQUAD |
| 635 |
#define NIPQUAD(addr) \ |
| 636 |
((unsigned char *)&addr)[0], \ |
| 637 |
((unsigned char *)&addr)[1], \ |
| 638 |
((unsigned char *)&addr)[2], \ |
| 639 |
((unsigned char *)&addr)[3] |
| 640 |
#endif |
| 641 |
#ifndef NIPQUAD_FMT |
| 642 |
#define NIPQUAD_FMT "%u.%u.%u.%u" |
| 643 |
#endif |
| 644 |
%} |
| 645 |
|
| 646 |
function socket_ipv4_sockname:string (sock:long) %{ /* pure */ |
| 647 |
struct socket *sock = (struct socket *)((long)STAP_ARG_sock); |
| 648 |
const struct proto_ops *ops = kread(&sock->ops); |
| 649 |
struct sockaddr_in in_addr; |
| 650 |
__be32 addr, port; |
| 651 |
int err, len; |
| 652 |
|
| 653 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0) |
| 654 |
err = ops->getname (sock, (struct sockaddr*)(&in_addr), 0); |
| 655 |
#else |
| 656 |
err = ops->getname (sock, (struct sockaddr*)(&in_addr), &len, 0); |
| 657 |
#endif |
| 658 |
if (!(err < 0)) { |
| 659 |
addr = in_addr.sin_addr.s_addr; |
| 660 |
port = htons(in_addr.sin_port); |
| 661 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 662 |
" sockname: AF_INET " NIPQUAD_FMT " port: %d", |
| 663 |
NIPQUAD(addr), port); |
| 664 |
} |
| 665 |
CATCH_DEREF_FAULT(); |
| 666 |
%} |
| 667 |
|
| 668 |
function socket_ipv4_peername:string (sock:long) %{ /* pure */ |
| 669 |
struct socket *sock = (struct socket *)((long)STAP_ARG_sock); |
| 670 |
const struct proto_ops *ops = kread(&sock->ops); |
| 671 |
struct sockaddr_in in_addr; |
| 672 |
__be32 addr, port; |
| 673 |
int err, len; |
| 674 |
|
| 675 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0) |
| 676 |
err = ops->getname (sock, (struct sockaddr*)(&in_addr), 1); |
| 677 |
#else |
| 678 |
err = ops->getname (sock, (struct sockaddr*)(&in_addr), &len, 1); |
| 679 |
#endif |
| 680 |
if (!(err < 0)) { |
| 681 |
addr = in_addr.sin_addr.s_addr; |
| 682 |
port = htons(in_addr.sin_port); |
| 683 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 684 |
" peername: AF_INET " NIPQUAD_FMT " port: %d", |
| 685 |
NIPQUAD(addr), port); |
| 686 |
} |
| 687 |
CATCH_DEREF_FAULT(); |
| 688 |
%} |
| 689 |
|
| 690 |
%{ |
| 691 |
#ifndef NIP6_FMT |
| 692 |
#define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x" |
| 693 |
#endif |
| 694 |
#ifndef NIP6 |
| 695 |
#define NIP6(addr) \ |
| 696 |
ntohs((addr).s6_addr16[0]), \ |
| 697 |
ntohs((addr).s6_addr16[1]), \ |
| 698 |
ntohs((addr).s6_addr16[2]), \ |
| 699 |
ntohs((addr).s6_addr16[3]), \ |
| 700 |
ntohs((addr).s6_addr16[4]), \ |
| 701 |
ntohs((addr).s6_addr16[5]), \ |
| 702 |
ntohs((addr).s6_addr16[6]), \ |
| 703 |
ntohs((addr).s6_addr16[7]) |
| 704 |
#endif |
| 705 |
|
| 706 |
%} |
| 707 |
|
| 708 |
function socket_ipv6_sockname:string (sock:long) %{ /* pure */ |
| 709 |
struct socket *sock = (struct socket *)((long)STAP_ARG_sock); |
| 710 |
const struct proto_ops *ops = kread(&sock->ops); |
| 711 |
struct sockaddr_in6 in_addr; |
| 712 |
__be32 port; |
| 713 |
int err, len; |
| 714 |
|
| 715 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0) |
| 716 |
err = ops->getname (sock, (struct sockaddr*)(&in_addr), 0); |
| 717 |
#else |
| 718 |
err = ops->getname (sock, (struct sockaddr*)(&in_addr), &len, 0); |
| 719 |
#endif |
| 720 |
if (!(err < 0)) { |
| 721 |
port = htons(in_addr.sin6_port); |
| 722 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 723 |
" sockname: AF_INET6 " NIP6_FMT " port: %d", |
| 724 |
NIP6(in_addr.sin6_addr), port); |
| 725 |
} |
| 726 |
CATCH_DEREF_FAULT(); |
| 727 |
%} |
| 728 |
|
| 729 |
function socket_ipv6_peername:string (sock:long) %{ /* pure */ |
| 730 |
struct socket *sock = (struct socket *)((long)STAP_ARG_sock); |
| 731 |
const struct proto_ops *ops = kread(&sock->ops); |
| 732 |
struct sockaddr_in6 in_addr; |
| 733 |
__be32 port; |
| 734 |
int err, len; |
| 735 |
|
| 736 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0) |
| 737 |
err = ops->getname (sock, (struct sockaddr*)(&in_addr), 1); |
| 738 |
#else |
| 739 |
err = ops->getname (sock, (struct sockaddr*)(&in_addr), &len, 1); |
| 740 |
#endif |
| 741 |
if (!(err < 0)) { |
| 742 |
port = htons(in_addr.sin6_port); |
| 743 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, |
| 744 |
" peername: AF_INET6 " NIP6_FMT " port: %d", |
| 745 |
NIP6(in_addr.sin6_addr), port); |
| 746 |
} |
| 747 |
CATCH_DEREF_FAULT(); |
| 748 |
%} |
| 749 |
|
| 750 |
function print_file_handle_general(task, fd) { |
| 751 |
printf("%4d: %s mode:%04o dev:%s ino:%d uid:%d gid:%d rdev:%s\n", |
| 752 |
fd, i_mode2str(task_file_handle_i_mode(task, fd)), |
| 753 |
task_file_handle_i_mode(task, fd) & 0777, |
| 754 |
task_file_handle_majmin_dev(task, fd), |
| 755 |
task_file_handle_i_node(task, fd), |
| 756 |
task_file_handle_uid(task, fd), |
| 757 |
task_file_handle_gid(task, fd), |
| 758 |
task_file_handle_majmin_rdev(task, fd)); |
| 759 |
} |
| 760 |
|
| 761 |
function print_file_handle_flags(task, fd) { |
| 762 |
printf(" %s %s\n", |
| 763 |
_sys_open_flag_str(task_file_handle_f_flags(task, fd)), |
| 764 |
task_file_handle_fd_flags(task, fd)); |
| 765 |
} |
| 766 |
|
| 767 |
function print_file_handle_flock(task, fd) { |
| 768 |
flock = task_file_handle_flock(task, fd) |
| 769 |
if (!isinstr(flock, "NULL")) |
| 770 |
printf("%s\n", flock) |
| 771 |
} |
| 772 |
|
| 773 |
function print_file_handle_d_path(task, fd) { |
| 774 |
printf(" %s\n", task_file_handle_d_path(task, fd)) |
| 775 |
} |
| 776 |
|
| 777 |
function print_socket_userlocks(sock) { |
| 778 |
slocks = socket_userlocks(sock) |
| 779 |
if (strlen(slocks) > 0) |
| 780 |
printf(" %s\n", slocks) |
| 781 |
} |
| 782 |
|
| 783 |
function print_unix_socket(sock) { |
| 784 |
sockname = socket_unix_sockname(sock) |
| 785 |
peername = socket_unix_peername(sock) |
| 786 |
printf("%s%s", strlen(sockname) > 0 ? sockname . "\n" : "", |
| 787 |
strlen(peername) > 0 ? peername . "\n" : "") |
| 788 |
try { # skip line in case of null pointers |
| 789 |
printf(" peercred pid: %d\n", |
| 790 |
@defined(@cast(sock, "socket", "kernel")->sk->sk_peer_pid) ? |
| 791 |
@cast(sock, "socket", "kernel")->sk->sk_peer_pid->numbers[0]->nr : |
| 792 |
@cast(sock, "socket", "kernel")->sk->sk_peercred->pid ); } catch { } |
| 793 |
} |
| 794 |
|
| 795 |
function print_ipv4_socket(sock) { |
| 796 |
sockname = socket_ipv4_sockname(sock) |
| 797 |
peername = socket_ipv4_peername(sock) |
| 798 |
printf("%s%s", strlen(sockname) > 0 ? sockname . "\n" : "", |
| 799 |
strlen(peername) > 0 ? peername . "\n" : "") |
| 800 |
} |
| 801 |
|
| 802 |
function print_ipv6_socket(sock) { |
| 803 |
sockname = socket_ipv6_sockname(sock) |
| 804 |
peername = socket_ipv6_peername(sock) |
| 805 |
printf("%s%s", strlen(sockname) > 0 ? sockname . "\n" : "", |
| 806 |
strlen(peername) > 0 ? peername . "\n" : "") |
| 807 |
} |
| 808 |
|
| 809 |
function print_socket_optname(sock) { |
| 810 |
str = socket_optname(sock) |
| 811 |
printf(" %s", strlen(str) > 0 ? str . "\n" : "") |
| 812 |
} |
| 813 |
|
| 814 |
probe begin { |
| 815 |
%( $# < 1 |
| 816 |
%? pid = target() |
| 817 |
%: pid = $1 |
| 818 |
%) |
| 819 |
task = pid2task(pid) |
| 820 |
if (task == 0) error (sprintf("Process-id %d is invalid, please provide valid PID as $1 or -x PID", pid)) |
| 821 |
|
| 822 |
max_fds = task_max_file_handles(task) |
| 823 |
|
| 824 |
printf("%6d: %s\n", pid, pid2execname(pid)) |
| 825 |
printf(" Current rlimit: %d file descriptors\n", max_fds) |
| 826 |
|
| 827 |
for (fd = 0; fd < max_fds; fd++) { |
| 828 |
if (task_valid_file_handle(task, fd)) { |
| 829 |
print_file_handle_general(task, fd) |
| 830 |
print_file_handle_flags(task, fd) |
| 831 |
print_file_handle_flock(task, fd) |
| 832 |
print_file_handle_d_path(task, fd) |
| 833 |
sock = task_file_handle_socket(task, fd) |
| 834 |
if (sock) { |
| 835 |
print_socket_optname(sock) |
| 836 |
fam = socket_family(sock) |
| 837 |
if (fam == %{ AF_UNIX %}) { |
| 838 |
print_unix_socket(sock) |
| 839 |
} else if (fam == %{ AF_INET %}) { |
| 840 |
print_ipv4_socket(sock) |
| 841 |
} else if (fam == %{ AF_INET6 %}) { |
| 842 |
print_ipv6_socket(sock) |
| 843 |
} |
| 844 |
print_socket_userlocks(sock) |
| 845 |
} |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
exit() |
| 850 |
} |