diff --git a/arch/aarch64/mcount-dynamic.c b/arch/aarch64/mcount-dynamic.c index a9e2fbfe2..c5826ea4c 100644 --- a/arch/aarch64/mcount-dynamic.c +++ b/arch/aarch64/mcount-dynamic.c @@ -63,13 +63,21 @@ int mcount_setup_trampoline(struct mcount_dynamic_info *mdi) mdi->trampoline -= sizeof(trampoline); if (unlikely(mdi->trampoline < mdi->text_addr + mdi->text_size)) { + void *trampoline_check; + mdi->trampoline += sizeof(trampoline); mdi->text_size += PAGE_SIZE; pr_dbg("adding a page for fentry trampoline at %#lx\n", mdi->trampoline); - mmap((void *)mdi->trampoline, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + trampoline_check = mmap((void *)mdi->trampoline, PAGE_SIZE, + PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (trampoline_check != (void *)mdi->trampoline) { + pr_err("could not map trampoline at desired location %#lx, got %#lx: %m\n", + mdi->trampoline, (uintptr_t)trampoline_check); + } } page_offset = mdi->text_addr & (PAGE_SIZE - 1); diff --git a/arch/i386/mcount-dynamic.c b/arch/i386/mcount-dynamic.c index 96bac34c2..bd7d795a8 100644 --- a/arch/i386/mcount-dynamic.c +++ b/arch/i386/mcount-dynamic.c @@ -30,10 +30,12 @@ int mcount_setup_trampoline(struct mcount_dynamic_info *mdi) pr_dbg2("adding a page for fentry trampoline at %#lx\n", mdi->trampoline); trampoline_check = mmap((void *)mdi->trampoline, PAGE_SIZE, PROT_READ | PROT_WRITE, - MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (trampoline_check == MAP_FAILED) - pr_err("failed to mmap trampoline for setup"); + if (trampoline_check != (void *)mdi->trampoline) { + pr_err("could not map trampoline at desired location %#lx, got %#lx: %m\n", + mdi->trampoline, (uintptr_t)trampoline_check); + } } if (mprotect((void *)mdi->text_addr, mdi->text_size, PROT_READ | PROT_WRITE)) { diff --git a/arch/x86_64/mcount-dynamic.c b/arch/x86_64/mcount-dynamic.c index a14642287..4da8e5436 100644 --- a/arch/x86_64/mcount-dynamic.c +++ b/arch/x86_64/mcount-dynamic.c @@ -43,10 +43,12 @@ int mcount_setup_trampoline(struct mcount_dynamic_info *mdi) trampoline_check = mmap((void *)mdi->trampoline, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (trampoline_check == MAP_FAILED) - pr_err("failed to mmap trampoline for setup"); + if (trampoline_check != (void *)mdi->trampoline) { + pr_err("could not map trampoline at desired location %#lx, got %#lx: %m\n", + mdi->trampoline, (uintptr_t)trampoline_check); + } } if (mprotect(PAGE_ADDR(mdi->text_addr), PAGE_LEN(mdi->text_addr, mdi->text_size), @@ -623,6 +625,7 @@ int mcount_unpatch_func(struct mcount_dynamic_info *mdi, struct uftrace_symbol * switch (mdi->type) { case DYNAMIC_FENTRY: + case DYNAMIC_PATCHABLE: result = unpatch_fentry_func(mdi, sym); break; diff --git a/libmcount/dynamic.c b/libmcount/dynamic.c index 6e7b56dd9..507162060 100644 --- a/libmcount/dynamic.c +++ b/libmcount/dynamic.c @@ -508,6 +508,8 @@ static void patch_patchable_func_matched(struct mcount_dynamic_info *mdi, struct .size = UINT_MAX, .name = namebuf, }; + bool found = false; + int match; char *soname = get_soname(map->libname); symtab = &map->mod->symtab; @@ -531,9 +533,19 @@ static void patch_patchable_func_matched(struct mcount_dynamic_info *mdi, struct continue; } - mcount_patch_func_with_stats(mdi, sym); + found = true; + match = match_pattern_list(map, soname, sym->name); + if (!match) + continue; + else if (match == 1) + mcount_patch_func_with_stats(mdi, sym); + else + mcount_unpatch_func(mdi, sym, NULL); } + if (!found) + stats.nomatch++; + free(soname); } diff --git a/tests/runtest.py b/tests/runtest.py index 53b9721fb..aa4819ab6 100755 --- a/tests/runtest.py +++ b/tests/runtest.py @@ -19,11 +19,8 @@ def is_32bit(filename): # e_ident[] indexes EI_CLASS = 4 - # EI_CLASS - ELFCLASSNONE = 0 + # EI_CLASS: ELFCLASSNONE, ELFCLASS32, ELFCLASS64, ELFCLASSNUM ELFCLASS32 = 1 - ELFCLASS64 = 2 - ELFCLASSNUM = 3 try: with open(filename, 'rb') as f: @@ -32,7 +29,7 @@ def is_32bit(filename): if ei_class == ELFCLASS32: return True - except: + except Exception: pass return False @@ -59,11 +56,11 @@ def get_elf_machine(filename): # read e_machine e_machine = f.read(2)[0] - if type(e_machine) is str: + if isinstance(e_machine, str): e_machine = ord(e_machine) return machine[e_machine] - except: + except Exception: pass return None @@ -144,7 +141,7 @@ def gen_port(self, start = 40000, end = 50000): s.bind(("localhost", port)) self.port = port return - except OSError as e: + except OSError: pass raise Exception("No available port found") @@ -156,7 +153,7 @@ def test_feature(self): for i in range(3, len(s) - 1): self.feature.add(s[i]) return True - except: + except Exception: return False def convert_abs_path(self, build_cmd): @@ -185,7 +182,7 @@ def build_it(self, build_cmd): except OSError as e: self.pr_debug(e.strerror) return TestBase.TEST_BUILD_FAIL - except: + except Exception: return TestBase.TEST_BUILD_FAIL def build(self, name, cflags='', ldflags=''): @@ -199,11 +196,9 @@ def build(self, name, cflags='', ldflags=''): build_cflags = ' '.join(TestBase.default_cflags + [self.cflags, cflags, \ os.getenv(lang['flags'], '')]) - build_ldflags = ' '.join([self.ldflags, ldflags, \ - os.getenv('LDFLAGS', '')]) + build_ldflags = ' '.join([self.ldflags, ldflags, os.getenv('LDFLAGS', '')]) - build_cmd = '%s -o %s %s %s %s' % \ - (lang['cc'], prog, build_cflags, src, build_ldflags) + build_cmd = '%s -o %s %s %s %s' % (lang['cc'], prog, build_cflags, src, build_ldflags) self.pr_debug("build command: %s" % build_cmd) return self.build_it(build_cmd) @@ -213,8 +208,7 @@ def build_notrace_lib(self, dstname, srcname, cflags='', ldflags =''): build_cflags = ' '.join(TestBase.default_cflags + [self.cflags, cflags, \ os.getenv(lang['flags'], '')]) - build_ldflags = ' '.join([self.ldflags, ldflags, \ - os.getenv('LDFLAGS', '')]) + build_ldflags = ' '.join([self.ldflags, ldflags, os.getenv('LDFLAGS', '')]) lib_cflags = build_cflags + ' -shared -fPIC' @@ -230,8 +224,7 @@ def build_libabc(self, cflags='', ldflags=''): build_cflags = ' '.join(TestBase.default_cflags + [self.cflags, cflags, \ os.getenv(lang['flags'], '')]) - build_ldflags = ' '.join([self.ldflags, ldflags, \ - os.getenv('LDFLAGS', '')]) + build_ldflags = ' '.join([self.ldflags, ldflags, os.getenv('LDFLAGS', '')]) lib_cflags = build_cflags + ' -shared -fPIC' @@ -239,8 +232,7 @@ def build_libabc(self, cflags='', ldflags=''): self.p_libs.append('libabc_test_lib.so') # build libabc_test_lib.so library - build_cmd = '%s -o libabc_test_lib.so %s s-lib.c %s' % \ - (lang['cc'], lib_cflags, build_ldflags) + build_cmd = '%s -o libabc_test_lib.so %s s-lib.c %s' % (lang['cc'], lib_cflags, build_ldflags) self.pr_debug("build command for library: %s" % build_cmd) return self.build_it(build_cmd) @@ -250,8 +242,7 @@ def build_libfoo(self, name, cflags='', ldflags=''): build_cflags = ' '.join(TestBase.default_cflags + [self.cflags, cflags, \ os.getenv(lang['flags'], '')]) - build_ldflags = ' '.join([self.ldflags, ldflags, \ - os.getenv('LDFLAGS', '')]) + build_ldflags = ' '.join([self.ldflags, ldflags, os.getenv('LDFLAGS', '')]) lib_cflags = build_cflags + ' -shared -fPIC' @@ -338,7 +329,7 @@ def task_sort(self, output, ignore_children=False): m = pid_patt.match(ln) try: pid = int(m.group(1)) - except: + except Exception: continue func = ln.split('|', 1)[-1] @@ -358,7 +349,7 @@ def task_sort(self, output, ignore_children=False): for p in pid_list: result += '\n'.join(pids[p]['result']) + '\n' result = result.strip() - except: + except Exception: pass # this leads to a failure with 'NG' return result @@ -393,7 +384,7 @@ def report_sort(self, output, ignored): try: if line[-1].startswith('__'): continue - except: + except Exception: pass result.append('%s %s' % (line[-2], line[-1])) @@ -430,7 +421,6 @@ def graph_sort(self, output, ignored): def dump_sort(self, output, ignored): """ This function post-processes output of the test to be compared . It ignores blank and comment (#) lines and remaining functions. """ - mode = 1 result = [] # A (raw) dump result consists of following data @@ -470,7 +460,7 @@ def chrome_sort(self, output, ignored): result = [] try: o = json.loads(output) - except: + except Exception: return '' for ln in o['traceEvents']: if ln['name'].startswith('__'): @@ -545,7 +535,7 @@ def check_perf_paranoid(self): if v >= 3: return False - except: + except Exception: pass return True @@ -698,7 +688,7 @@ def timeout_handler(sig, frame): dif = "%s: diff result of %s %s\n" % (name, compiler, cflags) try: p = sp.Popen(['colordiff', '-U1', 'expect', 'result'], stdout=sp.PIPE) - except: + except Exception: p = sp.Popen(['diff', '-U1', 'expect', 'result'], stdout=sp.PIPE) dif += p.communicate()[0].decode(errors='ignore') os.remove('expect')