-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs_usage.c
4052 lines (3331 loc) · 92.6 KB
/
fs_usage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1999-2020 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
* Reserved. This file contains Original Code and/or Modifications of
* Original Code as defined in and that are subject to the Apple Public
* Source License Version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. Please obtain a copy of the
* License at http://www.apple.com/publicsource and read it before using
* this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License."
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* SDKROOT=macosx.internal cc -I`xcrun -sdk macosx.internal --show-sdk-path`/System/Library/Frameworks/System.framework/Versions/B/PrivateHeaders -arch x86_64 -Os -lktrace -lutil -o fs_usage fs_usage.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <strings.h>
#include <fcntl.h>
#include <aio.h>
#include <string.h>
#include <dirent.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <termios.h>
#include <errno.h>
#include <err.h>
#include <TargetConditionals.h>
// NOTE: These are our private headers, since we don't have access to Apple's
// internal SDK. Most of these came from xnu, and other open-source tools
// IIRC
// TODO: document where these headers originally came from
#include "private_headers/System/sys/kdebug.h"
#include "private_headers/System/sys/fcntl.h"
#include "private_headers/our_ktrace.h"
#include "private_headers/iphone_disk.h"
#include "private_headers/dyld_cache_format.h"
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <mach/clock_types.h>
#include <mach/mach_time.h>
#define os_assert(_x) if (!(_x)) abort()
/*
* MAXCOLS controls when extra data kicks in.
* MAX_WIDE_MODE_COLS controls -w mode to get even wider data in path.
*/
#define MAXCOLS 132
#define MAX_WIDE_MODE_COLS 264
#define MAXWIDTH MAX_WIDE_MODE_COLS + 64
typedef struct th_info {
struct th_info *next;
uint64_t thread;
/* this is needed for execve()/posix_spawn(), because the command name at the end probe is the new name, which we don't want */
char command[MAXCOMLEN + 1];
/*
* sometimes a syscall can cause multiple VFS_LOOKUPs of the same vnode with multiple paths
* (e.g., one absolute, one relative). traditional fs_usage behavior was to display the
* *first* lookup, so we need to save it off once we see it.
*/
uint64_t vnodeid; /* the vp of the VFS_LOOKUP we're currently in, 0 if we are not in one */
char pathname[MAXPATHLEN];
char pathname2[MAXPATHLEN];
char *newest_pathname; /* points to pathname2 if it's filled, otherwise pathname if it's filled, otherwise NULL */
int pid;
int type;
uint64_t arg1;
uint64_t arg2;
uint64_t arg3;
uint64_t arg4;
uint64_t arg5;
uint64_t arg6;
uint64_t arg7;
uint64_t arg8;
int waited;
uint64_t stime;
} *th_info_t;
struct diskio {
struct diskio *next;
struct diskio *prev;
uint64_t type;
uint64_t bp;
uint64_t dev;
uint64_t blkno;
uint64_t iosize;
uint64_t io_errno;
uint64_t is_meta;
uint64_t vnodeid;
uint64_t issuing_thread;
pid_t issuing_pid;
uint64_t completion_thread;
char issuing_command[MAXCOMLEN + 1];
uint64_t issued_time;
uint64_t completed_time;
struct timeval completed_walltime;
uint32_t bc_info;
};
#define HASH_SIZE 1024
#define HASH_MASK (HASH_SIZE - 1)
void setup_ktrace_callbacks(void);
void extend_syscall(uint64_t thread, int type, ktrace_event_t event);
/* printing routines */
bool check_filter_mode(pid_t pid, th_info_t ti, uint64_t type, int error, int retval, char *sc_name);
void format_print(th_info_t ti, char *sc_name, ktrace_event_t event, uint64_t type, int format, uint64_t now, uint64_t stime, int waited, const char *pathname, const char *pathname2, struct diskio *dio);
int print_open(ktrace_event_t event, uint64_t flags);
/* metadata info hash routines */
void meta_add_name(uint64_t blockno, const char *pathname);
const char *meta_find_name(uint64_t blockno);
void meta_delete_all(void);
/* event ("thread info") routines */
void event_enter(int type, ktrace_event_t event);
void event_exit(char *sc_name, int type, ktrace_event_t event, int format);
th_info_t event_find(uint64_t thread, int type);
void event_delete(th_info_t ti_to_delete);
void event_delete_all(void);
void event_mark_thread_waited(uint64_t);
/* network fd set routines */
void fd_set_is_network(pid_t pid, uint64_t fd, bool set);
bool fd_is_network(pid_t pid, uint64_t fd);
void fd_clear_pid(pid_t pid);
void fd_clear_all(void);
/* shared region address lookup routines */
void init_shared_cache_mapping(void);
void lookup_name(uint64_t user_addr, char **type, char **name);
/* disk I/O tracking routines */
struct diskio *diskio_start(uint64_t type, uint64_t bp, uint64_t dev, uint64_t blkno, uint64_t iosize, ktrace_event_t event);
struct diskio *diskio_find(uint64_t bp);
struct diskio *diskio_complete(uint64_t bp, uint64_t io_errno, uint64_t resid, uint64_t thread, uint64_t curtime, struct timeval curtime_wall);
void diskio_print(struct diskio *dio);
void diskio_free(struct diskio *dio);
/* disk name routines */
#define NFS_DEV -1
#define CS_DEV -2
char *generate_cs_disk_name(uint64_t dev, char *s);
char *find_disk_name(uint64_t dev);
void cache_disk_names(void);
#define CLASS_MASK 0xff000000
#define CSC_MASK 0xffff0000
#define BSC_INDEX(type) ((type >> 2) & 0x3fff)
#define MACH_vmfault 0x01300008
#define MACH_pageout 0x01300004
#define MACH_sched 0x01400000
#define MACH_stkhandoff 0x01400008
#define MACH_idle 0x01400024
#define BSC_thread_terminate 0x040c05a4
#define HFS_update 0x3018000
#define HFS_modify_block_end 0x3018004
#define Throttled 0x3010184
#define SPEC_ioctl 0x3060000
#define SPEC_unmap_info 0x3060004
#define proc_exit 0x4010004
#define BC_IO_HIT 0x03070010
#define BC_IO_HIT_STALLED 0x03070020
#define BC_IO_MISS 0x03070040
#define BC_IO_MISS_CUT_THROUGH 0x03070080
#define BC_PLAYBACK_IO 0x03070100
#define BC_STR(s) ( \
(s == BC_IO_HIT) ? "HIT" : \
(s == BC_IO_HIT_STALLED) ? "STALL" : \
(s == BC_IO_MISS) ? "MISS" : \
(s == BC_IO_MISS_CUT_THROUGH) ? "CUT" : \
(s == BC_PLAYBACK_IO) ? "PLBK" : \
(s == 0x0) ? "NONE" : "UNKN" )
#define P_DISKIO_READ (DKIO_READ << 2)
#define P_DISKIO_ASYNC (DKIO_ASYNC << 2)
#define P_DISKIO_META (DKIO_META << 2)
#define P_DISKIO_PAGING (DKIO_PAGING << 2)
#define P_DISKIO_THROTTLE (DKIO_THROTTLE << 2)
#define P_DISKIO_PASSIVE (DKIO_PASSIVE << 2)
#define P_DISKIO_NOCACHE (DKIO_NOCACHE << 2)
#define P_DISKIO_TIER_MASK (DKIO_TIER_MASK << 2)
#define P_DISKIO_TIER_SHIFT (DKIO_TIER_SHIFT + 2)
#define P_DISKIO_TIER_UPGRADE (DKIO_TIER_UPGRADE << 2)
#define P_DISKIO (FSDBG_CODE(DBG_DKRW, 0))
#define P_DISKIO_DONE (P_DISKIO | (DKIO_DONE << 2))
#define P_DISKIO_TYPE (P_DISKIO | P_DISKIO_READ | P_DISKIO_META | P_DISKIO_PAGING)
#define P_DISKIO_MASK (CSC_MASK | 0x4)
#define P_WrData (P_DISKIO)
#define P_RdData (P_DISKIO | P_DISKIO_READ)
#define P_WrMeta (P_DISKIO | P_DISKIO_META)
#define P_RdMeta (P_DISKIO | P_DISKIO_META | P_DISKIO_READ)
#define P_PgOut (P_DISKIO | P_DISKIO_PAGING)
#define P_PgIn (P_DISKIO | P_DISKIO_PAGING | P_DISKIO_READ)
#define P_CS_Class 0x0a000000 // DBG_CORESTORAGE
#define P_CS_Type_Mask 0xfffffff0
#define P_CS_IO_Done 0x00000004
#define P_CS_ReadChunk 0x0a000200 // chopped up request
#define P_CS_WriteChunk 0x0a000210
#define P_CS_MetaRead 0x0a000300 // meta data
#define P_CS_MetaWrite 0x0a000310
#define P_CS_TransformRead 0x0a000500 // background transform
#define P_CS_TransformWrite 0x0a000510
#define P_CS_MigrationRead 0x0a000600 // composite disk block migration
#define P_CS_MigrationWrite 0x0a000610
#define P_CS_SYNC_DISK 0x0a010000
#define MSC_map_fd 0x010c00ac
#define BSC_BASE 0x040C0000
// Network related codes
#define BSC_recvmsg 0x040C006C
#define BSC_sendmsg 0x040C0070
#define BSC_recvfrom 0x040C0074
#define BSC_accept 0x040C0078
#define BSC_select 0x040C0174
#define BSC_socket 0x040C0184
#define BSC_connect 0x040C0188
#define BSC_bind 0x040C01A0
#define BSC_listen 0x040C01A8
#define BSC_sendto 0x040C0214
#define BSC_socketpair 0x040C021C
#define BSC_recvmsg_nocancel 0x040c0644
#define BSC_sendmsg_nocancel 0x040c0648
#define BSC_recvfrom_nocancel 0x040c064c
#define BSC_accept_nocancel 0x040c0650
#define BSC_connect_nocancel 0x040c0664
#define BSC_sendto_nocancel 0x040c0674
#define BSC_exit 0x040C0004
#define BSC_read 0x040C000C
#define BSC_write 0x040C0010
#define BSC_open 0x040C0014
#define BSC_close 0x040C0018
#define BSC_link 0x040C0024
#define BSC_unlink 0x040C0028
#define BSC_chdir 0x040c0030
#define BSC_fchdir 0x040c0034
#define BSC_mknod 0x040C0038
#define BSC_chmod 0x040C003C
#define BSC_chown 0x040C0040
#define BSC_getfsstat 0x040C0048
#define BSC_access 0x040C0084
#define BSC_chflags 0x040C0088
#define BSC_fchflags 0x040C008C
#define BSC_sync 0x040C0090
#define BSC_dup 0x040C00A4
#define BSC_ioctl 0x040C00D8
#define BSC_revoke 0x040C00E0
#define BSC_symlink 0x040C00E4
#define BSC_readlink 0x040C00E8
#define BSC_execve 0x040C00EC
#define BSC_umask 0x040C00F0
#define BSC_chroot 0x040C00F4
#define BSC_msync 0x040C0104
#define BSC_dup2 0x040C0168
#define BSC_fcntl 0x040C0170
#define BSC_fsync 0x040C017C
#define BSC_readv 0x040C01E0
#define BSC_writev 0x040C01E4
#define BSC_fchown 0x040C01EC
#define BSC_fchmod 0x040C01F0
#define BSC_rename 0x040C0200
#define BSC_flock 0x040C020C
#define BSC_mkfifo 0x040C0210
#define BSC_mkdir 0x040C0220
#define BSC_rmdir 0x040C0224
#define BSC_utimes 0x040C0228
#define BSC_futimes 0x040C022C
#define BSC_pread 0x040C0264
#define BSC_pwrite 0x040C0268
#define BSC_statfs 0x040C0274
#define BSC_fstatfs 0x040C0278
#define BSC_unmount 0x040C027C
#define BSC_mount 0x040C029C
#define BSC_fdatasync 0x040C02EC
#define BSC_stat 0x040C02F0
#define BSC_fstat 0x040C02F4
#define BSC_lstat 0x040C02F8
#define BSC_pathconf 0x040C02FC
#define BSC_fpathconf 0x040C0300
#define BSC_getdirentries 0x040C0310
#define BSC_mmap 0x040c0314
#define BSC_lseek 0x040c031c
#define BSC_truncate 0x040C0320
#define BSC_ftruncate 0x040C0324
#define BSC_undelete 0x040C0334
#define BSC_open_dprotected_np 0x040C0360
#define BSC_getattrlist 0x040C0370
#define BSC_setattrlist 0x040C0374
#define BSC_getdirentriesattr 0x040C0378
#define BSC_exchangedata 0x040C037C
#define BSC_checkuseraccess 0x040C0380
#define BSC_searchfs 0x040C0384
#define BSC_delete 0x040C0388
#define BSC_copyfile 0x040C038C
#define BSC_fgetattrlist 0x040C0390
#define BSC_fsetattrlist 0x040C0394
#define BSC_getxattr 0x040C03A8
#define BSC_fgetxattr 0x040C03AC
#define BSC_setxattr 0x040C03B0
#define BSC_fsetxattr 0x040C03B4
#define BSC_removexattr 0x040C03B8
#define BSC_fremovexattr 0x040C03BC
#define BSC_listxattr 0x040C03C0
#define BSC_flistxattr 0x040C03C4
#define BSC_fsctl 0x040C03C8
#define BSC_posix_spawn 0x040C03D0
#define BSC_ffsctl 0x040C03D4
#define BSC_open_extended 0x040C0454
#define BSC_umask_extended 0x040C0458
#define BSC_stat_extended 0x040C045C
#define BSC_lstat_extended 0x040C0460
#define BSC_fstat_extended 0x040C0464
#define BSC_chmod_extended 0x040C0468
#define BSC_fchmod_extended 0x040C046C
#define BSC_access_extended 0x040C0470
#define BSC_mkfifo_extended 0x040C048C
#define BSC_mkdir_extended 0x040C0490
#define BSC_aio_fsync 0x040C04E4
#define BSC_aio_return 0x040C04E8
#define BSC_aio_suspend 0x040C04EC
#define BSC_aio_cancel 0x040C04F0
#define BSC_aio_error 0x040C04F4
#define BSC_aio_read 0x040C04F8
#define BSC_aio_write 0x040C04FC
#define BSC_lio_listio 0x040C0500
#define BSC_sendfile 0x040C0544
#define BSC_stat64 0x040C0548
#define BSC_fstat64 0x040C054C
#define BSC_lstat64 0x040C0550
#define BSC_stat64_extended 0x040C0554
#define BSC_lstat64_extended 0x040C0558
#define BSC_fstat64_extended 0x040C055C
#define BSC_getdirentries64 0x040C0560
#define BSC_statfs64 0x040C0564
#define BSC_fstatfs64 0x040C0568
#define BSC_getfsstat64 0x040C056C
#define BSC_pthread_chdir 0x040C0570
#define BSC_pthread_fchdir 0x040C0574
#define BSC_lchown 0x040C05B0
#define BSC_read_nocancel 0x040c0630
#define BSC_write_nocancel 0x040c0634
#define BSC_open_nocancel 0x040c0638
#define BSC_close_nocancel 0x040c063c
#define BSC_msync_nocancel 0x040c0654
#define BSC_fcntl_nocancel 0x040c0658
#define BSC_select_nocancel 0x040c065c
#define BSC_fsync_nocancel 0x040c0660
#define BSC_readv_nocancel 0x040c066c
#define BSC_writev_nocancel 0x040c0670
#define BSC_pread_nocancel 0x040c0678
#define BSC_pwrite_nocancel 0x040c067c
#define BSC_aio_suspend_nocancel 0x40c0694
#define BSC_guarded_open_np 0x040c06e4
#define BSC_guarded_open_dprotected_np 0x040c0790
#define BSC_guarded_close_np 0x040c06e8
#define BSC_guarded_write_np 0x040c0794
#define BSC_guarded_pwrite_np 0x040c0798
#define BSC_guarded_writev_np 0x040c079c
#define BSC_fsgetpath 0x040c06ac
#define BSC_getattrlistbulk 0x040c0734
#define BSC_openat 0x040c073c
#define BSC_openat_nocancel 0x040c0740
#define BSC_renameat 0x040c0744
#define BSC_renameatx_np 0x040c07a0
#define BSC_chmodat 0x040c074c
#define BSC_chownat 0x040c0750
#define BSC_fstatat 0x040c0754
#define BSC_fstatat64 0x040c0758
#define BSC_linkat 0x040c075c
#define BSC_unlinkat 0x040c0760
#define BSC_readlinkat 0x040c0764
#define BSC_symlinkat 0x040c0768
#define BSC_mkdirat 0x040c076c
#define BSC_getattrlistat 0x040c0770
#define BSC_msync_extended 0x040e0104
#define BSC_pread_extended 0x040e0264
#define BSC_pwrite_extended 0x040e0268
#define BSC_guarded_pwrite_extended 0x040e0798
#define BSC_mmap_extended 0x040e0314
#define BSC_mmap_extended2 0x040f0314
#define FMT_NOTHING -1
#define FMT_DEFAULT 0
#define FMT_FD 1
#define FMT_FD_IO 2
#define FMT_FD_2 3
#define FMT_SOCKET 4
#define FMT_PGIN 5
#define FMT_PGOUT 6
#define FMT_CACHEHIT 7
#define FMT_DISKIO 8
#define FMT_LSEEK 9
#define FMT_PREAD 10
#define FMT_FTRUNC 11
#define FMT_TRUNC 12
#define FMT_SELECT 13
#define FMT_OPEN 14
#define FMT_AIO_FSYNC 15
#define FMT_AIO_RETURN 16
#define FMT_AIO_SUSPEND 17
#define FMT_AIO_CANCEL 18
#define FMT_AIO 19
#define FMT_LIO_LISTIO 20
#define FMT_MSYNC 21
#define FMT_FCNTL 22
#define FMT_ACCESS 23
#define FMT_CHMOD 24
#define FMT_FCHMOD 25
#define FMT_CHMOD_EXT 26
#define FMT_FCHMOD_EXT 27
#define FMT_CHFLAGS 28
#define FMT_FCHFLAGS 29
#define FMT_IOCTL 30
#define FMT_MMAP 31
#define FMT_UMASK 32
#define FMT_SENDFILE 33
#define FMT_IOCTL_SYNC 34
#define FMT_MOUNT 35
#define FMT_UNMOUNT 36
#define FMT_DISKIO_CS 37
#define FMT_SYNC_DISK_CS 38
#define FMT_IOCTL_UNMAP 39
#define FMT_UNMAP_INFO 40
#define FMT_HFS_update 41
#define FMT_FLOCK 42
#define FMT_AT 43
#define FMT_CHMODAT 44
#define FMT_OPENAT 45
#define FMT_RENAMEAT 46
#define FMT_IOCTL_SYNCCACHE 47
#define FMT_GUARDED_OPEN 48
#define FMT_RENAME 49
#define DBG_FUNC_ALL (DBG_FUNC_START | DBG_FUNC_END)
#pragma mark global state
ktrace_session_t s;
bool BC_flag = false;
bool RAW_flag = false;
bool wideflag = false;
bool include_waited_flag = false;
bool want_kernel_task = true;
bool filter_non_root_pids = false;
dispatch_source_t stop_timer, sigquit_source, sigpipe_source, sighup_source, sigterm_source, sigwinch_source;
uint64_t mach_time_of_first_event;
uint64_t start_time_ns = 0;
uint64_t end_time_ns = UINT64_MAX;
unsigned int columns = 0;
/*
* Network only or filesystem only output filter
* Default of zero means report all activity - no filtering
*/
#define FILESYS_FILTER 0x01
#define NETWORK_FILTER 0x02
#define EXEC_FILTER 0x08
#define PATHNAME_FILTER 0x10
#define DISKIO_FILTER 0x20
#define DEFAULT_DO_NOT_FILTER 0x00
int filter_mode = DEFAULT_DO_NOT_FILTER;
bool show_cachehits = false;
#pragma mark syscall lookup table
#define MAX_BSD_SYSCALL 526
struct bsd_syscall {
char *sc_name;
int sc_format;
};
#define NORMAL_SYSCALL(name) \
[BSC_INDEX(BSC_##name)] = {#name, FMT_DEFAULT}
#define SYSCALL(name, format) \
[BSC_INDEX(BSC_##name)] = {#name, format}
#define SYSCALL_NAMED(name, displayname, format) \
[BSC_INDEX(BSC_##name)] = {#displayname, format}
#define SYSCALL_WITH_NOCANCEL(name, format) \
[BSC_INDEX(BSC_##name)] = {#name, format}, \
[BSC_INDEX(BSC_##name##_nocancel)] = {#name, format}
const struct bsd_syscall bsd_syscalls[MAX_BSD_SYSCALL] = {
SYSCALL(sendfile, FMT_FD), /* this should be changed to FMT_SENDFILE once we add an extended info trace event */
SYSCALL_WITH_NOCANCEL(recvmsg, FMT_FD_IO),
SYSCALL_WITH_NOCANCEL(sendmsg, FMT_FD_IO),
SYSCALL_WITH_NOCANCEL(recvfrom, FMT_FD_IO),
SYSCALL_WITH_NOCANCEL(sendto, FMT_FD_IO),
SYSCALL_WITH_NOCANCEL(select, FMT_SELECT),
SYSCALL_WITH_NOCANCEL(accept, FMT_FD_2),
SYSCALL(socket, FMT_SOCKET),
SYSCALL_WITH_NOCANCEL(connect, FMT_FD),
SYSCALL(bind, FMT_FD),
SYSCALL(listen, FMT_FD),
SYSCALL(mmap, FMT_MMAP),
NORMAL_SYSCALL(socketpair),
NORMAL_SYSCALL(getxattr),
NORMAL_SYSCALL(setxattr),
NORMAL_SYSCALL(removexattr),
NORMAL_SYSCALL(listxattr),
NORMAL_SYSCALL(stat),
NORMAL_SYSCALL(stat64),
NORMAL_SYSCALL(stat_extended),
SYSCALL_NAMED(stat64_extended, stat_extended64, FMT_DEFAULT), /* should be stat64_extended ? */
SYSCALL(mount, FMT_MOUNT),
SYSCALL(unmount, FMT_UNMOUNT),
NORMAL_SYSCALL(exit),
NORMAL_SYSCALL(execve),
NORMAL_SYSCALL(posix_spawn),
SYSCALL_WITH_NOCANCEL(open, FMT_OPEN),
SYSCALL(open_extended, FMT_OPEN),
SYSCALL(guarded_open_np, FMT_GUARDED_OPEN),
SYSCALL_NAMED(open_dprotected_np, open_dprotected, FMT_OPEN),
SYSCALL(guarded_open_dprotected_np, FMT_GUARDED_OPEN),
SYSCALL(dup, FMT_FD_2),
SYSCALL(dup2, FMT_FD_2),
SYSCALL_WITH_NOCANCEL(close, FMT_FD),
SYSCALL(guarded_close_np, FMT_FD),
SYSCALL_WITH_NOCANCEL(read, FMT_FD_IO),
SYSCALL_WITH_NOCANCEL(write, FMT_FD_IO),
SYSCALL(guarded_write_np, FMT_FD_IO),
SYSCALL(guarded_pwrite_np, FMT_PREAD),
SYSCALL(guarded_writev_np, FMT_FD_IO),
SYSCALL(fgetxattr, FMT_FD),
SYSCALL(fsetxattr, FMT_FD),
SYSCALL(fremovexattr, FMT_FD),
SYSCALL(flistxattr, FMT_FD),
SYSCALL(fstat, FMT_FD),
SYSCALL(fstat64, FMT_FD),
SYSCALL(fstat_extended, FMT_FD),
SYSCALL(fstat64_extended, FMT_FD),
NORMAL_SYSCALL(lstat),
NORMAL_SYSCALL(lstat64),
NORMAL_SYSCALL(lstat_extended),
SYSCALL_NAMED(lstat64_extended, lstat_extended64, FMT_DEFAULT),
NORMAL_SYSCALL(link),
NORMAL_SYSCALL(unlink),
NORMAL_SYSCALL(mknod),
SYSCALL(umask, FMT_UMASK),
SYSCALL(umask_extended, FMT_UMASK),
SYSCALL(chmod, FMT_CHMOD),
SYSCALL(chmod_extended, FMT_CHMOD_EXT),
SYSCALL(fchmod, FMT_FCHMOD),
SYSCALL(fchmod_extended, FMT_FCHMOD_EXT),
NORMAL_SYSCALL(chown),
NORMAL_SYSCALL(lchown),
SYSCALL(fchown, FMT_FD),
SYSCALL(access, FMT_ACCESS),
NORMAL_SYSCALL(access_extended),
NORMAL_SYSCALL(chdir),
NORMAL_SYSCALL(pthread_chdir),
NORMAL_SYSCALL(chroot),
NORMAL_SYSCALL(utimes),
SYSCALL_NAMED(delete, delete-Carbon, FMT_DEFAULT),
NORMAL_SYSCALL(undelete),
NORMAL_SYSCALL(revoke),
NORMAL_SYSCALL(fsctl),
SYSCALL(ffsctl, FMT_FD),
SYSCALL(chflags, FMT_CHFLAGS),
SYSCALL(fchflags, FMT_FCHFLAGS),
SYSCALL(fchdir, FMT_FD),
SYSCALL(pthread_fchdir, FMT_FD),
SYSCALL(futimes, FMT_FD),
NORMAL_SYSCALL(sync),
NORMAL_SYSCALL(symlink),
NORMAL_SYSCALL(readlink),
SYSCALL_WITH_NOCANCEL(fsync, FMT_FD),
SYSCALL(fdatasync, FMT_FD),
SYSCALL_WITH_NOCANCEL(readv, FMT_FD_IO),
SYSCALL_WITH_NOCANCEL(writev, FMT_FD_IO),
SYSCALL_WITH_NOCANCEL(pread, FMT_PREAD),
SYSCALL_WITH_NOCANCEL(pwrite, FMT_PREAD),
NORMAL_SYSCALL(mkdir),
NORMAL_SYSCALL(mkdir_extended),
NORMAL_SYSCALL(mkfifo),
NORMAL_SYSCALL(mkfifo_extended),
NORMAL_SYSCALL(rmdir),
NORMAL_SYSCALL(statfs),
NORMAL_SYSCALL(statfs64),
NORMAL_SYSCALL(getfsstat),
NORMAL_SYSCALL(getfsstat64),
SYSCALL(fstatfs, FMT_FD),
SYSCALL(fstatfs64, FMT_FD),
NORMAL_SYSCALL(pathconf),
SYSCALL(fpathconf, FMT_FD),
SYSCALL(getdirentries, FMT_FD_IO),
SYSCALL(getdirentries64, FMT_FD_IO),
SYSCALL(lseek, FMT_LSEEK),
SYSCALL(truncate, FMT_TRUNC),
SYSCALL(ftruncate, FMT_FTRUNC),
SYSCALL(flock, FMT_FLOCK),
NORMAL_SYSCALL(getattrlist),
NORMAL_SYSCALL(setattrlist),
SYSCALL(fgetattrlist, FMT_FD),
SYSCALL(fsetattrlist, FMT_FD),
SYSCALL(getdirentriesattr, FMT_FD),
NORMAL_SYSCALL(exchangedata),
SYSCALL(rename, FMT_RENAME),
NORMAL_SYSCALL(copyfile),
NORMAL_SYSCALL(checkuseraccess),
NORMAL_SYSCALL(searchfs),
SYSCALL(aio_fsync, FMT_AIO_FSYNC),
SYSCALL(aio_return, FMT_AIO_RETURN),
SYSCALL_WITH_NOCANCEL(aio_suspend, FMT_AIO_SUSPEND),
SYSCALL(aio_cancel, FMT_AIO_CANCEL),
SYSCALL(aio_error, FMT_AIO),
SYSCALL(aio_read, FMT_AIO),
SYSCALL(aio_write, FMT_AIO),
SYSCALL(lio_listio, FMT_LIO_LISTIO),
SYSCALL_WITH_NOCANCEL(msync, FMT_MSYNC),
SYSCALL_WITH_NOCANCEL(fcntl, FMT_FCNTL),
SYSCALL(ioctl, FMT_IOCTL),
NORMAL_SYSCALL(fsgetpath),
NORMAL_SYSCALL(getattrlistbulk),
SYSCALL_WITH_NOCANCEL(openat, FMT_OPENAT), /* open_nocancel() was previously shown as "open_nocanel" (note spelling) */
SYSCALL(renameat, FMT_RENAMEAT),
SYSCALL(renameatx_np, FMT_RENAMEAT),
SYSCALL(chmodat, FMT_CHMODAT),
SYSCALL(chownat, FMT_AT),
SYSCALL(fstatat, FMT_AT),
SYSCALL(fstatat64, FMT_AT),
SYSCALL(linkat, FMT_AT),
SYSCALL(unlinkat, FMT_AT),
SYSCALL(readlinkat, FMT_AT),
SYSCALL(symlinkat, FMT_AT),
SYSCALL(mkdirat, FMT_AT),
SYSCALL(getattrlistat, FMT_AT),
};
static void
get_screenwidth(void)
{
struct winsize size;
columns = MAXCOLS;
if (isatty(STDOUT_FILENO)) {
if (ioctl(1, TIOCGWINSZ, &size) != -1) {
columns = size.ws_col;
if (columns > MAXWIDTH)
columns = MAXWIDTH;
}
}
}
static uint64_t
mach_to_nano(uint64_t mach)
{
uint64_t nanoseconds = 0;
os_assert(ktrace_convert_timestamp_to_nanoseconds(s, mach, &nanoseconds) == 0);
return nanoseconds;
}
static void
exit_usage(void)
{
const char *myname;
myname = getprogname();
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);
fprintf(stderr, " -e exclude the specified list of pids from the sample\n");
fprintf(stderr, " and exclude fs_usage by default\n");
fprintf(stderr, " -w force wider, detailed, output\n");
fprintf(stderr, " -f output is based on the mode provided\n");
fprintf(stderr, " mode = \"network\" Show network-related events\n");
fprintf(stderr, " mode = \"filesys\" Show filesystem-related events\n");
fprintf(stderr, " mode = \"pathname\" Show only pathname-related events\n");
fprintf(stderr, " mode = \"exec\" Show only exec and spawn events\n");
fprintf(stderr, " mode = \"diskio\" Show only disk I/O events\n");
fprintf(stderr, " mode = \"cachehit\" In addition, show cache hits\n");
fprintf(stderr, " -b annotate disk I/O events with BootCache info (if available)\n");
fprintf(stderr, " -t specifies timeout in seconds (for use in automated tools)\n");
fprintf(stderr, " -R specifies a raw trace file to process\n");
fprintf(stderr, " -S if -R is specified, selects a start point in microseconds\n");
fprintf(stderr, " -E if -R is specified, selects an end point in microseconds\n");
fprintf(stderr, " -u filter out non-root pids\n");
fprintf(stderr, " pid selects process(s) to sample\n");
fprintf(stderr, " cmd selects process(s) matching command string to sample\n");
fprintf(stderr, "By default (no options) the following processes are excluded from the output:\n");
fprintf(stderr, "fs_usage, Terminal, telnetd, sshd, rlogind, tcsh, csh, sh\n\n");
exit(1);
}
static void fs_usage_cleanup(const char *message)
{
if (s) {
ktrace_session_destroy(s);
}
fprintf(stderr, "Cleaning up tracing state because of %s\n", message);
}
uint get_uid_for_pid(uint pid)
{
int err;
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
struct kinfo_proc kp;
size_t len = sizeof(kp);
err = sysctl(mib, sizeof(mib)/sizeof(mib[0]), &kp, &len, NULL, 0);
if (err != 0)
{
perror("getting uid via sysctl() failed");
exit(1);
}
return kp.kp_eproc.e_ucred.cr_uid;
}
int
main(int argc, char *argv[])
{
char ch;
int rv;
bool exclude_pids = false;
uint64_t time_limit_ns = 0;
// TODO: re-enable this!
//os_set_crash_callback(&fs_usage_cleanup);
get_screenwidth();
s = ktrace_session_create();
os_assert(s != NULL);
(void)ktrace_ignore_process_filter_for_event(s, P_WrData);
(void)ktrace_ignore_process_filter_for_event(s, P_RdData);
(void)ktrace_ignore_process_filter_for_event(s, P_WrMeta);
(void)ktrace_ignore_process_filter_for_event(s, P_RdMeta);
(void)ktrace_ignore_process_filter_for_event(s, P_PgOut);
(void)ktrace_ignore_process_filter_for_event(s, P_PgIn);
while ((ch = getopt(argc, argv, "bewf:R:S:E:t:Wu")) != -1) {
switch (ch) {
case 'e':
exclude_pids = true;
break;
case 'w':
wideflag = 1;
columns = MAX_WIDE_MODE_COLS;
break;
case 'W':
include_waited_flag = true;
break;
case 'f':
if (!strcmp(optarg, "network"))
filter_mode |= NETWORK_FILTER;
else if (!strcmp(optarg, "filesys"))
filter_mode |= FILESYS_FILTER;
else if (!strcmp(optarg, "cachehit"))
show_cachehits = true;
else if (!strcmp(optarg, "exec"))
filter_mode |= EXEC_FILTER;
else if (!strcmp(optarg, "pathname"))
filter_mode |= PATHNAME_FILTER;
else if (!strcmp(optarg, "diskio"))
filter_mode |= DISKIO_FILTER;
break;
case 'b':
BC_flag = true;
break;
case 't':
time_limit_ns = (uint64_t)(NSEC_PER_SEC * atof(optarg));
if (time_limit_ns == 0) {
fprintf(stderr, "ERROR: could not set time limit to %s\n",
optarg);
exit(1);
}
break;
case 'R':
RAW_flag = true;
rv = ktrace_set_file(s, optarg);
if (rv) {
fprintf(stderr, "ERROR: reading trace from '%s' failed (%s)\n", optarg, strerror(errno));
exit(1);
}
break;
case 'S':
start_time_ns = NSEC_PER_SEC * atof(optarg);
break;
case 'E':
end_time_ns = NSEC_PER_SEC * atof(optarg);
break;
case 'u':
filter_non_root_pids = true;
break;
default:
exit_usage();
}
}
argc -= optind;
argv += optind;
if (time_limit_ns > 0 && RAW_flag) {
fprintf(stderr, "NOTE: time limit ignored when a raw file is specified\n");
time_limit_ns = 0;
}
if (!RAW_flag) {
if (geteuid() != 0) {
fprintf(stderr, "'fs_usage' must be run as root...\n");
exit(1);
}
/*
* ktrace can't both *in*clude and *ex*clude pids, so: if we are
* already excluding pids, or if we are not explicitly including
* or excluding any pids, then exclude the defaults.
*
* if on the other hand we are explicitly including pids, we'll
* filter the defaults out naturally.
*/
if (exclude_pids || argc == 0) {
ktrace_exclude_process(s, "fs_usage_ng");
ktrace_exclude_process(s, "fs_usage");
ktrace_exclude_process(s, "Terminal");
ktrace_exclude_process(s, "telnetd");
ktrace_exclude_process(s, "telnet");
ktrace_exclude_process(s, "sshd");
ktrace_exclude_process(s, "rlogind");
ktrace_exclude_process(s, "tcsh");
ktrace_exclude_process(s, "csh");
ktrace_exclude_process(s, "sh");
ktrace_exclude_process(s, "zsh");
#if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
ktrace_exclude_process(s, "dropbear");
#endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
}
}
/*
* If we're *in*cluding processes, also *in*clude the kernel_task, which
* issues trace points when disk I/Os complete. But set a flag for us to
* avoid showing events attributed to the kernel_task.
*
* If the user actually wants to those events, we'll change that flag in
* the loop below.
*/
if (argc > 0 && !exclude_pids) {
ktrace_filter_pid(s, 0);
want_kernel_task = false;
}
/*
* Process the list of specified pids, and in/exclude them as
* appropriate.
*/
while (argc > 0) {
pid_t pid;
char *name;
char *endptr;
name = argv[0];
pid = (pid_t)strtoul(name, &endptr, 10);
if (*name != '\0' && *endptr == '\0') {
if (exclude_pids) {
rv = ktrace_exclude_pid(s, pid);
} else {
if (pid == 0)
want_kernel_task = true;
else
rv = ktrace_filter_pid(s, pid);
}
} else {
if (exclude_pids) {
rv = ktrace_exclude_process(s, name);
} else {
if (!strcmp(name, "kernel_task"))
want_kernel_task = true;
else
rv = ktrace_filter_process(s, name);
}
}
if (rv == EINVAL) {
fprintf(stderr, "ERROR: cannot both include and exclude simultaneously\n");
exit(1);
} else {
os_assert(!rv);
}
argc--;
argv++;
}
/* provides SIGINT, SIGHUP, SIGPIPE, SIGTERM handlers */
ktrace_set_signal_handler(s);
ktrace_set_completion_handler(s, ^{
exit(0);
});
signal(SIGWINCH, SIG_IGN);
sigwinch_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGWINCH, 0, dispatch_get_main_queue());
dispatch_source_set_event_handler(sigwinch_source, ^{
if (!wideflag)
get_screenwidth();
});
dispatch_activate(sigwinch_source);
init_shared_cache_mapping();
cache_disk_names();
setup_ktrace_callbacks();
ktrace_set_dropped_events_handler(s, ^{
fprintf(stderr, "fs_usage: buffer overrun, events generated too quickly\n");
/* clear any state that is now potentially invalid */
event_delete_all();
fd_clear_all();
meta_delete_all();
});
ktrace_session_set_default_event_names_enabled(s, KTRACE_FEATURE_DISABLED);
ktrace_set_execnames_enabled(s, KTRACE_FEATURE_LAZY);
ktrace_set_vnode_paths_enabled(s, true);
/* no need to symbolicate addresses */
ktrace_set_uuid_map_enabled(s, KTRACE_FEATURE_DISABLED);
rv = ktrace_start(s, dispatch_get_main_queue());