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]]
16
17return function()
18  require("bcc.vendor.helpers")
19  local standalone = rawget(_G, "BCC_STANDALONE")
20  local progname = standalone or "bcc-probe"
21
22  local function print_usage()
23    io.stderr:write(string.format(
24      "usage: %s [[--version|--verbose] --] path_to_script.lua [...]\n",
25      progname))
26    os.exit(1)
27  end
28
29  local function print_version()
30    local jit = require("jit")
31    print(string.format("%s %s -- Running on %s (%s/%s)",
32      progname, rawget(_G, "BCC_VERSION") or "HEAD",
33      jit.version, jit.os, jit.arch))
34    os.exit(0)
35  end
36
37  while arg[1] and string.starts(arg[1], "-") do
38    local k = table.remove(arg, 1)
39    if k == "--" then
40      break
41    elseif standalone == nil and string.starts(k, "--so-path=") then
42      rawset(_G, "LIBBCC_SO_PATH", string.lstrip(k, "--so-path="))
43    elseif k == "--llvm-debug" then
44      rawset(_G, "LIBBCC_LLVM_DEBUG", 1)
45    elseif k == "-V" or k == "--verbose" then
46      log.enabled = true
47    elseif k == "-v" or k == "--version" then
48      print_version()
49    else
50      print_usage()
51    end
52  end
53
54  local tracefile = table.remove(arg, 1)
55  if not tracefile then print_usage() end
56
57  local BPF = require("bcc.bpf")
58  BPF.script_root(tracefile)
59
60  local USDT = require("bcc.usdt")
61  local utils = {
62    argparse = require("bcc.vendor.argparse"),
63    posix = require("bcc.vendor.posix"),
64    USDT = USDT,
65  }
66
67  local command = dofile(tracefile)
68  local res, err = xpcall(command, debug.traceback, BPF, utils)
69
70  if not res and err ~= "interrupted!" then
71    io.stderr:write("[ERROR] "..err.."\n")
72  end
73
74  BPF.cleanup()
75  USDT.cleanup()
76  return res, err
77end
78