Class: Dtrace::Provider

Inherits:
Object
  • Object
show all
Includes:
Dof::Constants
Defined in:
lib/dtrace/provider.rb,
lib/dtrace/provider/probedef.rb

Overview

A DTrace provider. Allows creation of USDT probes on a running Ruby program. You can use this with a Ruby interpreter compiled with the core DTrace probes, but you don’t have to.

Firing probes is explained in Dtrace::Probe.

Defined Under Namespace

Classes: ProbeDef

Constant Summary collapse

Typemap =
{ :string => 'char *', :integer => 'int' }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider_name, module_name) ⇒ Provider

Returns a new instance of Provider.



90
91
92
93
94
95
# File 'lib/dtrace/provider.rb', line 90

def initialize(provider_name, module_name)
  @name       = provider_name.to_s
  @module     = module_name.to_s
  @class      = camelize(provider_name)
  @probe_defs = []
end

Class Method Details

.create(name, options = {}) {|provider| ... } ⇒ Object

Creates a DTrace provider.

Example:

Dtrace::Provider.create :action_controller do |p|
  p.probe :process_start,  :string
  p.probe :process_finish, :string, :integer
end

The symbol passed to create becomes the name of the provider, and the class exposed under Dtrace::Probe in Ruby (camelized, so the above statement creates Dtrace::Probe::ActionController).

create yields a Provider for the current platform, on which you can call probe, to create the individual probes.

You can override the module name in the created probes, by passing in an hash:

Dtrace::Provider.create :foo, { :module => 'somemodule' } do |p|
  p.probe...
end

Yields:

  • (provider)


46
47
48
49
50
51
# File 'lib/dtrace/provider.rb', line 46

def self.create(name, options={})
  options[:module] ||= 'ruby'
  provider = Dtrace::Provider.new(name, options[:module])
  yield provider
  provider.enable
end

Instance Method Details

#enableObject



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
# File 'lib/dtrace/provider.rb', line 97

def enable
  f = Dtrace::Dof::File.new
  
  strtab = Dtrace::Dof::Section::Strtab.new(0)
  f.sections << strtab

  s = Dtrace::Dof::Section.new(DOF_SECT_PROBES, 1)
  probes = Array.new
  stubs = Hash.new
  argidx = 0
  offidx = 0
  @probe_defs.each do |pd|
    argc = pd.argc
    
    argv = 0
    pd.args.each do |type|
      i = strtab.add(type)
      argv = i if argv == 0
    end
    
    probe = Dtrace::Probe.new(argc)
    probes <<
      {
      :name     => strtab.add(pd.name),
      :func     => strtab.add(pd.function),
      :noffs    => 1,
      :enoffidx => offidx,
      :argidx   => argidx,
      :nenoffs  => 1,
      :offidx   => offidx,
      :addr     => probe.addr,
      :nargc    => argc,
      :xargc    => argc,
      :nargv    => argv,
      :xargv    => argv,
    }
    
    stubs[pd.name] = probe
    argidx += argc
    offidx += 1
  end
  s.data = probes
  f.sections << s

  s = Dtrace::Dof::Section.new(DOF_SECT_PRARGS, 2)
  s.data = Array.new
  @probe_defs.each do |pd|
    pd.args.each_with_index do |arg, i|
      s.data << i
    end
  end
  if s.data.empty?
    s.data = [ 0 ]
  end
  f.sections << s

  s = Dtrace::Dof::Section.new(DOF_SECT_PROFFS, 3)
  s.data = Array.new
  @probe_defs.each do |pd|
    s.data << stubs[pd.name].probe_offset(f.addr, pd.argc)
  end
  if s.data.empty?
    s.data = [ 0 ]
  end
  f.sections << s

  s = Dtrace::Dof::Section.new(DOF_SECT_PRENOFFS, 4)
  s.data = Array.new
  @probe_defs.each do |pd|
    s.data << stubs[pd.name].is_enabled_offset(f.addr)
  end
  if s.data.empty?
    s.data = [ 0 ]
  end
  f.sections << s
  
  s = Dtrace::Dof::Section.new(DOF_SECT_PROVIDER, 5)
  s.data = {
    :strtab => 0,
    :probes => 1,
    :prargs => 2,
    :proffs => 3,
    :prenoffs => 4,
    :name => strtab.add(@name),
    :provattr => { 
      :name  => DTRACE_STABILITY_EVOLVING,
      :data  => DTRACE_STABILITY_EVOLVING,
      :class => DTRACE_STABILITY_EVOLVING 
    },
    :modattr  => { 
      :name => DTRACE_STABILITY_PRIVATE,
      :data => DTRACE_STABILITY_PRIVATE,
      :class => DTRACE_STABILITY_EVOLVING 
    },
    :funcattr => { 
      :name => DTRACE_STABILITY_PRIVATE,
      :data => DTRACE_STABILITY_PRIVATE,
      :class => DTRACE_STABILITY_EVOLVING
    },
    :nameattr => { 
      :name => DTRACE_STABILITY_EVOLVING,
      :data => DTRACE_STABILITY_EVOLVING,
      :class => DTRACE_STABILITY_EVOLVING
    },
    :argsattr => {
      :name => DTRACE_STABILITY_EVOLVING,
      :data => DTRACE_STABILITY_EVOLVING,
      :class => DTRACE_STABILITY_EVOLVING
    },
  }
  f.sections << s
  
  f.generate
  Dtrace::Dof.loaddof(f, @module)

  eval "
class Dtrace::Probe::#{@class}
  def self.stubs=(s)
@@probes = s
  end
  def self.method_missing(name)
name = name.to_s
unless @@probes[name].nil?
  if @@probes[name].is_enabled?
    yield @@probes[name]
  end
end
  end
end"
  eval "Dtrace::Probe::#{@class}.stubs = stubs"
end

#probe(name, *types) ⇒ Object

Creates a DTrace USDT probe. Arguments are the probe name, and then the argument types it will accept. The following argument types are supported:

:string (char *) :integer (int)

Providing an options hash as the second argument allows you to override the function name, otherwise it will be taken from the caller of this function:

p.probe :foo, { :function => 'somefunction' }, :int, ...


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dtrace/provider.rb', line 66

def probe(name, *types) 
  options = {}
  if types[0].respond_to? :keys
    options = types.shift
  end
  caller = Kernel.caller[0].match(/`(.*)'/)
  if caller
    options[:function] ||= caller[1]
  else
    options[:function] ||= name
  end

  pd = Dtrace::Provider::ProbeDef.new(name, options[:function])
  types.each do |t|
    if Typemap[t].nil?
      raise Dtrace::Exception.new("type '#{t}' invalid")
    else
      pd.args << Typemap[t]
    end
  end

  @probe_defs << pd
end