Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add record and report features and fix some issues #534

Merged
merged 3 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions eBPF_Supermarket/User_Function_Tracer/src/demangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ static char *simplify(char *name) {
// remove function template "<...>"
for (size_t i = 0; i < len; i++) {
if (name[i] == '<') {
if (name[i + 1] == '<' && i >= 8 &&
strncmp(name + i - 8, "operator", 8) == 0) { // skip operator<<
i++;
size_t j = i + 1;
while (name[j] == ' ') ++j;
memmove(name + i + 1, name + j, len - j + 1);
len -= j - i - 1;
continue;
}
size_t j = i;
int nested = 1;
while (j + 1 < len) {
Expand All @@ -45,7 +54,6 @@ static char *simplify(char *name) {
}
memmove(name + i, name + j + 1, len - j);
len -= j - i + 1;
break;
}
}

Expand Down Expand Up @@ -96,7 +104,7 @@ static char *simplify(char *name) {
memmove(name + i, name + j + 1, len - j);
len -= j - i + 1;
// remove function return type
for (j = 0; j < i; j++) {
for (j = i; j > 0; j--) {
if (name[j] == ' ') {
if (j != 8 || strncmp(name, "operator", 8)) {
memmove(name, name + j + 1, len - j);
Expand Down
18 changes: 17 additions & 1 deletion eBPF_Supermarket/User_Function_Tracer/src/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void elf_head_free(struct elf_head *elf) {
elf_end(elf->e);
close(elf->fd);
}
elf = NULL;
}

size_t get_entry_address(const char *filename) {
Expand Down Expand Up @@ -95,6 +94,23 @@ bool elf_rela_entry_next(struct elf_rela_entry *elf_e, struct elf_section *elf_s
return true;
}

void elf_rel_entry_begin(struct elf_rel_entry *elf_e, struct elf_section *elf_s,
Elf_Data *dyn_sym_data) {
elf_e->i = 0;
elf_e->nentries = elf_s->shdr.sh_size / elf_s->shdr.sh_entsize;
elf_e->rel_data = elf_getdata(elf_s->scn, NULL);
elf_e->sym_data = dyn_sym_data;
}

bool elf_rel_entry_next(struct elf_rel_entry *elf_e, struct elf_section *elf_s) {
(void)elf_s;
if (elf_e->i >= elf_e->nentries) return false;
gelf_getrel(elf_e->rel_data, elf_e->i, &elf_e->rel);
gelf_getsym(elf_e->sym_data, GELF_R_SYM(elf_e->rel.r_info), &elf_e->sym);
elf_e->i++;
return true;
}

void elf_versym_entry_begin(struct elf_versym_entry *elf_e, struct elf_section *elf_s) {
elf_e->i = 0;
elf_e->nentries = elf_s->shdr.sh_size / elf_s->shdr.sh_entsize;
Expand Down
33 changes: 32 additions & 1 deletion eBPF_Supermarket/User_Function_Tracer/src/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,37 @@ void elf_rela_entry_begin(struct elf_rela_entry *elf_e, struct elf_section *elf_
*/
bool elf_rela_entry_next(struct elf_rela_entry *elf_e, struct elf_section *elf_s);

/**
* @brief 保存ELF重定位条目
*/
struct elf_rel_entry {
size_t i; /**< 当前条目序号 */
size_t nentries; /**< 条目总数 */
Elf_Data *sym_data; /**< 符号数据 */
Elf_Data *rel_data; /**< 重定位数据 */
GElf_Rel rel; /**< 重定位表项 */
GElf_Sym sym; /**< 符号表项 */
};

/**
* @brief 开始遍历ELF节(.rel)中的各个条目
* @param[out] elf_e 指向一个条目
* @param[in] elf_s 被遍历的ELF节
* @param[in] dyn_sym_data 动态符号数据
*/
void elf_rel_entry_begin(struct elf_rel_entry *elf_e, struct elf_section *elf_s,
Elf_Data *dyn_sym_data);

/**
* @brief 移动到下一个ELF条目
* @param[out] elf_e 指向一个条目
* @param[in] elf_s 被遍历的ELF节信息
* @return 指示是否遍历结束
* @retval 0 当前elf_e合法
* 1 当前elf_e不合法,即遍历结束
*/
bool elf_rel_entry_next(struct elf_rel_entry *elf_e, struct elf_section *elf_s);

struct elf_versym_entry {
size_t i; /**< 当前条目序号 */
size_t nentries; /**< 条目总数 */
Expand Down Expand Up @@ -212,4 +243,4 @@ void elf_verneed_entry_begin(struct elf_verneed_entry *elf_e, struct elf_section
*/
bool elf_verneed_entry_next(struct elf_verneed_entry *elf_e, struct elf_section *elf_s);

#endif // UTRACE_ELF_H
#endif // UTRACE_ELF_H
55 changes: 55 additions & 0 deletions eBPF_Supermarket/User_Function_Tracer/src/env.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2023 The LMP Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// author: [email protected]
//
// Save command line arguments info

#ifndef UTRACE_ENV_H
#define UTRACE_ENV_H

#include <stdio.h> // for FILE*

#include "report.h"
#include "vector.h"

struct env {
char *argv[12]; /**< -c/-commond */
bool avg_self; /**< --avg-self */
bool avg_total; /**< --avg-total */
bool flat; /**< --flat */
enum FORMAT format; /**< --format */
char *func_pattern; /**< -f/--function */
char *lib_pattern; /**< -l/--lib */
bool show_libname; /**< --libname */
unsigned int max_depth; /**< --max-depth */
char *nest_lib_pattern; /**< --nest-lib */
char *no_func_pattern; /**< --no-function */
char *no_lib_pattern; /**< --no-lib */
bool no_aslr; /**< --no-randomize-addr */
FILE *out; /**< -o/--output */
bool percent_self; /**< --percent-self */
bool percent_total; /**< --percent-total */
pid_t pid; /**< -p/--pid */
bool do_record; /**< --record */
bool do_report; /**< --report */
unsigned long long sample_time_ns; /**< --sample-time */
bool show_tid; /**< --tid */
struct vector *tids; /**< --tid-filter */
bool show_timestamp; /**< --timestamp */
unsigned long long min_duration; /**< --time-filter */
char *user; /**< -u/--user */
};

#endif // UTRACE_ENV_H
23 changes: 15 additions & 8 deletions eBPF_Supermarket/User_Function_Tracer/src/glob.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
#include <stdlib.h>
#include <string.h>

/**
* @brief regular glob match
*/
static bool glob_match(const char *text, const char *pattern) {
bool matched;
bool complemented;

while (*text != '\0' && *pattern != '\0') {
switch (*pattern) {
case '?':
case '?': // match any single char
++pattern;
++text;
break;
Expand All @@ -48,7 +51,7 @@ static bool glob_match(const char *text, const char *pattern) {
}
if (*pattern == '\0') return false;

char ch = *pattern; // ch may be ']' or '-', just treat it normally
char ch = *pattern; // `ch` may be ']' or '-', just treat it normally
matched |= (ch == *text);
++pattern;

Expand Down Expand Up @@ -108,14 +111,18 @@ static bool glob_match(const char *text, const char *pattern) {
bool glob_match_ext(const char *text, const char *pattern) {
char *dup_pattern = strdup(pattern);
char *glob_pattern = strtok(dup_pattern, ",");

bool matched = false;
while (glob_pattern) {
if (glob_match(text, glob_pattern)) {
matched = true;
break;
if (!glob_pattern) { // pattern is empty
matched = !strlen(text);
} else {
while (glob_pattern) { // split `pattern` by ',', and try to match each regular glob_pattern
// with `text`
if (glob_match(text, glob_pattern)) {
matched = true;
break;
}
glob_pattern = strtok(NULL, ",");
}
glob_pattern = strtok(NULL, ",");
}
free(dup_pattern);
return matched;
Expand Down
Loading