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, sym_off) 36 local sym = SYM() 37 local module_path 38 local new_addr 39 if libbcc.bcc_resolve_symname(module, symname, addr or 0x0, pid or 0, nil, sym) < 0 then 40 if sym[0].module == nil then 41 error("could not find library '%s' in the library path" % module) 42 else 43 module_path = ffi.string(sym[0].module) 44 libbcc.bcc_procutils_free(sym[0].module) 45 error("failed to resolve symbol '%s' in '%s'" % { 46 symname, module_path}) 47 end 48 end 49 new_addr = sym[0].offset + (sym_off or 0) 50 module_path = ffi.string(sym[0].module) 51 libbcc.bcc_procutils_free(sym[0].module) 52 return module_path, new_addr 53end 54 55return { create_cache=create_cache, check_path_symbol=check_path_symbol } 56