Skip to content

Commit

Permalink
Fix multiple issues found by covscan
Browse files Browse the repository at this point in the history
  • Loading branch information
ryncsn committed May 20, 2020
1 parent bd9e0c3 commit 2ad8f16
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 36 deletions.
20 changes: 14 additions & 6 deletions src/backend/ftrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ static void set_trace(const char* value, const char* path) {
char filename[FTRACE_MAX_PATH];
sprintf(filename, "%s/%s", TRACE_BASE, path);
FILE *file = fopen(filename, "w");
if (!file) {
log_error("Failed to open %s\n", filename);
return;
}
fprintf(file, "%s", value);
fclose(file);
}
Expand Down Expand Up @@ -116,7 +120,7 @@ static struct Tracenode* __process_stacktrace() {
int callsite_len = 0;
callsite_arg = ftrace_line + strlen(FTRACE_STACK_TRACE_SIGN);
callsite_len = strlen(callsite_arg);
strcpy(callsite, callsite_arg);
strncpy(callsite, callsite_arg, MAX_SYMBOL);
callsite[callsite_len - 1] = '\0';

// Process next traceline
Expand Down Expand Up @@ -219,6 +223,7 @@ static void do_ftrace_process() {
last = pre_last;
pre_last = NULL;
}

if (strncmp(last + 2, FTRACE_STACK_TRACE_EVENT, strlen(FTRACE_STACK_TRACE_EVENT) - 1) == 0) {
if (!task) {
log_debug("Got unexpected stacktrace!\n");
Expand All @@ -240,6 +245,11 @@ static void do_ftrace_process() {
event_info = pre_last;
pid_arg = pre_last;

if (!task_info || !event_info || !pid_arg) {
log_warn("Invalid line:\n%s\n", ftrace_line);
return;
}

while(pid_arg[0] != '-') {
pid_arg --;
if (pid_arg <= ftrace_line) {
Expand All @@ -252,6 +262,9 @@ static void do_ftrace_process() {
pid_arg[0] = '\0';
pid_arg++;
sscanf(pid_arg, "%u", &pid);
} else {
log_warn("Invalid line:\n%s\n", ftrace_line);
return;
}

event_info[0] = '\0';
Expand All @@ -261,11 +274,6 @@ static void do_ftrace_process() {
task_info++;
}

if (!task_info || !event_info || !pid_arg) {
log_warn("Invalid line:\n%s\n", ftrace_line);
return;
}

// char *event, *callsite;
event = strtok(event_info, " ");
// callsite = strtok(NULL, " ");
Expand Down
2 changes: 1 addition & 1 deletion src/backend/perf-events.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ static int perf_debug_handler(struct PerfEventRing *ring, const unsigned char* h

static int perf_handle_lost_event(const unsigned char* header, int cpu, char *event_name) {
struct perf_lost_events *body = (struct perf_lost_events*)header;
log_warn("Lost %d %s events on CPU %d!\n", body->lost, event_name, cpu);
log_warn("Lost %lu %s events on CPU %d!\n", body->lost, event_name, cpu);
return 0;
}

Expand Down
3 changes: 2 additions & 1 deletion src/backend/perf-sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ int perf_do_load_event_info(struct PerfEvent *event)
}
} else {
log_error("Failed to verify event name of %s\n", event->name);
return 1;
ret = 1;
goto out;
}

fgets(fmt_buffer, 1024, fmt_file);
Expand Down
4 changes: 1 addition & 3 deletions src/memstrack.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ static void init_fds(void) {
}

static void loop(void) {
int ret;

switch (ret = poll(m_pollfds, m_pollfd_num, 250)) {
switch (poll(m_pollfds, m_pollfd_num, 250)) {
// Resizing the terminal causes poll() to return -1
case -1:
default:
Expand Down
19 changes: 10 additions & 9 deletions src/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ int print_slab_usage()
log_info("%17s: %.1lf MB\n", entry->name, size_in_mb);
}

fclose(file);
return 0;
}

Expand Down Expand Up @@ -167,23 +168,23 @@ int parse_zone_info(struct zone_info **zone)
strncpy((*zone)->name, zone_name, ZONENAMELEN);

log_debug("Trying to parse node %d zone %s\n", node, zone_name, (*zone)->start_pfn, (*zone)->spanned);
if (parse_keyword(1, file, line, "free", "Node", "free %d", &(*zone)->free))
if (parse_keyword(1, file, line, "free", "Node", "free %lu", &(*zone)->free))
continue;
if (parse_keyword(0, file, line, "min", NULL, "min %d", &(*zone)->min))
if (parse_keyword(0, file, line, "min", NULL, "min %lu", &(*zone)->min))
continue;
if (parse_keyword(0, file, line, "low", NULL, "low %d", &(*zone)->low))
if (parse_keyword(0, file, line, "low", NULL, "low %lu", &(*zone)->low))
continue;
if (parse_keyword(0, file, line, "high", NULL, "low %d", &(*zone)->high))
if (parse_keyword(0, file, line, "high", NULL, "low %lu", &(*zone)->high))
continue;
if (parse_keyword(0, file, line, "spanned", NULL, "spanned %d", &(*zone)->spanned))
if (parse_keyword(0, file, line, "spanned", NULL, "spanned %lu", &(*zone)->spanned))
continue;
if (parse_keyword(0, file, line, "present", NULL, "present %d", &(*zone)->present))
if (parse_keyword(0, file, line, "present", NULL, "present %lu", &(*zone)->present))
continue;
if (parse_keyword(0, file, line, "managed", NULL, "managed %d", &(*zone)->managed))
if (parse_keyword(0, file, line, "managed", NULL, "managed %lu", &(*zone)->managed))
continue;
if (parse_keyword(1, file, line, "start_pfn", "Node", "start_pfn: %d", &(*zone)->start_pfn))
if (parse_keyword(1, file, line, "start_pfn", "Node", "start_pfn: %lu", &(*zone)->start_pfn))
continue;
log_debug("Page span is start: %d, spanned %d\n", node, zone_name, (*zone)->start_pfn, (*zone)->spanned);
log_debug("Page span is start: %lu, spanned %lu\n", (*zone)->start_pfn, (*zone)->spanned);

zone = &(*zone)->next_zone;
}
Expand Down
4 changes: 2 additions & 2 deletions src/report.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static void report_module_summary(void) {

for (int i = 0; i < module_map.size; ++i) {
log_info(
"Module %s using %.1lfMB (%d pages), peak allocation %.1lfMB (%d pages)\n",
"Module %s using %.1lfMB (%ld pages), peak allocation %.1lfMB (%ld pages)\n",
modules[i]->name,
modules[i]->tracenode.record->pages_alloc * ((double)page_size) / 1024 / 1024,
modules[i]->tracenode.record->pages_alloc,
Expand Down Expand Up @@ -66,7 +66,7 @@ static void report_task_summary (void) {
tasks = collect_tasks_sorted(0);
for (int i = 0; i < task_map.size; ++i) {
log_info(
"Task %s (%u) using %u pages, peak usage %u pages\n",
"Task %s (%ld) using %ld pages, peak usage %ld pages\n",
tasks[i]->task_name, tasks[i]->pid,
tasks[i]->tracenode.record->pages_alloc,
tasks[i]->tracenode.record->pages_alloc_peak);
Expand Down
38 changes: 27 additions & 11 deletions src/tracing.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ static char* get_process_name_by_pid(const int pid)
size = fread(buf, sizeof(char), TASK_NAME_LEN_MAX, f);
if (size > 0){
buf[size - 1] = '\0';
} else {
// TODO: empty name?
buf[0] = '\0';
}
fclose(f);
} else {
Expand Down Expand Up @@ -294,6 +297,10 @@ static void record_page_free(unsigned long pfn, unsigned long nr_pages) {

static void do_update_record(struct Tracenode *tracenode, struct PageEvent *pevent) {
if (pevent->pages_alloc > 0) {
if (!tracenode) {
log_debug("BUG: Page alloc event with NULL tracenode\n");
return;
}
record_page_alloc(tracenode, pevent->pfn, pevent->pages_alloc);
} else if (pevent->pages_alloc < 0) {
record_page_free(pevent->pfn, 0 - pevent->pages_alloc);
Expand Down Expand Up @@ -427,6 +434,11 @@ void load_kallsyms() {
FILE *proc_kallsyms = fopen("/proc/kallsyms", "r");
char read_buf[4096];

if (!proc_kallsyms) {
log_error("Failed to open /proc/kallsyms\n");
return;
}

while(fgets(read_buf, 4096, proc_kallsyms)) {
unsigned long long addr;
char *addr_arg = strtok(read_buf, " \t");
Expand Down Expand Up @@ -674,10 +686,15 @@ struct Tracenode **collect_tracenodes_sorted(struct Tracenode *root, int *count,
struct Tracenode **nodes, **tail;

*count = count_tracenodes(root);
tail = nodes = malloc(*count * sizeof(struct Tracenode*));
tail = nodes = calloc(*count * sizeof(struct Tracenode*), 1);
for_each_tracenode(root, tracenode_iter_collect, &tail);

for (int i = 0; i < *count; ++i) {
if (!nodes[i]) {
*count = i;
break;
}

if (shallow)
populate_tracenode_shallow(nodes[i]);
else
Expand Down Expand Up @@ -742,8 +759,8 @@ void print_tracenode_json(struct Tracenode* tracenode, void *blob) {
}
log_info("%s\"%s\": ", padding, get_tracenode_symbol(tracenode));
log_info("{\n");
log_info("%s \"pages_alloc\": %d", padding, tracenode->record->pages_alloc);
log_info(",\n%s \"pages_alloc_peak\": %d", padding, tracenode->record->pages_alloc_peak);
log_info("%s \"pages_alloc\": %ld", padding, tracenode->record->pages_alloc);
log_info(",\n%s \"pages_alloc_peak\": %ld", padding, tracenode->record->pages_alloc_peak);

if (tracenode->children) {
log_info(",\n%s \"tracenodes\": {\n", padding);
Expand All @@ -768,9 +785,9 @@ void print_task_json(struct Task* task) {

log_info(" {\n");
log_info(" \"task_name\": \"%s\",\n", task->task_name);
log_info(" \"pid\" :\"%d\",\n", task->pid);
log_info(" \"pages_alloc\": %d,\n", task->tracenode.record->pages_alloc);
log_info(" \"pages_alloc_peak\": %d,\n", task->tracenode.record->pages_alloc_peak);
log_info(" \"pid\" :\"%ld\",\n", task->pid);
log_info(" \"pages_alloc\": %ld,\n", task->tracenode.record->pages_alloc);
log_info(" \"pages_alloc_peak\": %ld,\n", task->tracenode.record->pages_alloc_peak);
log_info(" \"tracenodes\": {\n");
if(to_tracenode(task)->children) {
nodes = collect_tracenodes_sorted(to_tracenode(task)->children, &counter, 1);
Expand All @@ -792,18 +809,17 @@ void print_tracenode(struct Tracenode* tracenode, int current_indent, int substa

log_info("%s", get_tracenode_symbol(tracenode));

log_info(" Pages: %d (peak: %d)\n",
log_info(" Pages: %ld (peak: %ld)\n",
tracenode->record->pages_alloc,
tracenode->record->pages_alloc_peak);

if (m_sort_peak)
page_limit = tracenode->record->pages_alloc_peak;
else if (m_sort_alloc)
else
page_limit = tracenode->record->pages_alloc;

if (throttle) {
if (throttle)
page_limit = page_limit * throttle / 100;
}

if (tracenode->children) {
struct Tracenode **nodes;
Expand All @@ -827,7 +843,7 @@ void print_task(struct Task* task) {
struct Tracenode **nodes;

indent = 2;
log_info("%s Pages: %d (peak: %d)\n",
log_info("%s Pages: %ld (peak: %ld)\n",
task->task_name,
tn->record->pages_alloc,
tn->record->pages_alloc_peak);
Expand Down
7 changes: 5 additions & 2 deletions src/tui.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ static void trace_refresh_tui() {

info = &line_info;

// TODO: only work when it's single thread
for (int task_n = 0; task_n < tasks_num; ++task_n) {
if (tui_print_tracenode(to_tracenode(sorted_tasks[task_n]), 0))
return;
Expand Down Expand Up @@ -272,6 +273,7 @@ void tui_init(void) {

void tui_update(void) {
int ch;
int ret;

if (ui_fds[0].revents & POLLIN) {
/* On UI event */
Expand Down Expand Up @@ -306,7 +308,8 @@ void tui_update(void) {

if (ui_fds[1].revents & POLLIN) {
uint64_t time;
read(ui_fds[1].fd, &time, sizeof(time));
update_ui(trace_win);
ret = read(ui_fds[1].fd, &time, sizeof(time));
if (ret)
update_ui(trace_win);
}
}
4 changes: 3 additions & 1 deletion src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ void insert_tree_node(
struct TreeNode *root = *root_p;
int result;

if (root == NULL)
if (root == NULL) {
*root_p = src;
return;
}

while (1) {
result = comp(root, key);
Expand Down

0 comments on commit 2ad8f16

Please sign in to comment.