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. -DPRIVATE -D__APPLE_PRIVATE -O -o fs_usage fs_usage.c
29 #define Default_DELAY 1 /* default delay interval */
41 #include <sys/types.h>
42 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/socket.h>
51 #ifndef KERNEL_PRIVATE
52 #define KERNEL_PRIVATE
53 #include <sys/kdebug.h>
56 #include <sys/kdebug.h>
57 #endif /*KERNEL_PRIVATE*/
59 #include <sys/sysctl.h>
61 #import <mach/clock_types.h>
62 #import <mach/mach_time.h>
71 typedef struct LibraryRange
{
76 LibraryRange framework32
;
77 LibraryRange framework64
;
80 typedef struct LibraryInfo
{
86 LibraryInfo frameworkInfo
[MAXINDEX
];
87 int numFrameworks
= 0;
89 char *lookup_name(uint64_t user_addr
);
93 MAXCOLS controls when extra data kicks in.
94 MAX_WIDE_MODE_COLS controls -w mode to get even wider data in path.
95 If NUMPARMS changes to match the kernel, it will automatically
96 get reflected in the -w mode output.
99 #define PATHLENGTH (NUMPARMS*sizeof(long))
101 #define MAX_WIDE_MODE_COLS (PATHLENGTH + 80)
102 #define MAXWIDTH MAX_WIDE_MODE_COLS + 64
122 long pathname
[NUMPARMS
+ 1]; /* add room for null terminator */
125 #define MAX_THREADS 512
126 struct th_info th_state
[MAX_THREADS
];
128 kd_threadmap
*last_map
= NULL
;
130 int map_is_the_same
= 0;
132 int filemgr_in_progress
= 0;
133 int execs_in_progress
= 0;
136 int need_new_map
= 1;
141 int select_pid_mode
= 0; /* Flag set indicates that output is restricted
142 to selected pids or commands */
144 int one_good_pid
= 0; /* Used to fail gracefully when bad pids given */
151 #define USLEEP_BEHIND 2
152 #define USLEEP_MAX 32
153 int usleep_ms
= USLEEP_MIN
;
156 * Network only or filesystem only output filter
157 * Default of zero means report all activity - no filtering
159 #define FILESYS_FILTER 0x01
160 #define NETWORK_FILTER 0x02
161 #define CACHEHIT_FILTER 0x04
162 #define EXEC_FILTER 0x08
163 #define PATHNAME_FILTER 0x10
164 #define DEFAULT_DO_NOT_FILTER 0x00
166 int filter_mode
= CACHEHIT_FILTER
;
171 struct diskrec
*next
;
186 int completion_thread
;
187 char issuing_command
[MAXCOMLEN
];
189 double completed_time
;
192 struct diskrec
*disk_list
= NULL
;
193 struct diskio
*free_diskios
= NULL
;
194 struct diskio
*busy_diskios
= NULL
;
198 struct diskio
*insert_diskio();
199 struct diskio
*complete_diskio();
202 void format_print(struct th_info
*, char *, int, int, int, int, int, int, int, double, double, int, char *, struct diskio
*);
203 void exit_syscall(char *, int, int, int, int, int, int, int, double);
204 char *find_disk_name();
205 void cache_disk_names();
206 int ReadSharedCacheMap(const char *, LibraryRange
*);
207 void SortFrameworkAddresses();
208 void mark_thread_waited(int);
209 int check_filter_mode(struct th_info
*, int, int, int, char *);
210 void fs_usage_fd_set(unsigned int, unsigned int);
211 int fs_usage_fd_isset(unsigned int, unsigned int);
212 void fs_usage_fd_clear(unsigned int, unsigned int);
213 void init_arguments_buffer();
214 int get_real_command_name(int, char *, int);
215 void create_map_entry(int, int, char *);
217 void enter_syscall();
218 void extend_syscall();
219 void kill_thread_map();
221 #define CLASS_MASK 0xff000000
222 #define CSC_MASK 0xffff0000
223 #define BSC_INDEX(type) ((type >> 2) & 0x3fff)
226 #define TRACE_DATA_NEWTHREAD 0x07000004
227 #define TRACE_DATA_EXEC 0x07000008
228 #define TRACE_STRING_NEWTHREAD 0x07010004
229 #define TRACE_STRING_EXEC 0x07010008
231 #define MACH_vmfault 0x01300008
232 #define MACH_pageout 0x01300004
233 #define MACH_sched 0x01400000
234 #define MACH_stkhandoff 0x01400008
235 #define VFS_LOOKUP 0x03010090
236 #define BSC_exit 0x040C0004
238 #define P_DISKIO 0x03020000
239 #define P_DISKIO_DONE 0x03020004
240 #define P_DISKIO_MASK (CSC_MASK | 0x4)
242 #define P_WrData 0x03020000
243 #define P_RdData 0x03020008
244 #define P_WrMeta 0x03020020
245 #define P_RdMeta 0x03020028
246 #define P_PgOut 0x03020040
247 #define P_PgIn 0x03020048
248 #define P_WrDataAsync 0x03020010
249 #define P_RdDataAsync 0x03020018
250 #define P_WrMetaAsync 0x03020030
251 #define P_RdMetaAsync 0x03020038
252 #define P_PgOutAsync 0x03020050
253 #define P_PgInAsync 0x03020058
255 #define P_WrDataDone 0x03020004
256 #define P_RdDataDone 0x0302000C
257 #define P_WrMetaDone 0x03020024
258 #define P_RdMetaDone 0x0302002C
259 #define P_PgOutDone 0x03020044
260 #define P_PgInDone 0x0302004C
261 #define P_WrDataAsyncDone 0x03020014
262 #define P_RdDataAsyncDone 0x0302001C
263 #define P_WrMetaAsyncDone 0x03020034
264 #define P_RdMetaAsyncDone 0x0302003C
265 #define P_PgOutAsyncDone 0x03020054
266 #define P_PgInAsyncDone 0x0302005C
269 #define MSC_map_fd 0x010c00ac
271 #define BSC_BASE 0x040C0000
272 #define MSC_BASE 0x010C0000
274 // Network related codes
275 #define BSC_recvmsg 0x040C006C
276 #define BSC_sendmsg 0x040C0070
277 #define BSC_recvfrom 0x040C0074
278 #define BSC_accept 0x040C0078
279 #define BSC_select 0x040C0174
280 #define BSC_socket 0x040C0184
281 #define BSC_connect 0x040C0188
282 #define BSC_bind 0x040C01A0
283 #define BSC_listen 0x040C01A8
284 #define BSC_sendto 0x040C0214
285 #define BSC_socketpair 0x040C021C
287 #define BSC_read 0x040C000C
288 #define BSC_write 0x040C0010
289 #define BSC_open 0x040C0014
290 #define BSC_close 0x040C0018
291 #define BSC_link 0x040C0024
292 #define BSC_unlink 0x040C0028
293 #define BSC_chdir 0x040c0030
294 #define BSC_fchdir 0x040c0034
295 #define BSC_mknod 0x040C0038
296 #define BSC_chmod 0x040C003C
297 #define BSC_chown 0x040C0040
298 #define BSC_access 0x040C0084
299 #define BSC_chflags 0x040C0088
300 #define BSC_fchflags 0x040C008C
301 #define BSC_sync 0x040C0090
302 #define BSC_dup 0x040C00A4
303 #define BSC_ioctl 0x040C00D8
304 #define BSC_revoke 0x040C00E0
305 #define BSC_symlink 0x040C00E4
306 #define BSC_readlink 0x040C00E8
307 #define BSC_execve 0x040C00EC
308 #define BSC_chroot 0x040C00F4
309 #define BSC_msync 0x040C0104
310 #define BSC_dup2 0x040C0168
311 #define BSC_fcntl 0x040C0170
312 #define BSC_fsync 0x040C017C
313 #define BSC_readv 0x040C01E0
314 #define BSC_writev 0x040C01E4
315 #define BSC_fchown 0x040C01EC
316 #define BSC_fchmod 0x040C01F0
317 #define BSC_rename 0x040C0200
318 #define BSC_mkfifo 0x040c0210
319 #define BSC_mkdir 0x040C0220
320 #define BSC_rmdir 0x040C0224
321 #define BSC_utimes 0x040C0228
322 #define BSC_futimes 0x040C022C
323 #define BSC_pread 0x040C0264
324 #define BSC_pwrite 0x040C0268
325 #define BSC_statfs 0x040C0274
326 #define BSC_fstatfs 0x040C0278
327 #define BSC_stat 0x040C02F0
328 #define BSC_fstat 0x040C02F4
329 #define BSC_lstat 0x040C02F8
330 #define BSC_pathconf 0x040C02FC
331 #define BSC_fpathconf 0x040C0300
332 #define BSC_getdirentries 0x040C0310
333 #define BSC_mmap 0x040c0314
334 #define BSC_lseek 0x040c031c
335 #define BSC_truncate 0x040C0320
336 #define BSC_ftruncate 0x040C0324
337 #define BSC_undelete 0x040C0334
338 #define BSC_statv 0x040C0364
339 #define BSC_lstatv 0x040C0368
340 #define BSC_fstatv 0x040C036C
341 #define BSC_mkcomplex 0x040C0360
342 #define BSC_getattrlist 0x040C0370
343 #define BSC_setattrlist 0x040C0374
344 #define BSC_getdirentriesattr 0x040C0378
345 #define BSC_exchangedata 0x040C037C
346 #define BSC_checkuseraccess 0x040C0380
347 #define BSC_searchfs 0x040C0384
348 #define BSC_delete 0x040C0388
349 #define BSC_copyfile 0x040C038C
350 #define BSC_getxattr 0x040C03A8
351 #define BSC_fgetxattr 0x040C03AC
352 #define BSC_setxattr 0x040C03B0
353 #define BSC_fsetxattr 0x040C03B4
354 #define BSC_removexattr 0x040C03B8
355 #define BSC_fremovexattr 0x040C03BC
356 #define BSC_listxattr 0x040C03C0
357 #define BSC_flistxattr 0x040C03C4
358 #define BSC_fsctl 0x040C03C8
359 #define BSC_open_extended 0x040C0454
360 #define BSC_stat_extended 0x040C045C
361 #define BSC_lstat_extended 0x040C0460
362 #define BSC_fstat_extended 0x040C0464
363 #define BSC_chmod_extended 0x040C0468
364 #define BSC_fchmod_extended 0x040C046C
365 #define BSC_access_extended 0x040C0470
366 #define BSC_mkfifo_extended 0x040C048C
367 #define BSC_mkdir_extended 0x040C0490
368 #define BSC_load_shared_file 0x040C04A0
369 #define BSC_aio_fsync 0x040C04E4
370 #define BSC_aio_return 0x040C04E8
371 #define BSC_aio_suspend 0x040C04EC
372 #define BSC_aio_cancel 0x040C04F0
373 #define BSC_aio_error 0x040C04F4
374 #define BSC_aio_read 0x040C04F8
375 #define BSC_aio_write 0x040C04FC
376 #define BSC_lio_listio 0x040C0500
377 #define BSC_lchown 0x040C05B0
379 #define BSC_read_nocancel 0x040c0630
380 #define BSC_write_nocancel 0x040c0634
381 #define BSC_open_nocancel 0x040c0638
382 #define BSC_close_nocancel 0x040c063c
383 #define BSC_recvmsg_nocancel 0x040c0644
384 #define BSC_sendmsg_nocancel 0x040c0648
385 #define BSC_recvfrom_nocancel 0x040c064c
386 #define BSC_accept_nocancel 0x040c0650
387 #define BSC_msync_nocancel 0x040c0654
388 #define BSC_fcntl_nocancel 0x040c0658
389 #define BSC_select_nocancel 0x040c065c
390 #define BSC_fsync_nocancel 0x040c0660
391 #define BSC_connect_nocancel 0x040c0664
392 #define BSC_readv_nocancel 0x040c066c
393 #define BSC_writev_nocancel 0x040c0670
394 #define BSC_sendto_nocancel 0x040c0674
395 #define BSC_pread_nocancel 0x040c0678
396 #define BSC_pwrite_nocancel 0x040c067c
397 #define BSC_aio_suspend_nocancel 0x40c0694
399 #define BSC_msync_extended 0x040e0104
400 #define BSC_pread_extended 0x040e0264
401 #define BSC_pwrite_extended 0x040e0268
402 #define BSC_mmap_extended 0x040e0314
403 #define BSC_mmap_extended2 0x040f0314
405 // Carbon File Manager support
406 #define FILEMGR_PBGETCATALOGINFO 0x1e000020
407 #define FILEMGR_PBGETCATALOGINFOBULK 0x1e000024
408 #define FILEMGR_PBCREATEFILEUNICODE 0x1e000028
409 #define FILEMGR_PBCREATEDIRECTORYUNICODE 0x1e00002c
410 #define FILEMGR_PBCREATEFORK 0x1e000030
411 #define FILEMGR_PBDELETEFORK 0x1e000034
412 #define FILEMGR_PBITERATEFORK 0x1e000038
413 #define FILEMGR_PBOPENFORK 0x1e00003c
414 #define FILEMGR_PBREADFORK 0x1e000040
415 #define FILEMGR_PBWRITEFORK 0x1e000044
416 #define FILEMGR_PBALLOCATEFORK 0x1e000048
417 #define FILEMGR_PBDELETEOBJECT 0x1e00004c
418 #define FILEMGR_PBEXCHANGEOBJECT 0x1e000050
419 #define FILEMGR_PBGETFORKCBINFO 0x1e000054
420 #define FILEMGR_PBGETVOLUMEINFO 0x1e000058
421 #define FILEMGR_PBMAKEFSREF 0x1e00005c
422 #define FILEMGR_PBMAKEFSREFUNICODE 0x1e000060
423 #define FILEMGR_PBMOVEOBJECT 0x1e000064
424 #define FILEMGR_PBOPENITERATOR 0x1e000068
425 #define FILEMGR_PBRENAMEUNICODE 0x1e00006c
426 #define FILEMGR_PBSETCATALOGINFO 0x1e000070
427 #define FILEMGR_PBSETVOLUMEINFO 0x1e000074
428 #define FILEMGR_FSREFMAKEPATH 0x1e000078
429 #define FILEMGR_FSPATHMAKEREF 0x1e00007c
431 #define FILEMGR_PBGETCATINFO 0x1e010000
432 #define FILEMGR_PBGETCATINFOLITE 0x1e010004
433 #define FILEMGR_PBHGETFINFO 0x1e010008
434 #define FILEMGR_PBXGETVOLINFO 0x1e01000c
435 #define FILEMGR_PBHCREATE 0x1e010010
436 #define FILEMGR_PBHOPENDF 0x1e010014
437 #define FILEMGR_PBHOPENRF 0x1e010018
438 #define FILEMGR_PBHGETDIRACCESS 0x1e01001c
439 #define FILEMGR_PBHSETDIRACCESS 0x1e010020
440 #define FILEMGR_PBHMAPID 0x1e010024
441 #define FILEMGR_PBHMAPNAME 0x1e010028
442 #define FILEMGR_PBCLOSE 0x1e01002c
443 #define FILEMGR_PBFLUSHFILE 0x1e010030
444 #define FILEMGR_PBGETEOF 0x1e010034
445 #define FILEMGR_PBSETEOF 0x1e010038
446 #define FILEMGR_PBGETFPOS 0x1e01003c
447 #define FILEMGR_PBREAD 0x1e010040
448 #define FILEMGR_PBWRITE 0x1e010044
449 #define FILEMGR_PBGETFCBINFO 0x1e010048
450 #define FILEMGR_PBSETFINFO 0x1e01004c
451 #define FILEMGR_PBALLOCATE 0x1e010050
452 #define FILEMGR_PBALLOCCONTIG 0x1e010054
453 #define FILEMGR_PBSETFPOS 0x1e010058
454 #define FILEMGR_PBSETCATINFO 0x1e01005c
455 #define FILEMGR_PBGETVOLPARMS 0x1e010060
456 #define FILEMGR_PBSETVINFO 0x1e010064
457 #define FILEMGR_PBMAKEFSSPEC 0x1e010068
458 #define FILEMGR_PBHGETVINFO 0x1e01006c
459 #define FILEMGR_PBCREATEFILEIDREF 0x1e010070
460 #define FILEMGR_PBDELETEFILEIDREF 0x1e010074
461 #define FILEMGR_PBRESOLVEFILEIDREF 0x1e010078
462 #define FILEMGR_PBFLUSHVOL 0x1e01007c
463 #define FILEMGR_PBHRENAME 0x1e010080
464 #define FILEMGR_PBCATMOVE 0x1e010084
465 #define FILEMGR_PBEXCHANGEFILES 0x1e010088
466 #define FILEMGR_PBHDELETE 0x1e01008c
467 #define FILEMGR_PBDIRCREATE 0x1e010090
468 #define FILEMGR_PBCATSEARCH 0x1e010094
469 #define FILEMGR_PBHSETFLOCK 0x1e010098
470 #define FILEMGR_PBHRSTFLOCK 0x1e01009c
471 #define FILEMGR_PBLOCKRANGE 0x1e0100a0
472 #define FILEMGR_PBUNLOCKRANGE 0x1e0100a4
475 #define FILEMGR_CLASS 0x1e
476 #define FILEMGR_BASE 0x1e000000
478 #define FMT_DEFAULT 0
485 #define FMT_CACHEHIT 7
489 #define FMT_FTRUNC 11
491 #define FMT_SELECT 13
493 #define FMT_AIO_FSYNC 15
494 #define FMT_AIO_RETURN 16
495 #define FMT_AIO_SUSPEND 17
496 #define FMT_AIO_CANCEL 18
498 #define FMT_LIO_LISTIO 20
501 #define FMT_ACCESS 23
503 #define FMT_FCHMOD 25
504 #define FMT_CHMOD_EXT 26
505 #define FMT_FCHMOD_EXT 27
506 #define FMT_CHFLAGS 28
507 #define FMT_FCHFLAGS 29
511 #define MAX_BSD_SYSCALL 512
516 } bsd_syscalls
[MAX_BSD_SYSCALL
];
519 int bsd_syscall_types
[] = {
521 BSC_recvmsg_nocancel
,
523 BSC_sendmsg_nocancel
,
525 BSC_recvfrom_nocancel
,
532 BSC_connect_nocancel
,
601 BSC_getdirentriesattr
,
625 BSC_load_shared_file
,
629 BSC_aio_suspend_nocancel
,
645 #define MAX_FILEMGR 512
647 struct filemgr_call
{
649 } filemgr_calls
[MAX_FILEMGR
];
652 int filemgr_call_types
[] = {
653 FILEMGR_PBGETCATALOGINFO
,
654 FILEMGR_PBGETCATALOGINFOBULK
,
655 FILEMGR_PBCREATEFILEUNICODE
,
656 FILEMGR_PBCREATEDIRECTORYUNICODE
,
657 FILEMGR_PBCREATEFORK
,
658 FILEMGR_PBDELETEFORK
,
659 FILEMGR_PBITERATEFORK
,
663 FILEMGR_PBALLOCATEFORK
,
664 FILEMGR_PBDELETEOBJECT
,
665 FILEMGR_PBEXCHANGEOBJECT
,
666 FILEMGR_PBGETFORKCBINFO
,
667 FILEMGR_PBGETVOLUMEINFO
,
669 FILEMGR_PBMAKEFSREFUNICODE
,
670 FILEMGR_PBMOVEOBJECT
,
671 FILEMGR_PBOPENITERATOR
,
672 FILEMGR_PBRENAMEUNICODE
,
673 FILEMGR_PBSETCATALOGINFO
,
674 FILEMGR_PBSETVOLUMEINFO
,
675 FILEMGR_FSREFMAKEPATH
,
676 FILEMGR_FSPATHMAKEREF
,
678 FILEMGR_PBGETCATINFO
,
679 FILEMGR_PBGETCATINFOLITE
,
681 FILEMGR_PBXGETVOLINFO
,
685 FILEMGR_PBHGETDIRACCESS
,
686 FILEMGR_PBHSETDIRACCESS
,
696 FILEMGR_PBGETFCBINFO
,
699 FILEMGR_PBALLOCCONTIG
,
701 FILEMGR_PBSETCATINFO
,
702 FILEMGR_PBGETVOLPARMS
,
704 FILEMGR_PBMAKEFSSPEC
,
706 FILEMGR_PBCREATEFILEIDREF
,
707 FILEMGR_PBDELETEFILEIDREF
,
708 FILEMGR_PBRESOLVEFILEIDREF
,
712 FILEMGR_PBEXCHANGEFILES
,
719 FILEMGR_PBUNLOCKRANGE
,
729 int exclude_pids
= 0;
730 int exclude_default_pids
= 1;
733 struct kinfo_proc
*kp_buffer
= 0;
736 #define SAMPLE_SIZE 100000
740 #define EVENT_BASE 60000
742 int num_events
= EVENT_BASE
;
744 #define DBG_ZERO_FILL_FAULT 1
745 #define DBG_PAGEIN_FAULT 2
746 #define DBG_COW_FAULT 3
747 #define DBG_CACHE_HIT_FAULT 4
749 #define DBG_FUNC_ALL (DBG_FUNC_START | DBG_FUNC_END)
750 #define DBG_FUNC_MASK 0xfffffffc
752 double divisor
= 0.0; /* Trace divisor converts to microseconds */
758 kbufinfo_t bufinfo
= {0, 0, 0, 0, 0};
760 int total_threads
= 0;
761 kd_threadmap
*mapptr
= 0; /* pointer to list of threads */
763 /* defines for tracking file descriptor state */
764 #define FS_USAGE_FD_SETSIZE 256 /* Initial number of file descriptors per
765 thread that we will track */
767 #define FS_USAGE_NFDBITS (sizeof (unsigned long) * 8)
768 #define FS_USAGE_NFDBYTES(n) (((n) / FS_USAGE_NFDBITS) * sizeof (unsigned long))
771 unsigned int fd_valid
; /* set if this is a valid entry */
772 unsigned int fd_thread
;
773 unsigned int fd_setsize
; /* this is a bit count */
774 unsigned long *fd_setptr
; /* file descripter bitmap */
777 fd_threadmap
*fdmapptr
= 0; /* pointer to list of threads for fd tracking */
779 int trace_enabled
= 0;
780 int set_remove_flag
= 1;
792 void leave() /* exit under normal conditions -- INT handler */
797 void set_pidexclude();
804 if (exclude_pids
== 0) {
805 for (i
= 0; i
< num_of_pids
; i
++)
806 set_pidcheck(pids
[i
], 0);
809 for (i
= 0; i
< num_of_pids
; i
++)
810 set_pidexclude(pids
[i
], 0);
817 void get_screenwidth()
824 if (ioctl(1, TIOCGWINSZ
, &size
) != -1) {
825 columns
= size
.ws_col
;
827 if (columns
> MAXWIDTH
)
841 exit_usage(char *myname
) {
843 fprintf(stderr
, "Usage: %s [-e] [-w] [-f mode] [pid | cmd [pid | cmd]....]\n", myname
);
844 fprintf(stderr
, " -e exclude the specified list of pids from the sample\n");
845 fprintf(stderr
, " and exclude fs_usage by default\n");
846 fprintf(stderr
, " -w force wider, detailed, output\n");
847 fprintf(stderr
, " -f Output is based on the mode provided\n");
848 fprintf(stderr
, " mode = \"network\" Show only network related output\n");
849 fprintf(stderr
, " mode = \"filesys\" Show only file system related output\n");
850 fprintf(stderr
, " mode = \"pathname\" Show only pathname related output\n");
851 fprintf(stderr
, " mode = \"exec\" Show only execs\n");
852 fprintf(stderr
, " mode = \"cachehit\" In addition, show cachehits\n");
853 fprintf(stderr
, " pid selects process(s) to sample\n");
854 fprintf(stderr
, " cmd selects process(s) matching command string to sample\n");
855 fprintf(stderr
, "\n%s will handle a maximum list of %d pids.\n\n", myname
, MAX_PIDS
);
856 fprintf(stderr
, "By default (no options) the following processes are excluded from the output:\n");
857 fprintf(stderr
, "fs_usage, Terminal, telnetd, sshd, rlogind, tcsh, csh, sh\n\n");
863 int filemgr_index(type
) {
866 return (((type
>> 2) & 0x3fff) + 256);
868 return (((type
>> 2) & 0x3fff));
872 void init_tables(void)
877 for (i
= 0; i
< MAX_THREADS
; i
++)
878 th_state
[i
].my_index
= i
;
880 for (i
= 0; i
< MAX_BSD_SYSCALL
; i
++) {
881 bsd_syscalls
[i
].sc_name
= NULL
;
882 bsd_syscalls
[i
].sc_format
= FMT_DEFAULT
;
885 for (i
= 0; i
< MAX_FILEMGR
; i
++) {
886 filemgr_calls
[i
].fm_name
= NULL
;
889 for (i
= 0; (type
= bsd_syscall_types
[i
]); i
++) {
891 code
= BSC_INDEX(type
);
893 if (code
>= MAX_BSD_SYSCALL
) {
894 printf("BSD syscall init (%x): type exceeds table size\n", type
);
900 case BSC_recvmsg_nocancel
:
901 bsd_syscalls
[code
].sc_name
= "recvmsg";
902 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
906 case BSC_sendmsg_nocancel
:
907 bsd_syscalls
[code
].sc_name
= "sendmsg";
908 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
912 case BSC_recvfrom_nocancel
:
913 bsd_syscalls
[code
].sc_name
= "recvfrom";
914 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
918 case BSC_sendto_nocancel
:
919 bsd_syscalls
[code
].sc_name
= "sendto";
920 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
924 case BSC_select_nocancel
:
925 bsd_syscalls
[code
].sc_name
= "select";
926 bsd_syscalls
[code
].sc_format
= FMT_SELECT
;
930 case BSC_accept_nocancel
:
931 bsd_syscalls
[code
].sc_name
= "accept";
932 bsd_syscalls
[code
].sc_format
= FMT_FD_2
;
936 bsd_syscalls
[code
].sc_name
= "socket";
937 bsd_syscalls
[code
].sc_format
= FMT_SOCKET
;
941 case BSC_connect_nocancel
:
942 bsd_syscalls
[code
].sc_name
= "connect";
943 bsd_syscalls
[code
].sc_format
= FMT_FD
;
947 bsd_syscalls
[code
].sc_name
= "bind";
948 bsd_syscalls
[code
].sc_format
= FMT_FD
;
952 bsd_syscalls
[code
].sc_name
= "listen";
953 bsd_syscalls
[code
].sc_format
= FMT_FD
;
957 bsd_syscalls
[code
].sc_name
= "mmap";
958 bsd_syscalls
[code
].sc_format
= FMT_MMAP
;
962 bsd_syscalls
[code
].sc_name
= "socketpair";
966 bsd_syscalls
[code
].sc_name
= "getxattr";
970 bsd_syscalls
[code
].sc_name
= "setxattr";
973 case BSC_removexattr
:
974 bsd_syscalls
[code
].sc_name
= "removexattr";
978 bsd_syscalls
[code
].sc_name
= "listxattr";
982 bsd_syscalls
[code
].sc_name
= "stat";
985 case BSC_stat_extended
:
986 bsd_syscalls
[code
].sc_name
= "stat_extended";
990 bsd_syscalls
[code
].sc_name
= "execve";
993 case BSC_load_shared_file
:
994 bsd_syscalls
[code
].sc_name
= "load_sf";
998 case BSC_open_nocancel
:
999 bsd_syscalls
[code
].sc_name
= "open";
1000 bsd_syscalls
[code
].sc_format
= FMT_OPEN
;
1003 case BSC_open_extended
:
1004 bsd_syscalls
[code
].sc_name
= "open_extended";
1005 bsd_syscalls
[code
].sc_format
= FMT_OPEN
;
1009 bsd_syscalls
[code
].sc_name
= "dup";
1010 bsd_syscalls
[code
].sc_format
= FMT_FD_2
;
1014 bsd_syscalls
[code
].sc_name
= "dup2";
1015 bsd_syscalls
[code
].sc_format
= FMT_FD_2
;
1019 case BSC_close_nocancel
:
1020 bsd_syscalls
[code
].sc_name
= "close";
1021 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1025 case BSC_read_nocancel
:
1026 bsd_syscalls
[code
].sc_name
= "read";
1027 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1031 case BSC_write_nocancel
:
1032 bsd_syscalls
[code
].sc_name
= "write";
1033 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1037 bsd_syscalls
[code
].sc_name
= "fgetxattr";
1038 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1042 bsd_syscalls
[code
].sc_name
= "fsetxattr";
1043 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1046 case BSC_fremovexattr
:
1047 bsd_syscalls
[code
].sc_name
= "fremovexattr";
1048 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1051 case BSC_flistxattr
:
1052 bsd_syscalls
[code
].sc_name
= "flistxattr";
1053 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1057 bsd_syscalls
[code
].sc_name
= "fstat";
1058 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1061 case BSC_fstat_extended
:
1062 bsd_syscalls
[code
].sc_name
= "fstat_extended";
1063 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1067 bsd_syscalls
[code
].sc_name
= "lstat";
1070 case BSC_lstat_extended
:
1071 bsd_syscalls
[code
].sc_name
= "lstat_extended";
1075 bsd_syscalls
[code
].sc_name
= "lstatv";
1079 bsd_syscalls
[code
].sc_name
= "link";
1083 bsd_syscalls
[code
].sc_name
= "unlink";
1087 bsd_syscalls
[code
].sc_name
= "mknod";
1091 bsd_syscalls
[code
].sc_name
= "chmod";
1092 bsd_syscalls
[code
].sc_format
= FMT_CHMOD
;
1095 case BSC_chmod_extended
:
1096 bsd_syscalls
[code
].sc_name
= "chmod_extended";
1097 bsd_syscalls
[code
].sc_format
= FMT_CHMOD_EXT
;
1101 bsd_syscalls
[code
].sc_name
= "fchmod";
1102 bsd_syscalls
[code
].sc_format
= FMT_FCHMOD
;
1105 case BSC_fchmod_extended
:
1106 bsd_syscalls
[code
].sc_name
= "fchmod_extended";
1107 bsd_syscalls
[code
].sc_format
= FMT_FCHMOD_EXT
;
1111 bsd_syscalls
[code
].sc_name
= "chown";
1115 bsd_syscalls
[code
].sc_name
= "lchown";
1119 bsd_syscalls
[code
].sc_name
= "fchown";
1120 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1124 bsd_syscalls
[code
].sc_name
= "access";
1125 bsd_syscalls
[code
].sc_format
= FMT_ACCESS
;
1128 case BSC_access_extended
:
1129 bsd_syscalls
[code
].sc_name
= "access_extended";
1133 bsd_syscalls
[code
].sc_name
= "chdir";
1137 bsd_syscalls
[code
].sc_name
= "chroot";
1141 bsd_syscalls
[code
].sc_name
= "utimes";
1145 bsd_syscalls
[code
].sc_name
= "delete";
1149 bsd_syscalls
[code
].sc_name
= "undelete";
1153 bsd_syscalls
[code
].sc_name
= "revoke";
1157 bsd_syscalls
[code
].sc_name
= "fsctl";
1161 bsd_syscalls
[code
].sc_name
= "chflags";
1162 bsd_syscalls
[code
].sc_format
= FMT_CHFLAGS
;
1166 bsd_syscalls
[code
].sc_name
= "fchflags";
1167 bsd_syscalls
[code
].sc_format
= FMT_FCHFLAGS
;
1171 bsd_syscalls
[code
].sc_name
= "fchdir";
1172 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1176 bsd_syscalls
[code
].sc_name
= "futimes";
1177 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1181 bsd_syscalls
[code
].sc_name
= "sync";
1185 bsd_syscalls
[code
].sc_name
= "symlink";
1189 bsd_syscalls
[code
].sc_name
= "readlink";
1193 case BSC_fsync_nocancel
:
1194 bsd_syscalls
[code
].sc_name
= "fsync";
1195 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1199 case BSC_readv_nocancel
:
1200 bsd_syscalls
[code
].sc_name
= "readv";
1201 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1205 case BSC_writev_nocancel
:
1206 bsd_syscalls
[code
].sc_name
= "writev";
1207 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1211 case BSC_pread_nocancel
:
1212 bsd_syscalls
[code
].sc_name
= "pread";
1213 bsd_syscalls
[code
].sc_format
= FMT_PREAD
;
1217 case BSC_pwrite_nocancel
:
1218 bsd_syscalls
[code
].sc_name
= "pwrite";
1219 bsd_syscalls
[code
].sc_format
= FMT_PREAD
;
1223 bsd_syscalls
[code
].sc_name
= "mkdir";
1226 case BSC_mkdir_extended
:
1227 bsd_syscalls
[code
].sc_name
= "mkdir_extended";
1231 bsd_syscalls
[code
].sc_name
= "mkfifo";
1234 case BSC_mkfifo_extended
:
1235 bsd_syscalls
[code
].sc_name
= "mkfifo_extended";
1239 bsd_syscalls
[code
].sc_name
= "rmdir";
1243 bsd_syscalls
[code
].sc_name
= "statfs";
1247 bsd_syscalls
[code
].sc_name
= "fstatfs";
1248 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1252 bsd_syscalls
[code
].sc_name
= "pathconf";
1256 bsd_syscalls
[code
].sc_name
= "fpathconf";
1257 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1260 case BSC_getdirentries
:
1261 bsd_syscalls
[code
].sc_name
= "getdirentries";
1262 bsd_syscalls
[code
].sc_format
= FMT_FD_IO
;
1266 bsd_syscalls
[code
].sc_name
= "lseek";
1267 bsd_syscalls
[code
].sc_format
= FMT_LSEEK
;
1271 bsd_syscalls
[code
].sc_name
= "truncate";
1272 bsd_syscalls
[code
].sc_format
= FMT_TRUNC
;
1276 bsd_syscalls
[code
].sc_name
= "ftruncate";
1277 bsd_syscalls
[code
].sc_format
= FMT_FTRUNC
;
1281 bsd_syscalls
[code
].sc_name
= "statv";
1285 bsd_syscalls
[code
].sc_name
= "fstatv";
1286 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1290 bsd_syscalls
[code
].sc_name
= "mkcomplex";
1293 case BSC_getattrlist
:
1294 bsd_syscalls
[code
].sc_name
= "getattrlist";
1297 case BSC_setattrlist
:
1298 bsd_syscalls
[code
].sc_name
= "setattrlist";
1301 case BSC_getdirentriesattr
:
1302 bsd_syscalls
[code
].sc_name
= "getdirentriesattr";
1303 bsd_syscalls
[code
].sc_format
= FMT_FD
;
1306 case BSC_exchangedata
:
1307 bsd_syscalls
[code
].sc_name
= "exchangedata";
1311 bsd_syscalls
[code
].sc_name
= "rename";
1315 bsd_syscalls
[code
].sc_name
= "copyfile";
1318 case BSC_checkuseraccess
:
1319 bsd_syscalls
[code
].sc_name
= "checkuseraccess";
1323 bsd_syscalls
[code
].sc_name
= "searchfs";
1327 bsd_syscalls
[code
].sc_name
= "aio_fsync";
1328 bsd_syscalls
[code
].sc_format
= FMT_AIO_FSYNC
;
1331 case BSC_aio_return
:
1332 bsd_syscalls
[code
].sc_name
= "aio_return";
1333 bsd_syscalls
[code
].sc_format
= FMT_AIO_RETURN
;
1336 case BSC_aio_suspend
:
1337 case BSC_aio_suspend_nocancel
:
1338 bsd_syscalls
[code
].sc_name
= "aio_suspend";
1339 bsd_syscalls
[code
].sc_format
= FMT_AIO_SUSPEND
;
1342 case BSC_aio_cancel
:
1343 bsd_syscalls
[code
].sc_name
= "aio_cancel";
1344 bsd_syscalls
[code
].sc_format
= FMT_AIO_CANCEL
;
1348 bsd_syscalls
[code
].sc_name
= "aio_error";
1349 bsd_syscalls
[code
].sc_format
= FMT_AIO
;
1353 bsd_syscalls
[code
].sc_name
= "aio_read";
1354 bsd_syscalls
[code
].sc_format
= FMT_AIO
;
1358 bsd_syscalls
[code
].sc_name
= "aio_write";
1359 bsd_syscalls
[code
].sc_format
= FMT_AIO
;
1362 case BSC_lio_listio
:
1363 bsd_syscalls
[code
].sc_name
= "lio_listio";
1364 bsd_syscalls
[code
].sc_format
= FMT_LIO_LISTIO
;
1368 case BSC_msync_nocancel
:
1369 bsd_syscalls
[code
].sc_name
= "msync";
1370 bsd_syscalls
[code
].sc_format
= FMT_MSYNC
;
1374 case BSC_fcntl_nocancel
:
1375 bsd_syscalls
[code
].sc_name
= "fcntl";
1376 bsd_syscalls
[code
].sc_format
= FMT_FCNTL
;
1380 bsd_syscalls
[code
].sc_name
= "ioctl";
1381 bsd_syscalls
[code
].sc_format
= FMT_IOCTL
;
1386 for (i
= 0; (type
= filemgr_call_types
[i
]); i
++) {
1389 code
= filemgr_index(type
);
1391 if (code
>= MAX_FILEMGR
) {
1392 printf("FILEMGR call init (%x): type exceeds table size\n", type
);
1397 case FILEMGR_PBGETCATALOGINFO
:
1398 p
= "GetCatalogInfo";
1401 case FILEMGR_PBGETCATALOGINFOBULK
:
1402 p
= "GetCatalogInfoBulk";
1405 case FILEMGR_PBCREATEFILEUNICODE
:
1406 p
= "CreateFileUnicode";
1409 case FILEMGR_PBCREATEDIRECTORYUNICODE
:
1410 p
= "CreateDirectoryUnicode";
1413 case FILEMGR_PBCREATEFORK
:
1417 case FILEMGR_PBDELETEFORK
:
1421 case FILEMGR_PBITERATEFORK
:
1422 p
= "PBIterateFork";
1425 case FILEMGR_PBOPENFORK
:
1429 case FILEMGR_PBREADFORK
:
1433 case FILEMGR_PBWRITEFORK
:
1437 case FILEMGR_PBALLOCATEFORK
:
1438 p
= "PBAllocateFork";
1441 case FILEMGR_PBDELETEOBJECT
:
1442 p
= "PBDeleteObject";
1445 case FILEMGR_PBEXCHANGEOBJECT
:
1446 p
= "PBExchangeObject";
1449 case FILEMGR_PBGETFORKCBINFO
:
1450 p
= "PBGetForkCBInfo";
1453 case FILEMGR_PBGETVOLUMEINFO
:
1454 p
= "PBGetVolumeInfo";
1457 case FILEMGR_PBMAKEFSREF
:
1461 case FILEMGR_PBMAKEFSREFUNICODE
:
1462 p
= "PBMakeFSRefUnicode";
1465 case FILEMGR_PBMOVEOBJECT
:
1469 case FILEMGR_PBOPENITERATOR
:
1470 p
= "PBOpenIterator";
1473 case FILEMGR_PBRENAMEUNICODE
:
1474 p
= "PBRenameUnicode";
1477 case FILEMGR_PBSETCATALOGINFO
:
1478 p
= "SetCatalogInfo";
1481 case FILEMGR_PBSETVOLUMEINFO
:
1482 p
= "SetVolumeInfo";
1485 case FILEMGR_FSREFMAKEPATH
:
1486 p
= "FSRefMakePath";
1489 case FILEMGR_FSPATHMAKEREF
:
1490 p
= "FSPathMakeRef";
1493 case FILEMGR_PBGETCATINFO
:
1497 case FILEMGR_PBGETCATINFOLITE
:
1498 p
= "GetCatInfoLite";
1501 case FILEMGR_PBHGETFINFO
:
1505 case FILEMGR_PBXGETVOLINFO
:
1506 p
= "PBXGetVolInfo";
1509 case FILEMGR_PBHCREATE
:
1513 case FILEMGR_PBHOPENDF
:
1517 case FILEMGR_PBHOPENRF
:
1521 case FILEMGR_PBHGETDIRACCESS
:
1522 p
= "PBHGetDirAccess";
1525 case FILEMGR_PBHSETDIRACCESS
:
1526 p
= "PBHSetDirAccess";
1529 case FILEMGR_PBHMAPID
:
1533 case FILEMGR_PBHMAPNAME
:
1537 case FILEMGR_PBCLOSE
:
1541 case FILEMGR_PBFLUSHFILE
:
1545 case FILEMGR_PBGETEOF
:
1549 case FILEMGR_PBSETEOF
:
1553 case FILEMGR_PBGETFPOS
:
1557 case FILEMGR_PBREAD
:
1561 case FILEMGR_PBWRITE
:
1565 case FILEMGR_PBGETFCBINFO
:
1569 case FILEMGR_PBSETFINFO
:
1573 case FILEMGR_PBALLOCATE
:
1577 case FILEMGR_PBALLOCCONTIG
:
1578 p
= "PBAllocContig";
1581 case FILEMGR_PBSETFPOS
:
1585 case FILEMGR_PBSETCATINFO
:
1589 case FILEMGR_PBGETVOLPARMS
:
1590 p
= "PBGetVolParms";
1593 case FILEMGR_PBSETVINFO
:
1597 case FILEMGR_PBMAKEFSSPEC
:
1601 case FILEMGR_PBHGETVINFO
:
1605 case FILEMGR_PBCREATEFILEIDREF
:
1606 p
= "PBCreateFileIDRef";
1609 case FILEMGR_PBDELETEFILEIDREF
:
1610 p
= "PBDeleteFileIDRef";
1613 case FILEMGR_PBRESOLVEFILEIDREF
:
1614 p
= "PBResolveFileIDRef";
1617 case FILEMGR_PBFLUSHVOL
:
1621 case FILEMGR_PBHRENAME
:
1625 case FILEMGR_PBCATMOVE
:
1629 case FILEMGR_PBEXCHANGEFILES
:
1630 p
= "PBExchangeFiles";
1633 case FILEMGR_PBHDELETE
:
1637 case FILEMGR_PBDIRCREATE
:
1641 case FILEMGR_PBCATSEARCH
:
1645 case FILEMGR_PBHSETFLOCK
:
1649 case FILEMGR_PBHRSTFLOCK
:
1653 case FILEMGR_PBLOCKRANGE
:
1657 case FILEMGR_PBUNLOCKRANGE
:
1658 p
= "PBUnlockRange";
1665 filemgr_calls
[code
].fm_name
= p
;
1676 char *myname
= "fs_usage";
1679 struct sigaction osa
;
1683 void set_pidcheck();
1684 void set_pidexclude();
1687 if ( geteuid() != 0 ) {
1688 fprintf(stderr
, "'fs_usage' must be run as root...\n");
1695 if ((myname
= rindex(argv
[0], '/')) == 0) {
1703 while ((ch
= getopt(argc
, argv
, "ewf:")) != EOF
) {
1707 exclude_default_pids
= 0;
1711 if ((uint
)columns
< MAX_WIDE_MODE_COLS
)
1712 columns
= MAX_WIDE_MODE_COLS
;
1715 if (!strcmp(optarg
, "network"))
1716 filter_mode
|= NETWORK_FILTER
;
1717 else if (!strcmp(optarg
, "filesys"))
1718 filter_mode
|= FILESYS_FILTER
;
1719 else if (!strcmp(optarg
, "cachehit"))
1720 filter_mode
&= ~CACHEHIT_FILTER
; /* turns on CACHE_HIT */
1721 else if (!strcmp(optarg
, "exec"))
1722 filter_mode
|= EXEC_FILTER
;
1723 else if (!strcmp(optarg
, "pathname"))
1724 filter_mode
|= PATHNAME_FILTER
;
1736 * when excluding, fs_usage should be the first in line for pids[]
1738 * the !exclude_pids && argc == 0 catches the exclude_default_pids
1739 * case below where exclude_pids is later set and the fs_usage PID
1740 * needs to make it into pids[]
1742 if (exclude_pids
|| (!exclude_pids
&& argc
== 0))
1744 if (num_of_pids
< (MAX_PIDS
- 1))
1745 pids
[num_of_pids
++] = getpid();
1748 /* If we process any list of pids/cmds, then turn off the defaults */
1750 exclude_default_pids
= 0;
1752 while (argc
> 0 && num_of_pids
< (MAX_PIDS
- 1)) {
1759 /* Exclude a set of default pids */
1760 if (exclude_default_pids
)
1762 argtopid("Terminal");
1763 argtopid("telnetd");
1766 argtopid("rlogind");
1774 for (i
= 0; i
< num_of_pids
; i
++)
1777 fprintf(stderr
, "exclude pid %d\n", pids
[i
]);
1779 fprintf(stderr
, "pid %d\n", pids
[i
]);
1783 /* set up signal handlers */
1784 signal(SIGINT
, leave
);
1785 signal(SIGQUIT
, leave
);
1787 sigaction(SIGHUP
, (struct sigaction
*)NULL
, &osa
);
1789 if (osa
.sa_handler
== SIG_DFL
)
1790 signal(SIGHUP
, leave
);
1791 signal(SIGTERM
, leave
);
1792 signal(SIGWINCH
, sigwinch
);
1794 /* grab the number of cpus */
1799 len
= sizeof(num_cpus
);
1800 sysctl(mib
, 2, &num_cpus
, &len
, NULL
, 0);
1801 num_events
= EVENT_BASE
* num_cpus
;
1803 if ((my_buffer
= malloc(num_events
* sizeof(kd_buf
))) == (char *)0)
1804 quit("can't allocate memory for tracing info\n");
1806 if (ReadSharedCacheMap("/var/db/dyld/dyld_shared_cache_i386.map", &framework32
))
1807 ReadSharedCacheMap("/var/db/dyld/dyld_shared_cache_x86_64.map", &framework64
);
1809 ReadSharedCacheMap("/var/db/dyld/dyld_shared_cache_ppc.map", &framework32
);
1810 ReadSharedCacheMap("/var/db/dyld/dyld_shared_cache_ppc64.map", &framework64
);
1812 SortFrameworkAddresses();
1817 set_numbufs(num_events
);
1820 if (exclude_pids
== 0) {
1821 for (i
= 0; i
< num_of_pids
; i
++)
1822 set_pidcheck(pids
[i
], 1);
1824 for (i
= 0; i
< num_of_pids
; i
++)
1825 set_pidexclude(pids
[i
], 1);
1828 if (select_pid_mode
&& !one_good_pid
)
1831 An attempt to restrict output to a given
1832 pid or command has failed. Exit gracefully
1840 init_arguments_buffer();
1847 usleep(1000 * usleep_ms
);
1851 last_time
= time((long *)0);
1859 struct kinfo_proc
*kp
;
1864 mib
[2] = KERN_PROC_ALL
;
1867 if (sysctl(mib
, 4, NULL
, &bufSize
, NULL
, 0) < 0)
1868 quit("trace facility failure, KERN_PROC_ALL\n");
1870 if((kp
= (struct kinfo_proc
*)malloc(bufSize
)) == (struct kinfo_proc
*)0)
1871 quit("can't allocate memory for proc buffer\n");
1873 if (sysctl(mib
, 4, kp
, &bufSize
, NULL
, 0) < 0)
1874 quit("trace facility failure, KERN_PROC_ALL\n");
1876 kp_nentries
= bufSize
/ sizeof(struct kinfo_proc
);
1881 void destroy_thread(struct th_info
*ti
) {
1883 ti
->child_thread
= 0;
1887 if (ti
->my_index
< cur_start
)
1888 cur_start
= ti
->my_index
;
1890 if (ti
->my_index
== cur_max
) {
1891 while (ti
>= &th_state
[0]) {
1896 cur_max
= ti
->my_index
;
1901 struct th_info
*find_empty(void) {
1904 for (ti
= &th_state
[cur_start
]; ti
< &th_state
[MAX_THREADS
]; ti
++, cur_start
++) {
1905 if (ti
->thread
== 0) {
1906 if (cur_start
> cur_max
)
1907 cur_max
= cur_start
;
1913 return ((struct th_info
*)0);
1918 struct th_info
*find_thread(int thread
, int type
) {
1921 for (ti
= &th_state
[0]; ti
<= &th_state
[cur_max
]; ti
++) {
1922 if (ti
->thread
== thread
) {
1923 if (type
== ti
->type
)
1925 if (ti
->in_filemgr
) {
1934 return ((struct th_info
*)0);
1939 mark_thread_waited(int thread
) {
1942 for (ti
= th_state
; ti
<= &th_state
[cur_max
]; ti
++) {
1943 if (ti
->thread
== thread
) {
1954 mib
[1] = KERN_KDEBUG
;
1955 mib
[2] = KERN_KDENABLE
; /* protocol */
1958 mib
[5] = 0; /* no flags */
1959 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1960 quit("trace facility failure, KERN_KDENABLE\n");
1969 set_numbufs(int nbufs
)
1972 mib
[1] = KERN_KDEBUG
;
1973 mib
[2] = KERN_KDSETBUF
;
1976 mib
[5] = 0; /* no flags */
1977 if (sysctl(mib
, 4, NULL
, &needed
, NULL
, 0) < 0)
1978 quit("trace facility failure, KERN_KDSETBUF\n");
1981 mib
[1] = KERN_KDEBUG
;
1982 mib
[2] = KERN_KDSETUP
;
1985 mib
[5] = 0; /* no flags */
1986 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
1987 quit("trace facility failure, KERN_KDSETUP\n");
1991 set_pidcheck(int pid
, int on_off
)
1995 kr
.type
= KDBG_TYPENONE
;
1998 needed
= sizeof(kd_regtype
);
2000 mib
[1] = KERN_KDEBUG
;
2001 mib
[2] = KERN_KDPIDTR
;
2006 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0) {
2008 fprintf(stderr
, "pid %d does not exist\n", pid
);
2016 on_off == 0 turns off pid exclusion
2017 on_off == 1 turns on pid exclusion
2020 set_pidexclude(int pid
, int on_off
)
2026 kr
.type
= KDBG_TYPENONE
;
2029 needed
= sizeof(kd_regtype
);
2031 mib
[1] = KERN_KDEBUG
;
2032 mib
[2] = KERN_KDPIDEX
;
2037 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0) {
2039 fprintf(stderr
, "pid %d does not exist\n", pid
);
2044 get_bufinfo(kbufinfo_t
*val
)
2046 needed
= sizeof (*val
);
2048 mib
[1] = KERN_KDEBUG
;
2049 mib
[2] = KERN_KDGETBUF
;
2052 mib
[5] = 0; /* no flags */
2054 if (sysctl(mib
, 3, val
, &needed
, 0, 0) < 0)
2055 quit("trace facility failure, KERN_KDGETBUF\n");
2065 mib
[1] = KERN_KDEBUG
;
2066 mib
[2] = KERN_KDREMOVE
; /* protocol */
2069 mib
[5] = 0; /* no flags */
2070 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
2072 set_remove_flag
= 0;
2075 quit("the trace facility is currently in use...\n fs_usage, sc_usage, and latency use this feature.\n\n");
2077 quit("trace facility failure, KERN_KDREMOVE\n");
2085 kr
.type
= KDBG_RANGETYPE
;
2088 needed
= sizeof(kd_regtype
);
2090 mib
[1] = KERN_KDEBUG
;
2091 mib
[2] = KERN_KDSETREG
;
2094 mib
[5] = 0; /* no flags */
2096 if (sysctl(mib
, 3, &kr
, &needed
, NULL
, 0) < 0)
2097 quit("trace facility failure, KERN_KDSETREG\n");
2100 mib
[1] = KERN_KDEBUG
;
2101 mib
[2] = KERN_KDSETUP
;
2104 mib
[5] = 0; /* no flags */
2106 if (sysctl(mib
, 3, NULL
, &needed
, NULL
, 0) < 0)
2107 quit("trace facility failure, KERN_KDSETUP\n");
2116 void read_command_map();
2117 void create_map_entry();
2119 /* Get kernel buffer information */
2120 get_bufinfo(&bufinfo
);
2126 needed
= bufinfo
.nkdbufs
* sizeof(kd_buf
);
2128 mib
[1] = KERN_KDEBUG
;
2129 mib
[2] = KERN_KDREADTR
;
2132 mib
[5] = 0; /* no flags */
2134 if (sysctl(mib
, 3, my_buffer
, &needed
, NULL
, 0) < 0)
2135 quit("trace facility failure, KERN_KDREADTR\n");
2138 if (count
> (SAMPLE_SIZE
/ 8)) {
2139 if (usleep_ms
> USLEEP_BEHIND
)
2140 usleep_ms
= USLEEP_BEHIND
;
2141 else if (usleep_ms
> USLEEP_MIN
)
2144 } else if (count
< (SAMPLE_SIZE
/ 16)) {
2145 if (usleep_ms
< USLEEP_MAX
)
2149 if (bufinfo
.flags
& KDBG_WRAPPED
) {
2150 fprintf(stderr
, "fs_usage: buffer overrun, events generated too quickly: %d\n", count
);
2152 for (i
= 0; i
<= cur_max
; i
++) {
2153 th_state
[i
].thread
= 0;
2154 th_state
[i
].pid
= 0;
2155 th_state
[i
].pathptr
= (long *)NULL
;
2156 th_state
[i
].pathname
[0] = 0;
2161 map_is_the_same
= 0;
2166 kd
= (kd_buf
*)my_buffer
;
2168 fprintf(stderr
, "READTR returned %d items\n", count
);
2170 for (i
= 0; i
< count
; i
++) {
2171 int debugid
, thread
;
2183 thread
= kd
[i
].arg5
;
2184 debugid
= kd
[i
].debugid
;
2185 type
= kd
[i
].debugid
& DBG_FUNC_MASK
;
2187 now
= kd
[i
].timestamp
& KDBG_TIMESTAMP_MASK
;
2191 curr_time
= time((long *)0);
2193 * Compute bias seconds after each trace buffer read.
2194 * This helps resync timestamps with the system clock
2195 * in the event of a system sleep.
2197 if (bias_secs
== 0 || curr_time
< last_time
|| curr_time
> (last_time
+ 2)) {
2198 l_usecs
= (long long)(now
/ divisor
);
2199 secs
= l_usecs
/ 1000000;
2200 bias_secs
= curr_time
- secs
;
2204 if ((type
& P_DISKIO_MASK
) == P_DISKIO
) {
2205 insert_diskio(type
, kd
[i
].arg1
, kd
[i
].arg2
, kd
[i
].arg3
, kd
[i
].arg4
, thread
, (double)now
);
2208 if ((type
& P_DISKIO_MASK
) == P_DISKIO_DONE
) {
2209 if ((dio
= complete_diskio(kd
[i
].arg1
, kd
[i
].arg4
, kd
[i
].arg3
, thread
, (double)now
))) {
2218 case TRACE_DATA_NEWTHREAD
:
2220 if ((ti
= find_empty()) == NULL
)
2223 ti
->thread
= thread
;
2224 ti
->child_thread
= kd
[i
].arg1
;
2225 ti
->pid
= kd
[i
].arg2
;
2228 case TRACE_STRING_NEWTHREAD
:
2229 if ((ti
= find_thread(thread
, 0)) == (struct th_info
*)0)
2231 if (ti
->child_thread
== 0)
2233 create_map_entry(ti
->child_thread
, ti
->pid
, (char *)&kd
[i
].arg1
);
2239 case TRACE_DATA_EXEC
:
2241 if ((ti
= find_empty()) == NULL
)
2244 ti
->thread
= thread
;
2245 ti
->pid
= kd
[i
].arg1
;
2248 case TRACE_STRING_EXEC
:
2249 if ((ti
= find_thread(thread
, 0)) == (struct th_info
*)0)
2251 /* this is for backwards compatibility */
2252 create_map_entry(thread
, 0, (char *)&kd
[i
].arg1
);
2256 create_map_entry(thread
, ti
->pid
, (char *)&kd
[i
].arg1
);
2263 kill_thread_map(thread
);
2267 if (kd
[i
].arg4
& MAP_ANON
)
2272 case MACH_stkhandoff
:
2273 mark_thread_waited(thread
);
2277 if ((ti
= find_thread(thread
, 0)) == (struct th_info
*)0)
2280 if (ti
->pathptr
== NULL
) {
2281 sargptr
= ti
->pathname
;
2282 *sargptr
++ = kd
[i
].arg2
;
2283 *sargptr
++ = kd
[i
].arg3
;
2284 *sargptr
++ = kd
[i
].arg4
;
2286 * NULL terminate the 'string'
2289 ti
->pathptr
= sargptr
;
2291 sargptr
= ti
->pathptr
;
2294 * We don't want to overrun our pathname buffer if the
2295 * kernel sends us more VFS_LOOKUP entries than we can
2298 if (sargptr
>= &ti
->pathname
[NUMPARMS
]) {
2303 * We need to detect consecutive vfslookup entries.
2304 * So, if we get here and find a START entry,
2305 * fake the pathptr so we can bypass all further
2306 * vfslookup entries.
2308 if (debugid
& DBG_FUNC_START
) {
2309 ti
->pathptr
= &ti
->pathname
[NUMPARMS
];
2312 *sargptr
++ = kd
[i
].arg1
;
2313 *sargptr
++ = kd
[i
].arg2
;
2314 *sargptr
++ = kd
[i
].arg3
;
2315 *sargptr
++ = kd
[i
].arg4
;
2317 * NULL terminate the 'string'
2320 ti
->pathptr
= sargptr
;
2325 if (debugid
& DBG_FUNC_START
) {
2328 if ((type
& CLASS_MASK
) == FILEMGR_BASE
) {
2330 index
= filemgr_index(type
);
2332 if (index
>= MAX_FILEMGR
)
2335 if ((p
= filemgr_calls
[index
].fm_name
) == NULL
)
2340 enter_syscall(thread
, type
, &kd
[i
], p
, (double)now
);
2348 exit_syscall("PAGE_OUT_D", thread
, type
, 0, kd
[i
].arg1
, 0, 0, FMT_PGOUT
, (double)now
);
2350 exit_syscall("PAGE_OUT_V", thread
, type
, 0, kd
[i
].arg1
, 0, 0, FMT_PGOUT
, (double)now
);
2354 if (kd
[i
].arg4
== DBG_PAGEIN_FAULT
)
2355 exit_syscall("PAGE_IN", thread
, type
, 0, kd
[i
].arg1
, kd
[i
].arg2
, 0, FMT_PGIN
, (double)now
);
2356 else if (kd
[i
].arg4
== DBG_CACHE_HIT_FAULT
)
2357 exit_syscall("CACHE_HIT", thread
, type
, 0, kd
[i
].arg1
, kd
[i
].arg2
, 0, FMT_CACHEHIT
, (double)now
);
2359 if ((ti
= find_thread(thread
, type
))) {
2366 exit_syscall("map_fd", thread
, type
, kd
[i
].arg1
, kd
[i
].arg2
, 0, 0, FMT_FD
, (double)now
);
2369 case BSC_mmap_extended
:
2370 case BSC_mmap_extended2
:
2371 case BSC_msync_extended
:
2372 case BSC_pread_extended
:
2373 case BSC_pwrite_extended
:
2374 extend_syscall(thread
, type
, &kd
[i
]);
2378 if ((type
& CSC_MASK
) == BSC_BASE
) {
2380 index
= BSC_INDEX(type
);
2382 if (index
>= MAX_BSD_SYSCALL
)
2385 if (bsd_syscalls
[index
].sc_name
== NULL
)
2388 if (type
== BSC_execve
)
2389 execs_in_progress
--;
2391 exit_syscall(bsd_syscalls
[index
].sc_name
, thread
, type
, kd
[i
].arg1
, kd
[i
].arg2
, kd
[i
].arg3
, kd
[i
].arg4
,
2392 bsd_syscalls
[index
].sc_format
, (double)now
);
2397 if ((type
& CLASS_MASK
) == FILEMGR_BASE
) {
2399 index
= filemgr_index(type
);
2401 if (index
>= MAX_FILEMGR
)
2404 if (filemgr_calls
[index
].fm_name
== NULL
)
2407 exit_syscall(filemgr_calls
[index
].fm_name
, thread
, type
, kd
[i
].arg1
, kd
[i
].arg2
, kd
[i
].arg3
, kd
[i
].arg4
,
2408 FMT_DEFAULT
, (double)now
);
2418 enter_syscall_now(int thread
, int type
, kd_buf
*kd
, char *name
, double now
)
2426 kd_threadmap
*find_thread_map();
2433 if (execs_in_progress
) {
2434 if ((ti
= find_thread(thread
, BSC_execve
))) {
2436 exit_syscall("execve", thread
, BSC_execve
, 0, 0, 0, 0, FMT_DEFAULT
, (double)now
);
2437 execs_in_progress
--;
2441 if ((ti
= find_empty()) == NULL
)
2444 if ((type
& CLASS_MASK
) == FILEMGR_BASE
) {
2446 filemgr_in_progress
++;
2449 l_usecs
= (long long)(now
/ divisor
);
2450 secs
= l_usecs
/ 1000000;
2451 curr_time
= bias_secs
+ secs
;
2453 sprintf(buf
, "%-8.8s", &(ctime(&curr_time
)[11]));
2454 tsclen
= strlen(buf
);
2456 if (columns
> MAXCOLS
|| wideflag
) {
2457 usecs
= l_usecs
- (long long)((long long)secs
* 1000000);
2458 sprintf(&buf
[tsclen
], ".%03ld", (long)usecs
/ 1000);
2459 tsclen
= strlen(buf
);
2463 * Print timestamp column
2467 map
= find_thread_map(thread
);
2469 sprintf(buf
, " %-25.25s ", name
);
2470 nmclen
= strlen(buf
);
2473 sprintf(buf
, "(%d, 0x%x, 0x%x, 0x%x)", (short)kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
);
2474 argsclen
= strlen(buf
);
2477 * Calculate white space out to command
2479 if (columns
> MAXCOLS
|| wideflag
) {
2480 clen
= columns
- (tsclen
+ nmclen
+ argsclen
+ 20);
2482 clen
= columns
- (tsclen
+ nmclen
+ argsclen
+ 12);
2485 printf("%s", buf
); /* print the kdargs */
2486 memset(buf
, ' ', clen
);
2490 else if ((argsclen
+ clen
) > 0) {
2492 * no room so wipe out the kdargs
2494 memset(buf
, ' ', (argsclen
+ clen
));
2495 buf
[argsclen
+ clen
] = '\0';
2498 if (columns
> MAXCOLS
|| wideflag
)
2499 printf("%-20.20s\n", map
->command
);
2501 printf("%-12.12s\n", map
->command
);
2503 printf(" %-24.24s (%5d, %#x, 0x%x, 0x%x)\n", name
, (short)kd
->arg1
, kd
->arg2
, kd
->arg3
, kd
->arg4
);
2505 ti
->thread
= thread
;
2509 ti
->arg1
= kd
->arg1
;
2510 ti
->arg2
= kd
->arg2
;
2511 ti
->arg3
= kd
->arg3
;
2512 ti
->arg4
= kd
->arg4
;
2513 ti
->pathptr
= (long *)NULL
;
2514 ti
->pathname
[0] = 0;
2519 enter_syscall(int thread
, int type
, kd_buf
*kd
, char *name
, double now
)
2523 if (type
== MACH_pageout
|| type
== MACH_vmfault
|| type
== MSC_map_fd
) {
2524 enter_syscall_now(thread
, type
, kd
, name
, now
);
2527 if ((type
& CSC_MASK
) == BSC_BASE
) {
2529 index
= BSC_INDEX(type
);
2531 if (index
>= MAX_BSD_SYSCALL
)
2534 if (type
== BSC_execve
)
2535 execs_in_progress
++;
2537 if (bsd_syscalls
[index
].sc_name
)
2538 enter_syscall_now(thread
, type
, kd
, name
, now
);
2542 if ((type
& CLASS_MASK
) == FILEMGR_BASE
) {
2544 index
= filemgr_index(type
);
2546 if (index
>= MAX_FILEMGR
)
2549 if (filemgr_calls
[index
].fm_name
)
2550 enter_syscall_now(thread
, type
, kd
, name
, now
);
2555 * Handle system call extended trace data.
2557 * Wipe out the kd args that were collected upon syscall_entry
2558 * because it is the extended info that we really want, and it
2559 * is all we really need.
2563 extend_syscall(int thread
, int type
, kd_buf
*kd
)
2568 case BSC_mmap_extended
:
2569 if ((ti
= find_thread(thread
, BSC_mmap
)) == (struct th_info
*)0)
2571 ti
->arg8
= ti
->arg3
; /* save protection */
2572 ti
->arg1
= kd
->arg1
; /* the fd */
2573 ti
->arg3
= kd
->arg2
; /* bottom half address */
2574 ti
->arg5
= kd
->arg3
; /* bottom half size */
2576 case BSC_mmap_extended2
:
2577 if ((ti
= find_thread(thread
, BSC_mmap
)) == (struct th_info
*)0)
2579 ti
->arg2
= kd
->arg1
; /* top half address */
2580 ti
->arg4
= kd
->arg2
; /* top half size */
2581 ti
->arg6
= kd
->arg3
; /* top half file offset */
2582 ti
->arg7
= kd
->arg4
; /* bottom half file offset */
2584 case BSC_msync_extended
:
2585 if ((ti
= find_thread(thread
, BSC_msync
)) == (struct th_info
*)0) {
2586 if ((ti
= find_thread(thread
, BSC_msync_nocancel
)) == (struct th_info
*)0)
2589 ti
->arg4
= kd
->arg1
; /* top half address */
2590 ti
->arg5
= kd
->arg2
; /* top half size */
2592 case BSC_pread_extended
:
2593 if ((ti
= find_thread(thread
, BSC_pread
)) == (struct th_info
*)0) {
2594 if ((ti
= find_thread(thread
, BSC_pread_nocancel
)) == (struct th_info
*)0)
2597 ti
->arg1
= kd
->arg1
; /* the fd */
2598 ti
->arg2
= kd
->arg2
; /* nbytes */
2599 ti
->arg3
= kd
->arg3
; /* top half offset */
2600 ti
->arg4
= kd
->arg4
; /* bottom half offset */
2602 case BSC_pwrite_extended
:
2603 if ((ti
= find_thread(thread
, BSC_pwrite
)) == (struct th_info
*)0) {
2604 if ((ti
= find_thread(thread
, BSC_pwrite_nocancel
)) == (struct th_info
*)0)
2607 ti
->arg1
= kd
->arg1
; /* the fd */
2608 ti
->arg2
= kd
->arg2
; /* nbytes */
2609 ti
->arg3
= kd
->arg3
; /* top half offset */
2610 ti
->arg4
= kd
->arg4
; /* bottom half offset */
2619 exit_syscall(char *sc_name
, int thread
, int type
, int arg1
, int arg2
, int arg3
, int arg4
,
2620 int format
, double now
)
2624 if ((ti
= find_thread(thread
, type
)) == (struct th_info
*)0)
2627 if (check_filter_mode(ti
, type
, arg1
, arg2
, sc_name
))
2628 format_print(ti
, sc_name
, thread
, type
, arg1
, arg2
, arg3
, arg4
, format
, now
, ti
->stime
, ti
->waited
, (char *)ti
->pathname
, NULL
);
2630 if ((type
& CLASS_MASK
) == FILEMGR_BASE
) {
2633 if (filemgr_in_progress
> 0)
2634 filemgr_in_progress
--;
2641 get_mode_nibble(char * buf
, int smode
, int special
, char x_on
, char x_off
)
2660 get_mode_string(int mode
, char *buf
)
2662 memset(buf
, '-', 9);
2665 get_mode_nibble(&buf
[6], mode
, (mode
& 01000), 't', 'T');
2666 get_mode_nibble(&buf
[3], (mode
>>3), (mode
& 02000), 's', 'S');
2667 get_mode_nibble(&buf
[0], (mode
>>6), (mode
& 04000), 's', 'S');
2671 int clip_64bit(char *s
, uint64_t value
)
2675 if ( (value
& 0xff00000000000000LL
) )
2676 clen
= printf("%s0x%16.16qx", s
, value
);
2677 else if ( (value
& 0x00ff000000000000LL
) )
2678 clen
= printf("%s0x%14.14qx ", s
, value
);
2679 else if ( (value
& 0x0000ff0000000000LL
) )
2680 clen
= printf("%s0x%12.12qx ", s
, value
);
2681 else if ( (value
& 0x000000ff00000000LL
) )
2682 clen
= printf("%s0x%10.10qx ", s
, value
);
2684 clen
= printf("%s0x%8.8qx ", s
, value
);
2691 format_print(struct th_info
*ti
, char *sc_name
, int thread
, int type
, int arg1
, int arg2
, int arg3
, int arg4
,
2692 int format
, double now
, double stime
, int waited
, char *pathname
, struct diskio
*dio
)
2701 kd_threadmap
*find_thread_map();
2709 char *framework_name
;
2714 int need_msec_update
= 0;
2715 static char timestamp
[32];
2716 static int last_timestamp
= 0;
2717 static int timestamp_len
= 0;
2718 static int last_msec
= 0;
2724 command_name
= dio
->issuing_command
;
2726 if (map_is_the_same
&& thread
== last_thread
)
2729 if ((map
= find_thread_map(thread
))) {
2731 last_thread
= thread
;
2732 map_is_the_same
= 1;
2736 command_name
= map
->command
;
2738 l_usecs
= (long long)(now
/ divisor
);
2739 secs
= l_usecs
/ 1000000;
2740 curr_time
= bias_secs
+ secs
;
2742 if (last_timestamp
!= curr_time
) {
2743 timestamp_len
= sprintf(timestamp
, "%-8.8s", &(ctime(&curr_time
)[11]));
2744 last_timestamp
= curr_time
;
2745 need_msec_update
= 1;
2747 if (columns
> MAXCOLS
|| wideflag
) {
2750 tlen
= timestamp_len
;
2752 msec
= (l_usecs
- (long long)((long long)secs
* 1000000)) / 1000;
2754 if (msec
!= last_msec
|| need_msec_update
) {
2755 sprintf(×tamp
[tlen
], ".%03ld", (long)msec
);
2760 timestamp
[tlen
] = '\0';
2762 if (filemgr_in_progress
) {
2763 if (class != FILEMGR_CLASS
) {
2764 if (find_thread(thread
, -1)) {
2772 if ((class == FILEMGR_CLASS
) && (columns
> MAXCOLS
|| wideflag
))
2773 clen
= printf("%s %-20.20s", timestamp
, sc_name
);
2774 else if (in_filemgr
)
2775 clen
= printf("%s %-15.15s", timestamp
, sc_name
);
2777 clen
= printf("%s %-17.17s", timestamp
, sc_name
);
2779 framework_name
= (char *)0;
2781 if (columns
> MAXCOLS
|| wideflag
) {
2783 off_t offset_reassembled
= 0LL;
2789 * pathname based system calls or
2790 * calls with no fd or pathname (i.e. sync)
2793 clen
+= printf(" [%3d] ", arg1
);
2795 clen
+= printf(" ");
2800 * fd based system call... no I/O
2803 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
2805 clen
+= printf(" F=%-3d", ti
->arg1
);
2813 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
2815 clen
+= printf(" F=%-3d F=%-3d", ti
->arg1
, arg2
);
2820 * system calls with fd's that return an I/O completion count
2823 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
2825 clen
+= printf(" F=%-3d B=0x%-6x", ti
->arg1
, arg2
);
2832 user_addr
= ((uint64_t)arg2
<< 32) | (uint32_t)arg3
;
2833 framework_name
= lookup_name(user_addr
);
2834 clen
+= clip_64bit(" A=", user_addr
);
2841 user_addr
= ((uint64_t)arg2
<< 32) | (uint32_t)arg3
;
2843 framework_name
= lookup_name(user_addr
);
2844 clen
+= clip_64bit(" A=", user_addr
);
2851 clen
+= printf(" B=0x%-8x", arg2
);
2859 clen
+= printf(" D=0x%8.8x [%3d]", dio
->blkno
, dio
->io_errno
);
2861 clen
+= printf(" D=0x%8.8x B=0x%-6x /dev/%s", dio
->blkno
, dio
->iosize
, find_disk_name(dio
->dev
));
2873 if (ti
->arg3
& MS_ASYNC
)
2874 mlen
+= sprintf(&buf
[mlen
], "MS_ASYNC | ");
2876 mlen
+= sprintf(&buf
[mlen
], "MS_SYNC | ");
2878 if (ti
->arg3
& MS_INVALIDATE
)
2879 mlen
+= sprintf(&buf
[mlen
], "MS_INVALIDATE | ");
2880 if (ti
->arg3
& MS_KILLPAGES
)
2881 mlen
+= sprintf(&buf
[mlen
], "MS_KILLPAGES | ");
2882 if (ti
->arg3
& MS_DEACTIVATE
)
2883 mlen
+= sprintf(&buf
[mlen
], "MS_DEACTIVATE | ");
2885 if (ti
->arg3
& ~(MS_ASYNC
| MS_SYNC
| MS_INVALIDATE
| MS_KILLPAGES
| MS_DEACTIVATE
))
2886 mlen
+= sprintf(&buf
[mlen
], "UNKNOWN | ");
2889 buf
[mlen
- 3] = '\0';
2892 clen
+= printf(" [%3d]", arg1
);
2894 user_addr
= (((off_t
)(unsigned int)(ti
->arg4
)) << 32) | (unsigned int)(ti
->arg1
);
2895 clen
+= clip_64bit(" A=", user_addr
);
2897 user_size
= (((off_t
)(unsigned int)(ti
->arg5
)) << 32) | (unsigned int)(ti
->arg2
);
2899 clen
+= printf(" B=0x%-16qx <%s>", user_size
, buf
);
2912 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
2914 clen
+= printf(" F=%-3d", ti
->arg1
);
2978 case F_PATHPKG_CHECK
:
2979 p
= "PATHPKG_CHECK";
2991 clen
+= printf(" <%s>", p
);
2993 clen
+= printf(" <CMD=%d>", ti
->arg2
);
3004 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
3006 clen
+= printf(" F=%-3d", ti
->arg1
);
3008 clen
+= printf(" <CMD=0x%x>", ti
->arg2
);
3018 clen
+= printf(" [%3d]", arg1
);
3020 clen
+= printf(" S=%-3d", arg2
);
3027 * pread, pwrite, lseek
3029 clen
+= printf(" F=%-3d", ti
->arg1
);
3032 clen
+= printf("[%3d] ", arg1
);
3034 if (format
== FMT_PREAD
)
3035 clen
+= printf(" B=0x%-8x ", arg2
);
3037 clen
+= printf(" ");
3039 if (format
== FMT_PREAD
)
3040 offset_reassembled
= (((off_t
)(unsigned int)(ti
->arg3
)) << 32) | (unsigned int)(ti
->arg4
);
3043 offset_reassembled
= (((off_t
)(unsigned int)(arg2
)) << 32) | (unsigned int)(arg3
);
3045 offset_reassembled
= (((off_t
)(unsigned int)(arg3
)) << 32) | (unsigned int)(arg2
);
3047 clen
+= clip_64bit("O=", offset_reassembled
);
3049 if (format
== FMT_LSEEK
) {
3052 if (ti
->arg4
== SEEK_SET
)
3054 else if (ti
->arg4
== SEEK_CUR
)
3056 else if (ti
->arg4
== SEEK_END
)
3061 clen
+= printf(" <%s>", mode
);
3069 clen
+= printf(" F=%-3d ", ti
->arg1
);
3072 clen
+= printf("[%3d] ", arg1
);
3075 user_addr
= (((off_t
)(unsigned int)(ti
->arg2
)) << 32) | (unsigned int)(ti
->arg3
);
3077 clen
+= clip_64bit("A=", user_addr
);
3079 offset_reassembled
= (((off_t
)(unsigned int)(ti
->arg6
)) << 32) | (unsigned int)(ti
->arg7
);
3081 clen
+= clip_64bit("O=", offset_reassembled
);
3083 user_size
= (((off_t
)(unsigned int)(ti
->arg4
)) << 32) | (unsigned int)(ti
->arg5
);
3085 clen
+= printf("B=0x%-16qx", user_size
);
3087 clen
+= printf(" <");
3089 if (ti
->arg8
& PROT_READ
)
3090 clen
+= printf("READ");
3092 if (ti
->arg8
& PROT_WRITE
)
3093 clen
+= printf("|WRITE");
3095 if (ti
->arg8
& PROT_EXEC
)
3096 clen
+= printf("|EXEC");
3098 clen
+= printf(">");
3105 * ftruncate, truncate
3107 if (format
== FMT_FTRUNC
)
3108 clen
+= printf(" F=%-3d", ti
->arg1
);
3110 clen
+= printf(" ");
3113 clen
+= printf("[%3d]", arg1
);
3116 offset_reassembled
= (((off_t
)(unsigned int)(ti
->arg2
)) << 32) | (unsigned int)(ti
->arg3
);
3118 offset_reassembled
= (((off_t
)(unsigned int)(ti
->arg3
)) << 32) | (unsigned int)(ti
->arg2
);
3120 clen
+= clip_64bit(" O=", offset_reassembled
);
3133 if (format
== FMT_FCHFLAGS
) {
3135 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
3137 clen
+= printf(" F=%-3d", ti
->arg1
);
3140 clen
+= printf(" [%3d] ", arg1
);
3145 if (ti
->arg2
& UF_NODUMP
)
3146 mlen
+= sprintf(&buf
[mlen
], "UF_NODUMP | ");
3147 if (ti
->arg2
& UF_IMMUTABLE
)
3148 mlen
+= sprintf(&buf
[mlen
], "UF_IMMUTABLE | ");
3149 if (ti
->arg2
& UF_APPEND
)
3150 mlen
+= sprintf(&buf
[mlen
], "UF_APPEND | ");
3151 if (ti
->arg2
& UF_OPAQUE
)
3152 mlen
+= sprintf(&buf
[mlen
], "UF_OPAQUE | ");
3153 if (ti
->arg2
& SF_ARCHIVED
)
3154 mlen
+= sprintf(&buf
[mlen
], "SF_ARCHIVED | ");
3155 if (ti
->arg2
& SF_IMMUTABLE
)
3156 mlen
+= sprintf(&buf
[mlen
], "SF_IMMUTABLE | ");
3157 if (ti
->arg2
& SF_APPEND
)
3158 mlen
+= sprintf(&buf
[mlen
], "SF_APPEND | ");
3161 mlen
+= sprintf(&buf
[mlen
], "CLEAR_ALL_FLAGS | ");
3162 else if (ti
->arg2
& ~(UF_NODUMP
| UF_IMMUTABLE
| UF_APPEND
| SF_ARCHIVED
| SF_IMMUTABLE
| SF_APPEND
))
3163 mlen
+= sprintf(&buf
[mlen
], "UNKNOWN | ");
3172 memset(&buf
[mlen
], ' ', 21 - mlen
);
3175 clen
+= printf("%s", buf
);
3182 case FMT_FCHMOD_EXT
:
3187 * fchmod, fchmod_extended, chmod, chmod_extended
3191 if (format
== FMT_FCHMOD
|| format
== FMT_FCHMOD_EXT
) {
3193 clen
+= printf(" F=%-3d[%3d] ", ti
->arg1
, arg1
);
3195 clen
+= printf(" F=%-3d ", ti
->arg1
);
3198 clen
+= printf(" [%3d] ", arg1
);
3200 clen
+= printf(" ");
3202 if (format
== FMT_FCHMOD
|| format
== FMT_CHMOD
)
3207 get_mode_string(mode
, &buf
[0]);
3210 clen
+= printf("<%s> ", buf
);
3212 clen
+= printf("<%s>", buf
);
3223 memset(mode
, '_', 4);
3226 if (ti
->arg2
& R_OK
)
3228 if (ti
->arg2
& W_OK
)
3230 if (ti
->arg2
& X_OK
)
3232 if (ti
->arg2
== F_OK
)
3236 clen
+= printf(" [%3d] (%s) ", arg1
, mode
);
3238 clen
+= printf(" (%s) ", mode
);
3251 memset(mode
, '_', 6);
3254 if (ti
->arg2
& O_RDWR
) {
3257 } else if (ti
->arg2
& O_WRONLY
)
3262 if (ti
->arg2
& O_CREAT
)
3265 if (ti
->arg2
& O_APPEND
)
3268 if (ti
->arg2
& O_TRUNC
)
3271 if (ti
->arg2
& O_EXCL
)
3275 clen
+= printf(" [%3d] (%s) ", arg1
, mode
);
3277 clen
+= printf(" F=%-3d (%s) ", arg2
, mode
);
3311 domain
= "AF_IMPLINK";
3322 type
= "SOCK_STREAM";
3326 type
= "SOCK_DGRAM";
3333 case SOCK_SEQPACKET
:
3334 type
= "SOCK_SEQPACKET";
3347 clen
+= printf(" [%3d] <%s, %s, 0x%x>", arg1
, domain
, type
, ti
->arg3
);
3349 clen
+= printf(" F=%-3d <%s, %s, 0x%x>", arg2
, domain
, type
, ti
->arg3
);
3356 * aio_fsync [errno] AIOCBP OP
3360 if (ti
->arg1
== O_SYNC
|| ti
->arg1
== 0)
3363 else if (ti
->arg1
== O_DSYNC
)
3370 clen
+= printf(" [%3d] P=0x%8.8x <%s>", arg1
, ti
->arg2
, op
);
3372 clen
+= printf(" P=0x%8.8x <%s>", ti
->arg2
, op
);
3376 case FMT_AIO_RETURN
:
3378 * aio_return [errno] AIOCBP IOSIZE
3381 clen
+= printf(" [%3d] P=0x%8.8x", arg1
, ti
->arg1
);
3383 clen
+= printf(" P=0x%8.8x B=0x%-8x", ti
->arg1
, arg2
);
3386 case FMT_AIO_SUSPEND
:
3388 * aio_suspend [errno] NENTS
3391 clen
+= printf(" [%3d] N=%d", arg1
, ti
->arg2
);
3393 clen
+= printf(" N=%d", ti
->arg2
);
3396 case FMT_AIO_CANCEL
:
3398 * aio_cancel [errno] FD or AIOCBP (if non-null)
3402 clen
+= printf(" [%3d] P=0x%8.8x", arg1
, ti
->arg2
);
3404 clen
+= printf(" P=0x%8.8x", ti
->arg2
);
3407 clen
+= printf(" F=%-3d[%3d]", ti
->arg1
, arg1
);
3409 clen
+= printf(" F=%-3d", ti
->arg1
);
3415 * aio_error, aio_read, aio_write [errno] AIOCBP
3418 clen
+= printf(" [%3d] P=0x%8.8x", arg1
, ti
->arg1
);
3420 clen
+= printf(" P=0x%8.8x", ti
->arg1
);
3423 case FMT_LIO_LISTIO
:
3426 * lio_listio [errno] NENTS MODE
3430 if (ti
->arg1
== LIO_NOWAIT
)
3432 else if (ti
->arg1
== LIO_WAIT
)
3438 clen
+= printf(" [%3d] N=%d <%s>", arg1
, ti
->arg3
, op
);
3440 clen
+= printf(" N=%d <%s>", ti
->arg3
, op
);
3448 * Calculate space available to print pathname
3450 if (columns
> MAXCOLS
|| wideflag
)
3451 clen
= columns
- (clen
+ 14 + 20);
3453 clen
= columns
- (clen
+ 14 + 12);
3455 if (class != FILEMGR_CLASS
&& !nopadding
)
3459 len
= sprintf(&buf
[0], " %s ", framework_name
);
3460 else if (*pathname
!= '\0')
3461 len
= sprintf(&buf
[0], " %s ", pathname
);
3467 * Add null padding if column length
3468 * is wider than the pathname length.
3470 memset(&buf
[len
], ' ', clen
- len
);
3475 } else if (clen
== len
) {
3478 } else if ((clen
> 0) && (clen
< len
)) {
3480 * This prints the tail end of the pathname
3482 buf
[len
-clen
] = ' ';
3484 pathname
= &buf
[len
- clen
];
3491 * fudge some additional system call overhead
3492 * that currently isn't tracked... this also
3493 * insures that we see a minimum of 1 us for
3496 usecs
= (unsigned long)(((now
- stime
) + (divisor
-1)) / divisor
);
3497 secs
= usecs
/ 1000000;
3498 usecs
-= secs
* 1000000;
3500 if (class != FILEMGR_CLASS
&& !nopadding
)
3510 if (columns
> MAXCOLS
|| wideflag
)
3511 printf("%s%s %3ld.%06ld%s %-20.20s\n", p1
, pathname
, (unsigned long)secs
, (unsigned long)usecs
, p2
, command_name
);
3513 printf("%s%s %3ld.%06ld%s %-12.12s\n", p1
, pathname
, (unsigned long)secs
, (unsigned long)usecs
, p2
, command_name
);
3524 * This flag is turned off when calling
3525 * quit() due to a set_remove() failure.
3527 if (set_remove_flag
)
3530 fprintf(stderr
, "fs_usage: ");
3532 fprintf(stderr
, "%s", s
);
3540 struct mach_timebase_info mti
;
3542 mach_timebase_info(&mti
);
3544 divisor
= ((double)mti
.denom
/ (double)mti
.numer
) * 1000;
3548 void read_command_map()
3552 int prev_total_threads
;
3560 prev_total_threads
= total_threads
;
3561 total_threads
= bufinfo
.nkdthreads
;
3562 size
= bufinfo
.nkdthreads
* sizeof(kd_threadmap
);
3566 if ((mapptr
= (kd_threadmap
*) malloc(size
)))
3568 bzero (mapptr
, size
);
3570 /* Now read the threadmap */
3572 mib
[1] = KERN_KDEBUG
;
3573 mib
[2] = KERN_KDTHRMAP
;
3576 mib
[5] = 0; /* no flags */
3577 if (sysctl(mib
, 3, mapptr
, &size
, NULL
, 0) < 0)
3579 /* This is not fatal -- just means I cant map command strings */
3586 if (mapptr
&& (filter_mode
& (NETWORK_FILTER
| FILESYS_FILTER
)))
3590 /* We accept the fact that we lose file descriptor state if the
3592 for (i
= 0; i
< prev_total_threads
; i
++)
3594 if (fdmapptr
[i
].fd_setptr
)
3595 free (fdmapptr
[i
].fd_setptr
);
3601 size
= total_threads
* sizeof(fd_threadmap
);
3602 if ((fdmapptr
= (fd_threadmap
*) malloc(size
)))
3604 bzero (fdmapptr
, size
);
3605 /* reinitialize file descriptor state map */
3606 for (i
= 0; i
< total_threads
; i
++)
3608 fdmapptr
[i
].fd_thread
= mapptr
[i
].thread
;
3609 fdmapptr
[i
].fd_valid
= mapptr
[i
].valid
;
3610 fdmapptr
[i
].fd_setsize
= 0;
3611 fdmapptr
[i
].fd_setptr
= 0;
3616 /* Resolve any LaunchCFMApp command names */
3617 if (mapptr
&& arguments
)
3619 for (i
=0; i
< total_threads
; i
++)
3623 pid
= mapptr
[i
].valid
;
3625 if (pid
== 0 || pid
== 1)
3627 else if (!strncmp(mapptr
[i
].command
,"LaunchCFMA", 10))
3629 (void)get_real_command_name(pid
, mapptr
[i
].command
, sizeof(mapptr
[i
].command
));
3636 void create_map_entry(int thread
, int pid
, char *command
)
3640 fd_threadmap
*fdmap
= 0;
3645 for (i
= 0, map
= 0; !map
&& i
< total_threads
; i
++)
3647 if ((int)mapptr
[i
].thread
== thread
)
3649 map
= &mapptr
[i
]; /* Reuse this entry, the thread has been
3651 if ((filter_mode
& (NETWORK_FILTER
| FILESYS_FILTER
)) && fdmapptr
)
3653 fdmap
= &fdmapptr
[i
];
3654 if (fdmap
->fd_thread
!= thread
) /* This shouldn't happen */
3655 fdmap
= (fd_threadmap
*)0;
3660 if (!map
) /* look for invalid entries that I can reuse*/
3662 for (i
= 0, map
= 0; !map
&& i
< total_threads
; i
++)
3664 if (mapptr
[i
].valid
== 0 )
3665 map
= &mapptr
[i
]; /* Reuse this invalid entry */
3666 if ((filter_mode
& (NETWORK_FILTER
| FILESYS_FILTER
)) && fdmapptr
)
3668 fdmap
= &fdmapptr
[i
];
3676 * If reach here, then this is a new thread and
3677 * there are no invalid entries to reuse
3678 * Double the size of the thread map table.
3680 n
= total_threads
* 2;
3681 mapptr
= (kd_threadmap
*) realloc(mapptr
, n
* sizeof(kd_threadmap
));
3682 bzero(&mapptr
[total_threads
], total_threads
*sizeof(kd_threadmap
));
3683 map
= &mapptr
[total_threads
];
3685 if ((filter_mode
& (NETWORK_FILTER
| FILESYS_FILTER
)) && fdmapptr
)
3687 fdmapptr
= (fd_threadmap
*)realloc(fdmapptr
, n
* sizeof(fd_threadmap
));
3688 bzero(&fdmapptr
[total_threads
], total_threads
*sizeof(fd_threadmap
));
3689 fdmap
= &fdmapptr
[total_threads
];
3696 map
->thread
= thread
;
3698 * The trace entry that returns the command name will hold
3699 * at most, MAXCOMLEN chars, and in that case, is not
3700 * guaranteed to be null terminated.
3702 (void)strncpy (map
->command
, command
, MAXCOMLEN
);
3703 map
->command
[MAXCOMLEN
] = '\0';
3707 fdmap
->fd_valid
= 1;
3708 fdmap
->fd_thread
= thread
;
3709 if (fdmap
->fd_setptr
)
3711 free(fdmap
->fd_setptr
);
3712 fdmap
->fd_setptr
= (unsigned long *)0;
3714 fdmap
->fd_setsize
= 0;
3717 if (pid
== 0 || pid
== 1)
3719 else if (!strncmp(map
->command
, "LaunchCFMA", 10))
3720 (void)get_real_command_name(pid
, map
->command
, sizeof(map
->command
));
3724 kd_threadmap
*find_thread_map(int thread
)
3730 return((kd_threadmap
*)0);
3732 for (i
= 0; i
< total_threads
; i
++)
3735 if (map
->valid
&& ((int)map
->thread
== thread
))
3740 return ((kd_threadmap
*)0);
3743 fd_threadmap
*find_fd_thread_map(int thread
)
3746 fd_threadmap
*fdmap
= 0;
3749 return((fd_threadmap
*)0);
3751 for (i
= 0; i
< total_threads
; i
++)
3753 fdmap
= &fdmapptr
[i
];
3754 if (fdmap
->fd_valid
&& ((int)fdmap
->fd_thread
== thread
))
3759 return ((fd_threadmap
*)0);
3764 kill_thread_map(int thread
)
3767 fd_threadmap
*fdmap
;
3769 if (thread
== last_thread
)
3770 map_is_the_same
= 0;
3772 if ((map
= find_thread_map(thread
))) {
3775 map
->command
[0] = '\0';
3778 if ((filter_mode
& (NETWORK_FILTER
| FILESYS_FILTER
)))
3780 if ((fdmap
= find_fd_thread_map(thread
)))
3782 fdmap
->fd_valid
= 0;
3783 fdmap
->fd_thread
= 0;
3784 if (fdmap
->fd_setptr
)
3786 free (fdmap
->fd_setptr
);
3787 fdmap
->fd_setptr
= (unsigned long *)0;
3789 fdmap
->fd_setsize
= 0;
3802 ret
= (int)strtol(str
, &cp
, 10);
3803 if (cp
== str
|| *cp
) {
3804 /* Assume this is a command string and find matching pids */
3808 for (i
=0; i
< kp_nentries
&& num_of_pids
< (MAX_PIDS
- 1); i
++) {
3809 if(kp_buffer
[i
].kp_proc
.p_stat
== 0)
3812 if(!strncmp(str
, kp_buffer
[i
].kp_proc
.p_comm
,
3813 sizeof(kp_buffer
[i
].kp_proc
.p_comm
) -1))
3814 pids
[num_of_pids
++] = kp_buffer
[i
].kp_proc
.p_pid
;
3818 else if (num_of_pids
< (MAX_PIDS
- 1))
3819 pids
[num_of_pids
++] = ret
;
3826 char *lookup_name(uint64_t user_addr
)
3829 register int start
, last
;
3831 if (numFrameworks
&& ((user_addr
>= framework32
.b_address
&& user_addr
< framework32
.e_address
) ||
3832 (user_addr
>= framework64
.b_address
&& user_addr
< framework64
.e_address
))) {
3835 last
= numFrameworks
;
3837 for (i
= numFrameworks
/ 2; i
>= 0 && i
< numFrameworks
; ) {
3839 if (user_addr
>= frameworkInfo
[i
].b_address
&& user_addr
< frameworkInfo
[i
].e_address
)
3840 return(frameworkInfo
[i
].name
);
3842 if (user_addr
>= frameworkInfo
[i
].b_address
) {
3844 i
= start
+ ((last
- i
) / 2);
3847 i
= start
+ ((i
- start
) / 2);
3856 * Comparison routines for sorting
3858 static int compareFrameworkAddress(const void *aa
, const void *bb
)
3860 LibraryInfo
*a
= (LibraryInfo
*)aa
;
3861 LibraryInfo
*b
= (LibraryInfo
*)bb
;
3863 if (a
->b_address
< b
->b_address
) return -1;
3864 if (a
->b_address
== b
->b_address
) return 0;
3869 int scanline(char *inputstring
, char **argv
, int maxtokens
)
3872 char **ap
= argv
, *p
, *val
;
3874 for (p
= inputstring
; n
< maxtokens
&& p
!= NULL
; )
3876 while ((val
= strsep(&p
, " \t")) != NULL
&& *val
== '\0');
3885 int ReadSharedCacheMap(const char *path
, LibraryRange
*lr
)
3890 uint64_t b_frameworkAddress
, b_frameworkDataAddress
;
3891 uint64_t e_frameworkAddress
, e_frameworkDataAddress
;
3892 char frameworkName
[256];
3895 char *substring
,*ptr
;
3898 bzero(buf
, sizeof(buf
));
3899 bzero(tokens
, sizeof(tokens
));
3905 if ((fd
= fopen(path
, "r")) == 0)
3909 while (fgets(buf
, 1023, fd
)) {
3910 if (strncmp(buf
, "mapping", 7))
3913 buf
[strlen(buf
)-1] = 0;
3915 frameworkName
[0] = 0;
3918 b_frameworkAddress
= 0;
3919 b_frameworkDataAddress
= 0;
3920 e_frameworkAddress
= 0;
3921 e_frameworkDataAddress
= 0;
3924 * Extract lib name from path name
3926 if ((substring
= strrchr(buf
, '.')))
3929 * There is a ".": name is whatever is between the "/" around the "."
3931 while ( *substring
!= '/') { /* find "/" before "." */
3935 strncpy(frameworkName
, substring
, 256); /* copy path from "/" */
3936 frameworkName
[255] = 0;
3937 substring
= frameworkName
;
3939 while ( *substring
!= '/' && *substring
) /* find "/" after "." and stop string there */
3946 * No ".": take segment after last "/"
3954 substring
= ptr
+ 1;
3957 strncpy(frameworkName
, substring
, 256);
3958 frameworkName
[255] = 0;
3960 while (fgets(buf
, 1023, fd
) && numFrameworks
< (MAXINDEX
- 2))
3965 buf
[strlen(buf
)-1] = 0;
3967 ntokens
= scanline(buf
, tokens
, 64);
3972 if (strncmp(tokens
[0], "__TEXT", 6) == 0) {
3973 b_frameworkAddress
= strtoull(tokens
[1], 0, 16);
3974 e_frameworkAddress
= strtoull(tokens
[3], 0, 16);
3975 } else if (strncmp(tokens
[0], "__DATA", 6) == 0) {
3976 b_frameworkDataAddress
= strtoull(tokens
[1], 0, 16);
3977 e_frameworkDataAddress
= strtoull(tokens
[3], 0, 16);
3978 } else if (strncmp(tokens
[0], "__LINKEDIT", 10) == 0)
3982 * Make sure that we have 2 addresses
3984 if (b_frameworkAddress
&& b_frameworkDataAddress
) {
3986 frameworkInfo
[numFrameworks
].b_address
= b_frameworkAddress
;
3987 frameworkInfo
[numFrameworks
].e_address
= e_frameworkAddress
;
3989 frameworkInfo
[numFrameworks
+1].b_address
= b_frameworkDataAddress
;
3990 frameworkInfo
[numFrameworks
+1].e_address
= e_frameworkDataAddress
;
3992 frameworkInfo
[numFrameworks
].name
= (char *)malloc(strlen(frameworkName
) + 1);
3993 strcpy(frameworkInfo
[numFrameworks
].name
, frameworkName
);
3994 frameworkInfo
[numFrameworks
+1].name
= frameworkInfo
[numFrameworks
].name
;
3998 printf("%s: %qx-%qx %qx-%qx\n", frameworkName
, b_frameworkAddress
, e_frameworkAddress
, b_frameworkDataAddress
, e_frameworkDataAddress
);
4000 if (lr
->b_address
== 0)
4001 lr
->b_address
= b_frameworkAddress
;
4003 if (b_frameworkAddress
< lr
->b_address
)
4004 lr
->b_address
= b_frameworkAddress
;
4006 if (b_frameworkDataAddress
< lr
->b_address
)
4007 lr
->b_address
= b_frameworkDataAddress
;
4009 if (e_frameworkAddress
> lr
->e_address
)
4010 lr
->e_address
= e_frameworkAddress
;
4012 if (e_frameworkDataAddress
> lr
->e_address
)
4013 lr
->e_address
= e_frameworkDataAddress
;
4015 if (fgets(buf
, 1023, fd
) == 0)
4018 buf
[strlen(buf
)-1] = 0;
4027 SortFrameworkAddresses()
4030 frameworkInfo
[numFrameworks
].b_address
= frameworkInfo
[numFrameworks
- 1].b_address
+ 0x800000;
4031 frameworkInfo
[numFrameworks
].e_address
= frameworkInfo
[numFrameworks
].b_address
;
4032 frameworkInfo
[numFrameworks
].name
= (char *)0;
4034 qsort(frameworkInfo
, numFrameworks
, sizeof(LibraryInfo
), compareFrameworkAddress
);
4038 struct diskio
*insert_diskio(int type
, int bp
, int dev
, int blkno
, int io_size
, int thread
, double curtime
)
4040 register struct diskio
*dio
;
4041 register kd_threadmap
*map
;
4043 if ((dio
= free_diskios
))
4044 free_diskios
= dio
->next
;
4046 if ((dio
= (struct diskio
*)malloc(sizeof(struct diskio
))) == NULL
)
4055 dio
->iosize
= io_size
;
4056 dio
->issued_time
= curtime
;
4057 dio
->issuing_thread
= thread
;
4059 if ((map
= find_thread_map(thread
)))
4061 strncpy(dio
->issuing_command
, map
->command
, MAXCOMLEN
);
4062 dio
->issuing_command
[MAXCOMLEN
-1] = '\0';
4065 strcpy(dio
->issuing_command
, "");
4067 dio
->next
= busy_diskios
;
4069 dio
->next
->prev
= dio
;
4076 struct diskio
*complete_diskio(int bp
, int io_errno
, int resid
, int thread
, double curtime
)
4078 register struct diskio
*dio
;
4080 for (dio
= busy_diskios
; dio
; dio
= dio
->next
) {
4081 if (dio
->bp
== bp
) {
4083 if (dio
== busy_diskios
) {
4084 if ((busy_diskios
= dio
->next
))
4085 dio
->next
->prev
= NULL
;
4088 dio
->next
->prev
= dio
->prev
;
4089 dio
->prev
->next
= dio
->next
;
4091 dio
->iosize
-= resid
;
4092 dio
->io_errno
= io_errno
;
4093 dio
->completed_time
= curtime
;
4094 dio
->completion_thread
= thread
;
4099 return ((struct diskio
*)0);
4103 void free_diskio(struct diskio
*dio
)
4105 dio
->next
= free_diskios
;
4110 void print_diskio(struct diskio
*dio
)
4114 switch (dio
->type
) {
4135 p
= " RdMeta[async]";
4138 p
= " WrMeta[async]";
4141 p
= " RdData[async]";
4144 p
= " WrData[async]";
4150 p
= " PgOut[async]";
4156 if (check_filter_mode(NULL
, dio
->type
, 0, 0, p
))
4157 format_print(NULL
, p
, dio
->issuing_thread
, dio
->type
, 0, 0, 0, 0, FMT_DISKIO
, dio
->completed_time
, dio
->issued_time
, 1, "", dio
);
4161 void cache_disk_names()
4166 struct diskrec
*dnp
;
4169 if ((dirp
= opendir("/dev")) == NULL
)
4172 while ((dir
= readdir(dirp
)) != NULL
) {
4173 char nbuf
[MAXPATHLEN
];
4175 if (dir
->d_namlen
< 5 || strncmp("disk", dir
->d_name
, 4))
4177 snprintf(nbuf
, MAXPATHLEN
, "%s/%s", "/dev", dir
->d_name
);
4179 if (stat(nbuf
, &st
) < 0)
4182 if ((dnp
= (struct diskrec
*)malloc(sizeof(struct diskrec
))) == NULL
)
4185 if ((dnp
->diskname
= (char *)malloc(dir
->d_namlen
+ 1)) == NULL
) {
4189 strncpy(dnp
->diskname
, dir
->d_name
, dir
->d_namlen
);
4190 dnp
->diskname
[dir
->d_namlen
] = 0;
4191 dnp
->dev
= st
.st_rdev
;
4193 dnp
->next
= disk_list
;
4196 (void) closedir(dirp
);
4200 char *find_disk_name(int dev
)
4202 struct diskrec
*dnp
;
4207 for (dnp
= disk_list
; dnp
; dnp
= dnp
->next
) {
4208 if (dnp
->dev
== dev
)
4209 return (dnp
->diskname
);
4211 return ("NOTFOUND");
4215 fs_usage_fd_set(thread
, fd
)
4216 unsigned int thread
;
4220 fd_threadmap
*fdmap
;
4222 if(!(fdmap
= find_fd_thread_map(thread
)))
4225 /* If the map is not allocated, then now is the time */
4226 if (fdmap
->fd_setptr
== (unsigned long *)0)
4228 fdmap
->fd_setptr
= (unsigned long *)malloc(FS_USAGE_NFDBYTES(FS_USAGE_FD_SETSIZE
));
4229 if (fdmap
->fd_setptr
)
4231 fdmap
->fd_setsize
= FS_USAGE_FD_SETSIZE
;
4232 bzero(fdmap
->fd_setptr
,(FS_USAGE_NFDBYTES(FS_USAGE_FD_SETSIZE
)));
4238 /* If the map is not big enough, then reallocate it */
4239 while (fdmap
->fd_setsize
<= fd
)
4241 fprintf(stderr
, "reallocating bitmap for threadid %d, fd = %d, setsize = %d\n",
4242 thread
, fd
, fdmap
->fd_setsize
);
4243 n
= fdmap
->fd_setsize
* 2;
4244 fdmap
->fd_setptr
= (unsigned long *)realloc(fdmap
->fd_setptr
, (FS_USAGE_NFDBYTES(n
)));
4245 bzero(&fdmap
->fd_setptr
[(fdmap
->fd_setsize
/FS_USAGE_NFDBITS
)], (FS_USAGE_NFDBYTES(fdmap
->fd_setsize
)));
4246 fdmap
->fd_setsize
= n
;
4250 fdmap
->fd_setptr
[fd
/FS_USAGE_NFDBITS
] |= (1 << ((fd
) % FS_USAGE_NFDBITS
));
4257 0 : File Descriptor bit is not set
4258 1 : File Descriptor bit is set
4262 fs_usage_fd_isset(thread
, fd
)
4263 unsigned int thread
;
4267 fd_threadmap
*fdmap
;
4269 if(!(fdmap
= find_fd_thread_map(thread
)))
4272 if (fdmap
->fd_setptr
== (unsigned long *)0)
4275 if (fd
< fdmap
->fd_setsize
)
4276 ret
= fdmap
->fd_setptr
[fd
/FS_USAGE_NFDBITS
] & (1 << (fd
% FS_USAGE_NFDBITS
));
4282 fs_usage_fd_clear(thread
, fd
)
4283 unsigned int thread
;
4288 if (!(map
= find_fd_thread_map(thread
)))
4291 if (map
->fd_setptr
== (unsigned long *)0)
4295 if (fd
< map
->fd_setsize
)
4296 map
->fd_setptr
[fd
/FS_USAGE_NFDBITS
] &= ~(1 << (fd
% FS_USAGE_NFDBITS
));
4303 * ret = 1 means print the entry
4304 * ret = 0 means don't print the entry
4307 check_filter_mode(struct th_info
* ti
, int type
, int error
, int retval
, char *sc_name
)
4310 int network_fd_isset
= 0;
4313 if (filter_mode
== DEFAULT_DO_NOT_FILTER
)
4316 if (sc_name
[0] == 'C' && !strcmp (sc_name
, "CACHE_HIT")) {
4317 if (filter_mode
& CACHEHIT_FILTER
)
4318 /* Do not print if cachehit filter is set */
4323 if (filter_mode
& EXEC_FILTER
)
4325 if (type
== BSC_execve
)
4330 if (filter_mode
& PATHNAME_FILTER
)
4332 if (ti
&& ti
->pathname
[0])
4334 if (type
== BSC_close
|| type
== BSC_close_nocancel
)
4339 if ( !(filter_mode
& (FILESYS_FILTER
| NETWORK_FILTER
)))
4343 if (ti
== (struct th_info
*)0)
4345 if (filter_mode
& FILESYS_FILTER
)
4354 case BSC_close_nocancel
:
4356 network_fd_isset
= fs_usage_fd_isset(ti
->thread
, fd
);
4359 fs_usage_fd_clear(ti
->thread
,fd
);
4362 if (network_fd_isset
)
4364 if (filter_mode
& NETWORK_FILTER
)
4367 else if (filter_mode
& FILESYS_FILTER
)
4372 case BSC_read_nocancel
:
4373 case BSC_write_nocancel
:
4374 /* we don't care about error in this case */
4376 network_fd_isset
= fs_usage_fd_isset(ti
->thread
, fd
);
4377 if (network_fd_isset
)
4379 if (filter_mode
& NETWORK_FILTER
)
4382 else if (filter_mode
& FILESYS_FILTER
)
4386 case BSC_accept_nocancel
:
4390 fs_usage_fd_set(ti
->thread
, fd
);
4391 if (filter_mode
& NETWORK_FILTER
)
4401 case BSC_sendto_nocancel
:
4402 case BSC_recvfrom_nocancel
:
4403 case BSC_recvmsg_nocancel
:
4404 case BSC_sendmsg_nocancel
:
4405 case BSC_connect_nocancel
:
4408 fs_usage_fd_set(ti
->thread
, fd
);
4409 if (filter_mode
& NETWORK_FILTER
)
4413 case BSC_select_nocancel
:
4414 case BSC_socketpair
:
4415 /* Cannot determine info about file descriptors */
4416 if (filter_mode
& NETWORK_FILTER
)
4421 ret
=0; /* We track these cases for fd state only */
4422 fd
= ti
->arg1
; /* oldd */
4423 network_fd_isset
= fs_usage_fd_isset(ti
->thread
, fd
);
4424 if (error
== 0 && network_fd_isset
)
4426 /* then we are duping a socket descriptor */
4427 fd
= retval
; /* the new fd */
4428 fs_usage_fd_set(ti
->thread
, fd
);
4433 if (filter_mode
& FILESYS_FILTER
)
4442 * Allocate a buffer that is large enough to hold the maximum arguments
4443 * to execve(). This is used when getting the arguments to programs
4444 * when we see LaunchCFMApps. If this fails, it is not fatal, we will
4445 * simply not resolve the command name.
4449 init_arguments_buffer()
4456 mib
[1] = KERN_ARGMAX
;
4457 size
= sizeof(argmax
);
4458 if (sysctl(mib
, 2, &argmax
, &size
, NULL
, 0) == -1)
4462 /* Hack to avoid kernel bug. */
4463 if (argmax
> 8192) {
4468 arguments
= (char *)malloc(argmax
);
4475 get_real_command_name(int pid
, char *cbuf
, int csize
)
4478 * Get command and arguments.
4482 char *command_beg
, *command
, *command_end
;
4489 bzero(arguments
, argmax
);
4494 * A sysctl() is made to find out the full path that the command
4498 mib
[1] = KERN_PROCARGS
;
4502 if (sysctl(mib
, 3, arguments
, (size_t *)&argmax
, NULL
, 0) < 0) {
4506 /* Skip the saved exec_path. */
4507 for (cp
= arguments
; cp
< &arguments
[argmax
]; cp
++) {
4509 /* End of exec_path reached. */
4513 if (cp
== &arguments
[argmax
]) {
4517 /* Skip trailing '\0' characters. */
4518 for (; cp
< &arguments
[argmax
]; cp
++) {
4520 /* Beginning of first argument reached. */
4524 if (cp
== &arguments
[argmax
]) {
4530 * Make sure that the command is '\0'-terminated. This protects
4531 * against malicious programs; under normal operation this never
4532 * ends up being a problem..
4534 for (; cp
< &arguments
[argmax
]; cp
++) {
4536 /* End of first argument reached. */
4540 if (cp
== &arguments
[argmax
]) {
4543 command_end
= command
= cp
;
4545 /* Get the basename of command. */
4546 for (command
--; command
>= command_beg
; command
--) {
4547 if (*command
== '/') {
4553 (void) strncpy(cbuf
, (char *)command
, csize
);
4554 cbuf
[csize
-1] = '\0';