Skip to content

WIP - Add debug logging #21

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 12 additions & 0 deletions addr2line.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ func workload(context *Context, insert_func ins_f) {
rs, _ := context.a2l.Resolve(e.Addr2ln_offset)
context.mu.Unlock()
if len(rs) == 0 {
Debug("workload: ", e.Query_str, "NONE")
qready = fmt.Sprintf(e.Query_str, "NONE")
}
for _, a := range rs {
Debug("workload: ", e.Query_str, filepath.Clean(a.File))
qready = fmt.Sprintf(e.Query_str, filepath.Clean(a.File))
if a.Function == strings.ReplaceAll(e.Addr2ln_name, "sym.", "") {
break
Expand All @@ -135,20 +137,28 @@ func Generate_Query_Str(Q_WL *Workload) error {
switch arg := (*Q_WL).Query_args.(type) {
case Insert_Instance_Args:
(*Q_WL).Query_str = fmt.Sprintf(Query_fmts[0], arg.Version, arg.Patchlevel, arg.Sublevel, arg.Extraversion, arg.Note)
Debug("Generate_Query_Str: Insert_Instance_Args: ", (*Q_WL).Query_str)
case Insert_Config_Args:
(*Q_WL).Query_str = fmt.Sprintf(Query_fmts[1], arg.Config_key, arg.Config_val, arg.Instance_no)
Debug("Generate_Query_Str: Insert_Config_Args: ", (*Q_WL).Query_str)
case Insert_Files_Ind_Args:
(*Q_WL).Query_str = fmt.Sprintf(Query_fmts[2], arg.Id)
Debug("Generate_Query_Str: Insert_Files_Ind_Args: ", (*Q_WL).Query_str)
case Insert_Symbols_Ind_Args:
(*Q_WL).Query_str = fmt.Sprintf(Query_fmts[3], arg.Id)
Debug("Generate_Query_Str: Insert_Symbols_Ind_Args: ", (*Q_WL).Query_str)
case Insert_Tags_Ind_Args:
(*Q_WL).Query_str = fmt.Sprintf(Query_fmts[4], arg.Id)
Debug("Generate_Query_Str: Insert_Tags_Ind_Args: ", (*Q_WL).Query_str)
case Insert_Symbols_Files_Args:
(*Q_WL).Query_str = fmt.Sprintf(Query_fmts[5], arg.Id, arg.Symbol_Name, arg.Symbol_Offset, arg.Symbol_Type)
Debug("Generate_Query_Str: Insert_Symbols_Files_Args: ", (*Q_WL).Query_str)
case Insert_Xrefs_Args:
(*Q_WL).Query_str = fmt.Sprintf(Query_fmts[6], arg.Caller_Offset, arg.Callee_Offset, arg.Id, arg.Source_line, arg.Calling_Offset)
Debug("Generate_Query_Str: Insert_Xrefs_Args: ", (*Q_WL).Query_str)
case Insert_Tags_Args:
(*Q_WL).Query_str = fmt.Sprintf(Query_fmts[7], arg.addr2line_prefix)
Debug("Generate_Query_Str: Insert_Tags_Args: ", (*Q_WL).Query_str)
default:
err = errors.New("GENERATE_QUERY: Unknown workload argument")
}
Expand All @@ -162,10 +172,12 @@ func query_mgmt(ctx *Context, Q_WL *Workload) error {
case GENERATE_QUERY:
err = Generate_Query_Str(Q_WL)
case EXECUTE_QUERY_ONLY:
Debug("query_mgmt: Pushing ", fmt.Sprintf("%#v", *Q_WL))
(*ctx).ch_workload <- *Q_WL
case GENERATE_QUERY_AND_EXECUTE, GENERATE_QUERY_AND_EXECUTE_W_A2L:
err = Generate_Query_Str(Q_WL)
if err == nil {
Debug("query_mgmt: Pushing ", fmt.Sprintf("%#v", *Q_WL))
(*ctx).ch_workload <- *Q_WL
}
default:
Expand Down
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func cmd_line_item_init() []cmd_line_items {
push_cmd_line_item("-d", "Forces to use a specified DB DSN", true, func_DBDSN, &res)
push_cmd_line_item("-n", "Forecs use specified note (default 'upstream')", true, func_Note, &res)
push_cmd_line_item("-c", "Checks dependencies", false, func_check, &res)
push_cmd_line_item("--debug", "Enable debug mode", false, func_Debug, &res)
push_cmd_line_item("-h", "This Help", false, func_help, &res)

return res
Expand Down Expand Up @@ -126,6 +127,11 @@ func func_Note(conf *configuration, note []string) error {
return nil
}

func func_Debug(conf *configuration, dummy []string) error {
debugEnabled = true
return nil
}

func func_check(conf *configuration, dummy []string) error {
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func Connect_db(t *Connect_token) *sql.DB {
// Executes insert queries
func Insert_data(context *Context, query string) {

Debug("Insert_data: ", query)
_, err := (*context).DB.Exec(query)
if err != nil {
fmt.Println("##################################################")
Expand All @@ -88,6 +89,7 @@ func Insert_data(context *Context, query string) {
func Insert_datawID(context *Context, query string) int {
var res int

Debug("Insert_datawID", query)
rows, err := (*context).DB.Query(query)
if err != nil {
fmt.Println("##################################################")
Expand Down
49 changes: 49 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2023 Red Hat, Inc.
* SPDX-License-Identifier: GPL-2.0-or-later
*/

package main

import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
)

var (
debugEnabled = false

InfoLogger *log.Logger
DebugLogger *log.Logger
)

func init() {
logFile, err := os.OpenFile(filepath.Base(os.Args[0])+".log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
if err != nil {
log.Fatal(err)
}
log.SetOutput(logFile)
DebugLogger = log.New(logFile, "DEBUG: ", log.Ldate|log.Ltime)
InfoLogger = log.New(logFile, "INFO: ", log.Ldate|log.Ltime)
}

func Debug(v ...any) {
if debugEnabled {
DebugLogger.Println(v...)
}
}

func Info(v ...any) {
InfoLogger.Println(v...)
}

func FormatStruct(v any) string {
b, err := json.MarshalIndent(v, "=", " ")
if err != nil {
log.Fatal(err)
}
return fmt.Sprintf(string(b))
}
20 changes: 11 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ func main() {
strip(conf.StripBin, conf.LinuxWDebug, conf.LinuxWODebug)
t := Connect_token{conf.DBDriver, conf.DBDSN}
context := A2L_resolver__init(conf.LinuxWDebug, Connect_db(&t), false)

config, _ := get_FromFile(conf.KConfig_fn)
makefile, _ := get_FromFile(conf.KMakefile)
v, err := get_version(makefile)
if err != nil {
panic(err)
}
wl = &Workload{Workload_type: GENERATE_QUERY, Query_args: Insert_Instance_Args{v.Version, v.Patchlevel, v.Sublevel, v.Extraversion, conf.Note}}
query_mgmt(context, wl)
id = Insert_datawID(context, (*wl).Query_str)

if conf.Mode&(ENABLE_VERSION_CONFIG) != 0 {
config, _ := get_FromFile(conf.KConfig_fn)
makefile, _ := get_FromFile(conf.KMakefile)
v, err := get_version(makefile)
if err != nil {
panic(err)
}
wl = &Workload{Workload_type: GENERATE_QUERY, Query_args: Insert_Instance_Args{v.Version, v.Patchlevel, v.Sublevel, v.Extraversion, conf.Note}}
query_mgmt(context, wl)
id = Insert_datawID(context, (*wl).Query_str)
kconfig := parse_config(config)

fmt.Println("store config")
Expand Down
2 changes: 2 additions & 0 deletions r2tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func Getxrefs(r2p *r2.Pipe, current uint64, indcall []uint64, funcs []func_data,
if error != nil {
fmt.Printf("Error while parsing data: %s", error)
}
Debug("Getxrefs:", FormatStruct(&xrefs))

blocs := get_func_space(r2p, current, funcs)
for _, ic := range indcall {
Expand Down Expand Up @@ -396,6 +397,7 @@ func get_all_funcdata(r2p *r2.Pipe) []func_data {
}
}
sort.SliceStable(functions, func(i, j int) bool { return functions[i].Offset < functions[j].Offset })
Debug("get_all_funcdata:", FormatStruct(&functions))
return functions
}

Expand Down