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 SYM = ffi.typeof("struct bcc_symbol[1]") 19 20local function create_cache(pid) 21 return { 22 _CACHE = libbcc.bcc_symcache_new(pid or -1, nil), 23 resolve = function(self, addr) 24 local sym = SYM() 25 if libbcc.bcc_symcache_resolve(self._CACHE, addr, sym) < 0 then 26 return "[unknown]", 0x0 27 end 28 local name_res = ffi.string(sym[0].demangle_name) 29 libbcc.bcc_symbol_free_demangle_name(sym); 30 return name_res, sym[0].offset 31 end 32 } 33end 34 35local function check_path_symbol(module, symname, addr, pid) 36 local sym = SYM() 37 local module_path 38 if libbcc.bcc_resolve_symname(module, symname, addr or 0x0, pid or 0, nil, sym) < 0 then 39 if sym[0].module == nil then 40 error("could not find library '%s' in the library path" % module) 41 else 42 module_path = ffi.string(sym[0].module) 43 libbcc.bcc_procutils_free(sym[0].module) 44 error("failed to resolve symbol '%s' in '%s'" % { 45 symname, module_path}) 46 end 47 end 48 module_path = ffi.string(sym[0].module) 49 libbcc.bcc_procutils_free(sym[0].module) 50 return module_path, sym[0].offset 51end 52 53return { create_cache=create_cache, check_path_symbol=check_path_symbol } 54