Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Introduction to DTrace
Frank Lefebvre
CocoaHeads Paris
November 2016 @franklefebvre
About DTrace
• Overview
• History
• Dynamic: providers, consumers
• Systemwide
• Zero disable cost
Security considerations
• Privileges
• dtrace: root
• Instruments: requests admin password
• Restrictions
• Supported plat...
System Integrity Protection
• Your applications on a development system
• It just works
• Your applications in production
...
Disabling SIP
• Check current status
csrutil status
• Reboot in Recovery mode
csrutil enable --without dtrace
csrutil disa...
Probe specifiers
• probe ID
• provider:module:function:name
• wildcards
• escaping
Providers
• General providers
• syscall, fbt, io, pid, profile…
• Language providers
• objc_runtime, python, sh…
• Special...
DTrace one-liners
• probes
'syscall::open*:entry, syscall::open*:return'
• probes {actions}
'syscall::open*:entry {printf(...
Using one-liners
• Run globally
dtrace -n (program)
• Attach to existing process
dtrace -n (program) -p (pid)
• Launch exe...
Hello, World!
#!/usr/sbin/dtrace -s
#pragma D option quiet
dtrace:::BEGIN
{
printf("Hello World!n");
exit(0);
}
Structure of a D program
#!/usr/sbin/dtrace -s
provider:module:function:name
/predicate/
{
action statement;
action statem...
Action statements
• C-like syntax
• No control flow
• No function definitions
• Variables
• Built-in variables
Aggregations
• Simple aggregation
@variable = function(…);
• Key-based aggregation
@variable[key] = function(…);
• Aggrega...
Top 5 processes
#!/usr/sbin/dtrace -s
#pragma D option quiet
profile:::tick-1001
{
@counters[execname] = count();
}
profil...
Creating your own probes
• Create provider definition file
• Generate C header file
• Import in Objective-C code
• Add probe ...
Create provider definition file
/* provider.d */
provider cocoaheads {
probe some_action(int);
probe another_action(char*);
...
Generate header file
dtrace -h -s provider.d
/* provider.h */
COCOAHEADS_SOME_ACTION()
COCOAHEADS_SOME_ACTION_ENABLED()
COC...
Add probe macros
#import "provider.h"
COCOAHEADS_SOME_ACTION(value);
if (COCOAHEADS_ANOTHER_ACTION_ENABLED()) {
NSString* ...
Using your probes in Swift code
// File: provider_functions.h
#import "provider.h"
static inline void CocoaHeadsSomeAction...
Your custom probe in DTrace
• Provider: cocoaheads<pid>
• Module: <your executable name>
• Function: CocoaHeadsSomeAction
...
Recap
CocoaHeads Paris
November 2016 @franklefebvre
Q&A
CocoaHeads Paris
November 2016 @franklefebvre
Prochain SlideShare
Chargement dans…5
×

Découvrir dtrace en ligne de commande.

2 203 vues

Publié le

Découvrir dtrace en ligne de commande. Présentation de l'outil dtrace et du langage D. — Frank Lefebvre

Publié dans : Logiciels
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

Découvrir dtrace en ligne de commande.

  1. 1. Introduction to DTrace Frank Lefebvre CocoaHeads Paris November 2016 @franklefebvre
  2. 2. About DTrace • Overview • History • Dynamic: providers, consumers • Systemwide • Zero disable cost
  3. 3. Security considerations • Privileges • dtrace: root • Instruments: requests admin password • Restrictions • Supported platforms • System Integrity Protection
  4. 4. System Integrity Protection • Your applications on a development system • It just works • Your applications in production • com.apple.security.get-task-allow entitlement • or disable System Integrity Protection • Other executables • Disable System Integrity Protection
  5. 5. Disabling SIP • Check current status csrutil status • Reboot in Recovery mode csrutil enable --without dtrace csrutil disable csrutil clear
  6. 6. Probe specifiers • probe ID • provider:module:function:name • wildcards • escaping
  7. 7. Providers • General providers • syscall, fbt, io, pid, profile… • Language providers • objc_runtime, python, sh… • Special probes • dtrace:::BEGIN, dtrace:::END
  8. 8. DTrace one-liners • probes 'syscall::open*:entry, syscall::open*:return' • probes {actions} 'syscall::open*:entry {printf("%sn", copyinstr(arg0));}' • probes /predicate/ {actions} 'syscall::open*:entry /execname == "Safari"/ {printf("%s n", copyinstr(arg0));}'
  9. 9. Using one-liners • Run globally dtrace -n (program) • Attach to existing process dtrace -n (program) -p (pid) • Launch executable (restrictions apply) dtrace -n (program) -c (command)
  10. 10. Hello, World! #!/usr/sbin/dtrace -s #pragma D option quiet dtrace:::BEGIN { printf("Hello World!n"); exit(0); }
  11. 11. Structure of a D program #!/usr/sbin/dtrace -s provider:module:function:name /predicate/ { action statement; action statement; } … Header Clause Clause
  12. 12. Action statements • C-like syntax • No control flow • No function definitions • Variables • Built-in variables
  13. 13. Aggregations • Simple aggregation @variable = function(…); • Key-based aggregation @variable[key] = function(…); • Aggregating functions count, sum, avg, min, max, quantize…
  14. 14. Top 5 processes #!/usr/sbin/dtrace -s #pragma D option quiet profile:::tick-1001 { @counters[execname] = count(); } profile:::tick-1sec { trunc(@counters, 5); printf("n"); printa("%s --> %@dn", @counters); trunc(@counters); }
  15. 15. Creating your own probes • Create provider definition file • Generate C header file • Import in Objective-C code • Add probe macros to source code • Profit!
  16. 16. Create provider definition file /* provider.d */ provider cocoaheads { probe some_action(int); probe another_action(char*); };
  17. 17. Generate header file dtrace -h -s provider.d /* provider.h */ COCOAHEADS_SOME_ACTION() COCOAHEADS_SOME_ACTION_ENABLED() COCOAHEADS_ANOTHER_ACTION() COCOAHEADS_ANOTHER_ACTION_ENABLED()
  18. 18. Add probe macros #import "provider.h" COCOAHEADS_SOME_ACTION(value); if (COCOAHEADS_ANOTHER_ACTION_ENABLED()) { NSString* s = some_expensive_call(); COCOAHEADS_ANOTHER_ACTION([s UTF8String]); }
  19. 19. Using your probes in Swift code // File: provider_functions.h #import "provider.h" static inline void CocoaHeadsSomeAction(int arg) { COCOAHEADS_SOME_ACTION(arg); }
  20. 20. Your custom probe in DTrace • Provider: cocoaheads<pid> • Module: <your executable name> • Function: CocoaHeadsSomeAction • Name: some_action • Arg0: <value>
  21. 21. Recap CocoaHeads Paris November 2016 @franklefebvre
  22. 22. Q&A CocoaHeads Paris November 2016 @franklefebvre

×