Skip to content

Commit

Permalink
Add dir.sourcebase newabi + faster bin.dbginfo on macOS ##bin
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae committed Jun 22, 2024
1 parent be299a6 commit b42018f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 44 deletions.
102 changes: 58 additions & 44 deletions libr/bin/dbginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,59 +124,73 @@ R_API R_NULLABLE char *r_bin_addr2text(RBin *bin, ut64 addr, int origin) {
RBinDbgItem *di = r_bin_dbgitem_at (bin, addr);
if (!di) {
di = r_bin_dbgitem_api (bin, addr);
if (!di) {
return NULL;
}
}
if (di) {
char *res = NULL;
char *basename = strdup (r_file_basename (di->file));
// early optimization because mac's home is slow
char *res = NULL;
char *filename = strdup (di->file);
#if R2_USE_NEW_ABI
if (R_STR_ISNOTEMPTY (bin->srcdir_base) && r_str_startswith (filename, bin->srcdir_base)) {
char *fn = strdup (filename + strlen (bin->srcdir_base));
free (filename);
filename = fn;
}
#endif
char *basename = strdup (r_file_basename (di->file));
#if __APPLE__
// early optimization because mac's home is slow
if (r_str_startswith (filename, "/home")) {
// XXX
free (filename);
filename = strdup (basename);
}
#endif
#if 0
if (R_STR_ISEMPTY (bin->srcdir) && (di->file[0] == '/')) {
char *res = r_str_newf ("%s:%d:%d", basename, di->line, di->column);
r_bin_dbgitem_free (di);
return res;
}
if (R_STR_ISEMPTY (bin->srcdir) && (di->file[0] == '/')) {
char *res = r_str_newf ("%s:%d:%d", basename, di->line, di->column);
r_bin_dbgitem_free (di);
return res;
}
#endif
char *filename = strdup (di->file);
// check absolute path
if (!r_file_exists (di->file)) {
// check in current directory
if (r_file_exists (basename)) {
// check absolute path
if (!r_file_exists (filename)) {
// check in current directory
if (strcmp (filename, basename) && r_file_exists (basename)) {
free (filename);
filename = strdup (basename);
} else if (R_STR_ISNOTEMPTY (bin->srcdir)) {
char *nf = r_str_newf ("%s/%s", bin->srcdir, basename);
// check in srcdircurrent directory
if (r_file_exists (nf)) {
free (filename);
filename = strdup (basename);
} else if (R_STR_ISNOTEMPTY (bin->srcdir)) {
char *nf = r_str_newf ("%s/%s", bin->srcdir, basename);
// check in srcdircurrent directory
if (r_file_exists (nf)) {
free (filename);
filename = nf;
} else {
free (nf);
}
}
}
// out contains the contents of the slurped line
char *out = r_file_slurp_line (filename, di->line, 0);
if (origin) {
// filename + text or fullpath + text
if (di->column > 0) {
res = r_str_newf ("%s:%d:%d%s", (origin > 1)? di->file: basename,
di->line, di->column, r_str_get (out));
filename = nf;
} else {
res = r_str_newf ("%s:%d%s", (origin > 1)? di->file: basename,
di->line, r_str_get (out));
free (nf);
}
free (out);
}
}
// out contains the contents of the slurped line
char *out = r_file_slurp_line (filename, di->line, 0);
if (origin) {
// filename + text or fullpath + text
if (di->column > 0) {
res = r_str_newf ("%s:%d:%d%s", (origin > 1)? di->file: basename,
di->line, di->column, r_str_get (out));
} else {
// just the text from the file
free (res);
res = out;
res = r_str_newf ("%s:%d%s", (origin > 1)? di->file: basename,
di->line, r_str_get (out));
}
free (filename);
free (basename);
r_bin_dbgitem_free (di);
return res;
free (out);
} else {
// just the text from the file
free (res);
res = out;
}
return NULL;
free (filename);
free (basename);
r_bin_dbgitem_free (di);
return res;
}

R_API char *r_bin_addr2fileline(RBin *bin, ut64 addr) {
Expand Down
17 changes: 17 additions & 0 deletions libr/core/cconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,20 @@ static bool cb_dirsrc(void *user, void *data) {
return true;
}

#if R2_USE_NEW_ABI
static bool cb_dirsrc_base(void *user, void *data) {
RConfigNode *node = (RConfigNode*) data;
RCore *core = (RCore *)user;
free (core->bin->srcdir);
if (R_STR_ISNOTEMPTY (node->value)) {
core->bin->srcdir = strdup (node->value);
} else {
core->bin->srcdir = NULL;
}
return true;
}
#endif

static bool cb_cfgsanbox_grain(void *user, void *data) {
RConfigNode *node = (RConfigNode*) data;
if (strstr (node->value, "?")) {
Expand Down Expand Up @@ -3883,6 +3897,9 @@ R_API int r_core_config_init(RCore *core) {
SETPREF ("dir.plugins", path, "path to plugin files to be loaded at startup");
free (path);
}
#if R2_USE_NEW_ABI
SETCB ("dir.source.base", "", &cb_dirsrc_base, "path to trim out from the one in dwarf");
#endif
SETCB ("dir.source", "", &cb_dirsrc, "path to find source files");
SETPREF ("dir.types", "/usr/include", "default colon-separated list of paths to find C headers to cparse types");
SETPREF ("dir.libs", "", "specify path to find libraries to load when bin.libs=true");
Expand Down
3 changes: 3 additions & 0 deletions libr/include/r_bin.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ struct r_bin_t {
char strfilter; // string filtering
char *strpurge; // purge false positive strings
char *srcdir; // dir.source
#if R2_USE_NEW_ABI
char *srcdir_base; // dir.source.base
#endif
char *prefix; // bin.prefix
char *strenc;
ut64 filter_rules;
Expand Down

0 comments on commit b42018f

Please sign in to comment.