This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathfasttrap.c
2809 lines (2380 loc) · 77.1 KB
/
fasttrap.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/codesign.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/conf.h>
#include <sys/systm.h>
#include <sys/kauth.h>
#include <sys/utfconv.h>
#include <sys/fasttrap.h>
#include <sys/fasttrap_impl.h>
#include <sys/fasttrap_isa.h>
#include <sys/dtrace.h>
#include <sys/dtrace_impl.h>
#include <sys/proc.h>
#include <security/mac_framework.h>
#include <miscfs/devfs/devfs.h>
#include <sys/proc_internal.h>
#include <sys/dtrace_glue.h>
#include <sys/dtrace_ptss.h>
#include <kern/cs_blobs.h>
#include <kern/thread.h>
#include <kern/zalloc.h>
#include <mach/thread_act.h>
extern kern_return_t kernel_thread_start_priority(thread_continue_t continuation, void *parameter, integer_t priority, thread_t *new_thread);
/* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
#define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
__private_extern__
void
qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *));
/*
* User-Land Trap-Based Tracing
* ----------------------------
*
* The fasttrap provider allows DTrace consumers to instrument any user-level
* instruction to gather data; this includes probes with semantic
* signifigance like entry and return as well as simple offsets into the
* function. While the specific techniques used are very ISA specific, the
* methodology is generalizable to any architecture.
*
*
* The General Methodology
* -----------------------
*
* With the primary goal of tracing every user-land instruction and the
* limitation that we can't trust user space so don't want to rely on much
* information there, we begin by replacing the instructions we want to trace
* with trap instructions. Each instruction we overwrite is saved into a hash
* table keyed by process ID and pc address. When we enter the kernel due to
* this trap instruction, we need the effects of the replaced instruction to
* appear to have occurred before we proceed with the user thread's
* execution.
*
* Each user level thread is represented by a ulwp_t structure which is
* always easily accessible through a register. The most basic way to produce
* the effects of the instruction we replaced is to copy that instruction out
* to a bit of scratch space reserved in the user thread's ulwp_t structure
* (a sort of kernel-private thread local storage), set the PC to that
* scratch space and single step. When we reenter the kernel after single
* stepping the instruction we must then adjust the PC to point to what would
* normally be the next instruction. Of course, special care must be taken
* for branches and jumps, but these represent such a small fraction of any
* instruction set that writing the code to emulate these in the kernel is
* not too difficult.
*
* Return probes may require several tracepoints to trace every return site,
* and, conversely, each tracepoint may activate several probes (the entry
* and offset 0 probes, for example). To solve this muliplexing problem,
* tracepoints contain lists of probes to activate and probes contain lists
* of tracepoints to enable. If a probe is activated, it adds its ID to
* existing tracepoints or creates new ones as necessary.
*
* Most probes are activated _before_ the instruction is executed, but return
* probes are activated _after_ the effects of the last instruction of the
* function are visible. Return probes must be fired _after_ we have
* single-stepped the instruction whereas all other probes are fired
* beforehand.
*
*
* Lock Ordering
* -------------
*
* The lock ordering below -- both internally and with respect to the DTrace
* framework -- is a little tricky and bears some explanation. Each provider
* has a lock (ftp_mtx) that protects its members including reference counts
* for enabled probes (ftp_rcount), consumers actively creating probes
* (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider
* from being freed. A provider is looked up by taking the bucket lock for the
* provider hash table, and is returned with its lock held. The provider lock
* may be taken in functions invoked by the DTrace framework, but may not be
* held while calling functions in the DTrace framework.
*
* To ensure consistency over multiple calls to the DTrace framework, the
* creation lock (ftp_cmtx) should be held. Naturally, the creation lock may
* not be taken when holding the provider lock as that would create a cyclic
* lock ordering. In situations where one would naturally take the provider
* lock and then the creation lock, we instead up a reference count to prevent
* the provider from disappearing, drop the provider lock, and acquire the
* creation lock.
*
* Briefly:
* bucket lock before provider lock
* DTrace before provider lock
* creation lock before DTrace
* never hold the provider lock and creation lock simultaneously
*/
static dtrace_meta_provider_id_t fasttrap_meta_id;
static thread_t fasttrap_cleanup_thread;
static LCK_GRP_DECLARE(fasttrap_lck_grp, "fasttrap");
static LCK_ATTR_DECLARE(fasttrap_lck_attr, 0, 0);
static LCK_MTX_DECLARE_ATTR(fasttrap_cleanup_mtx,
&fasttrap_lck_grp, &fasttrap_lck_attr);
#define FASTTRAP_CLEANUP_PROVIDER 0x1
#define FASTTRAP_CLEANUP_TRACEPOINT 0x2
static uint32_t fasttrap_cleanup_work = 0;
/*
* Generation count on modifications to the global tracepoint lookup table.
*/
static volatile uint64_t fasttrap_mod_gen;
/*
* APPLE NOTE: When the fasttrap provider is loaded, fasttrap_max is computed
* base on system memory. Each time a probe is created, fasttrap_total is
* incremented by the number of tracepoints that may be associated with that
* probe; fasttrap_total is capped at fasttrap_max.
*/
static uint32_t fasttrap_max;
static uint32_t fasttrap_retired;
static uint32_t fasttrap_total;
#define FASTTRAP_TPOINTS_DEFAULT_SIZE 0x4000
#define FASTTRAP_PROVIDERS_DEFAULT_SIZE 0x100
#define FASTTRAP_PROCS_DEFAULT_SIZE 0x100
fasttrap_hash_t fasttrap_tpoints;
static fasttrap_hash_t fasttrap_provs;
static fasttrap_hash_t fasttrap_procs;
static uint64_t fasttrap_pid_count; /* pid ref count */
static LCK_MTX_DECLARE_ATTR(fasttrap_count_mtx, /* lock on ref count */
&fasttrap_lck_grp, &fasttrap_lck_attr);
#define FASTTRAP_ENABLE_FAIL 1
#define FASTTRAP_ENABLE_PARTIAL 2
static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t);
static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t);
static fasttrap_provider_t *fasttrap_provider_lookup(proc_t*, fasttrap_provider_type_t, const char *,
const dtrace_pattr_t *);
static void fasttrap_provider_retire(proc_t*, const char *, int);
static void fasttrap_provider_free(fasttrap_provider_t *);
static fasttrap_proc_t *fasttrap_proc_lookup(pid_t);
static void fasttrap_proc_release(fasttrap_proc_t *);
#define FASTTRAP_PROVS_INDEX(pid, name) \
((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
#define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
/*
* APPLE NOTE: To save memory, some common memory allocations are given
* a unique zone. For example, dtrace_probe_t is 72 bytes in size,
* which means it would fall into the kalloc.128 bucket. With
* 20k elements allocated, the space saved is substantial.
*/
ZONE_DECLARE(fasttrap_tracepoint_t_zone, "dtrace.fasttrap_tracepoint_t",
sizeof(fasttrap_tracepoint_t), ZC_NONE);
/*
* APPLE NOTE: fasttrap_probe_t's are variable in size. Some quick profiling has shown
* that the sweet spot for reducing memory footprint is covering the first
* three sizes. Everything larger goes into the common pool.
*/
#define FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS 4
struct zone *fasttrap_probe_t_zones[FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS];
static const char *fasttrap_probe_t_zone_names[FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS] = {
"",
"dtrace.fasttrap_probe_t[1]",
"dtrace.fasttrap_probe_t[2]",
"dtrace.fasttrap_probe_t[3]"
};
static int
fasttrap_highbit(ulong_t i)
{
int h = 1;
if (i == 0)
return (0);
#ifdef _LP64
if (i & 0xffffffff00000000ul) {
h += 32; i >>= 32;
}
#endif
if (i & 0xffff0000) {
h += 16; i >>= 16;
}
if (i & 0xff00) {
h += 8; i >>= 8;
}
if (i & 0xf0) {
h += 4; i >>= 4;
}
if (i & 0xc) {
h += 2; i >>= 2;
}
if (i & 0x2) {
h += 1;
}
return (h);
}
static uint_t
fasttrap_hash_str(const char *p)
{
unsigned int g;
uint_t hval = 0;
while (*p) {
hval = (hval << 4) + *p++;
if ((g = (hval & 0xf0000000)) != 0)
hval ^= g >> 24;
hval &= ~g;
}
return (hval);
}
/*
* APPLE NOTE: fasttrap_sigtrap not implemented
*/
void
fasttrap_sigtrap(proc_t *p, uthread_t t, user_addr_t pc)
{
#pragma unused(p, t, pc)
#if !defined(__APPLE__)
sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
sqp->sq_info.si_signo = SIGTRAP;
sqp->sq_info.si_code = TRAP_DTRACE;
sqp->sq_info.si_addr = (caddr_t)pc;
mutex_enter(&p->p_lock);
sigaddqa(p, t, sqp);
mutex_exit(&p->p_lock);
if (t != NULL)
aston(t);
#endif /* __APPLE__ */
printf("fasttrap_sigtrap called with no implementation.\n");
}
/*
* This function ensures that no threads are actively using the memory
* associated with probes that were formerly live.
*/
static void
fasttrap_mod_barrier(uint64_t gen)
{
unsigned int i;
if (gen < fasttrap_mod_gen)
return;
fasttrap_mod_gen++;
for (i = 0; i < NCPU; i++) {
lck_mtx_lock(&cpu_core[i].cpuc_pid_lock);
lck_mtx_unlock(&cpu_core[i].cpuc_pid_lock);
}
}
static void fasttrap_pid_cleanup(uint32_t);
static unsigned int
fasttrap_pid_cleanup_providers(void)
{
fasttrap_provider_t **fpp, *fp;
fasttrap_bucket_t *bucket;
dtrace_provider_id_t provid;
unsigned int later = 0, i;
/*
* Iterate over all the providers trying to remove the marked
* ones. If a provider is marked but not retired, we just
* have to take a crack at removing it -- it's no big deal if
* we can't.
*/
for (i = 0; i < fasttrap_provs.fth_nent; i++) {
bucket = &fasttrap_provs.fth_table[i];
lck_mtx_lock(&bucket->ftb_mtx);
fpp = (fasttrap_provider_t **)&bucket->ftb_data;
while ((fp = *fpp) != NULL) {
if (!fp->ftp_marked) {
fpp = &fp->ftp_next;
continue;
}
lck_mtx_lock(&fp->ftp_mtx);
/*
* If this provider has consumers actively
* creating probes (ftp_ccount) or is a USDT
* provider (ftp_mcount), we can't unregister
* or even condense.
*/
if (fp->ftp_ccount != 0 ||
fp->ftp_mcount != 0) {
fp->ftp_marked = 0;
lck_mtx_unlock(&fp->ftp_mtx);
continue;
}
if (!fp->ftp_retired || fp->ftp_rcount != 0)
fp->ftp_marked = 0;
lck_mtx_unlock(&fp->ftp_mtx);
/*
* If we successfully unregister this
* provider we can remove it from the hash
* chain and free the memory. If our attempt
* to unregister fails and this is a retired
* provider, increment our flag to try again
* pretty soon. If we've consumed more than
* half of our total permitted number of
* probes call dtrace_condense() to try to
* clean out the unenabled probes.
*/
provid = fp->ftp_provid;
if (dtrace_unregister(provid) != 0) {
if (fasttrap_total > fasttrap_max / 2)
(void) dtrace_condense(provid);
later += fp->ftp_marked;
fpp = &fp->ftp_next;
} else {
*fpp = fp->ftp_next;
fasttrap_provider_free(fp);
}
}
lck_mtx_unlock(&bucket->ftb_mtx);
}
return later;
}
typedef struct fasttrap_tracepoint_spec {
pid_t fttps_pid;
user_addr_t fttps_pc;
} fasttrap_tracepoint_spec_t;
static fasttrap_tracepoint_spec_t *fasttrap_retired_spec;
static size_t fasttrap_cur_retired = 0, fasttrap_retired_size;
static LCK_MTX_DECLARE_ATTR(fasttrap_retired_mtx,
&fasttrap_lck_grp, &fasttrap_lck_attr);
#define DEFAULT_RETIRED_SIZE 256
static void
fasttrap_tracepoint_cleanup(void)
{
size_t i;
pid_t pid = 0;
user_addr_t pc;
proc_t *p = PROC_NULL;
fasttrap_tracepoint_t *tp = NULL;
lck_mtx_lock(&fasttrap_retired_mtx);
fasttrap_bucket_t *bucket;
for (i = 0; i < fasttrap_cur_retired; i++) {
pc = fasttrap_retired_spec[i].fttps_pc;
if (fasttrap_retired_spec[i].fttps_pid != pid) {
pid = fasttrap_retired_spec[i].fttps_pid;
if (p != PROC_NULL) {
sprunlock(p);
}
if ((p = sprlock(pid)) == PROC_NULL) {
pid = 0;
continue;
}
}
bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
lck_mtx_lock(&bucket->ftb_mtx);
for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
tp->ftt_proc->ftpc_acount != 0)
break;
}
/*
* Check that the tracepoint is not gone or has not been
* re-activated for another probe
*/
if (tp == NULL || tp->ftt_retired == 0) {
lck_mtx_unlock(&bucket->ftb_mtx);
continue;
}
fasttrap_tracepoint_remove(p, tp);
lck_mtx_unlock(&bucket->ftb_mtx);
}
if (p != PROC_NULL) {
sprunlock(p);
}
fasttrap_cur_retired = 0;
lck_mtx_unlock(&fasttrap_retired_mtx);
}
void
fasttrap_tracepoint_retire(proc_t *p, fasttrap_tracepoint_t *tp)
{
if (tp->ftt_retired)
return;
lck_mtx_lock(&fasttrap_retired_mtx);
fasttrap_tracepoint_spec_t *s = &fasttrap_retired_spec[fasttrap_cur_retired++];
s->fttps_pid = p->p_pid;
s->fttps_pc = tp->ftt_pc;
if (fasttrap_cur_retired == fasttrap_retired_size) {
fasttrap_tracepoint_spec_t *new_retired = kmem_zalloc(
fasttrap_retired_size * 2 *
sizeof(*fasttrap_retired_spec),
KM_SLEEP);
memcpy(new_retired, fasttrap_retired_spec, sizeof(*fasttrap_retired_spec) * fasttrap_retired_size);
kmem_free(fasttrap_retired_spec, sizeof(*fasttrap_retired_spec) * fasttrap_retired_size);
fasttrap_retired_size *= 2;
fasttrap_retired_spec = new_retired;
}
lck_mtx_unlock(&fasttrap_retired_mtx);
tp->ftt_retired = 1;
fasttrap_pid_cleanup(FASTTRAP_CLEANUP_TRACEPOINT);
}
static void
fasttrap_pid_cleanup_compute_priority(void)
{
if (fasttrap_total > (fasttrap_max / 100 * 90) || fasttrap_retired > fasttrap_max / 2) {
thread_precedence_policy_data_t precedence = {12 /* BASEPRI_PREEMPT_HIGH */};
thread_policy_set(fasttrap_cleanup_thread, THREAD_PRECEDENCE_POLICY, (thread_policy_t) &precedence, THREAD_PRECEDENCE_POLICY_COUNT);
}
else {
thread_precedence_policy_data_t precedence = {-39 /* BASEPRI_USER_INITIATED */};
thread_policy_set(fasttrap_cleanup_thread, THREAD_PRECEDENCE_POLICY, (thread_policy_t) &precedence, THREAD_PRECEDENCE_POLICY_COUNT);
}
}
/*
* This is the timeout's callback for cleaning up the providers and their
* probes.
*/
/*ARGSUSED*/
__attribute__((noreturn))
static void
fasttrap_pid_cleanup_cb(void)
{
uint32_t work = 0;
lck_mtx_lock(&fasttrap_cleanup_mtx);
msleep(&fasttrap_pid_cleanup_cb, &fasttrap_cleanup_mtx, PRIBIO, "fasttrap_pid_cleanup_cb", NULL);
while (1) {
unsigned int later = 0;
work = os_atomic_xchg(&fasttrap_cleanup_work, 0, relaxed);
lck_mtx_unlock(&fasttrap_cleanup_mtx);
if (work & FASTTRAP_CLEANUP_PROVIDER) {
later = fasttrap_pid_cleanup_providers();
}
if (work & FASTTRAP_CLEANUP_TRACEPOINT) {
fasttrap_tracepoint_cleanup();
}
lck_mtx_lock(&fasttrap_cleanup_mtx);
fasttrap_pid_cleanup_compute_priority();
if (!fasttrap_cleanup_work) {
/*
* If we were unable to remove a retired provider, try again after
* a second. This situation can occur in certain circumstances where
* providers cannot be unregistered even though they have no probes
* enabled because of an execution of dtrace -l or something similar.
* If the timeout has been disabled (set to 1 because we're trying
* to detach), we set fasttrap_cleanup_work to ensure that we'll
* get a chance to do that work if and when the timeout is reenabled
* (if detach fails).
*/
if (later > 0) {
struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
msleep(&fasttrap_pid_cleanup_cb, &fasttrap_cleanup_mtx, PRIBIO, "fasttrap_pid_cleanup_cb", &t);
}
else
msleep(&fasttrap_pid_cleanup_cb, &fasttrap_cleanup_mtx, PRIBIO, "fasttrap_pid_cleanup_cb", NULL);
}
}
}
/*
* Activates the asynchronous cleanup mechanism.
*/
static void
fasttrap_pid_cleanup(uint32_t work)
{
lck_mtx_lock(&fasttrap_cleanup_mtx);
os_atomic_or(&fasttrap_cleanup_work, work, relaxed);
fasttrap_pid_cleanup_compute_priority();
wakeup(&fasttrap_pid_cleanup_cb);
lck_mtx_unlock(&fasttrap_cleanup_mtx);
}
static int
fasttrap_setdebug(proc_t *p)
{
LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_OWNED);
/*
* CS_KILL and CS_HARD will cause code-signing to kill the process
* when the process text is modified, so register the intent
* to allow invalid access beforehand.
*/
if ((p->p_csflags & (CS_KILL|CS_HARD))) {
proc_unlock(p);
for (int i = 0; i < DTRACE_NCLIENTS; i++) {
dtrace_state_t *state = dtrace_state_get(i);
if (state == NULL)
continue;
if (state->dts_cred.dcr_cred == NULL)
continue;
/*
* The get_task call flags whether the process should
* be flagged to have the cs_allow_invalid call
* succeed. We want the best credential that any dtrace
* client has, so try all of them.
*/
/*
* mac_proc_check_get_task() can trigger upcalls. It's
* not safe to hold proc references accross upcalls, so
* just drop the reference. Given the context, it
* should not be possible for the process to actually
* disappear.
*/
struct proc_ident pident = proc_ident(p);
sprunlock(p);
p = PROC_NULL;
(void) mac_proc_check_get_task(state->dts_cred.dcr_cred, &pident, TASK_FLAVOR_CONTROL);
p = sprlock(pident.p_pid);
if (p == PROC_NULL) {
return (ESRCH);
}
}
int rc = cs_allow_invalid(p);
proc_lock(p);
if (rc == 0) {
return (EACCES);
}
}
return (0);
}
/*
* This is called from cfork() via dtrace_fasttrap_fork(). The child
* process's address space is a (roughly) a copy of the parent process's so
* we have to remove all the instrumentation we had previously enabled in the
* parent.
*/
static void
fasttrap_fork(proc_t *p, proc_t *cp)
{
pid_t ppid = p->p_pid;
unsigned int i;
ASSERT(current_proc() == p);
LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
ASSERT(p->p_dtrace_count > 0);
ASSERT(cp->p_dtrace_count == 0);
/*
* This would be simpler and faster if we maintained per-process
* hash tables of enabled tracepoints. It could, however, potentially
* slow down execution of a tracepoint since we'd need to go
* through two levels of indirection. In the future, we should
* consider either maintaining per-process ancillary lists of
* enabled tracepoints or hanging a pointer to a per-process hash
* table of enabled tracepoints off the proc structure.
*/
/*
* We don't have to worry about the child process disappearing
* because we're in fork().
*/
if (cp != sprlock(cp->p_pid)) {
printf("fasttrap_fork: sprlock(%d) returned a different proc\n", cp->p_pid);
return;
}
proc_lock(cp);
if (fasttrap_setdebug(cp) == ESRCH) {
printf("fasttrap_fork: failed to re-acquire proc\n");
return;
}
proc_unlock(cp);
/*
* Iterate over every tracepoint looking for ones that belong to the
* parent process, and remove each from the child process.
*/
for (i = 0; i < fasttrap_tpoints.fth_nent; i++) {
fasttrap_tracepoint_t *tp;
fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i];
lck_mtx_lock(&bucket->ftb_mtx);
for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
if (tp->ftt_pid == ppid &&
tp->ftt_proc->ftpc_acount != 0) {
fasttrap_tracepoint_remove(cp, tp);
/*
* The count of active providers can only be
* decremented (i.e. to zero) during exec,
* exit, and removal of a meta provider so it
* should be impossible to drop the count
* mid-fork.
*/
ASSERT(tp->ftt_proc->ftpc_acount != 0);
}
}
lck_mtx_unlock(&bucket->ftb_mtx);
}
/*
* Free any ptss pages/entries in the child.
*/
dtrace_ptss_fork(p, cp);
sprunlock(cp);
}
/*
* This is called from proc_exit() or from exec_common() if p_dtrace_probes
* is set on the proc structure to indicate that there is a pid provider
* associated with this process.
*/
static void
fasttrap_exec_exit(proc_t *p)
{
ASSERT(p == current_proc());
LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_OWNED);
LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_NOTOWNED);
/* APPLE NOTE: Okay, the locking here is really odd and needs some
* explaining. This method is always called with the proc_lock held.
* We must drop the proc_lock before calling fasttrap_provider_retire
* to avoid a deadlock when it takes the bucket lock.
*
* Next, the dtrace_ptss_exec_exit function requires the sprlock
* be held, but not the proc_lock.
*
* Finally, we must re-acquire the proc_lock
*/
proc_unlock(p);
/*
* We clean up the pid provider for this process here; user-land
* static probes are handled by the meta-provider remove entry point.
*/
fasttrap_provider_retire(p, FASTTRAP_PID_NAME, 0);
/*
* APPLE NOTE: We also need to remove any aliased providers.
* XXX optimization: track which provider types are instantiated
* and only retire as needed.
*/
fasttrap_provider_retire(p, FASTTRAP_OBJC_NAME, 0);
fasttrap_provider_retire(p, FASTTRAP_ONESHOT_NAME, 0);
/*
* This should be called after it is no longer possible for a user
* thread to execute (potentially dtrace instrumented) instructions.
*/
lck_mtx_lock(&p->p_dtrace_sprlock);
dtrace_ptss_exec_exit(p);
lck_mtx_unlock(&p->p_dtrace_sprlock);
proc_lock(p);
}
/*ARGSUSED*/
static void
fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc)
{
#pragma unused(arg, desc)
/*
* There are no "default" pid probes.
*/
}
static int
fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
{
fasttrap_tracepoint_t *tp, *new_tp = NULL;
fasttrap_bucket_t *bucket;
fasttrap_id_t *id;
pid_t pid;
user_addr_t pc;
ASSERT(index < probe->ftp_ntps);
pid = probe->ftp_pid;
pc = probe->ftp_tps[index].fit_tp->ftt_pc;
id = &probe->ftp_tps[index].fit_id;
ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
//ASSERT(!(p->p_flag & SVFORK));
/*
* Before we make any modifications, make sure we've imposed a barrier
* on the generation in which this probe was last modified.
*/
fasttrap_mod_barrier(probe->ftp_gen);
bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
/*
* If the tracepoint has already been enabled, just add our id to the
* list of interested probes. This may be our second time through
* this path in which case we'll have constructed the tracepoint we'd
* like to install. If we can't find a match, and have an allocated
* tracepoint ready to go, enable that one now.
*
* A tracepoint whose process is defunct is also considered defunct.
*/
again:
lck_mtx_lock(&bucket->ftb_mtx);
for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
int rc = 0;
/*
* Note that it's safe to access the active count on the
* associated proc structure because we know that at least one
* provider (this one) will still be around throughout this
* operation.
*/
if (tp->ftt_pid != pid || tp->ftt_pc != pc ||
tp->ftt_proc->ftpc_acount == 0)
continue;
/*
* Now that we've found a matching tracepoint, it would be
* a decent idea to confirm that the tracepoint is still
* enabled and the trap instruction hasn't been overwritten.
* Since this is a little hairy, we'll punt for now.
*/
if (!tp->ftt_installed) {
if (fasttrap_tracepoint_install(p, tp) != 0)
rc = FASTTRAP_ENABLE_PARTIAL;
}
/*
* This can't be the first interested probe. We don't have
* to worry about another thread being in the midst of
* deleting this tracepoint (which would be the only valid
* reason for a tracepoint to have no interested probes)
* since we're holding P_PR_LOCK for this process.
*/
ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL);
switch (id->fti_ptype) {
case DTFTP_ENTRY:
case DTFTP_OFFSETS:
case DTFTP_IS_ENABLED:
id->fti_next = tp->ftt_ids;
dtrace_membar_producer();
tp->ftt_ids = id;
dtrace_membar_producer();
break;
case DTFTP_RETURN:
case DTFTP_POST_OFFSETS:
id->fti_next = tp->ftt_retids;
dtrace_membar_producer();
tp->ftt_retids = id;
dtrace_membar_producer();
break;
default:
ASSERT(0);
}
tp->ftt_retired = 0;
lck_mtx_unlock(&bucket->ftb_mtx);
if (new_tp != NULL) {
new_tp->ftt_ids = NULL;
new_tp->ftt_retids = NULL;
}
return rc;
}
/*
* If we have a good tracepoint ready to go, install it now while
* we have the lock held and no one can screw with us.
*/
if (new_tp != NULL) {
int rc = 0;
new_tp->ftt_next = bucket->ftb_data;
dtrace_membar_producer();
bucket->ftb_data = new_tp;
dtrace_membar_producer();
lck_mtx_unlock(&bucket->ftb_mtx);
/*
* Activate the tracepoint in the ISA-specific manner.
* If this fails, we need to report the failure, but
* indicate that this tracepoint must still be disabled
* by calling fasttrap_tracepoint_disable().
*/
if (fasttrap_tracepoint_install(p, new_tp) != 0)
rc = FASTTRAP_ENABLE_PARTIAL;
/*
* Increment the count of the number of tracepoints active in
* the victim process.
*/
//ASSERT(p->p_proc_flag & P_PR_LOCK);
p->p_dtrace_count++;
return (rc);
}
lck_mtx_unlock(&bucket->ftb_mtx);
/*
* Initialize the tracepoint that's been preallocated with the probe.
*/
new_tp = probe->ftp_tps[index].fit_tp;
new_tp->ftt_retired = 0;
ASSERT(new_tp->ftt_pid == pid);
ASSERT(new_tp->ftt_pc == pc);
ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc);
ASSERT(new_tp->ftt_ids == NULL);
ASSERT(new_tp->ftt_retids == NULL);
switch (id->fti_ptype) {
case DTFTP_ENTRY:
case DTFTP_OFFSETS:
case DTFTP_IS_ENABLED:
id->fti_next = NULL;
new_tp->ftt_ids = id;
break;
case DTFTP_RETURN:
case DTFTP_POST_OFFSETS:
id->fti_next = NULL;
new_tp->ftt_retids = id;
break;
default:
ASSERT(0);
}
/*
* If the ISA-dependent initialization goes to plan, go back to the
* beginning and try to install this freshly made tracepoint.
*/
if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0)
goto again;
new_tp->ftt_ids = NULL;
new_tp->ftt_retids = NULL;
return (FASTTRAP_ENABLE_FAIL);
}
static void
fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
{
fasttrap_bucket_t *bucket;
fasttrap_provider_t *provider = probe->ftp_prov;
fasttrap_tracepoint_t **pp, *tp;
fasttrap_id_t *id, **idp;
pid_t pid;
user_addr_t pc;
ASSERT(index < probe->ftp_ntps);
pid = probe->ftp_pid;
pc = probe->ftp_tps[index].fit_tp->ftt_pc;
id = &probe->ftp_tps[index].fit_id;
ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
/*
* Find the tracepoint and make sure that our id is one of the
* ones registered with it.
*/
bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
lck_mtx_lock(&bucket->ftb_mtx);
for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
if (tp->ftt_pid == pid && tp->ftt_pc == pc &&
tp->ftt_proc == provider->ftp_proc)
break;
}
/*
* If we somehow lost this tracepoint, we're in a world of hurt.
*/
ASSERT(tp != NULL);
switch (id->fti_ptype) {
case DTFTP_ENTRY:
case DTFTP_OFFSETS:
case DTFTP_IS_ENABLED:
ASSERT(tp->ftt_ids != NULL);
idp = &tp->ftt_ids;
break;
case DTFTP_RETURN:
case DTFTP_POST_OFFSETS:
ASSERT(tp->ftt_retids != NULL);
idp = &tp->ftt_retids;
break;
default:
/* Fix compiler warning... */
idp = NULL;
ASSERT(0);
}
while ((*idp)->fti_probe != probe) {
idp = &(*idp)->fti_next;
ASSERT(*idp != NULL);
}
id = *idp;
*idp = id->fti_next;
dtrace_membar_producer();
ASSERT(id->fti_probe == probe);
/*