• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1--[[
2Copyright 2016 GitHub, Inc
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15]]
16local ffi = require("ffi")
17local libbcc = require("bcc.libbcc")
18local Usdt = class("USDT")
19
20Usdt.static.open_contexts = {}
21
22function Usdt.static.cleanup()
23  for _, context in ipairs(Usdt.static.open_contexts) do
24    context:_cleanup()
25  end
26end
27
28function Usdt:initialize(args)
29  assert(args.pid or args.path)
30
31  if args.pid then
32    self.pid = args.pid
33    self.context = libbcc.bcc_usdt_new_frompid(args.pid)
34  elseif args.path then
35    self.path = args.path
36    self.context = libbcc.bcc_usdt_new_frompath(args.path)
37  end
38
39  assert(self.context ~= nil, "failed to create USDT context")
40  table.insert(Usdt.open_contexts, self)
41end
42
43function Usdt:enable_probe(args)
44  assert(args.probe and args.fn_name)
45  assert(libbcc.bcc_usdt_enable_probe(
46    self.context, args.probe, args.fn_name) == 0)
47end
48
49function Usdt:_cleanup()
50  libbcc.bcc_usdt_close(self.context)
51  self.context = nil
52end
53
54function Usdt:_get_text()
55  local argc = libbcc.bcc_usdt_genargs(self.context)
56  assert(argc ~= nil)
57  return ffi.string(argc)
58end
59
60function Usdt:_attach_uprobes(bpf)
61  local uprobes = {}
62  local cb = ffi.cast("bcc_usdt_uprobe_cb",
63    function(binpath, fn_name, addr, pid)
64      table.insert(uprobes, {name=ffi.string(binpath),
65        addr=addr, fn_name=ffi.string(fn_name), pid=pid})
66    end)
67
68  libbcc.bcc_usdt_foreach_uprobe(self.context, cb)
69  cb:free()
70
71  for _, args in ipairs(uprobes) do
72    bpf:attach_uprobe(args)
73  end
74end
75
76return Usdt
77