2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
26 cc -I/System/Library/Frameworks/System.framework/Versions/B/PrivateHeaders -DPRIVATE -D__APPLE_PRIVATE -arch x86_64 -arch i386 -O -lutil -o fs_usage fs_usage.c
44 #include <sys/types.h>
45 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/socket.h>
50 #include <sys/sysctl.h>
54 #ifndef KERNEL_PRIVATE
55 #define KERNEL_PRIVATE
56 #include <sys/kdebug.h>
59 #include <sys/kdebug.h>
60 #endif /*KERNEL_PRIVATE*/
62 #import <mach/clock_types.h>
63 #import <mach/mach_time.h>
67 #define F_OPENFROM 56 /* SPI: open a file relative to fd (must be a dir) */
68 #define F_UNLINKFROM 57 /* SPI: open a file relative to fd (must be a dir) */
69 #define F_CHECK_OPENEVT 58 /* SPI: if a process is marked OPENEVT, or in O_EVTONLY on opens of this vnode */
80 #define RAW_VERSION0 0x55aa0000
81 #define RAW_VERSION1 0x55aa0101
87 typedef struct LibraryRange
{
92 LibraryRange framework32
;
93 LibraryRange framework64
;
105 char *frameworkType
[] = {
116 typedef struct LibraryInfo
{
123 LibraryInfo frameworkInfo
[MAXINDEX
];
124 int numFrameworks
= 0;
128 * MAXCOLS controls when extra data kicks in.
129 * MAX_WIDE_MODE_COLS controls -w mode to get even wider data in path.
130 * If NUMPARMS changes to match the kernel, it will automatically
131 * get reflected in the -w mode output.
134 #define PATHLENGTH (NUMPARMS*sizeof(uintptr_t))
137 #define MAX_WIDE_MODE_COLS (PATHLENGTH + 80)
138 #define MAXWIDTH MAX_WIDE_MODE_COLS + 64
140 #define MAX_PATHNAMES 3
141 #define MAX_SCALL_PATHNAMES 2
143 typedef struct th_info
*th_info_t
;
146 uintptr_t pathname
[NUMPARMS
+ 1]; /* add room for null terminator */
152 uintptr_t child_thread
;
173 struct lookup lookups
[MAX_PATHNAMES
];
177 typedef struct threadmap
* threadmap_t
;
183 unsigned int tm_setsize
; /* this is a bit count */
184 unsigned long *tm_setptr
; /* file descripter bitmap */
185 char tm_command
[MAXCOMLEN
+ 1];
189 typedef struct vnode_info
* vnode_info_t
;
192 vnode_info_t vn_next
;
194 uintptr_t vn_pathname
[NUMPARMS
+ 1];
197 typedef struct meta_info
* meta_info_t
;
205 #define HASH_SIZE 1024
206 #define HASH_MASK (HASH_SIZE - 1)
208 th_info_t th_info_hash
[HASH_SIZE
];
209 th_info_t th_info_freelist
;
211 threadmap_t threadmap_hash
[HASH_SIZE
];
212 threadmap_t threadmap_freelist
;
215 #define VN_HASH_SHIFT 3
216 #define VN_HASH_SIZE 16384
217 #define VN_HASH_MASK (VN_HASH_SIZE - 1)
219 vnode_info_t vn_info_hash
[VN_HASH_SIZE
];
220 meta_info_t m_info_hash
[VN_HASH_SIZE
];
223 int filemgr_in_progress
= 0;
224 int need_new_map
= 1;
230 int one_good_pid
= 0; /* Used to fail gracefully when bad pids given */
231 int select_pid_mode
= 0; /* Flag set indicates that output is restricted
232 to selected pids or commands */
239 #define USLEEP_BEHIND 2
240 #define USLEEP_MAX 32
241 int usleep_ms
= USLEEP_MIN
;
244 * Network only or filesystem only output filter
245 * Default of zero means report all activity - no filtering
247 #define FILESYS_FILTER 0x01
248 #define NETWORK_FILTER 0x02
249 #define EXEC_FILTER 0x08
250 #define PATHNAME_FILTER 0x10
251 #define DISKIO_FILTER 0x20
252 #define DEFAULT_DO_NOT_FILTER 0x00
254 int filter_mode
= DEFAULT_DO_NOT_FILTER
;
256 boolean_t show_cachehits
= FALSE
;
262 struct diskrec
*next
;
278 uintptr_t issuing_thread
;
279 uintptr_t completion_thread
;
280 char issuing_command
[MAXCOMLEN
];
282 double completed_time
;
286 struct diskrec
*disk_list
= NULL
;
287 struct diskio
*free_diskios
= NULL
;
288 struct diskio
*busy_diskios
= NULL
;
291 struct diskio
*insert_diskio();
292 struct diskio
*find_diskio(int);
293 struct diskio
*complete_diskio();
297 int check_filter_mode(struct th_info
*, int, int, int, char *);
298 void format_print(struct th_info
*, char *, uintptr_t, int, uintptr_t, uintptr_t, uintptr_t, uintptr_t, int, double, double, int, char *, struct diskio
*);
299 void enter_event_now(uintptr_t, int, kd_buf
*, char *, double);
300 void enter_event(uintptr_t, int, kd_buf
*, char *, double);
301 void exit_event(char *, uintptr_t, int, uintptr_t, uintptr_t, uintptr_t, uintptr_t, int, double);
302 void extend_syscall(uintptr_t, int, kd_buf
*);
304 char *generate_cs_disk_name(int, char *s
);
305 char *find_disk_name(int);
306 void cache_disk_names();
307 void recache_disk_names();
309 void lookup_name(uint64_t user_addr
, char **type
, char **name
);
310 int ReadSharedCacheMap(const char *, LibraryRange
*, char *);
311 void SortFrameworkAddresses();
313 void fs_usage_fd_set(uintptr_t, unsigned int);
314 int fs_usage_fd_isset(uintptr_t, unsigned int);
315 void fs_usage_fd_clear(uintptr_t, unsigned int);
317 void init_arguments_buffer();
318 int get_real_command_name(int, char *, int);
320 void delete_all_events();
321 void delete_event(th_info_t
);
322 th_info_t
add_event(uintptr_t, int);
323 th_info_t
find_event(uintptr_t, int);
324 void mark_thread_waited(uintptr_t);
326 void read_command_map();
327 void delete_all_map_entries();
328 void create_map_entry(uintptr_t, int, char *);
329 void delete_map_entry(uintptr_t);
330 threadmap_t
find_map_entry(uintptr_t);
332 char *add_vnode_name(uint64_t, char *);
333 char *find_vnode_name(uint64_t);
334 char *find_meta_name(uint64_t);
335 void add_meta_name(uint64_t, char *);
341 void set_pidexclude();
345 #define CLASS_MASK 0xff000000
346 #define CSC_MASK 0xffff0000
347 #define BSC_INDEX(type) ((type >> 2) & 0x3fff)
350 #define TRACE_DATA_NEWTHREAD 0x07000004
351 #define TRACE_DATA_EXEC 0x07000008
352 #define TRACE_STRING_NEWTHREAD 0x07010004
353 #define TRACE_STRING_EXEC 0x07010008
355 #define MACH_vmfault 0x01300008
356 #define MACH_pageout 0x01300004
357 #define MACH_sched 0x01400000
358 #define MACH_stkhandoff 0x01400008
359 #define MACH_idle 0x01400024
360 #define VFS_LOOKUP 0x03010090
361 #define VFS_ALIAS_VP 0x03010094
363 #define BSC_thread_terminate 0x040c05a4
365 #define HFS_update 0x3018000
366 #define HFS_modify_block_end 0x3018004
368 #define Throttled 0x3010184
369 #define SPEC_ioctl 0x3060000
370 #define SPEC_unmap_info 0x3060004
371 #define proc_exit 0x4010004
373 #define BC_IO_HIT 0x03070010
374 #define BC_IO_HIT_STALLED 0x03070020
375 #define BC_IO_MISS 0x03070040
376 #define BC_IO_MISS_CUT_THROUGH 0x03070080
377 #define BC_PLAYBACK_IO 0x03070100
378 #define BC_STR(s) ( \
379 (s == BC_IO_HIT) ? "HIT" : \
380 (s == BC_IO_HIT_STALLED) ? "STALL" : \
381 (s == BC_IO_MISS) ? "MISS" : \
382 (s == BC_IO_MISS_CUT_THROUGH) ? "CUT" : \
383 (s == BC_PLAYBACK_IO) ? "PLBK" : \
384 (s == 0x0) ? "NONE" : "UNKN" )
387 #define DKIO_NOCACHE 0x80
390 #define P_DISKIO_READ (DKIO_READ << 2)
391 #define P_DISKIO_ASYNC (DKIO_ASYNC << 2)
392 #define P_DISKIO_META (DKIO_META << 2)
393 #define P_DISKIO_PAGING (DKIO_PAGING << 2)
394 #define P_DISKIO_THROTTLE (DKIO_THROTTLE << 2)
395 #define P_DISKIO_PASSIVE (DKIO_PASSIVE << 2)
396 #define P_DISKIO_NOCACHE (DKIO_NOCACHE << 2)
397 #define P_DISKIO_TIER_MASK (DKIO_TIER_MASK << 2)
398 #define P_DISKIO_TIER_SHIFT (DKIO_TIER_SHIFT + 2)
400 #define P_DISKIO (FSDBG_CODE(DBG_DKRW, 0))
401 #define P_DISKIO_DONE (P_DISKIO | (DKIO_DONE << 2))
402 #define P_DISKIO_TYPE (P_DISKIO | P_DISKIO_READ | P_DISKIO_META | P_DISKIO_PAGING)
403 #define P_DISKIO_MASK (CSC_MASK | 0x4)
405 #define P_WrData (P_DISKIO)
406 #define P_RdData (P_DISKIO | P_DISKIO_READ)
407 #define P_WrMeta (P_DISKIO | P_DISKIO_META)
408 #define P_RdMeta (P_DISKIO | P_DISKIO_META | P_DISKIO_READ)
409 #define P_PgOut (P_DISKIO | P_DISKIO_PAGING)
410 #define P_PgIn (P_DISKIO | P_DISKIO_PAGING | P_DISKIO_READ)
412 #define P_CS_Class 0x0a000000 // DBG_CORESTORAGE
413 #define P_CS_Type_Mask 0xfffffff0
414 #define P_CS_IO_Done 0x00000004
416 #define P_CS_ReadChunk 0x0a000200 // chopped up request
417 #define P_CS_WriteChunk 0x0a000210
418 #define P_CS_MetaRead 0x0a000300 // meta data
419 #define P_CS_MetaWrite 0x0a000310
420 #define P_CS_TransformRead 0x0a000500 // background transform
421 #define P_CS_TransformWrite 0x0a000510
422 #define P_CS_MigrationRead 0x0a000600 // composite disk block migration
423 #define P_CS_MigrationWrite 0x0a000610
424 #define P_CS_SYNC_DISK 0x0a010000
426 #define MSC_map_fd 0x010c00ac
428 #define BSC_BASE 0x040C0000
429 #define MSC_BASE 0x010C0000
431 // Network related codes
432 #define BSC_recvmsg 0x040C006C
433 #define BSC_sendmsg 0x040C0070
434 #define BSC_recvfrom 0x040C0074
435 #define BSC_accept 0x040C0078
436 #define BSC_select 0x040C0174
437 #define BSC_socket 0x040C0184
438 #define BSC_connect 0x040C0188
439 #define BSC_bind 0x040C01A0
440 #define BSC_listen 0x040C01A8
441 #define BSC_sendto 0x040C0214
442 #define BSC_socketpair 0x040C021C
443 #define BSC_recvmsg_nocancel 0x040c0644
444 #define BSC_sendmsg_nocancel 0x040c0648
445 #define BSC_recvfrom_nocancel 0x040c064c
446 #define BSC_accept_nocancel 0x040c0650
447 #define BSC_connect_nocancel 0x040c0664
448 #define BSC_sendto_nocancel 0x040c0674
450 #define BSC_exit 0x040C0004
451 #define BSC_read 0x040C000C
452 #define BSC_write 0x040C0010
453 #define BSC_open 0x040C0014
454 #define BSC_close 0x040C0018
455 #define BSC_link 0x040C0024
456 #define BSC_unlink 0x040C0028
457 #define BSC_chdir 0x040c0030
458 #define BSC_fchdir 0x040c0034
459 #define BSC_mknod 0x040C0038
460 #define BSC_chmod 0x040C003C
461 #define BSC_chown 0x040C0040
462 #define BSC_getfsstat 0x040C0048
463 #define BSC_access 0x040C0084
464 #define BSC_chflags 0x040C0088
465 #define BSC_fchflags 0x040C008C
466 #define BSC_sync 0x040C0090
467 #define BSC_dup 0x040C00A4
468 #define BSC_ioctl 0x040C00D8
469 #define BSC_revoke 0x040C00E0
470 #define BSC_symlink 0x040C00E4
471 #define BSC_readlink 0x040C00E8
472 #define BSC_execve 0x040C00EC
473 #define BSC_umask 0x040C00F0
474 #define BSC_chroot 0x040C00F4
475 #define BSC_msync 0x040C0104
476 #define BSC_dup2 0x040C0168
477 #define BSC_fcntl 0x040C0170
478 #define BSC_fsync 0x040C017C
479 #define BSC_readv 0x040C01E0
480 #define BSC_writev 0x040C01E4
481 #define BSC_fchown 0x040C01EC
482 #define BSC_fchmod 0x040C01F0
483 #define BSC_rename 0x040C0200
484 #define BSC_flock 0x040C020C
485 #define BSC_mkfifo 0x040C0210
486 #define BSC_mkdir 0x040C0220
487 #define BSC_rmdir 0x040C0224
488 #define BSC_utimes 0x040C0228
489 #define BSC_futimes 0x040C022C
490 #define BSC_pread 0x040C0264
491 #define BSC_pwrite 0x040C0268
492 #define BSC_statfs 0x040C0274
493 #define BSC_fstatfs 0x040C0278
494 #define BSC_unmount 0x040C027C
495 #define BSC_mount 0x040C029C
496 #define BSC_fdatasync 0x040C02EC
497 #define BSC_stat 0x040C02F0
498 #define BSC_fstat 0x040C02F4
499 #define BSC_lstat 0x040C02F8
500 #define BSC_pathconf 0x040C02FC
501 #define BSC_fpathconf 0x040C0300
502 #define BSC_getdirentries 0x040C0310
503 #define BSC_mmap 0x040c0314
504 #define BSC_lseek 0x040c031c
505 #define BSC_truncate 0x040C0320
506 #define BSC_ftruncate 0x040C0324
507 #define BSC_undelete 0x040C0334
508 #define BSC_open_dprotected_np 0x040C0360
509 #define BSC_getattrlist 0x040C0370
510 #define BSC_setattrlist 0x040C0374
511 #define BSC_getdirentriesattr 0x040C0378
512 #define BSC_exchangedata 0x040C037C
513 #define BSC_checkuseraccess 0x040C0380
514 #define BSC_searchfs 0x040C0384
515 #define BSC_delete 0x040C0388
516 #define BSC_copyfile 0x040C038C
517 #define BSC_fgetattrlist 0x040C0390
518 #define BSC_fsetattrlist 0x040C0394
519 #define BSC_getxattr 0x040C03A8
520 #define BSC_fgetxattr 0x040C03AC
521 #define BSC_setxattr 0x040C03B0
522 #define BSC_fsetxattr 0x040C03B4
523 #define BSC_removexattr 0x040C03B8
524 #define BSC_fremovexattr 0x040C03BC
525 #define BSC_listxattr 0x040C03C0
526 #define BSC_flistxattr 0x040C03C4
527 #define BSC_fsctl 0x040C03C8
528 #define BSC_posix_spawn 0x040C03D0
529 #define BSC_ffsctl 0x040C03D4
530 #define BSC_open_extended 0x040C0454
531 #define BSC_umask_extended 0x040C0458
532 #define BSC_stat_extended 0x040C045C
533 #define BSC_lstat_extended 0x040C0460
534 #define BSC_fstat_extended 0x040C0464
535 #define BSC_chmod_extended 0x040C0468
536 #define BSC_fchmod_extended 0x040C046C
537 #define BSC_access_extended 0x040C0470
538 #define BSC_mkfifo_extended 0x040C048C
539 #define BSC_mkdir_extended 0x040C0490
540 #define BSC_aio_fsync 0x040C04E4
541 #define BSC_aio_return 0x040C04E8
542 #define BSC_aio_suspend 0x040C04EC
543 #define BSC_aio_cancel 0x040C04F0
544 #define BSC_aio_error 0x040C04F4
545 #define BSC_aio_read 0x040C04F8
546 #define BSC_aio_write 0x040C04FC
547 #define BSC_lio_listio 0x040C0500
548 #define BSC_sendfile 0x040C0544
549 #define BSC_stat64 0x040C0548
550 #define BSC_fstat64 0x040C054C
551 #define BSC_lstat64 0x040C0550
552 #define BSC_stat64_extended 0x040C0554
553 #define BSC_lstat64_extended 0x040C0558
554 #define BSC_fstat64_extended 0x040C055C
555 #define BSC_getdirentries64 0x040C0560
556 #define BSC_statfs64 0x040C0564
557 #define BSC_fstatfs64 0x040C0568
558 #define BSC_getfsstat64 0x040C056C
559 #define BSC_pthread_chdir 0x040C0570
560 #define BSC_pthread_fchdir 0x040C0574
561 #define BSC_lchown 0x040C05B0
563 #define BSC_read_nocancel 0x040c0630
564 #define BSC_write_nocancel 0x040c0634
565 #define BSC_open_nocancel 0x040c0638
566 #define BSC_close_nocancel 0x040c063c
567 #define BSC_msync_nocancel 0x040c0654
568 #define BSC_fcntl_nocancel 0x040c0658
569 #define BSC_select_nocancel 0x040c065c
570 #define BSC_fsync_nocancel 0x040c0660
571 #define BSC_readv_nocancel 0x040c066c
572 #define BSC_writev_nocancel 0x040c0670
573 #define BSC_pread_nocancel 0x040c0678
574 #define BSC_pwrite_nocancel 0x040c067c
575 #define BSC_aio_suspend_nocancel 0x40c0694
576 #define BSC_guarded_open_np 0x040c06e4
577 #define BSC_guarded_close_np 0x040c06e8
579 #define BSC_fsgetpath 0x040c06ac
581 #define BSC_msync_extended 0x040e0104
582 #define BSC_pread_extended 0x040e0264
583 #define BSC_pwrite_extended 0x040e0268
584 #define BSC_mmap_extended 0x040e0314
585 #define BSC_mmap_extended2 0x040f0314
587 // Carbon File Manager support
588 #define FILEMGR_PBGETCATALOGINFO 0x1e000020
589 #define FILEMGR_PBGETCATALOGINFOBULK 0x1e000024
590 #define FILEMGR_PBCREATEFILEUNICODE 0x1e000028
591 #define FILEMGR_PBCREATEDIRECTORYUNICODE 0x1e00002c
592 #define FILEMGR_PBCREATEFORK 0x1e000030
593 #define FILEMGR_PBDELETEFORK 0x1e000034
594 #define FILEMGR_PBITERATEFORK 0x1e000038
595 #define FILEMGR_PBOPENFORK 0x1e00003c
596 #define FILEMGR_PBREADFORK 0x1e000040
597 #define FILEMGR_PBWRITEFORK 0x1e000044
598 #define FILEMGR_PBALLOCATEFORK 0x1e000048
599 #define FILEMGR_PBDELETEOBJECT 0x1e00004c
600 #define FILEMGR_PBEXCHANGEOBJECT 0x1e000050
601 #define FILEMGR_PBGETFORKCBINFO 0x1e000054
602 #define FILEMGR_PBGETVOLUMEINFO 0x1e000058
603 #define FILEMGR_PBMAKEFSREF 0x1e00005c
604 #define FILEMGR_PBMAKEFSREFUNICODE 0x1e000060
605 #define FILEMGR_PBMOVEOBJECT 0x1e000064
606 #define FILEMGR_PBOPENITERATOR 0x1e000068
607 #define FILEMGR_PBRENAMEUNICODE 0x1e00006c
608 #define FILEMGR_PBSETCATALOGINFO 0x1e000070
609 #define FILEMGR_PBSETVOLUMEINFO 0x1e000074
610 #define FILEMGR_FSREFMAKEPATH 0x1e000078
611 #define FILEMGR_FSPATHMAKEREF 0x1e00007c
613 #define FILEMGR_PBGETCATINFO 0x1e010000
614 #define FILEMGR_PBGETCATINFOLITE 0x1e010004
615 #define FILEMGR_PBHGETFINFO 0x1e010008
616 #define FILEMGR_PBXGETVOLINFO 0x1e01000c
617 #define FILEMGR_PBHCREATE 0x1e010010
618 #define FILEMGR_PBHOPENDF 0x1e010014
619 #define FILEMGR_PBHOPENRF 0x1e010018
620 #define FILEMGR_PBHGETDIRACCESS 0x1e01001c
621 #define FILEMGR_PBHSETDIRACCESS 0x1e010020
622 #define FILEMGR_PBHMAPID 0x1e010024
623 #define FILEMGR_PBHMAPNAME 0x1e010028
624 #define FILEMGR_PBCLOSE 0x1e01002c
625 #define FILEMGR_PBFLUSHFILE 0x1e010030
626 #define FILEMGR_PBGETEOF 0x1e010034
627 #define FILEMGR_PBSETEOF 0x1e010038
628 #define FILEMGR_PBGETFPOS 0x1e01003c
629 #define FILEMGR_PBREAD 0x1e010040
630 #define FILEMGR_PBWRITE 0x1e010044
631 #define FILEMGR_PBGETFCBINFO 0x1e010048
632 #define FILEMGR_PBSETFINFO 0x1e01004c
633 #define FILEMGR_PBALLOCATE 0x1e010050
634 #define FILEMGR_PBALLOCCONTIG 0x1e010054
635 #define FILEMGR_PBSETFPOS 0x1e010058
636 #define FILEMGR_PBSETCATINFO 0x1e01005c
637 #define FILEMGR_PBGETVOLPARMS 0x1e010060
638 #define FILEMGR_PBSETVINFO 0x1e010064
639 #define FILEMGR_PBMAKEFSSPEC 0x1e010068
640 #define FILEMGR_PBHGETVINFO 0x1e01006c
641 #define FILEMGR_PBCREATEFILEIDREF 0x1e010070
642 #define FILEMGR_PBDELETEFILEIDREF 0x1e010074
643 #define FILEMGR_PBRESOLVEFILEIDREF 0x1e010078
644 #define FILEMGR_PBFLUSHVOL 0x1e01007c
645 #define FILEMGR_PBHRENAME 0x1e010080
646 #define FILEMGR_PBCATMOVE 0x1e010084
647 #define FILEMGR_PBEXCHANGEFILES 0x1e010088
648 #define FILEMGR_PBHDELETE 0x1e01008c
649 #define FILEMGR_PBDIRCREATE 0x1e010090
650 #define FILEMGR_PBCATSEARCH 0x1e010094
651 #define FILEMGR_PBHSETFLOCK 0x1e010098
652 #define FILEMGR_PBHRSTFLOCK 0x1e01009c
653 #define FILEMGR_PBLOCKRANGE 0x1e0100a0
654 #define FILEMGR_PBUNLOCKRANGE 0x1e0100a4
657 #define FILEMGR_CLASS 0x1e
658 #define FILEMGR_BASE 0x1e000000
660 #define FMT_DEFAULT 0
667 #define FMT_CACHEHIT 7
671 #define FMT_FTRUNC 11
673 #define FMT_SELECT 13
675 #define FMT_AIO_FSYNC 15
676 #define FMT_AIO_RETURN 16
677 #define FMT_AIO_SUSPEND 17
678 #define FMT_AIO_CANCEL 18
680 #define FMT_LIO_LISTIO 20
683 #define FMT_ACCESS 23
685 #define FMT_FCHMOD 25
686 #define FMT_CHMOD_EXT 26
687 #define FMT_FCHMOD_EXT 27
688 #define FMT_CHFLAGS 28
689 #define FMT_FCHFLAGS 29
693 #define FMT_SENDFILE 33
694 #define FMT_IOCTL_SYNC 34
696 #define FMT_UNMOUNT 36
697 #define FMT_DISKIO_CS 37
698 #define FMT_SYNC_DISK_CS 38
699 #define FMT_IOCTL_UNMAP 39
700 #define FMT_UNMAP_INFO 40
701 #define FMT_HFS_update 41
704 #define MAX_BSD_SYSCALL 512
709 } bsd_syscalls
[MAX_BSD_SYSCALL
];
712 int bsd_syscall_types
[] = {
714 BSC_recvmsg_nocancel
,
716 BSC_sendmsg_nocancel
,
718 BSC_recvfrom_nocancel
,
725 BSC_connect_nocancel
,
795 BSC_open_dprotected_np
,
800 BSC_getdirentriesattr
,
829 BSC_aio_suspend_nocancel
,
846 BSC_lstat64_extended
,
847 BSC_fstat64_extended
,
856 BSC_guarded_close_np
,
862 #define MAX_FILEMGR 512
864 struct filemgr_call
{
866 } filemgr_calls
[MAX_FILEMGR
];
869 int filemgr_call_types
[] = {
870 FILEMGR_PBGETCATALOGINFO
,
871 FILEMGR_PBGETCATALOGINFOBULK
,
872 FILEMGR_PBCREATEFILEUNICODE
,
873 FILEMGR_PBCREATEDIRECTORYUNICODE
,
874 FILEMGR_PBCREATEFORK
,
875 FILEMGR_PBDELETEFORK
,
876 FILEMGR_PBITERATEFORK
,
880 FILEMGR_PBALLOCATEFORK
,
881 FILEMGR_PBDELETEOBJECT
,
882 FILEMGR_PBEXCHANGEOBJECT
,
883 FILEMGR_PBGETFORKCBINFO
,
884 FILEMGR_PBGETVOLUMEINFO
,
886 FILEMGR_PBMAKEFSREFUNICODE
,
887 FILEMGR_PBMOVEOBJECT
,
888 FILEMGR_PBOPENITERATOR
,
889 FILEMGR_PBRENAMEUNICODE
,
890 FILEMGR_PBSETCATALOGINFO
,
891 FILEMGR_PBSETVOLUMEINFO
,
892 FILEMGR_FSREFMAKEPATH
,
893 FILEMGR_FSPATHMAKEREF
,
895 FILEMGR_PBGETCATINFO
,
896 FILEMGR_PBGETCATINFOLITE
,
898 FILEMGR_PBXGETVOLINFO
,
902 FILEMGR_PBHGETDIRACCESS
,
903 FILEMGR_PBHSETDIRACCESS
,
913 FILEMGR_PBGETFCBINFO
,
916 FILEMGR_PBALLOCCONTIG
,
918 FILEMGR_PBSETCATINFO
,
919 FILEMGR_PBGETVOLPARMS
,
921 FILEMGR_PBMAKEFSSPEC
,
923 FILEMGR_PBCREATEFILEIDREF
,
924 FILEMGR_PBDELETEFILEIDREF
,
925 FILEMGR_PBRESOLVEFILEIDREF
,
929 FILEMGR_PBEXCHANGEFILES
,
936 FILEMGR_PBUNLOCKRANGE
,
946 int exclude_pids
= 0;
947 int exclude_default_pids
= 1;
950 struct kinfo_proc
*kp_buffer
= 0;
953 #define EVENT_BASE 60000
955 int num_events
= EVENT_BASE
;
958 #define DBG_FUNC_ALL (DBG_FUNC_START | DBG_FUNC_END)
959 #define DBG_FUNC_MASK 0xfffffffc
961 double divisor
= 0.0; /* Trace divisor converts to microseconds */
967 kbufinfo_t bufinfo
= {0, 0, 0, 0, 0};
970 /* defines for tracking file descriptor state */
971 #define FS_USAGE_FD_SETSIZE 256 /* Initial number of file descriptors per
972 thread that we will track */
974 #define FS_USAGE_NFDBITS (sizeof (unsigned long) * 8)
975 #define FS_USAGE_NFDBYTES(n) (((n) / FS_USAGE_NFDBITS) * sizeof (unsigned long))
977 int trace_enabled
= 0;
978 int set_remove_flag
= 1;
982 char *RAW_file
= (char *)0;
986 uint64_t sample_TOD_secs
;
987 uint32_t sample_TOD_usecs
;
989 double bias_now
= 0.0;
990 double start_time
= 0.0;
991 double end_time
= 999999999999.9;
1005 void leave() /* exit under normal conditions -- INT handler */
1009 void set_pidcheck();
1010 void set_pidexclude();
1017 if (exclude_pids
== 0) {
1018 for (i
= 0; i
< num_of_pids
; i
++)
1019 set_pidcheck(pids
[i
], 0);
1022 for (i
= 0; i
< num_of_pids
; i
++)
1023 set_pidexclude(pids
[i
], 0);
1039 * This flag is turned off when calling
1040 * quit() due to a set_remove() failure.
1042 if (set_remove_flag
)
1045 fprintf(stderr
, "fs_usage: ");
1047 fprintf(stderr
, "%s", s
);
1053 void get_screenwidth()
1055 struct winsize size
;
1060 if (ioctl(1, TIOCGWINSZ
, &size
) != -1) {
1061 columns
= size
.ws_col
;
1063 if (columns
> MAXWIDTH
)
1079 struct mach_timebase_info mti
;
1081 mach_timebase_info(&mti
);
1083 divisor
= ((double)mti
.denom
/ (double)mti
.numer
) * 1000;
1088 exit_usage(char *myname
) {
1090 fprintf(stderr
, "Usage: %s [-e] [-w] [-f mode] [-b] [-t seconds] [-R rawfile [-S start_time] [-E end_time]] [pid | cmd [pid | cmd] ...]\n", myname
);
1091 fprintf(stderr
, " -e exclude the specified list of pids from the sample\n");
1092 fprintf(stderr
, " and exclude fs_usage by default\n");
1093 fprintf(stderr
, " -w force wider, detailed, output\n");
1094 fprintf(stderr
, " -f output is based on the mode provided\n");
1095 fprintf(stderr
, " mode = \"network\" Show network-related events\n");
1096 fprintf(stderr
, " mode = \"filesys\" Show filesystem-related events\n");
1097 fprintf(stderr
, " mode = \"pathname\" Show only pathname-related events\n");
1098 fprintf(stderr
, " mode = \"exec\" Show only exec and spawn events\n");
1099 fprintf(stderr
, " mode = \"diskio\" Show only disk I/O events\n");
1100 fprintf(stderr
, " mode = \"cachehit\" In addition, show cache hits\n");
1101 fprintf(stderr
, " -b annotate disk I/O events with BootCache info (if available)\n");
1102 fprintf(stderr
, " -t specifies timeout in seconds (for use in automated tools)\n");
1103 fprintf(stderr
, " -R specifies a raw trace file to process\n");
1104 fprintf(stderr
, " -S if -R is specified, selects a start point in microseconds\n");
1105 fprintf(stderr
, " -E if -R is specified, selects an end point in microseconds\n");
1106 fprintf(stderr
, " pid selects process(s) to sample\n");
1107 fprintf(stderr
, " cmd selects process(s) matching command string to sample\n");
1108 fprintf(stderr
, "\n%s will handle a maximum list of %d pids.\n\n", myname
, MAX_PIDS
);
1109 fprintf(stderr
, "By default (no options) the following processes are excluded from the output:\n");
1110 fprintf(stderr
, "fs_usage, Terminal, telnetd, sshd, rlogind, tcsh, csh, sh\n\n");
1116 int filemgr_index(type
) {
1119 return (((type
>> 2) & 0x3fff) + 256);
1121 return (((type
>> 2) & 0x3fff));
1125 void init_tables(void)
1131 for (i
= 0; i
< MAX_BSD_SYSCALL
; i
++) {
1132 bsd_syscalls
[i
].sc_name
= NULL
;
1133 bsd_syscalls
[i
].sc_format
= FMT_DEFAULT
;
1136 for (i
= 0; i
< MAX_FILEMGR
; i
++) {
1137 filemgr_calls
[i
].fm_name
= NULL
;
1140 for (i
= 0; (type
= bsd_syscall_types
[i
]); i
++) {
1142 code
= BSC_INDEX(type
);
1144 if (code
>= MAX_BSD_SYSCALL
) {
1145 printf("BSD syscall init (%x): type exceeds table size\n", type
);
1151 bsd_syscalls
[code
].sc_name
= "sendfile";
1152 bsd_syscalls
[code
].sc_format
= FMT_FD
; /* this should be changed to FMT_SENDFILE */
1153 break; /* once we add an extended info trace event */
1156 case BSC_recvmsg_nocancel
:
1157 bsd_syscalls
[code
].sc_name
= "recvmsg";
1158 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1162 case BSC_sendmsg_nocancel
:
1163 bsd_syscalls
[code
].sc_name
= "sendmsg";
1164 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1168 case BSC_recvfrom_nocancel
:
1169 bsd_syscalls
[code
].sc_name
= "recvfrom";
1170 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1174 case BSC_sendto_nocancel
:
1175 bsd_syscalls
[code
].sc_name
= "sendto";
1176 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1180 case BSC_select_nocancel
:
1181 bsd_syscalls
[code
].sc_name
= "select";
1182 bsd_syscalls
[code
].sc_format
= FMT_SELECT
;
1186 case BSC_accept_nocancel
:
1187 bsd_syscalls
[code
].sc_name
= "accept";
1188 bsd_syscalls
[code
].sc_format
= FMT_FD_2
;
1192 bsd_syscalls
[code
].sc_name
= "socket";
1193 bsd_syscalls
[code
].sc_format
= FMT_SOCKET
;
1197 case BSC_connect_nocancel
:
1198 bsd_syscalls
[code
].sc_name
= "connect";
1199 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1203 bsd_syscalls
[code
].sc_name
= "bind";
1204 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1208 bsd_syscalls
[code
].sc_name
= "listen";
1209 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1213 bsd_syscalls
[code
].sc_name
= "mmap";
1214 bsd_syscalls
[code
].sc_format
= FMT_MMAP
;
1217 case BSC_socketpair
:
1218 bsd_syscalls
[code
].sc_name
= "socketpair";
1222 bsd_syscalls
[code
].sc_name
= "getxattr";
1226 bsd_syscalls
[code
].sc_name
= "setxattr";
1229 case BSC_removexattr
:
1230 bsd_syscalls
[code
].sc_name
= "removexattr";
1234 bsd_syscalls
[code
].sc_name
= "listxattr";
1238 bsd_syscalls
[code
].sc_name
= "stat";
1242 bsd_syscalls
[code
].sc_name
= "stat64";
1245 case BSC_stat_extended
:
1246 bsd_syscalls
[code
].sc_name
= "stat_extended";
1249 case BSC_stat64_extended
:
1250 bsd_syscalls
[code
].sc_name
= "stat_extended64";
1254 bsd_syscalls
[code
].sc_name
= "mount";
1255 bsd_syscalls
[code
].sc_format
= FMT_MOUNT
;
1259 bsd_syscalls
[code
].sc_name
= "unmount";
1260 bsd_syscalls
[code
].sc_format
= FMT_UNMOUNT
;
1264 bsd_syscalls
[code
].sc_name
= "exit";
1268 bsd_syscalls
[code
].sc_name
= "execve";
1271 case BSC_posix_spawn
:
1272 bsd_syscalls
[code
].sc_name
= "posix_spawn";
1276 case BSC_open_nocancel
:
1277 bsd_syscalls
[code
].sc_name
= "open";
1278 bsd_syscalls
[code
].sc_format
= FMT_OPEN
;
1281 case BSC_open_extended
:
1282 bsd_syscalls
[code
].sc_name
= "open_extended";
1283 bsd_syscalls
[code
].sc_format
= FMT_OPEN
;
1286 case BSC_guarded_open_np
:
1287 bsd_syscalls
[code
].sc_name
= "guarded_open_np";
1288 bsd_syscalls
[code
].sc_format
= FMT_OPEN
;
1291 case BSC_open_dprotected_np
:
1292 bsd_syscalls
[code
].sc_name
= "open_dprotected";
1293 bsd_syscalls
[code
].sc_format
= FMT_OPEN
;
1297 bsd_syscalls
[code
].sc_name
= "dup";
1298 bsd_syscalls
[code
].sc_format
= FMT_FD_2
;
1302 bsd_syscalls
[code
].sc_name
= "dup2";
1303 bsd_syscalls
[code
].sc_format
= FMT_FD_2
;
1307 case BSC_close_nocancel
:
1308 bsd_syscalls
[code
].sc_name
= "close";
1309 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1312 case BSC_guarded_close_np
:
1313 bsd_syscalls
[code
].sc_name
= "guarded_close_np";
1314 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1318 case BSC_read_nocancel
:
1319 bsd_syscalls
[code
].sc_name
= "read";
1320 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1324 case BSC_write_nocancel
:
1325 bsd_syscalls
[code
].sc_name
= "write";
1326 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1330 bsd_syscalls
[code
].sc_name
= "fgetxattr";
1331 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1335 bsd_syscalls
[code
].sc_name
= "fsetxattr";
1336 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1339 case BSC_fremovexattr
:
1340 bsd_syscalls
[code
].sc_name
= "fremovexattr";
1341 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1344 case BSC_flistxattr
:
1345 bsd_syscalls
[code
].sc_name
= "flistxattr";
1346 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1350 bsd_syscalls
[code
].sc_name
= "fstat";
1351 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1355 bsd_syscalls
[code
].sc_name
= "fstat64";
1356 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1359 case BSC_fstat_extended
:
1360 bsd_syscalls
[code
].sc_name
= "fstat_extended";
1361 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1364 case BSC_fstat64_extended
:
1365 bsd_syscalls
[code
].sc_name
= "fstat64_extended";
1366 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1370 bsd_syscalls
[code
].sc_name
= "lstat";
1374 bsd_syscalls
[code
].sc_name
= "lstat64";
1377 case BSC_lstat_extended
:
1378 bsd_syscalls
[code
].sc_name
= "lstat_extended";
1381 case BSC_lstat64_extended
:
1382 bsd_syscalls
[code
].sc_name
= "lstat_extended64";
1386 bsd_syscalls
[code
].sc_name
= "link";
1390 bsd_syscalls
[code
].sc_name
= "unlink";
1394 bsd_syscalls
[code
].sc_name
= "mknod";
1398 bsd_syscalls
[code
].sc_name
= "umask";
1399 bsd_syscalls
[code
].sc_format
= FMT_UMASK
;
1402 case BSC_umask_extended
:
1403 bsd_syscalls
[code
].sc_name
= "umask_extended";
1404 bsd_syscalls
[code
].sc_format
= FMT_UMASK
;
1408 bsd_syscalls
[code
].sc_name
= "chmod";
1409 bsd_syscalls
[code
].sc_format
= FMT_CHMOD
;
1412 case BSC_chmod_extended
:
1413 bsd_syscalls
[code
].sc_name
= "chmod_extended";
1414 bsd_syscalls
[code
].sc_format
= FMT_CHMOD_EXT
;
1418 bsd_syscalls
[code
].sc_name
= "fchmod";
1419 bsd_syscalls
[code
].sc_format
= FMT_FCHMOD
;
1422 case BSC_fchmod_extended
:
1423 bsd_syscalls
[code
].sc_name
= "fchmod_extended";
1424 bsd_syscalls
[code
].sc_format
= FMT_FCHMOD_EXT
;
1428 bsd_syscalls
[code
].sc_name
= "chown";
1432 bsd_syscalls
[code
].sc_name
= "lchown";
1436 bsd_syscalls
[code
].sc_name
= "fchown";
1437 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1441 bsd_syscalls
[code
].sc_name
= "access";
1442 bsd_syscalls
[code
].sc_format
= FMT_ACCESS
;
1445 case BSC_access_extended
:
1446 bsd_syscalls
[code
].sc_name
= "access_extended";
1450 bsd_syscalls
[code
].sc_name
= "chdir";
1453 case BSC_pthread_chdir
:
1454 bsd_syscalls
[code
].sc_name
= "pthread_chdir";
1458 bsd_syscalls
[code
].sc_name
= "chroot";
1462 bsd_syscalls
[code
].sc_name
= "utimes";
1466 bsd_syscalls
[code
].sc_name
= "delete-Carbon";
1470 bsd_syscalls
[code
].sc_name
= "undelete";
1474 bsd_syscalls
[code
].sc_name
= "revoke";
1478 bsd_syscalls
[code
].sc_name
= "fsctl";
1482 bsd_syscalls
[code
].sc_name
= "ffsctl";
1483 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1487 bsd_syscalls
[code
].sc_name
= "chflags";
1488 bsd_syscalls
[code
].sc_format
= FMT_CHFLAGS
;
1492 bsd_syscalls
[code
].sc_name
= "fchflags";
1493 bsd_syscalls
[code
].sc_format
= FMT_FCHFLAGS
;
1497 bsd_syscalls
[code
].sc_name
= "fchdir";
1498 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1501 case BSC_pthread_fchdir
:
1502 bsd_syscalls
[code
].sc_name
= "pthread_fchdir";
1503 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1507 bsd_syscalls
[code
].sc_name
= "futimes";
1508 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1512 bsd_syscalls
[code
].sc_name
= "sync";
1516 bsd_syscalls
[code
].sc_name
= "symlink";
1520 bsd_syscalls
[code
].sc_name
= "readlink";
1524 case BSC_fsync_nocancel
:
1525 bsd_syscalls
[code
].sc_name
= "fsync";
1526 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1530 bsd_syscalls
[code
].sc_name
= "fdatasync";
1531 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1535 case BSC_readv_nocancel
:
1536 bsd_syscalls
[code
].sc_name
= "readv";
1537 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1541 case BSC_writev_nocancel
:
1542 bsd_syscalls
[code
].sc_name
= "writev";
1543 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1547 case BSC_pread_nocancel
:
1548 bsd_syscalls
[code
].sc_name
= "pread";
1549 bsd_syscalls
[code
].sc_format
= FMT_PREAD
;
1553 case BSC_pwrite_nocancel
:
1554 bsd_syscalls
[code
].sc_name
= "pwrite";
1555 bsd_syscalls
[code
].sc_format
= FMT_PREAD
;
1559 bsd_syscalls
[code
].sc_name
= "mkdir";
1562 case BSC_mkdir_extended
:
1563 bsd_syscalls
[code
].sc_name
= "mkdir_extended";
1567 bsd_syscalls
[code
].sc_name
= "mkfifo";
1570 case BSC_mkfifo_extended
:
1571 bsd_syscalls
[code
].sc_name
= "mkfifo_extended";
1575 bsd_syscalls
[code
].sc_name
= "rmdir";
1579 bsd_syscalls
[code
].sc_name
= "statfs";
1583 bsd_syscalls
[code
].sc_name
= "statfs64";
1587 bsd_syscalls
[code
].sc_name
= "getfsstat";
1590 case BSC_getfsstat64
:
1591 bsd_syscalls
[code
].sc_name
= "getfsstat64";
1595 bsd_syscalls
[code
].sc_name
= "fstatfs";
1596 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1600 bsd_syscalls
[code
].sc_name
= "fstatfs64";
1601 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1605 bsd_syscalls
[code
].sc_name
= "pathconf";
1609 bsd_syscalls
[code
].sc_name
= "fpathconf";
1610 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1613 case BSC_getdirentries
:
1614 bsd_syscalls
[code
].sc_name
= "getdirentries";
1615 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1618 case BSC_getdirentries64
:
1619 bsd_syscalls
[code
].sc_name
= "getdirentries64";
1620 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1624 bsd_syscalls
[code
].sc_name
= "lseek";
1625 bsd_syscalls
[code
].sc_format
= FMT_LSEEK
;
1629 bsd_syscalls
[code
].sc_name
= "truncate";
1630 bsd_syscalls
[code
].sc_format
= FMT_TRUNC
;
1634 bsd_syscalls
[code
].sc_name
= "ftruncate";
1635 bsd_syscalls
[code
].sc_format
= FMT_FTRUNC
;
1639 bsd_syscalls
[code
].sc_name
= "flock";
1640 bsd_syscalls
[code
].sc_format
= FMT_FLOCK
;
1643 case BSC_getattrlist
:
1644 bsd_syscalls
[code
].sc_name
= "getattrlist";
1647 case BSC_setattrlist
:
1648 bsd_syscalls
[code
].sc_name
= "setattrlist";
1651 case BSC_fgetattrlist
:
1652 bsd_syscalls
[code
].sc_name
= "fgetattrlist";
1653 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1656 case BSC_fsetattrlist
:
1657 bsd_syscalls
[code
].sc_name
= "fsetattrlist";
1658 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1661 case BSC_getdirentriesattr
:
1662 bsd_syscalls
[code
].sc_name
= "getdirentriesattr";
1663 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1666 case BSC_exchangedata
:
1667 bsd_syscalls
[code
].sc_name
= "exchangedata";
1671 bsd_syscalls
[code
].sc_name
= "rename";
1675 bsd_syscalls
[code
].sc_name
= "copyfile";
1678 case BSC_checkuseraccess
:
1679 bsd_syscalls
[code
].sc_name
= "checkuseraccess";
1683 bsd_syscalls
[code
].sc_name
= "searchfs";
1687 bsd_syscalls
[code
].sc_name
= "aio_fsync";
1688 bsd_syscalls
[code
].sc_format
= FMT_AIO_FSYNC
;
1691 case BSC_aio_return
:
1692 bsd_syscalls
[code
].sc_name
= "aio_return";
1693 bsd_syscalls
[code
].sc_format
= FMT_AIO_RETURN
;
1696 case BSC_aio_suspend
:
1697 case BSC_aio_suspend_nocancel
:
1698 bsd_syscalls
[code
].sc_name
= "aio_suspend";
1699 bsd_syscalls
[code
].sc_format
= FMT_AIO_SUSPEND
;
1702 case BSC_aio_cancel
:
1703 bsd_syscalls
[code
].sc_name
= "aio_cancel";
1704 bsd_syscalls
[code
].sc_format
= FMT_AIO_CANCEL
;
1708 bsd_syscalls
[code
].sc_name
= "aio_error";
1709 bsd_syscalls
[code
].sc_format
= FMT_AIO
;
1713 bsd_syscalls
[code
].sc_name
= "aio_read";
1714 bsd_syscalls
[code
].sc_format
= FMT_AIO
;
1718 bsd_syscalls
[code
].sc_name
= "aio_write";
1719 bsd_syscalls
[code
].sc_format
= FMT_AIO
;
1722 case BSC_lio_listio
:
1723 bsd_syscalls
[code
].sc_name
= "lio_listio";
1724 bsd_syscalls
[code
].sc_format
= FMT_LIO_LISTIO
;
1728 case BSC_msync_nocancel
:
1729 bsd_syscalls
[code
].sc_name
= "msync";
1730 bsd_syscalls
[code
].sc_format
= FMT_MSYNC
;
1734 case BSC_fcntl_nocancel
:
1735 bsd_syscalls
[code
].sc_name
= "fcntl";
1736 bsd_syscalls
[code
].sc_format
= FMT_FCNTL
;
1740 bsd_syscalls
[code
].sc_name
= "ioctl";
1741 bsd_syscalls
[code
].sc_format
= FMT_IOCTL
;
1745 bsd_syscalls
[code
].sc_name
= "fsgetpath";
1750 for (i
= 0; (type
= filemgr_call_types
[i
]); i
++) {
1753 code
= filemgr_index(type
);
1755 if (code
>= MAX_FILEMGR
) {
1756 printf("FILEMGR call init (%x): type exceeds table size\n", type
);
1761 case FILEMGR_PBGETCATALOGINFO
:
1762 p
= "GetCatalogInfo";
1765 case FILEMGR_PBGETCATALOGINFOBULK
:
1766 p
= "GetCatalogInfoBulk";
1769 case FILEMGR_PBCREATEFILEUNICODE
:
1770 p
= "CreateFileUnicode";
1773 case FILEMGR_PBCREATEDIRECTORYUNICODE
:
1774 p
= "CreateDirectoryUnicode";
1777 case FILEMGR_PBCREATEFORK
:
1781 case FILEMGR_PBDELETEFORK
:
1785 case FILEMGR_PBITERATEFORK
:
1786 p
= "PBIterateFork";
1789 case FILEMGR_PBOPENFORK
:
1793 case FILEMGR_PBREADFORK
:
1797 case FILEMGR_PBWRITEFORK
:
1801 case FILEMGR_PBALLOCATEFORK
:
1802 p
= "PBAllocateFork";
1805 case FILEMGR_PBDELETEOBJECT
:
1806 p
= "PBDeleteObject";
1809 case FILEMGR_PBEXCHANGEOBJECT
:
1810 p
= "PBExchangeObject";
1813 case FILEMGR_PBGETFORKCBINFO
:
1814 p
= "PBGetForkCBInfo";
1817 case FILEMGR_PBGETVOLUMEINFO
:
1818 p
= "PBGetVolumeInfo";
1821 case FILEMGR_PBMAKEFSREF
:
1825 case FILEMGR_PBMAKEFSREFUNICODE
:
1826 p
= "PBMakeFSRefUnicode";
1829 case FILEMGR_PBMOVEOBJECT
:
1833 case FILEMGR_PBOPENITERATOR
:
1834 p
= "PBOpenIterator";
1837 case FILEMGR_PBRENAMEUNICODE
:
1838 p
= "PBRenameUnicode";
1841 case FILEMGR_PBSETCATALOGINFO
:
1842 p
= "SetCatalogInfo";
1845 case FILEMGR_PBSETVOLUMEINFO
:
1846 p
= "SetVolumeInfo";
1849 case FILEMGR_FSREFMAKEPATH
:
1850 p
= "FSRefMakePath";
1853 case FILEMGR_FSPATHMAKEREF
:
1854 p
= "FSPathMakeRef";
1857 case FILEMGR_PBGETCATINFO
:
1861 case FILEMGR_PBGETCATINFOLITE
:
1862 p
= "GetCatInfoLite";
1865 case FILEMGR_PBHGETFINFO
:
1869 case FILEMGR_PBXGETVOLINFO
:
1870 p
= "PBXGetVolInfo";
1873 case FILEMGR_PBHCREATE
:
1877 case FILEMGR_PBHOPENDF
:
1881 case FILEMGR_PBHOPENRF
:
1885 case FILEMGR_PBHGETDIRACCESS
:
1886 p
= "PBHGetDirAccess";
1889 case FILEMGR_PBHSETDIRACCESS
:
1890 p
= "PBHSetDirAccess";
1893 case FILEMGR_PBHMAPID
:
1897 case FILEMGR_PBHMAPNAME
:
1901 case FILEMGR_PBCLOSE
:
1905 case FILEMGR_PBFLUSHFILE
:
1909 case FILEMGR_PBGETEOF
:
1913 case FILEMGR_PBSETEOF
:
1917 case FILEMGR_PBGETFPOS
:
1921 case FILEMGR_PBREAD
:
1925 case FILEMGR_PBWRITE
:
1929 case FILEMGR_PBGETFCBINFO
:
1933 case FILEMGR_PBSETFINFO
:
1937 case FILEMGR_PBALLOCATE
:
1941 case FILEMGR_PBALLOCCONTIG
:
1942 p
= "PBAllocContig";
1945 case FILEMGR_PBSETFPOS
:
1949 case FILEMGR_PBSETCATINFO
:
1953 case FILEMGR_PBGETVOLPARMS
:
1954 p
= "PBGetVolParms";
1957 case FILEMGR_PBSETVINFO
:
1961 case FILEMGR_PBMAKEFSSPEC
:
1965 case FILEMGR_PBHGETVINFO
:
1969 case FILEMGR_PBCREATEFILEIDREF
:
1970 p
= "PBCreateFileIDRef";
1973 case FILEMGR_PBDELETEFILEIDREF
:
1974 p
= "PBDeleteFileIDRef";
1977 case FILEMGR_PBRESOLVEFILEIDREF
:
1978 p
= "PBResolveFileIDRef";
1981 case FILEMGR_PBFLUSHVOL
:
1985 case FILEMGR_PBHRENAME
:
1989 case FILEMGR_PBCATMOVE
:
1993 case FILEMGR_PBEXCHANGEFILES
:
1994 p
= "PBExchangeFiles";
1997 case FILEMGR_PBHDELETE
:
2001 case FILEMGR_PBDIRCREATE
:
2005 case FILEMGR_PBCATSEARCH
:
2009 case FILEMGR_PBHSETFLOCK
:
2013 case FILEMGR_PBHRSTFLOCK
:
2017 case FILEMGR_PBLOCKRANGE
:
2021 case FILEMGR_PBUNLOCKRANGE
:
2022 p
= "PBUnlockRange";
2029 filemgr_calls
[code
].fm_name
= p
;
2040 char *myname
= "fs_usage";
2044 time_t stop_at_time
= 0;
2046 if (0 != reexec_to_match_kernel()) {
2047 fprintf(stderr
, "Could not re-execute: %d\n", errno
);
2056 if ((myname
= rindex(argv
[0], '/')) == 0)
2062 while ((ch
= getopt(argc
, argv
, "bewf:R:S:E:t:")) != EOF
) {
2068 exclude_default_pids
= 0;
2073 if ((uint
)columns
< MAX_WIDE_MODE_COLS
)
2074 columns
= MAX_WIDE_MODE_COLS
;
2078 if (!strcmp(optarg
, "network"))
2079 filter_mode
|= NETWORK_FILTER
;
2080 else if (!strcmp(optarg
, "filesys"))
2081 filter_mode
|= FILESYS_FILTER
;
2082 else if (!strcmp(optarg
, "cachehit"))
2083 show_cachehits
= TRUE
;
2084 else if (!strcmp(optarg
, "exec"))
2085 filter_mode
|= EXEC_FILTER
;
2086 else if (!strcmp(optarg
, "pathname"))
2087 filter_mode
|= PATHNAME_FILTER
;
2088 else if (!strcmp(optarg
, "diskio"))
2089 filter_mode
|= DISKIO_FILTER
;
2097 stop_at_time
= time(NULL
) + strtoul(optarg
, NULL
, 10);
2106 start_time
= atof(optarg
);
2110 end_time
= atof(optarg
);
2118 if ( geteuid() != 0 ) {
2119 fprintf(stderr
, "'fs_usage' must be run as root...\n");
2127 * when excluding, fs_usage should be the first in line for pids[]
2129 * the !exclude_pids && argc == 0 catches the exclude_default_pids
2130 * case below where exclude_pids is later set and the fs_usage PID
2131 * needs to make it into pids[]
2133 if (exclude_pids
|| (!exclude_pids
&& argc
== 0)) {
2134 if (num_of_pids
< (MAX_PIDS
- 1))
2135 pids
[num_of_pids
++] = getpid();
2139 * If we process any list of pids/cmds, then turn off the defaults
2142 exclude_default_pids
= 0;
2144 while (argc
> 0 && num_of_pids
< (MAX_PIDS
- 1)) {
2151 * Exclude a set of default pids
2153 if (exclude_default_pids
) {
2154 argtopid("Terminal");
2155 argtopid("telnetd");
2158 argtopid("rlogind");
2165 for (i
= 0; i
< num_of_pids
; i
++) {
2167 fprintf(stderr
, "exclude pid %d\n", pids
[i
]);
2169 fprintf(stderr
, "pid %d\n", pids
[i
]);
2173 struct sigaction osa
;
2177 /* set up signal handlers */
2178 signal(SIGINT
, leave
);
2179 signal(SIGQUIT
, leave
);
2181 sigaction(SIGHUP
, (struct sigaction
*)NULL
, &osa
);
2183 if (osa
.sa_handler
== SIG_DFL
)
2184 signal(SIGHUP
, leave
);
2185 signal(SIGTERM
, leave
);
2187 * grab the number of cpus
2192 len
= sizeof(num_cpus
);
2194 sysctl(mib
, 2, &num_cpus
, &len
, NULL
, 0);
2195 num_events
= EVENT_BASE
* num_cpus
;
2197 signal(SIGWINCH
, sigwinch
);
2199 if ((my_buffer
= malloc(num_events
* sizeof(kd_buf
))) == (char *)0)
2200 quit("can't allocate memory for tracing info\n");
2202 if (ReadSharedCacheMap("/var/db/dyld/dyld_shared_cache_i386.map", &framework32
, "/var/db/dyld/dyld_shared_cache_i386")) {
2203 ReadSharedCacheMap("/var/db/dyld/dyld_shared_cache_x86_64.map", &framework64
, "/var/db/dyld/dyld_shared_cache_x86_64");
2205 ReadSharedCacheMap("/var/db/dyld/dyld_shared_cache_ppc.map", &framework32
, "/var/db/dyld/dyld_shared_cache_ppc");
2206 ReadSharedCacheMap("/var/db/dyld/dyld_shared_cache_ppc64.map", &framework64
, "/var/db/dyld/dyld_shared_cache_ppc64");
2208 SortFrameworkAddresses();
2215 set_numbufs(num_events
);
2218 if (exclude_pids
== 0) {
2219 for (i
= 0; i
< num_of_pids
; i
++)
2220 set_pidcheck(pids
[i
], 1);
2222 for (i
= 0; i
< num_of_pids
; i
++)
2223 set_pidexclude(pids
[i
], 1);
2225 if (select_pid_mode
&& !one_good_pid
) {
2227 * An attempt to restrict output to a given
2228 * pid or command has failed. Exit gracefully
2238 init_arguments_buffer();
2247 while (stop_at_time
== 0 || last_time
< stop_at_time
) {
2249 usleep(1000 * usleep_ms
);
2253 last_time
= time((long *)0);
2262 struct kinfo_proc
*kp
;
2266 mib
[2] = KERN_PROC_ALL
;
2269 if (sysctl(mib
, 4, NULL
, &bufSize
, NULL
, 0) < 0)
2270 quit("trace facility failure, KERN_PROC_ALL\n");
2272 if ((kp
= (struct kinfo_proc
*)malloc(bufSize
)) == (struct kinfo_proc
*)0)
2273 quit("can't allocate memory for proc buffer\n");
2275 if (sysctl(mib
, 4, kp
, &bufSize
, NULL
, 0) < 0)
2276 quit("trace facility failure, KERN_PROC_ALL\n");
2278 kp_nentries
= bufSize
/ sizeof(struct kinfo_proc
);
2287 mib
[1] = KERN_KDEBUG
;
2288 mib
[2] = KERN_KDENABLE
; /* protocol */
2291 mib
[5] = 0; /* no flags */
2293 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
2294 quit("trace facility failure, KERN_KDENABLE\n");
2303 set_numbufs(int nbufs
)
2306 mib
[1] = KERN_KDEBUG
;
2307 mib
[2] = KERN_KDSETBUF
;
2310 mib
[5] = 0; /* no flags */
2312 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
2313 quit("trace facility failure, KERN_KDSETBUF\n");
2316 mib
[1] = KERN_KDEBUG
;
2317 mib
[2] = KERN_KDSETUP
;
2320 mib
[5] = 0; /* no flags */
2322 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
2323 quit("trace facility failure, KERN_KDSETUP\n");
2326 #define ENCODE_CSC_LOW(class, subclass) \
2327 ( (uint16_t) ( ((class) & 0xff) << 8 ) | ((subclass) & 0xff) )
2332 uint8_t type_filter_bitmap
[KDBG_TYPEFILTER_BITMAP_SIZE
];
2333 bzero(type_filter_bitmap
, sizeof(type_filter_bitmap
));
2335 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_TRACE
,DBG_TRACE_DATA
));
2336 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_TRACE
,DBG_TRACE_STRING
));
2338 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_MACH
,DBG_MACH_EXCP_SC
)); //0x010c
2339 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_MACH
,DBG_MACH_VM
)); //0x0130
2340 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_MACH
,DBG_MACH_SCHED
)); //0x0140
2342 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_FSYSTEM
,DBG_FSRW
)); //0x0301
2343 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_FSYSTEM
,DBG_DKRW
)); //0x0302
2344 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_FSYSTEM
,DBG_IOCTL
)); //0x0306
2345 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_FSYSTEM
,DBG_BOOTCACHE
)); //0x0307
2347 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_BSD
,DBG_BSD_EXCP_SC
)); //0x040c
2348 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_BSD
,DBG_BSD_PROC
)); //0x0401
2349 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_BSD
,DBG_BSD_SC_EXTENDED_INFO
)); //0x040e
2350 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_BSD
,DBG_BSD_SC_EXTENDED_INFO2
)); //0x040f
2352 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_CORESTORAGE
,DBG_CS_IO
)); //0x0a00
2353 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(DBG_CORESTORAGE
, 1)); //0x0a01 for P_SCCS_SYNC_DIS
2355 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(FILEMGR_CLASS
, 0)); //Carbon File Manager
2356 setbit(type_filter_bitmap
, ENCODE_CSC_LOW(FILEMGR_CLASS
, 1)); //Carbon File Manager
2359 int mib
[] = { CTL_KERN
, KERN_KDEBUG
, KERN_KDSET_TYPEFILTER
};
2360 size_t needed
= KDBG_TYPEFILTER_BITMAP_SIZE
;
2361 if(sysctl(mib
, 3, type_filter_bitmap
, &needed
, NULL
, 0)) {
2362 quit("trace facility failure, KERN_KDSET_TYPEFILTER\n");
2367 set_pidcheck(int pid
, int on_off
)
2371 kr
.type
= KDBG_TYPENONE
;
2374 needed
= sizeof(kd_regtype
);
2376 mib
[1] = KERN_KDEBUG
;
2377 mib
[2] = KERN_KDPIDTR
;
2382 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0) {
2384 fprintf(stderr
, "pid %d does not exist\n", pid
);
2390 * on_off == 0 turns off pid exclusion
2391 * on_off == 1 turns on pid exclusion
2394 set_pidexclude(int pid
, int on_off
)
2400 kr
.type
= KDBG_TYPENONE
;
2403 needed
= sizeof(kd_regtype
);
2405 mib
[1] = KERN_KDEBUG
;
2406 mib
[2] = KERN_KDPIDEX
;
2411 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0) {
2413 fprintf(stderr
, "pid %d does not exist\n", pid
);
2418 get_bufinfo(kbufinfo_t
*val
)
2420 needed
= sizeof (*val
);
2422 mib
[1] = KERN_KDEBUG
;
2423 mib
[2] = KERN_KDGETBUF
;
2426 mib
[5] = 0; /* no flags */
2428 if (sysctl(mib
, 3, val
, &needed
, 0, 0) < 0)
2429 quit("trace facility failure, KERN_KDGETBUF\n");
2439 mib
[1] = KERN_KDEBUG
;
2440 mib
[2] = KERN_KDREMOVE
; /* protocol */
2443 mib
[5] = 0; /* no flags */
2445 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0) {
2446 set_remove_flag
= 0;
2449 quit("the trace facility is currently in use...\n fs_usage, sc_usage, and latency use this feature.\n\n");
2451 quit("trace facility failure, KERN_KDREMOVE\n");
2459 kr
.type
= KDBG_RANGETYPE
;
2462 needed
= sizeof(kd_regtype
);
2465 mib
[1] = KERN_KDEBUG
;
2466 mib
[2] = KERN_KDSETREG
;
2469 mib
[5] = 0; /* no flags */
2471 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0)
2472 quit("trace facility failure, KERN_KDSETREG\n");
2475 mib
[1] = KERN_KDEBUG
;
2476 mib
[2] = KERN_KDSETUP
;
2479 mib
[5] = 0; /* no flags */
2481 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
2482 quit("trace facility failure, KERN_KDSETUP\n");
2492 uint32_t my_buffer_size
= 0;
2495 get_bufinfo(&bufinfo
);
2497 my_buffer_size
= num_events
* sizeof(kd_buf
);
2504 needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
2507 mib
[1] = KERN_KDEBUG
;
2508 mib
[2] = KERN_KDREADTR
;
2511 mib
[5] = 0; /* no flags */
2513 if (sysctl(mib
, 3, my_buffer
, &needed
, NULL
, 0) < 0)
2514 quit("trace facility failure, KERN_KDREADTR\n");
2517 if (count
> (num_events
/ 8)) {
2518 if (usleep_ms
> USLEEP_BEHIND
)
2519 usleep_ms
= USLEEP_BEHIND
;
2520 else if (usleep_ms
> USLEEP_MIN
)
2523 } else if (count
< (num_events
/ 16)) {
2524 if (usleep_ms
< USLEEP_MAX
)
2528 if (bufinfo
.flags
& KDBG_WRAPPED
) {
2529 fprintf(stderr
, "fs_usage: buffer overrun, events generated too quickly: %d\n", count
);
2531 delete_all_events();
2541 if ((bytes_read
= read(RAW_fd
, my_buffer
, my_buffer_size
)) < sizeof(kd_buf
))
2543 count
= bytes_read
/ sizeof(kd_buf
);
2545 kd
= (kd_buf
*)my_buffer
;
2547 fprintf(stderr
, "READTR returned %d items\n", count
);
2549 for (i
= 0; i
< count
; i
++) {
2563 thread
= kd
[i
].arg5
;
2564 debugid
= kd
[i
].debugid
;
2565 type
= kd
[i
].debugid
& DBG_FUNC_MASK
;
2567 now
= kdbg_get_timestamp(&kd
[i
]);
2569 if (i
== 0 && !RAW_flag
) {
2571 curr_time
= time((long *)0);
2573 * Compute bias seconds after each trace buffer read.
2574 * This helps resync timestamps with the system clock
2575 * in the event of a system sleep.
2577 if (bias_secs
== 0 || curr_time
< last_time
|| curr_time
> (last_time
+ 2)) {
2578 l_usecs
= (long long)(now
/ divisor
);
2579 secs
= l_usecs
/ 1000000;
2580 bias_secs
= curr_time
- secs
;
2583 if (RAW_flag
&& bias_now
== 0.0)
2586 if ((type
& P_DISKIO_MASK
) == P_DISKIO
) {
2587 insert_diskio(type
, kd
[i
].arg1
, kd
[i
].arg2
, kd
[i
].arg3
, kd
[i
].arg4
, thread
, (double)now
);
2590 if ((type
& P_DISKIO_MASK
) == P_DISKIO_DONE
) {
2591 if ((dio
= complete_diskio(kd
[i
].arg1
, kd
[i
].arg4
, kd
[i
].arg3
, thread
, (double)now
))) {
2592 dio
->vnodeid
= kd
[i
].arg2
;
2599 if ((type
& CLASS_MASK
) == P_CS_Class
) {
2601 // the usual DBG_FUNC_START/END does not work for i/o since it will
2602 // return on a different thread, this code uses the P_CS_IO_Done (0x4) bit
2603 // instead. the trace command doesn't know how handle either method
2604 // (unmatched start/end or 0x4) but works a little better this way.
2606 int cs_type
= type
& P_CS_Type_Mask
; // strip out the done bit
2607 bool start
= (type
& P_CS_IO_Done
) != P_CS_IO_Done
;
2611 case P_CS_ReadChunk
:
2612 case P_CS_WriteChunk
:
2614 case P_CS_MetaWrite
:
2616 insert_diskio(cs_type
, kd
[i
].arg2
, kd
[i
].arg1
, kd
[i
].arg3
, kd
[i
].arg4
, thread
, (double)now
);
2618 if ((dio
= complete_diskio(kd
[i
].arg2
, kd
[i
].arg4
, kd
[i
].arg3
, thread
, (double)now
))) {
2625 case P_CS_TransformRead
:
2626 case P_CS_TransformWrite
:
2627 case P_CS_MigrationRead
:
2628 case P_CS_MigrationWrite
:
2630 insert_diskio(cs_type
, kd
[i
].arg2
, CS_DEV
, kd
[i
].arg3
, kd
[i
].arg4
, thread
, (double)now
);
2632 if ((dio
= complete_diskio(kd
[i
].arg2
, kd
[i
].arg4
, kd
[i
].arg3
, thread
, (double)now
))) {
2639 case P_CS_SYNC_DISK
:
2641 enter_event(thread
, cs_type
, &kd
[i
], NULL
, (double)now
);
2643 exit_event(" SyncCacheCS", thread
, cs_type
, kd
[i
].arg1
, 0, 0, 0, FMT_SYNC_DISK_CS
, (double)now
);
2648 continue; // ignore other cs timestamps
2653 case TRACE_DATA_NEWTHREAD
:
2655 if ((ti
= add_event(thread
, TRACE_DATA_NEWTHREAD
)) == NULL
)
2657 ti
->child_thread
= kd
[i
].arg1
;
2658 ti
->pid
= kd
[i
].arg2
;
2662 case TRACE_STRING_NEWTHREAD
:
2663 if ((ti
= find_event(thread
, TRACE_DATA_NEWTHREAD
)) == (struct th_info
*)0)
2666 create_map_entry(ti
->child_thread
, ti
->pid
, (char *)&kd
[i
].arg1
);
2671 case TRACE_DATA_EXEC
:
2672 if ((ti
= add_event(thread
, TRACE_DATA_EXEC
)) == NULL
)
2675 ti
->pid
= kd
[i
].arg1
;
2678 case TRACE_STRING_EXEC
:
2679 if ((ti
= find_event(thread
, BSC_execve
))) {
2680 if (ti
->lookups
[0].pathname
[0])
2681 exit_event("execve", thread
, BSC_execve
, 0, 0, 0, 0, FMT_DEFAULT
, (double)now
);
2683 } else if ((ti
= find_event(thread
, BSC_posix_spawn
))) {
2684 if (ti
->lookups
[0].pathname
[0])
2685 exit_event("posix_spawn", thread
, BSC_posix_spawn
, 0, 0, 0, 0, FMT_DEFAULT
, (double)now
);
2687 if ((ti
= find_event(thread
, TRACE_DATA_EXEC
)) == (struct th_info
*)0)
2690 create_map_entry(thread
, ti
->pid
, (char *)&kd
[i
].arg1
);
2695 case BSC_thread_terminate
:
2696 delete_map_entry(thread
);
2703 kd
[i
].arg1
= kd
[i
].arg2
>> 8;
2708 if (kd
[i
].arg4
& MAP_ANON
)
2714 case MACH_stkhandoff
:
2715 mark_thread_waited(thread
);
2719 case BC_IO_HIT_STALLED
:
2721 case BC_IO_MISS_CUT_THROUGH
:
2722 case BC_PLAYBACK_IO
:
2723 if ((dio
= find_diskio(kd
[i
].arg1
)) != NULL
)
2724 dio
->bc_info
= type
;
2727 case HFS_modify_block_end
:
2728 if ((ti
= find_event(thread
, 0))) {
2730 add_meta_name(kd
[i
].arg2
, ti
->nameptr
);
2735 add_vnode_name(kd
[i
].arg2
, find_vnode_name(kd
[i
].arg1
));
2739 if ((ti
= find_event(thread
, 0)) == (struct th_info
*)0)
2742 if (debugid
& DBG_FUNC_START
) {
2744 if (ti
->in_hfs_update
) {
2745 ti
->pn_work_index
= (MAX_PATHNAMES
- 1);
2747 if (ti
->pn_scall_index
< MAX_SCALL_PATHNAMES
)
2748 ti
->pn_work_index
= ti
->pn_scall_index
;
2752 sargptr
= &ti
->lookups
[ti
->pn_work_index
].pathname
[0];
2754 ti
->vnodeid
= kd
[i
].arg1
;
2756 *sargptr
++ = kd
[i
].arg2
;
2757 *sargptr
++ = kd
[i
].arg3
;
2758 *sargptr
++ = kd
[i
].arg4
;
2760 * NULL terminate the 'string'
2764 ti
->pathptr
= sargptr
;
2766 sargptr
= ti
->pathptr
;
2769 * We don't want to overrun our pathname buffer if the
2770 * kernel sends us more VFS_LOOKUP entries than we can
2771 * handle and we only handle 2 pathname lookups for
2772 * a given system call
2777 if ((uintptr_t)sargptr
< (uintptr_t)&ti
->lookups
[ti
->pn_work_index
].pathname
[NUMPARMS
]) {
2779 *sargptr
++ = kd
[i
].arg1
;
2780 *sargptr
++ = kd
[i
].arg2
;
2781 *sargptr
++ = kd
[i
].arg3
;
2782 *sargptr
++ = kd
[i
].arg4
;
2784 * NULL terminate the 'string'
2789 if (debugid
& DBG_FUNC_END
) {
2791 ti
->nameptr
= add_vnode_name(ti
->vnodeid
, &ti
->lookups
[ti
->pn_work_index
].pathname
[0]);
2793 if (ti
->pn_work_index
== ti
->pn_scall_index
) {
2795 ti
->pn_scall_index
++;
2797 if (ti
->pn_scall_index
< MAX_SCALL_PATHNAMES
)
2798 ti
->pathptr
= &ti
->lookups
[ti
->pn_scall_index
].pathname
[0];
2803 ti
->pathptr
= sargptr
;
2808 if (debugid
& DBG_FUNC_START
) {
2811 if ((type
& CLASS_MASK
) == FILEMGR_BASE
) {
2813 index
= filemgr_index(type
);
2815 if (index
>= MAX_FILEMGR
)
2818 if ((p
= filemgr_calls
[index
].fm_name
) == NULL
)
2823 enter_event(thread
, type
, &kd
[i
], p
, (double)now
);
2830 exit_event(" THROTTLED", thread
, type
, 0, 0, 0, 0, FMT_DEFAULT
, (double)now
);
2834 exit_event(" HFS_update", thread
, type
, kd
[i
].arg1
, kd
[i
].arg2
, 0, 0, FMT_HFS_update
, (double)now
);
2837 case SPEC_unmap_info
:
2838 if (check_filter_mode(NULL
, SPEC_unmap_info
, 0, 0, "SPEC_unmap_info"))
2839 format_print(NULL
, " TrimExtent", thread
, type
, kd
[i
].arg1
, kd
[i
].arg2
, kd
[i
].arg3
, 0, FMT_UNMAP_INFO
, now
, now
, 0, "", NULL
);
2843 if (kd
[i
].arg2
== DKIOCSYNCHRONIZECACHE
)
2844 exit_event("IOCTL", thread
, type
, kd
[i
].arg1
, kd
[i
].arg2
, 0, 0, FMT_IOCTL_SYNC
, (double)now
);
2845 else if (kd
[i
].arg2
== DKIOCUNMAP
)
2846 exit_event("IOCTL", thread
, type
, kd
[i
].arg1
, kd
[i
].arg2
, 0, 0, FMT_IOCTL_UNMAP
, (double)now
);
2848 if ((ti
= find_event(thread
, type
)))
2855 exit_event("PAGE_OUT_ANON", thread
, type
, 0, kd
[i
].arg1
, 0, 0, FMT_PGOUT
, (double)now
);
2857 exit_event("PAGE_OUT_FILE", thread
, type
, 0, kd
[i
].arg1
, 0, 0, FMT_PGOUT
, (double)now
);
2861 if (kd
[i
].arg4
== DBG_PAGEIN_FAULT
)
2862 exit_event("PAGE_IN", thread
, type
, 0, kd
[i
].arg1
, kd
[i
].arg2
, 0, FMT_PGIN
, (double)now
);
2863 else if (kd
[i
].arg4
== DBG_PAGEINV_FAULT
)
2864 exit_event("PAGE_IN_FILE", thread
, type
, 0, kd
[i
].arg1
, kd
[i
].arg2
, 0, FMT_PGIN
, (double)now
);
2865 else if (kd
[i
].arg4
== DBG_PAGEIND_FAULT
)
2866 exit_event("PAGE_IN_ANON", thread
, type
, 0, kd
[i
].arg1
, kd
[i
].arg2
, 0, FMT_PGIN
, (double)now
);
2867 else if (kd
[i
].arg4
== DBG_CACHE_HIT_FAULT
)
2868 exit_event("CACHE_HIT", thread
, type
, 0, kd
[i
].arg1
, kd
[i
].arg2
, 0, FMT_CACHEHIT
, (double)now
);
2870 if ((ti
= find_event(thread
, type
)))
2876 exit_event("map_fd", thread
, type
, kd
[i
].arg1
, kd
[i
].arg2
, 0, 0, FMT_FD
, (double)now
);
2879 case BSC_mmap_extended
:
2880 case BSC_mmap_extended2
:
2881 case BSC_msync_extended
:
2882 case BSC_pread_extended
:
2883 case BSC_pwrite_extended
:
2884 extend_syscall(thread
, type
, &kd
[i
]);
2888 if ((type
& CSC_MASK
) == BSC_BASE
) {
2890 if ((index
= BSC_INDEX(type
)) >= MAX_BSD_SYSCALL
)
2893 if (bsd_syscalls
[index
].sc_name
) {
2894 exit_event(bsd_syscalls
[index
].sc_name
, thread
, type
, kd
[i
].arg1
, kd
[i
].arg2
, kd
[i
].arg3
, kd
[i
].arg4
,
2895 bsd_syscalls
[index
].sc_format
, (double)now
);
2897 if (type
== BSC_exit
)
2898 delete_map_entry(thread
);
2900 } else if ((type
& CLASS_MASK
) == FILEMGR_BASE
) {
2902 if ((index
= filemgr_index(type
)) >= MAX_FILEMGR
)
2905 if (filemgr_calls
[index
].fm_name
) {
2906 exit_event(filemgr_calls
[index
].fm_name
, thread
, type
, kd
[i
].arg1
, kd
[i
].arg2
, kd
[i
].arg3
, kd
[i
].arg4
,
2907 FMT_DEFAULT
, (double)now
);
2916 enter_event_now(uintptr_t thread
, int type
, kd_buf
*kd
, char *name
, double now
)
2930 if ((ti
= add_event(thread
, type
)) == NULL
)
2934 ti
->arg1
= kd
->arg1
;
2935 ti
->arg2
= kd
->arg2
;
2936 ti
->arg3
= kd
->arg3
;
2937 ti
->arg4
= kd
->arg4
;
2942 ti
->in_hfs_update
= 1;
2946 if ((type
& CLASS_MASK
) == FILEMGR_BASE
&&
2947 (!RAW_flag
|| (now
>= start_time
&& now
<= end_time
))) {
2949 filemgr_in_progress
++;
2953 l_usecs
= (long long)((now
- bias_now
) / divisor
);
2954 l_usecs
+= (sample_TOD_secs
* 1000000) + sample_TOD_usecs
;
2956 l_usecs
= (long long)(now
/ divisor
);
2957 secs
= l_usecs
/ 1000000;
2958 curr_time
= bias_secs
+ secs
;
2960 sprintf(buf
, "%-8.8s", &(ctime(&curr_time
)[11]));
2961 tsclen
= strlen(buf
);
2963 if (columns
> MAXCOLS
|| wideflag
) {
2964 usecs
= l_usecs
- (long long)((long long)secs
* 1000000);
2965 sprintf(&buf
[tsclen
], ".%06ld", (long)usecs
);
2966 tsclen
= strlen(buf
);
2970 * Print timestamp column
2974 tme
= find_map_entry(thread
);
2976 sprintf(buf
, " %-25.25s ", name
);
2977 nmclen
= strlen(buf
);
2980 sprintf(buf
, "(%d, 0x%lx, 0x%lx, 0x%lx)", (short)kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
);
2981 argsclen
= strlen(buf
);
2984 * Calculate white space out to command
2986 if (columns
> MAXCOLS
|| wideflag
) {
2987 clen
= columns
- (tsclen
+ nmclen
+ argsclen
+ 20 + 11);
2989 clen
= columns
- (tsclen
+ nmclen
+ argsclen
+ 12);
2992 printf("%s", buf
); /* print the kdargs */
2993 memset(buf
, ' ', clen
);
2997 else if ((argsclen
+ clen
) > 0) {
2999 * no room so wipe out the kdargs
3001 memset(buf
, ' ', (argsclen
+ clen
));
3002 buf
[argsclen
+ clen
] = '\0';
3005 if (columns
> MAXCOLS
|| wideflag
)
3006 printf("%s.%d\n", tme
->tm_command
, (int)thread
);
3008 printf("%-12.12s\n", tme
->tm_command
);
3010 printf(" %-24.24s (%5d, %#lx, 0x%lx, 0x%lx)\n", name
, (short)kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
);
3016 enter_event(uintptr_t thread
, int type
, kd_buf
*kd
, char *name
, double now
)
3022 case P_CS_SYNC_DISK
:
3029 enter_event_now(thread
, type
, kd
, name
, now
);
3033 if ((type
& CSC_MASK
) == BSC_BASE
) {
3035 if ((index
= BSC_INDEX(type
)) >= MAX_BSD_SYSCALL
)
3038 if (bsd_syscalls
[index
].sc_name
)
3039 enter_event_now(thread
, type
, kd
, name
, now
);
3042 if ((type
& CLASS_MASK
) == FILEMGR_BASE
) {
3044 if ((index
= filemgr_index(type
)) >= MAX_FILEMGR
)
3047 if (filemgr_calls
[index
].fm_name
)
3048 enter_event_now(thread
, type
, kd
, name
, now
);
3054 * Handle system call extended trace data.
3056 * Wipe out the kd args that were collected upon syscall_entry
3057 * because it is the extended info that we really want, and it
3058 * is all we really need.
3062 extend_syscall(uintptr_t thread
, int type
, kd_buf
*kd
)
3067 case BSC_mmap_extended
:
3068 if ((ti
= find_event(thread
, BSC_mmap
)) == (struct th_info
*)0)
3070 ti
->arg8
= ti
->arg3
; /* save protection */
3071 ti
->arg1
= kd
->arg1
; /* the fd */
3072 ti
->arg3
= kd
->arg2
; /* bottom half address */
3073 ti
->arg5
= kd
->arg3
; /* bottom half size */
3075 case BSC_mmap_extended2
:
3076 if ((ti
= find_event(thread
, BSC_mmap
)) == (struct th_info
*)0)
3078 ti
->arg2
= kd
->arg1
; /* top half address */
3079 ti
->arg4
= kd
->arg2
; /* top half size */
3080 ti
->arg6
= kd
->arg3
; /* top half file offset */
3081 ti
->arg7
= kd
->arg4
; /* bottom half file offset */
3083 case BSC_msync_extended
:
3084 if ((ti
= find_event(thread
, BSC_msync
)) == (struct th_info
*)0) {
3085 if ((ti
= find_event(thread
, BSC_msync_nocancel
)) == (struct th_info
*)0)
3088 ti
->arg4
= kd
->arg1
; /* top half address */
3089 ti
->arg5
= kd
->arg2
; /* top half size */
3091 case BSC_pread_extended
:
3092 if ((ti
= find_event(thread
, BSC_pread
)) == (struct th_info
*)0) {
3093 if ((ti
= find_event(thread
, BSC_pread_nocancel
)) == (struct th_info
*)0)
3096 ti
->arg1
= kd
->arg1
; /* the fd */
3097 ti
->arg2
= kd
->arg2
; /* nbytes */
3098 ti
->arg3
= kd
->arg3
; /* top half offset */
3099 ti
->arg4
= kd
->arg4
; /* bottom half offset */
3101 case BSC_pwrite_extended
:
3102 if ((ti
= find_event(thread
, BSC_pwrite
)) == (struct th_info
*)0) {
3103 if ((ti
= find_event(thread
, BSC_pwrite_nocancel
)) == (struct th_info
*)0)
3106 ti
->arg1
= kd
->arg1
; /* the fd */
3107 ti
->arg2
= kd
->arg2
; /* nbytes */
3108 ti
->arg3
= kd
->arg3
; /* top half offset */
3109 ti
->arg4
= kd
->arg4
; /* bottom half offset */
3118 exit_event(char *sc_name
, uintptr_t thread
, int type
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
, uintptr_t arg4
,
3119 int format
, double now
)
3123 if ((ti
= find_event(thread
, type
)) == (struct th_info
*)0)
3128 if (check_filter_mode(ti
, type
, arg1
, arg2
, sc_name
))
3129 format_print(ti
, sc_name
, thread
, type
, arg1
, arg2
, arg3
, arg4
, format
, now
, ti
->stime
, ti
->waited
, (char *)&ti
->lookups
[0].pathname
[0], NULL
);
3134 ti
->in_hfs_update
= 0;
3137 if ((type
& CLASS_MASK
) == FILEMGR_BASE
) {
3140 if (filemgr_in_progress
> 0)
3141 filemgr_in_progress
--;
3148 get_mode_nibble(char * buf
, int smode
, int special
, char x_on
, char x_off
)
3167 get_mode_string(int mode
, char *buf
)
3169 memset(buf
, '-', 9);
3172 get_mode_nibble(&buf
[6], mode
, (mode
& 01000), 't', 'T');
3173 get_mode_nibble(&buf
[3], (mode
>>3), (mode
& 02000), 's', 'S');
3174 get_mode_nibble(&buf
[0], (mode
>>6), (mode
& 04000), 's', 'S');
3178 int clip_64bit(char *s
, uint64_t value
)
3182 if ( (value
& 0xff00000000000000LL
) )
3183 clen
= printf("%s0x%16.16qx", s
, value
);
3184 else if ( (value
& 0x00ff000000000000LL
) )
3185 clen
= printf("%s0x%14.14qx ", s
, value
);
3186 else if ( (value
& 0x0000ff0000000000LL
) )
3187 clen
= printf("%s0x%12.12qx ", s
, value
);
3188 else if ( (value
& 0x000000ff00000000LL
) )
3189 clen
= printf("%s0x%10.10qx ", s
, value
);
3191 clen
= printf("%s0x%8.8qx ", s
, value
);
3198 format_print(struct th_info
*ti
, char *sc_name
, uintptr_t thread
, int type
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
, uintptr_t arg4
,
3199 int format
, double now
, double stime
, int waited
, char *pathname
, struct diskio
*dio
)
3214 char *framework_name
;
3215 char *framework_type
;
3219 char cs_diskname
[32];
3221 static char timestamp
[32];
3222 static int last_timestamp
= -1;
3223 static int timestamp_len
= 0;
3228 l_usecs
= (long long)((now
- bias_now
) / divisor
);
3230 if ((double)l_usecs
< start_time
|| (double)l_usecs
> end_time
)
3233 l_usecs
+= (sample_TOD_secs
* 1000000) + sample_TOD_usecs
;
3236 l_usecs
= (long long)(now
/ divisor
);
3237 secs
= l_usecs
/ 1000000;
3238 curr_time
= bias_secs
+ secs
;
3243 command_name
= dio
->issuing_command
;
3247 if ((tme
= find_map_entry(thread
)))
3248 command_name
= tme
->tm_command
;
3250 if (last_timestamp
!= curr_time
) {
3251 timestamp_len
= sprintf(timestamp
, "%-8.8s", &(ctime(&curr_time
)[11]));
3252 last_timestamp
= curr_time
;
3254 if (columns
> MAXCOLS
|| wideflag
) {
3257 tlen
= timestamp_len
;
3259 usec
= (l_usecs
- (long long)((long long)secs
* 1000000));
3261 sprintf(×tamp
[tlen
], ".%06ld", (long)usec
);
3264 timestamp
[tlen
] = '\0';
3266 if (filemgr_in_progress
) {
3267 if (class != FILEMGR_CLASS
) {
3268 if (find_event(thread
, -1))
3275 if ((class == FILEMGR_CLASS
) && (columns
> MAXCOLS
|| wideflag
))
3276 clen
= printf("%s %-20.20s", timestamp
, sc_name
);
3277 else if (in_filemgr
)
3278 clen
= printf("%s %-15.15s", timestamp
, sc_name
);
3280 clen
= printf("%s %-17.17s", timestamp
, sc_name
);
3283 framework_name
= NULL
;
3285 if (columns
> MAXCOLS
|| wideflag
) {
3287 off_t offset_reassembled
= 0LL;
3293 * pathname based system calls or
3294 * calls with no fd or pathname (i.e. sync)
3297 clen
+= printf(" [%3d] ", arg1
);
3299 clen
+= printf(" ");
3304 * fd based system call... no I/O
3307 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
3309 clen
+= printf(" F=%-3d", ti
->arg1
);
3317 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
3319 clen
+= printf(" F=%-3d F=%-3d", ti
->arg1
, arg2
);
3324 * system calls with fd's that return an I/O completion count
3327 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
3329 clen
+= printf(" F=%-3d B=0x%-6x", ti
->arg1
, arg2
);
3336 user_addr
= ((uint64_t)arg2
<< 32) | (uint32_t)arg3
;
3338 lookup_name(user_addr
, &framework_type
, &framework_name
);
3339 clen
+= clip_64bit(" A=", user_addr
);
3346 user_addr
= ((uint64_t)arg2
<< 32) | (uint32_t)arg3
;
3348 lookup_name(user_addr
, &framework_type
, &framework_name
);
3349 clen
+= clip_64bit(" A=", user_addr
);
3356 clen
+= printf(" B=0x%-8x", arg2
);
3359 case FMT_HFS_update
:
3362 int sflag
= (int)arg2
;
3364 memset(sbuf
, '_', 6);
3381 clen
+= printf(" (%s) ", sbuf
);
3383 pathname
= find_vnode_name(arg1
);
3394 clen
+= printf(" D=0x%8.8x [%3d]", dio
->blkno
, dio
->io_errno
);
3397 clen
+= printf(" D=0x%8.8x B=0x%-6x BC:%s /dev/%s ", dio
->blkno
, dio
->iosize
, BC_STR(dio
->bc_info
), find_disk_name(dio
->dev
));
3399 clen
+= printf(" D=0x%8.8x B=0x%-6x /dev/%s ", dio
->blkno
, dio
->iosize
, find_disk_name(dio
->dev
));
3402 pathname
= find_meta_name(dio
->blkno
);
3404 pathname
= find_vnode_name(dio
->vnodeid
);
3414 clen
+= printf(" D=0x%8.8x [%3d]", dio
->blkno
, dio
->io_errno
);
3416 clen
+= printf(" D=0x%8.8x B=0x%-6x /dev/%s", dio
->blkno
, dio
->iosize
, generate_cs_disk_name(dio
->dev
, &cs_diskname
[0]));
3419 case FMT_SYNC_DISK_CS
:
3421 * physical disk sync cache
3423 clen
+= printf(" /dev/%s", generate_cs_disk_name(arg1
, &cs_diskname
[0]));
3436 if (ti
->arg3
& MS_ASYNC
)
3437 mlen
+= sprintf(&buf
[mlen
], "MS_ASYNC | ");
3439 mlen
+= sprintf(&buf
[mlen
], "MS_SYNC | ");
3441 if (ti
->arg3
& MS_INVALIDATE
)
3442 mlen
+= sprintf(&buf
[mlen
], "MS_INVALIDATE | ");
3443 if (ti
->arg3
& MS_KILLPAGES
)
3444 mlen
+= sprintf(&buf
[mlen
], "MS_KILLPAGES | ");
3445 if (ti
->arg3
& MS_DEACTIVATE
)
3446 mlen
+= sprintf(&buf
[mlen
], "MS_DEACTIVATE | ");
3448 if (ti
->arg3
& ~(MS_ASYNC
| MS_SYNC
| MS_INVALIDATE
| MS_KILLPAGES
| MS_DEACTIVATE
))
3449 mlen
+= sprintf(&buf
[mlen
], "UNKNOWN | ");
3452 buf
[mlen
- 3] = '\0';
3455 clen
+= printf(" [%3d]", arg1
);
3457 user_addr
= (((off_t
)(unsigned int)(ti
->arg4
)) << 32) | (unsigned int)(ti
->arg1
);
3458 clen
+= clip_64bit(" A=", user_addr
);
3460 user_size
= (((off_t
)(unsigned int)(ti
->arg5
)) << 32) | (unsigned int)(ti
->arg2
);
3462 clen
+= printf(" B=0x%-16qx <%s>", user_size
, buf
);
3476 if (ti
->arg2
& LOCK_SH
)
3477 mlen
+= sprintf(&buf
[mlen
], "LOCK_SH | ");
3478 if (ti
->arg2
& LOCK_EX
)
3479 mlen
+= sprintf(&buf
[mlen
], "LOCK_EX | ");
3480 if (ti
->arg2
& LOCK_NB
)
3481 mlen
+= sprintf(&buf
[mlen
], "LOCK_NB | ");
3482 if (ti
->arg2
& LOCK_UN
)
3483 mlen
+= sprintf(&buf
[mlen
], "LOCK_UN | ");
3485 if (ti
->arg2
& ~(LOCK_SH
| LOCK_EX
| LOCK_NB
| LOCK_UN
))
3486 mlen
+= sprintf(&buf
[mlen
], "UNKNOWN | ");
3489 buf
[mlen
- 3] = '\0';
3492 clen
+= printf(" F=%-3d[%3d] <%s>", ti
->arg1
, arg1
, buf
);
3494 clen
+= printf(" F=%-3d <%s>", ti
->arg1
, buf
);
3508 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
3510 clen
+= printf(" F=%-3d", ti
->arg1
);
3574 case F_PATHPKG_CHECK
:
3575 p
= "PATHPKG_CHECK";
3589 case F_CHECK_OPENEVT
:
3590 p
= "CHECK_OPENEVT";
3600 case F_GLOBAL_NOCACHE
:
3602 p
= "CACHING OFF (GLOBAL)";
3604 p
= "CACHING ON (GLOBAL)";
3610 clen
+= printf(" <%s>", p
);
3612 clen
+= printf(" <%s> F=%d", p
, fd
);
3614 clen
+= printf(" <CMD=%d>", ti
->arg2
);
3625 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
3627 clen
+= printf(" F=%-3d", ti
->arg1
);
3629 clen
+= printf(" <CMD=0x%x>", ti
->arg2
);
3634 case FMT_IOCTL_SYNC
:
3639 clen
+= printf(" <DKIOCSYNCHRONIZECACHE> /dev/%s", find_disk_name(arg1
));
3644 case FMT_IOCTL_UNMAP
:
3649 clen
+= printf(" <DKIOCUNMAP> /dev/%s", find_disk_name(arg1
));
3654 case FMT_UNMAP_INFO
:
3656 clen
+= printf(" D=0x%8.8x B=0x%-6x /dev/%s", arg2
, arg3
, find_disk_name(arg1
));
3666 clen
+= printf(" [%3d]", arg1
);
3668 clen
+= printf(" S=%-3d", arg2
);
3675 * pread, pwrite, lseek
3677 clen
+= printf(" F=%-3d", ti
->arg1
);
3680 clen
+= printf("[%3d] ", arg1
);
3682 if (format
== FMT_PREAD
)
3683 clen
+= printf(" B=0x%-8x ", arg2
);
3685 clen
+= printf(" ");
3687 if (format
== FMT_PREAD
)
3688 offset_reassembled
= (((off_t
)(unsigned int)(ti
->arg3
)) << 32) | (unsigned int)(ti
->arg4
);
3691 offset_reassembled
= (((off_t
)(unsigned int)(arg2
)) << 32) | (unsigned int)(arg3
);
3693 offset_reassembled
= (((off_t
)(unsigned int)(arg3
)) << 32) | (unsigned int)(arg2
);
3695 clen
+= clip_64bit("O=", offset_reassembled
);
3697 if (format
== FMT_LSEEK
) {
3700 if (ti
->arg4
== SEEK_SET
)
3702 else if (ti
->arg4
== SEEK_CUR
)
3704 else if (ti
->arg4
== SEEK_END
)
3709 clen
+= printf(" <%s>", mode
);
3717 clen
+= printf(" F=%-3d ", ti
->arg1
);
3720 clen
+= printf("[%3d] ", arg1
);
3723 user_addr
= (((off_t
)(unsigned int)(ti
->arg2
)) << 32) | (unsigned int)(ti
->arg3
);
3725 clen
+= clip_64bit("A=", user_addr
);
3727 offset_reassembled
= (((off_t
)(unsigned int)(ti
->arg6
)) << 32) | (unsigned int)(ti
->arg7
);
3729 clen
+= clip_64bit("O=", offset_reassembled
);
3731 user_size
= (((off_t
)(unsigned int)(ti
->arg4
)) << 32) | (unsigned int)(ti
->arg5
);
3733 clen
+= printf("B=0x%-16qx", user_size
);
3735 clen
+= printf(" <");
3737 if (ti
->arg8
& PROT_READ
)
3738 clen
+= printf("READ");
3740 if (ti
->arg8
& PROT_WRITE
)
3741 clen
+= printf("|WRITE");
3743 if (ti
->arg8
& PROT_EXEC
)
3744 clen
+= printf("|EXEC");
3746 clen
+= printf(">");
3753 * ftruncate, truncate
3755 if (format
== FMT_FTRUNC
)
3756 clen
+= printf(" F=%-3d", ti
->arg1
);
3758 clen
+= printf(" ");
3761 clen
+= printf("[%3d]", arg1
);
3764 offset_reassembled
= (((off_t
)(unsigned int)(ti
->arg2
)) << 32) | (unsigned int)(ti
->arg3
);
3766 offset_reassembled
= (((off_t
)(unsigned int)(ti
->arg3
)) << 32) | (unsigned int)(ti
->arg2
);
3768 clen
+= clip_64bit(" O=", offset_reassembled
);
3781 if (format
== FMT_FCHFLAGS
) {
3783 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
3785 clen
+= printf(" F=%-3d", ti
->arg1
);
3788 clen
+= printf(" [%3d] ", arg1
);
3793 if (ti
->arg2
& UF_NODUMP
)
3794 mlen
+= sprintf(&buf
[mlen
], "UF_NODUMP | ");
3795 if (ti
->arg2
& UF_IMMUTABLE
)
3796 mlen
+= sprintf(&buf
[mlen
], "UF_IMMUTABLE | ");
3797 if (ti
->arg2
& UF_APPEND
)
3798 mlen
+= sprintf(&buf
[mlen
], "UF_APPEND | ");
3799 if (ti
->arg2
& UF_OPAQUE
)
3800 mlen
+= sprintf(&buf
[mlen
], "UF_OPAQUE | ");
3801 if (ti
->arg2
& SF_ARCHIVED
)
3802 mlen
+= sprintf(&buf
[mlen
], "SF_ARCHIVED | ");
3803 if (ti
->arg2
& SF_IMMUTABLE
)
3804 mlen
+= sprintf(&buf
[mlen
], "SF_IMMUTABLE | ");
3805 if (ti
->arg2
& SF_APPEND
)
3806 mlen
+= sprintf(&buf
[mlen
], "SF_APPEND | ");
3809 mlen
+= sprintf(&buf
[mlen
], "CLEAR_ALL_FLAGS | ");
3810 else if (ti
->arg2
& ~(UF_NODUMP
| UF_IMMUTABLE
| UF_APPEND
| SF_ARCHIVED
| SF_IMMUTABLE
| SF_APPEND
))
3811 mlen
+= sprintf(&buf
[mlen
], "UNKNOWN | ");
3820 memset(&buf
[mlen
], ' ', 19 - mlen
);
3823 clen
+= printf("%s", buf
);
3831 case FMT_FCHMOD_EXT
:
3836 * fchmod, fchmod_extended, chmod, chmod_extended
3840 if (format
== FMT_FCHMOD
|| format
== FMT_FCHMOD_EXT
) {
3842 clen
+= printf(" F=%-3d[%3d] ", ti
->arg1
, arg1
);
3844 clen
+= printf(" F=%-3d ", ti
->arg1
);
3847 clen
+= printf(" [%3d] ", arg1
);
3849 clen
+= printf(" ");
3851 if (format
== FMT_UMASK
)
3853 else if (format
== FMT_FCHMOD
|| format
== FMT_CHMOD
)
3858 get_mode_string(mode
, &buf
[0]);
3861 clen
+= printf("<%s> ", buf
);
3863 clen
+= printf("<%s>", buf
);
3874 memset(mode
, '_', 4);
3877 if (ti
->arg2
& R_OK
)
3879 if (ti
->arg2
& W_OK
)
3881 if (ti
->arg2
& X_OK
)
3883 if (ti
->arg2
== F_OK
)
3887 clen
+= printf(" [%3d] (%s) ", arg1
, mode
);
3889 clen
+= printf(" (%s) ", mode
);
3898 clen
+= printf(" [%3d] <FLGS=0x%x> ", arg1
, ti
->arg3
);
3900 clen
+= printf(" <FLGS=0x%x> ", ti
->arg3
);
3910 if (ti
->arg2
& MNT_FORCE
)
3911 mountflag
= "<FORCE>";
3916 clen
+= printf(" [%3d] %s ", arg1
, mountflag
);
3918 clen
+= printf(" %s ", mountflag
);
3931 memset(mode
, '_', 6);
3934 if (ti
->arg2
& O_RDWR
) {
3937 } else if (ti
->arg2
& O_WRONLY
)
3942 if (ti
->arg2
& O_CREAT
)
3945 if (ti
->arg2
& O_APPEND
)
3948 if (ti
->arg2
& O_TRUNC
)
3951 if (ti
->arg2
& O_EXCL
)
3955 clen
+= printf(" [%3d] (%s) ", arg1
, mode
);
3957 clen
+= printf(" F=%-3d (%s) ", arg2
, mode
);
3991 domain
= "AF_IMPLINK";
4002 type
= "SOCK_STREAM";
4006 type
= "SOCK_DGRAM";
4013 case SOCK_SEQPACKET
:
4014 type
= "SOCK_SEQPACKET";
4027 clen
+= printf(" [%3d] <%s, %s, 0x%x>", arg1
, domain
, type
, ti
->arg3
);
4029 clen
+= printf(" F=%-3d <%s, %s, 0x%x>", arg2
, domain
, type
, ti
->arg3
);
4036 * aio_fsync [errno] AIOCBP OP
4040 if (ti
->arg1
== O_SYNC
|| ti
->arg1
== 0)
4043 else if (ti
->arg1
== O_DSYNC
)
4050 clen
+= printf(" [%3d] P=0x%8.8x <%s>", arg1
, ti
->arg2
, op
);
4052 clen
+= printf(" P=0x%8.8x <%s>", ti
->arg2
, op
);
4056 case FMT_AIO_RETURN
:
4058 * aio_return [errno] AIOCBP IOSIZE
4061 clen
+= printf(" [%3d] P=0x%8.8x", arg1
, ti
->arg1
);
4063 clen
+= printf(" P=0x%8.8x B=0x%-8x", ti
->arg1
, arg2
);
4066 case FMT_AIO_SUSPEND
:
4068 * aio_suspend [errno] NENTS
4071 clen
+= printf(" [%3d] N=%d", arg1
, ti
->arg2
);
4073 clen
+= printf(" N=%d", ti
->arg2
);
4076 case FMT_AIO_CANCEL
:
4078 * aio_cancel [errno] FD or AIOCBP (if non-null)
4082 clen
+= printf(" [%3d] P=0x%8.8x", arg1
, ti
->arg2
);
4084 clen
+= printf(" P=0x%8.8x", ti
->arg2
);
4087 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
4089 clen
+= printf(" F=%-3d", ti
->arg1
);
4095 * aio_error, aio_read, aio_write [errno] AIOCBP
4098 clen
+= printf(" [%3d] P=0x%8.8x", arg1
, ti
->arg1
);
4100 clen
+= printf(" P=0x%8.8x", ti
->arg1
);
4103 case FMT_LIO_LISTIO
:
4106 * lio_listio [errno] NENTS MODE
4110 if (ti
->arg1
== LIO_NOWAIT
)
4112 else if (ti
->arg1
== LIO_WAIT
)
4118 clen
+= printf(" [%3d] N=%d <%s>", arg1
, ti
->arg3
, op
);
4120 clen
+= printf(" N=%d <%s>", ti
->arg3
, op
);
4128 * Calculate space available to print pathname
4130 if (columns
> MAXCOLS
|| wideflag
)
4131 clen
= columns
- (clen
+ 14 + 20 + 11);
4133 clen
= columns
- (clen
+ 14 + 12);
4135 if (class != FILEMGR_CLASS
&& !nopadding
)
4139 len
= sprintf(&buf
[0], " %s %s ", framework_type
, framework_name
);
4140 else if (*pathname
!= '\0') {
4141 len
= sprintf(&buf
[0], " %s ", pathname
);
4143 if (format
== FMT_MOUNT
&& ti
->lookups
[1].pathname
[0]) {
4146 memset(&buf
[len
], ' ', 2);
4148 len2
= sprintf(&buf
[len
+2], " %s ", (char *)&ti
->lookups
[1].pathname
[0]);
4149 len
= len
+ 2 + len2
;
4156 * Add null padding if column length
4157 * is wider than the pathname length.
4159 memset(&buf
[len
], ' ', clen
- len
);
4164 } else if (clen
== len
) {
4167 } else if ((clen
> 0) && (clen
< len
)) {
4169 * This prints the tail end of the pathname
4171 buf
[len
-clen
] = ' ';
4173 pathname
= &buf
[len
- clen
];
4180 * fudge some additional system call overhead
4181 * that currently isn't tracked... this also
4182 * insures that we see a minimum of 1 us for
4185 usecs
= (unsigned long)(((now
- stime
) + (divisor
-1)) / divisor
);
4186 secs
= usecs
/ 1000000;
4187 usecs
-= secs
* 1000000;
4189 if (class != FILEMGR_CLASS
&& !nopadding
)
4199 if (columns
> MAXCOLS
|| wideflag
)
4200 printf("%s%s %3ld.%06ld%s %s.%d\n", p1
, pathname
, (unsigned long)secs
, (unsigned long)usecs
, p2
, command_name
, (int)thread
);
4202 printf("%s%s %3ld.%06ld%s %-12.12s\n", p1
, pathname
, (unsigned long)secs
, (unsigned long)usecs
, p2
, command_name
);
4207 add_meta_name(uint64_t blockno
, char *pathname
) {
4211 hashid
= blockno
& VN_HASH_MASK
;
4213 for (mi
= m_info_hash
[hashid
]; mi
; mi
= mi
->m_next
) {
4214 if (mi
->m_blkno
== blockno
)
4218 mi
= (meta_info_t
)malloc(sizeof(struct meta_info
));
4220 mi
->m_next
= m_info_hash
[hashid
];
4221 m_info_hash
[hashid
] = mi
;
4222 mi
->m_blkno
= blockno
;
4224 mi
->m_nameptr
= pathname
;
4228 find_meta_name(uint64_t blockno
) {
4232 hashid
= blockno
& VN_HASH_MASK
;
4234 for (mi
= m_info_hash
[hashid
]; mi
; mi
= mi
->m_next
) {
4235 if (mi
->m_blkno
== blockno
)
4236 return (mi
->m_nameptr
);
4243 add_vnode_name(uint64_t vn_id
, char *pathname
) {
4247 hashid
= (vn_id
>> VN_HASH_SHIFT
) & VN_HASH_MASK
;
4249 for (vn
= vn_info_hash
[hashid
]; vn
; vn
= vn
->vn_next
) {
4250 if (vn
->vn_id
== vn_id
)
4254 vn
= (vnode_info_t
)malloc(sizeof(struct vnode_info
));
4256 vn
->vn_next
= vn_info_hash
[hashid
];
4257 vn_info_hash
[hashid
] = vn
;
4260 strcpy(vn
->vn_pathname
, pathname
);
4262 return (&vn
->vn_pathname
);
4267 find_vnode_name(uint64_t vn_id
) {
4271 hashid
= (vn_id
>> VN_HASH_SHIFT
) & VN_HASH_MASK
;
4273 for (vn
= vn_info_hash
[hashid
]; vn
; vn
= vn
->vn_next
) {
4274 if (vn
->vn_id
== vn_id
)
4275 return (vn
->vn_pathname
);
4282 delete_event(th_info_t ti_to_delete
) {
4287 hashid
= ti_to_delete
->thread
& HASH_MASK
;
4289 if ((ti
= th_info_hash
[hashid
])) {
4290 if (ti
== ti_to_delete
)
4291 th_info_hash
[hashid
] = ti
->next
;
4295 for (ti
= ti
->next
; ti
; ti
= ti
->next
) {
4296 if (ti
== ti_to_delete
) {
4297 ti_prev
->next
= ti
->next
;
4304 ti
->next
= th_info_freelist
;
4305 th_info_freelist
= ti
;
4311 add_event(uintptr_t thread
, int type
) {
4316 if ((ti
= th_info_freelist
))
4317 th_info_freelist
= ti
->next
;
4319 ti
= (th_info_t
)malloc(sizeof(struct th_info
));
4321 hashid
= thread
& HASH_MASK
;
4323 ti
->next
= th_info_hash
[hashid
];
4324 th_info_hash
[hashid
] = ti
;
4326 ti
->thread
= thread
;
4331 ti
->in_hfs_update
= 0;
4333 ti
->pathptr
= &ti
->lookups
[0].pathname
[0];
4334 ti
->pn_scall_index
= 0;
4335 ti
->pn_work_index
= 0;
4337 for (i
= 0; i
< MAX_PATHNAMES
; i
++)
4338 ti
->lookups
[i
].pathname
[0] = 0;
4344 find_event(uintptr_t thread
, int type
) {
4348 hashid
= thread
& HASH_MASK
;
4350 for (ti
= th_info_hash
[hashid
]; ti
; ti
= ti
->next
) {
4351 if (ti
->thread
== thread
) {
4352 if (type
== ti
->type
)
4354 if (ti
->in_filemgr
) {
4363 return ((th_info_t
) 0);
4367 delete_all_events() {
4369 th_info_t ti_next
= 0;
4372 for (i
= 0; i
< HASH_SIZE
; i
++) {
4374 for (ti
= th_info_hash
[i
]; ti
; ti
= ti_next
) {
4376 ti
->next
= th_info_freelist
;
4377 th_info_freelist
= ti
;
4379 th_info_hash
[i
] = 0;
4385 mark_thread_waited(uintptr_t thread
) {
4389 hashid
= thread
& HASH_MASK
;
4391 for (ti
= th_info_hash
[hashid
]; ti
; ti
= ti
->next
) {
4392 if (ti
->thread
== thread
)
4398 void read_command_map()
4402 int total_threads
= 0;
4403 kd_threadmap
*mapptr
= 0;
4405 delete_all_map_entries();
4409 total_threads
= bufinfo
.nkdthreads
;
4410 size
= bufinfo
.nkdthreads
* sizeof(kd_threadmap
);
4413 if ((mapptr
= (kd_threadmap
*) malloc(size
))) {
4416 bzero (mapptr
, size
);
4418 * Now read the threadmap
4421 mib
[1] = KERN_KDEBUG
;
4422 mib
[2] = KERN_KDTHRMAP
;
4425 mib
[5] = 0; /* no flags */
4427 if (sysctl(mib
, 3, mapptr
, &size
, NULL
, 0) < 0) {
4429 * This is not fatal -- just means I cant map command strings
4440 RAW_fd
= open(RAW_file
, O_RDONLY
);
4443 perror("Can't open RAW file");
4446 if (read(RAW_fd
, &header
, sizeof(RAW_header
)) != sizeof(RAW_header
)) {
4447 perror("read failed");
4450 if (header
.version_no
!= RAW_VERSION1
) {
4451 header
.version_no
= RAW_VERSION0
;
4452 header
.TOD_secs
= time((long *)0);
4453 header
.TOD_usecs
= 0;
4455 lseek(RAW_fd
, (off_t
)0, SEEK_SET
);
4457 if (read(RAW_fd
, &header
.thread_count
, sizeof(int)) != sizeof(int)) {
4458 perror("read failed");
4462 sample_TOD_secs
= header
.TOD_secs
;
4463 sample_TOD_usecs
= header
.TOD_usecs
;
4465 total_threads
= header
.thread_count
;
4466 size
= total_threads
* sizeof(kd_threadmap
);
4469 if ((mapptr
= (kd_threadmap
*) malloc(size
))) {
4470 bzero (mapptr
, size
);
4472 if (read(RAW_fd
, mapptr
, size
) != size
) {
4478 if (header
.version_no
!= RAW_VERSION0
) {
4479 offset
= lseek(RAW_fd
, (off_t
)0, SEEK_CUR
);
4480 offset
= (offset
+ (4095)) & ~4095;
4482 lseek(RAW_fd
, offset
, SEEK_SET
);
4485 for (i
= 0; i
< total_threads
; i
++)
4486 create_map_entry(mapptr
[i
].thread
, mapptr
[i
].valid
, &mapptr
[i
].command
[0]);
4492 void delete_all_map_entries()
4494 threadmap_t tme
= 0;
4495 threadmap_t tme_next
= 0;
4498 for (i
= 0; i
< HASH_SIZE
; i
++) {
4500 for (tme
= threadmap_hash
[i
]; tme
; tme
= tme_next
) {
4502 free(tme
->tm_setptr
);
4503 tme_next
= tme
->tm_next
;
4504 tme
->tm_next
= threadmap_freelist
;
4505 threadmap_freelist
= tme
;
4507 threadmap_hash
[i
] = 0;
4512 void create_map_entry(uintptr_t thread
, int pid
, char *command
)
4517 if ((tme
= threadmap_freelist
))
4518 threadmap_freelist
= tme
->tm_next
;
4520 tme
= (threadmap_t
)malloc(sizeof(struct threadmap
));
4522 tme
->tm_thread
= thread
;
4523 tme
->tm_setsize
= 0;
4526 (void)strncpy (tme
->tm_command
, command
, MAXCOMLEN
);
4527 tme
->tm_command
[MAXCOMLEN
] = '\0';
4529 hashid
= thread
& HASH_MASK
;
4531 tme
->tm_next
= threadmap_hash
[hashid
];
4532 threadmap_hash
[hashid
] = tme
;
4534 if (pid
!= 0 && pid
!= 1) {
4535 if (!strncmp(command
, "LaunchCFMA", 10))
4536 (void)get_real_command_name(pid
, tme
->tm_command
, MAXCOMLEN
);
4542 find_map_entry(uintptr_t thread
)
4547 hashid
= thread
& HASH_MASK
;
4549 for (tme
= threadmap_hash
[hashid
]; tme
; tme
= tme
->tm_next
) {
4550 if (tme
->tm_thread
== thread
)
4558 delete_map_entry(uintptr_t thread
)
4560 threadmap_t tme
= 0;
4561 threadmap_t tme_prev
;
4564 hashid
= thread
& HASH_MASK
;
4566 if ((tme
= threadmap_hash
[hashid
])) {
4567 if (tme
->tm_thread
== thread
)
4568 threadmap_hash
[hashid
] = tme
->tm_next
;
4572 for (tme
= tme
->tm_next
; tme
; tme
= tme
->tm_next
) {
4573 if (tme
->tm_thread
== thread
) {
4574 tme_prev
->tm_next
= tme
->tm_next
;
4582 free(tme
->tm_setptr
);
4584 tme
->tm_next
= threadmap_freelist
;
4585 threadmap_freelist
= tme
;
4592 fs_usage_fd_set(uintptr_t thread
, unsigned int fd
)
4596 if ((tme
= find_map_entry(thread
)) == 0)
4599 * If the map is not allocated, then now is the time
4601 if (tme
->tm_setptr
== (unsigned long *)0) {
4602 if ((tme
->tm_setptr
= (unsigned long *)malloc(FS_USAGE_NFDBYTES(FS_USAGE_FD_SETSIZE
))) == 0)
4605 tme
->tm_setsize
= FS_USAGE_FD_SETSIZE
;
4606 bzero(tme
->tm_setptr
, (FS_USAGE_NFDBYTES(FS_USAGE_FD_SETSIZE
)));
4609 * If the map is not big enough, then reallocate it
4611 while (tme
->tm_setsize
<= fd
) {
4614 n
= tme
->tm_setsize
* 2;
4615 tme
->tm_setptr
= (unsigned long *)realloc(tme
->tm_setptr
, (FS_USAGE_NFDBYTES(n
)));
4617 bzero(&tme
->tm_setptr
[(tme
->tm_setsize
/FS_USAGE_NFDBITS
)], (FS_USAGE_NFDBYTES(tme
->tm_setsize
)));
4618 tme
->tm_setsize
= n
;
4623 tme
->tm_setptr
[fd
/FS_USAGE_NFDBITS
] |= (1 << ((fd
) % FS_USAGE_NFDBITS
));
4629 * 0 : File Descriptor bit is not set
4630 * 1 : File Descriptor bit is set
4633 fs_usage_fd_isset(uintptr_t thread
, unsigned int fd
)
4638 if ((tme
= find_map_entry(thread
))) {
4639 if (tme
->tm_setptr
&& fd
< tme
->tm_setsize
)
4640 ret
= tme
->tm_setptr
[fd
/FS_USAGE_NFDBITS
] & (1 << (fd
% FS_USAGE_NFDBITS
));
4647 fs_usage_fd_clear(uintptr_t thread
, unsigned int fd
)
4651 if ((tme
= find_map_entry(thread
))) {
4652 if (tme
->tm_setptr
&& fd
< tme
->tm_setsize
)
4653 tme
->tm_setptr
[fd
/FS_USAGE_NFDBITS
] &= ~(1 << (fd
% FS_USAGE_NFDBITS
));
4666 ret
= (int)strtol(str
, &cp
, 10);
4668 if (cp
== str
|| *cp
) {
4670 * Assume this is a command string and find matching pids
4675 for (i
= 0; i
< kp_nentries
&& num_of_pids
< (MAX_PIDS
- 1); i
++) {
4676 if (kp_buffer
[i
].kp_proc
.p_stat
== 0)
4679 if (!strncmp(str
, kp_buffer
[i
].kp_proc
.p_comm
,
4680 sizeof(kp_buffer
[i
].kp_proc
.p_comm
) -1))
4681 pids
[num_of_pids
++] = kp_buffer
[i
].kp_proc
.p_pid
;
4685 else if (num_of_pids
< (MAX_PIDS
- 1))
4686 pids
[num_of_pids
++] = ret
;
4692 lookup_name(uint64_t user_addr
, char **type
, char **name
)
4700 if (numFrameworks
) {
4702 if ((user_addr
>= framework32
.b_address
&& user_addr
< framework32
.e_address
) ||
4703 (user_addr
>= framework64
.b_address
&& user_addr
< framework64
.e_address
)) {
4706 last
= numFrameworks
;
4708 for (i
= numFrameworks
/ 2; start
< last
; i
= start
+ ((last
- start
) / 2)) {
4709 if (user_addr
> frameworkInfo
[i
].e_address
)
4714 if (start
< numFrameworks
&&
4715 user_addr
>= frameworkInfo
[start
].b_address
&& user_addr
< frameworkInfo
[start
].e_address
) {
4716 *type
= frameworkType
[frameworkInfo
[start
].r_type
];
4717 *name
= frameworkInfo
[start
].name
;
4725 * Comparison routines for sorting
4727 static int compareFrameworkAddress(const void *aa
, const void *bb
)
4729 LibraryInfo
*a
= (LibraryInfo
*)aa
;
4730 LibraryInfo
*b
= (LibraryInfo
*)bb
;
4732 if (a
->b_address
< b
->b_address
) return -1;
4733 if (a
->b_address
== b
->b_address
) return 0;
4738 int scanline(char *inputstring
, char **argv
, int maxtokens
)
4741 char **ap
= argv
, *p
, *val
;
4743 for (p
= inputstring
; n
< maxtokens
&& p
!= NULL
; ) {
4745 while ((val
= strsep(&p
, " \t")) != NULL
&& *val
== '\0');
4756 int ReadSharedCacheMap(const char *path
, LibraryRange
*lr
, char *linkedit_name
)
4758 uint64_t b_address
, e_address
;
4762 char frameworkName
[256];
4766 int linkedit_found
= 0;
4767 char *substring
, *ptr
;
4769 bzero(buf
, sizeof(buf
));
4770 bzero(tokens
, sizeof(tokens
));
4775 if ((fd
= fopen(path
, "r")) == 0)
4778 while (fgets(buf
, 1023, fd
)) {
4779 if (strncmp(buf
, "mapping", 7))
4782 buf
[strlen(buf
)-1] = 0;
4784 frameworkName
[0] = 0;
4788 * Extract lib name from path name
4790 if ((substring
= strrchr(buf
, '.')))
4793 * There is a ".": name is whatever is between the "/" around the "."
4795 while ( *substring
!= '/') /* find "/" before "." */
4799 strncpy(frameworkName
, substring
, 256); /* copy path from "/" */
4800 frameworkName
[255] = 0;
4801 substring
= frameworkName
;
4803 while ( *substring
!= '/' && *substring
) /* find "/" after "." and stop string there */
4810 * No ".": take segment after last "/"
4817 substring
= ptr
+ 1;
4820 strncpy(frameworkName
, substring
, 256);
4821 frameworkName
[255] = 0;
4823 fnp
= (char *)malloc(strlen(frameworkName
) + 1);
4824 strcpy(fnp
, frameworkName
);
4826 while (fgets(buf
, 1023, fd
) && numFrameworks
< (MAXINDEX
- 2)) {
4830 buf
[strlen(buf
)-1] = 0;
4832 ntokens
= scanline(buf
, tokens
, 64);
4837 if (strncmp(tokens
[0], "__TEXT", 6) == 0)
4839 else if (strncmp(tokens
[0], "__DATA", 6) == 0)
4841 else if (strncmp(tokens
[0], "__OBJC", 6) == 0)
4843 else if (strncmp(tokens
[0], "__IMPORT", 8) == 0)
4845 else if (strncmp(tokens
[0], "__UNICODE", 9) == 0)
4847 else if (strncmp(tokens
[0], "__IMAGE", 7) == 0)
4849 else if (strncmp(tokens
[0], "__LINKEDIT", 10) == 0)
4854 if (type
== LINKEDIT_R
&& linkedit_found
)
4858 b_address
= strtoull(tokens
[1], 0, 16);
4859 e_address
= strtoull(tokens
[3], 0, 16);
4861 frameworkInfo
[numFrameworks
].b_address
= b_address
;
4862 frameworkInfo
[numFrameworks
].e_address
= e_address
;
4863 frameworkInfo
[numFrameworks
].r_type
= type
;
4865 if (type
== LINKEDIT_R
) {
4866 frameworkInfo
[numFrameworks
].name
= linkedit_name
;
4869 frameworkInfo
[numFrameworks
].name
= fnp
;
4871 printf("%s(%d): %qx-%qx\n", frameworkInfo
[numFrameworks
].name
, type
, b_address
, e_address
);
4873 if (lr
->b_address
== 0 || b_address
< lr
->b_address
)
4874 lr
->b_address
= b_address
;
4876 if (lr
->e_address
== 0 || e_address
> lr
->e_address
)
4877 lr
->e_address
= e_address
;
4881 if (type
== LINKEDIT_R
)
4884 if (fgets(buf
, 1023, fd
) == 0)
4887 buf
[strlen(buf
)-1] = 0;
4892 printf("%s range, %qx-%qx\n", path
, lr
->b_address
, lr
->e_address
);
4899 SortFrameworkAddresses()
4902 frameworkInfo
[numFrameworks
].b_address
= frameworkInfo
[numFrameworks
- 1].b_address
+ 0x800000;
4903 frameworkInfo
[numFrameworks
].e_address
= frameworkInfo
[numFrameworks
].b_address
;
4904 frameworkInfo
[numFrameworks
].name
= (char *)0;
4906 qsort(frameworkInfo
, numFrameworks
, sizeof(LibraryInfo
), compareFrameworkAddress
);
4910 struct diskio
*insert_diskio(int type
, int bp
, int dev
, int blkno
, int io_size
, uintptr_t thread
, double curtime
)
4915 if ((dio
= free_diskios
))
4916 free_diskios
= dio
->next
;
4918 if ((dio
= (struct diskio
*)malloc(sizeof(struct diskio
))) == NULL
)
4927 dio
->iosize
= io_size
;
4928 dio
->issued_time
= curtime
;
4929 dio
->issuing_thread
= thread
;
4933 if ((tme
= find_map_entry(thread
))) {
4934 strncpy(dio
->issuing_command
, tme
->tm_command
, MAXCOMLEN
);
4935 dio
->issuing_command
[MAXCOMLEN
-1] = '\0';
4937 strcpy(dio
->issuing_command
, "");
4939 dio
->next
= busy_diskios
;
4941 dio
->next
->prev
= dio
;
4947 struct diskio
*find_diskio(int bp
) {
4950 for (dio
= busy_diskios
; dio
; dio
= dio
->next
) {
4959 struct diskio
*complete_diskio(int bp
, int io_errno
, int resid
, uintptr_t thread
, double curtime
)
4963 if ((dio
= find_diskio(bp
)) == NULL
) return NULL
;
4965 if (dio
== busy_diskios
) {
4966 if ((busy_diskios
= dio
->next
))
4967 dio
->next
->prev
= NULL
;
4970 dio
->next
->prev
= dio
->prev
;
4971 dio
->prev
->next
= dio
->next
;
4974 dio
->iosize
-= resid
;
4975 dio
->io_errno
= io_errno
;
4976 dio
->completed_time
= curtime
;
4977 dio
->completion_thread
= thread
;
4983 void free_diskio(struct diskio
*dio
)
4985 dio
->next
= free_diskios
;
4990 void print_diskio(struct diskio
*dio
)
4995 int format
= FMT_DISKIO
;
5001 if ((type
& P_CS_Class
) == P_CS_Class
) {
5005 case P_CS_ReadChunk
:
5008 format
= FMT_DISKIO_CS
;
5010 case P_CS_WriteChunk
:
5013 format
= FMT_DISKIO_CS
;
5018 format
= FMT_DISKIO_CS
;
5020 case P_CS_MetaWrite
:
5023 format
= FMT_DISKIO_CS
;
5025 case P_CS_TransformRead
:
5029 case P_CS_TransformWrite
:
5033 case P_CS_MigrationRead
:
5037 case P_CS_MigrationWrite
:
5042 strncpy(buf
, p
, len
);
5045 switch (type
& P_DISKIO_TYPE
) {
5078 strncpy(buf
, p
, len
);
5082 if (type
& P_DISKIO_ASYNC
)
5087 if (type
& P_DISKIO_NOCACHE
)
5090 int tier
= (type
& P_DISKIO_TIER_MASK
) >> P_DISKIO_TIER_SHIFT
;
5093 if (tier
> 0 && tier
< 10)
5094 buf
[len
++] = '0' + tier
;
5097 if (type
& P_DISKIO_PASSIVE
)
5105 if (check_filter_mode(NULL
, type
, 0, 0, buf
))
5106 format_print(NULL
, buf
, dio
->issuing_thread
, type
, 0, 0, 0, 0, format
, dio
->completed_time
, dio
->issued_time
, 1, "", dio
);
5110 void cache_disk_names()
5115 struct diskrec
*dnp
;
5118 if ((dirp
= opendir("/dev")) == NULL
)
5121 while ((dir
= readdir(dirp
)) != NULL
) {
5122 char nbuf
[MAXPATHLEN
];
5124 if (dir
->d_namlen
< 5 || strncmp("disk", dir
->d_name
, 4))
5127 snprintf(nbuf
, MAXPATHLEN
, "%s/%s", "/dev", dir
->d_name
);
5129 if (stat(nbuf
, &st
) < 0)
5132 if ((dnp
= (struct diskrec
*)malloc(sizeof(struct diskrec
))) == NULL
)
5135 if ((dnp
->diskname
= (char *)malloc(dir
->d_namlen
+ 1)) == NULL
) {
5139 strncpy(dnp
->diskname
, dir
->d_name
, dir
->d_namlen
);
5140 dnp
->diskname
[dir
->d_namlen
] = 0;
5141 dnp
->dev
= st
.st_rdev
;
5143 dnp
->next
= disk_list
;
5146 (void) closedir(dirp
);
5150 void recache_disk_names()
5152 struct diskrec
*dnp
, *next_dnp
;
5154 for (dnp
= disk_list
; dnp
; dnp
= next_dnp
) {
5155 next_dnp
= dnp
->next
;
5157 free(dnp
->diskname
);
5166 char *find_disk_name(int dev
)
5168 struct diskrec
*dnp
;
5177 for (i
= 0; i
< 2; i
++) {
5178 for (dnp
= disk_list
; dnp
; dnp
= dnp
->next
) {
5179 if (dnp
->dev
== dev
)
5180 return (dnp
->diskname
);
5182 recache_disk_names();
5184 return ("NOTFOUND");
5188 char *generate_cs_disk_name(int dev
, char *s
)
5193 sprintf(s
, "disk%ds%d", (dev
>> 16) & 0xffff, dev
& 0xffff);
5201 * ret = 1 means print the entry
5202 * ret = 0 means don't print the entry
5206 * meaning of filter flags:
5207 * cachehit turn on display of CACHE_HIT events (which are filtered out by default)
5209 * exec show exec/posix_spawn
5210 * pathname show events with a pathname and close()
5211 * diskio show disk I/Os
5212 * filesys show filesystem events
5213 * network show network events
5215 * filters may be combined; default is all filters on (except cachehit)
5218 check_filter_mode(struct th_info
*ti
, int type
, int error
, int retval
, char *sc_name
)
5221 int network_fd_isset
= 0;
5224 /* cachehit is special -- it's not on by default */
5225 if (sc_name
[0] == 'C' && !strcmp(sc_name
, "CACHE_HIT")) {
5226 if (show_cachehits
) return 1;
5230 if (filter_mode
== DEFAULT_DO_NOT_FILTER
)
5233 if (filter_mode
& DISKIO_FILTER
) {
5234 if ((type
& P_DISKIO_MASK
) == P_DISKIO
)
5238 if (filter_mode
& EXEC_FILTER
) {
5239 if (type
== BSC_execve
|| type
== BSC_posix_spawn
)
5243 if (filter_mode
& PATHNAME_FILTER
) {
5244 if (ti
&& ti
->lookups
[0].pathname
[0])
5246 if (type
== BSC_close
|| type
== BSC_close_nocancel
||
5247 type
== BSC_guarded_close_np
)
5251 if (ti
== (struct th_info
*)0) {
5252 if (filter_mode
& FILESYS_FILTER
)
5260 case BSC_close_nocancel
:
5261 case BSC_guarded_close_np
:
5263 network_fd_isset
= fs_usage_fd_isset(ti
->thread
, fd
);
5266 fs_usage_fd_clear(ti
->thread
,fd
);
5268 if (network_fd_isset
) {
5269 if (filter_mode
& NETWORK_FILTER
)
5272 if (filter_mode
& FILESYS_FILTER
)
5278 case BSC_read_nocancel
:
5279 case BSC_write_nocancel
:
5281 * we don't care about error in these cases
5284 network_fd_isset
= fs_usage_fd_isset(ti
->thread
, fd
);
5286 if (network_fd_isset
) {
5287 if (filter_mode
& NETWORK_FILTER
)
5290 if (filter_mode
& FILESYS_FILTER
)
5295 case BSC_accept_nocancel
:
5300 fs_usage_fd_set(ti
->thread
, fd
);
5301 if (filter_mode
& NETWORK_FILTER
)
5312 case BSC_sendto_nocancel
:
5313 case BSC_recvfrom_nocancel
:
5314 case BSC_recvmsg_nocancel
:
5315 case BSC_sendmsg_nocancel
:
5316 case BSC_connect_nocancel
:
5320 fs_usage_fd_set(ti
->thread
, fd
);
5321 if (filter_mode
& NETWORK_FILTER
)
5326 case BSC_select_nocancel
:
5327 case BSC_socketpair
:
5329 * Cannot determine info about file descriptors
5331 if (filter_mode
& NETWORK_FILTER
)
5338 * We track these cases for fd state only
5341 network_fd_isset
= fs_usage_fd_isset(ti
->thread
, fd
);
5343 if (error
== 0 && network_fd_isset
) {
5345 * then we are duping a socket descriptor
5347 fd
= retval
; /* the new fd */
5348 fs_usage_fd_set(ti
->thread
, fd
);
5353 if (filter_mode
& FILESYS_FILTER
)
5362 * Allocate a buffer that is large enough to hold the maximum arguments
5363 * to execve(). This is used when getting the arguments to programs
5364 * when we see LaunchCFMApps. If this fails, it is not fatal, we will
5365 * simply not resolve the command name.
5369 init_arguments_buffer()
5375 mib
[1] = KERN_ARGMAX
;
5376 size
= sizeof(argmax
);
5378 if (sysctl(mib
, 2, &argmax
, &size
, NULL
, 0) == -1)
5381 /* Hack to avoid kernel bug. */
5382 if (argmax
> 8192) {
5386 arguments
= (char *)malloc(argmax
);
5391 get_real_command_name(int pid
, char *cbuf
, int csize
)
5394 * Get command and arguments.
5398 char *command_beg
, *command
, *command_end
;
5404 bzero(arguments
, argmax
);
5409 * A sysctl() is made to find out the full path that the command
5413 mib
[1] = KERN_PROCARGS2
;
5417 if (sysctl(mib
, 3, arguments
, (size_t *)&argmax
, NULL
, 0) < 0)
5421 * Skip the saved exec_path
5423 for (cp
= arguments
; cp
< &arguments
[argmax
]; cp
++) {
5426 * End of exec_path reached
5431 if (cp
== &arguments
[argmax
])
5435 * Skip trailing '\0' characters
5437 for (; cp
< &arguments
[argmax
]; cp
++) {
5440 * Beginning of first argument reached
5445 if (cp
== &arguments
[argmax
])
5450 * Make sure that the command is '\0'-terminated. This protects
5451 * against malicious programs; under normal operation this never
5452 * ends up being a problem..
5454 for (; cp
< &arguments
[argmax
]; cp
++) {
5457 * End of first argument reached
5462 if (cp
== &arguments
[argmax
])
5465 command_end
= command
= cp
;
5468 * Get the basename of command
5470 for (command
--; command
>= command_beg
; command
--) {
5471 if (*command
== '/') {
5476 (void) strncpy(cbuf
, (char *)command
, csize
);
5477 cbuf
[csize
-1] = '\0';