Skip to content

Commit 326bbba

Browse files
committed
separate
1 parent 0a437ec commit 326bbba

File tree

8 files changed

+140
-93
lines changed

8 files changed

+140
-93
lines changed

librz/bin/pdb/pdb.c

+65-40
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,15 @@
88

99
#include "pdb.h"
1010

11-
/**
12-
* \brief Prints out types in a default format "idpi" command
13-
*
14-
* \param pdb pdb structure for printing function
15-
* \param types List of types
16-
*/
17-
static void print_types_regular(RzTypeDB *db, const RzPdb *pdb, const RzList *types) {
18-
rz_return_if_fail(pdb);
11+
static char *pdb_type_as_string_regular(const RzTypeDB *db, const RzPdb *pdb, const RzList *types) {
12+
rz_return_val_if_fail(pdb && db, NULL);
1913
if (!types) {
2014
eprintf("there is nothing to print!\n");
2115
}
2216
RzListIter *it;
2317
RzBaseType *type;
18+
RzStrBuf *buf = rz_strbuf_new(NULL);
2419
rz_list_foreach (types, it, type) {
25-
RzStrBuf *buf = rz_strbuf_new(NULL);
2620
switch (type->kind) {
2721
case RZ_BASE_TYPE_KIND_STRUCT: {
2822
rz_strbuf_appendf(buf, "struct %s { \n", type->name);
@@ -62,22 +56,22 @@ static void print_types_regular(RzTypeDB *db, const RzPdb *pdb, const RzList *ty
6256
default:
6357
break;
6458
}
65-
rz_cons_printf("%s\n", rz_strbuf_get(buf));
66-
rz_strbuf_free(buf);
6759
}
60+
char *str = strdup(rz_strbuf_get(buf));
61+
rz_strbuf_free(buf);
62+
return str;
6863
}
6964

70-
/**
71-
* \brief Prints out types in a json format - "idpij" command
72-
*
73-
* \param pdb pdb structure for printing function
74-
* \param types List of types
75-
*/
76-
static void print_types_json(RzTypeDB *db, const RzPdb *pdb, PJ *pj, const RzList *types) {
77-
rz_return_if_fail(pdb && types && pj);
65+
static char *pdb_type_as_string_json(const RzTypeDB *db, const RzPdb *pdb, const RzList *types) {
66+
rz_return_val_if_fail(db && pdb && types, NULL);
7867

7968
RzListIter *it;
8069
RzBaseType *type;
70+
PJ *pj = pj_new();
71+
if (!pj) {
72+
return NULL;
73+
}
74+
pj_o(pj);
8175
rz_list_foreach (types, it, type) {
8276
pj_o(pj);
8377
switch (type->kind) {
@@ -130,65 +124,90 @@ static void print_types_json(RzTypeDB *db, const RzPdb *pdb, PJ *pj, const RzLis
130124
}
131125
pj_end(pj);
132126
}
127+
pj_end(pj);
128+
char *str = strdup(pj_string(pj));
129+
pj_free(pj);
130+
return str;
133131
}
134132

135133
/**
136-
* \brief Prints out all the type information in regular,json or pf format
137-
*
138-
* \param pdb PDB information
134+
* \brief return the output text for types in PDB
135+
* \param db RzTypeDB
136+
* \param pdb PDB instance
139137
* \param mode printing mode
138+
* \return string of pdb types
140139
*/
141-
RZ_API void rz_bin_pdb_print_types(RzTypeDB *db, const RzPdb *pdb, PJ *pj, const int mode) {
140+
RZ_API RZ_OWN char *rz_bin_pdb_types_as_string(RZ_NONNULL const RzTypeDB *db, RZ_NONNULL const RzPdb *pdb, const RzOutputMode mode) {
141+
rz_return_val_if_fail(db && pdb, NULL);
142142
TpiStream *stream = pdb->s_tpi;
143-
144143
if (!stream) {
145144
eprintf("There is no tpi stream in current pdb\n");
146-
return;
145+
return NULL;
147146
}
148147
switch (mode) {
149-
case 'd':
150-
print_types_regular(db, pdb, stream->print_type);
151-
return;
152-
case 'j':
153-
print_types_json(db, pdb, pj, stream->print_type);
154-
return;
148+
case RZ_OUTPUT_MODE_STANDARD:
149+
return pdb_type_as_string_regular(db, pdb, stream->print_type);
150+
case RZ_OUTPUT_MODE_JSON:
151+
return pdb_type_as_string_json(db, pdb, stream->print_type);
152+
default:
153+
return NULL;
155154
}
156155
}
157156

158-
RZ_API void rz_bin_pdb_print_gvars(RzPdb *pdb, ut64 img_base, PJ *pj, int format) {
157+
/**
158+
* \brief return the output text for global symbols in PDB
159+
*
160+
* \param pdb PDB instance
161+
* \param img_base image base addr
162+
* \param mode print mode
163+
* \return string of pdb symbols
164+
*/
165+
RZ_API RZ_OWN char *rz_bin_pdb_gvars_as_string(RZ_NONNULL const RzPdb *pdb, const ut64 img_base, RzOutputMode mode) {
166+
rz_return_val_if_fail(pdb, NULL);
159167
PeImageSectionHeader *sctn_header = 0;
160168
GDataStream *gsym_data_stream = 0;
161169
PeStream *pe_stream = 0;
162170
OmapStream *omap_stream;
163171
GDataGlobal *gdata = 0;
164172
RzListIter *it = 0;
173+
PJ *pj = NULL;
165174
char *name;
166-
167-
if (format == 'j') {
175+
RzStrBuf *buf = rz_strbuf_new(NULL);
176+
if (!buf) {
177+
return NULL;
178+
}
179+
if (mode == RZ_OUTPUT_MODE_JSON) {
180+
pj = pj_new();
181+
if (!pj) {
182+
rz_strbuf_free(buf);
183+
return NULL;
184+
}
185+
pj_o(pj);
168186
pj_ka(pj, "gvars");
169187
}
170188
gsym_data_stream = pdb->s_gdata;
171189
pe_stream = pdb->s_pe;
172190
omap_stream = pdb->s_omap;
173191
if (!pe_stream) {
174-
return;
192+
rz_strbuf_free(buf);
193+
return NULL;
175194
}
176195
rz_list_foreach (gsym_data_stream->global_list, it, gdata) {
177196
sctn_header = rz_list_get_n(pe_stream->sections_hdrs, (gdata->segment - 1));
178197
if (sctn_header) {
179198
name = rz_bin_demangle_msvc(gdata->name);
180199
name = (name) ? name : strdup(gdata->name);
181-
switch (format) {
182-
case 'j': // JSON
200+
switch (mode) {
201+
case RZ_OUTPUT_MODE_JSON: // JSON
183202
pj_o(pj);
184203
pj_kN(pj, "address", (img_base + omap_remap(omap_stream, gdata->offset + sctn_header->virtual_address)));
185204
pj_kN(pj, "symtype", gdata->symtype);
186205
pj_ks(pj, "section_name", sctn_header->name);
187206
pj_ks(pj, "gdata_name", name);
188207
pj_end(pj);
189208
break;
190-
case 'd':
191-
rz_cons_printf("0x%08" PFMT64x " %d %.*s %s\n",
209+
case RZ_OUTPUT_MODE_STANDARD:
210+
rz_strbuf_appendf(buf, "0x%08" PFMT64x " %d %.*s %s\n",
192211
(ut64)(img_base + omap_remap(omap_stream, gdata->offset + sctn_header->virtual_address)),
193212
gdata->symtype, PDB_SIZEOF_SECTION_NAME, sctn_header->name, name);
194213
break;
@@ -198,9 +217,15 @@ RZ_API void rz_bin_pdb_print_gvars(RzPdb *pdb, ut64 img_base, PJ *pj, int format
198217
free(name);
199218
}
200219
}
201-
if (format == 'j') {
220+
if (mode == RZ_OUTPUT_MODE_JSON) {
221+
pj_end(pj);
202222
pj_end(pj);
223+
rz_strbuf_append(buf, pj_string(pj));
224+
pj_free(pj);
203225
}
226+
char *str = strdup(rz_strbuf_get(buf));
227+
rz_strbuf_free(buf);
228+
return str;
204229
}
205230

206231
static bool parse_pdb_stream(RzPdb *pdb, MsfStream *stream) {

librz/bin/pdb/pdb.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ typedef struct rz_pdb_t {
112112

113113
RZ_API RZ_OWN RzPdb *rz_bin_pdb_parse_from_file(RZ_NONNULL const char *filename);
114114
RZ_API RZ_OWN RzPdb *rz_bin_pdb_parse_from_buf(RZ_NONNULL const RzBuffer *buf);
115-
RZ_API void rz_bin_pdb_print_types(RzTypeDB *db, const RzPdb *pdb, PJ *pj, const int mode);
116-
RZ_API void rz_bin_pdb_print_gvars(RzPdb *pdb, ut64 img_base, PJ *pj, int format);
115+
RZ_API RZ_OWN char *rz_bin_pdb_types_as_string(RZ_NONNULL const RzTypeDB *db, RZ_NONNULL const RzPdb *pdb, const RzOutputMode mode);
116+
RZ_API RZ_OWN char *rz_bin_pdb_gvars_as_string(RZ_NONNULL const RzPdb *pdb, const ut64 img_base, RzOutputMode mode);
117117
RZ_API void rz_bin_pdb_free(RzPdb *pdb);
118118

119119
// OMAP

librz/core/cbin.c

+1-45
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <rz_config.h>
77
#include "rz_util.h"
88
#include "rz_util/rz_time.h"
9-
#include "../bin/pdb/pdb.h"
109

1110
#define is_in_range(at, from, sz) ((at) >= (from) && (at) < ((from) + (sz)))
1211

@@ -2111,49 +2110,6 @@ RZ_API void rz_core_bin_print_source_line_info(RzCore *core, const RzBinSourceLi
21112110
}
21122111
}
21132112

2114-
RZ_API bool rz_core_pdb_info(RzCore *core, const char *file, PJ *pj, int mode) {
2115-
rz_return_val_if_fail(core && file, false);
2116-
2117-
ut64 baddr = rz_config_get_i(core->config, "bin.baddr");
2118-
if (core->bin->cur && core->bin->cur->o && core->bin->cur->o->opts.baseaddr) {
2119-
baddr = core->bin->cur->o->opts.baseaddr;
2120-
} else {
2121-
eprintf("Warning: Cannot find base address, flags will probably be misplaced\n");
2122-
}
2123-
2124-
RzPdb *pdb = rz_bin_pdb_parse_from_file(file);
2125-
if (!pdb) {
2126-
return false;
2127-
}
2128-
2129-
switch (mode) {
2130-
case RZ_MODE_SET:
2131-
rz_core_cmd0(core, ".iP*");
2132-
return true;
2133-
case RZ_MODE_JSON:
2134-
mode = 'j';
2135-
break;
2136-
case RZ_MODE_PRINT:
2137-
mode = 'd';
2138-
break;
2139-
default:
2140-
break;
2141-
}
2142-
if (mode == 'j') {
2143-
pj_o(pj);
2144-
}
2145-
// Save compound types into types database
2146-
rz_parse_pdb_types(core->analysis->typedb, pdb);
2147-
rz_bin_pdb_print_types(core->analysis->typedb, pdb, pj, mode);
2148-
rz_bin_pdb_print_gvars(pdb, baddr, pj, mode);
2149-
rz_bin_pdb_free(pdb);
2150-
2151-
if (mode == 'j') {
2152-
pj_end(pj);
2153-
}
2154-
return true;
2155-
}
2156-
21572113
static int bin_main(RzCore *r, RzBinFile *binfile, PJ *pj, int mode, int va) {
21582114
if (!binfile) {
21592115
return false;
@@ -4354,7 +4310,7 @@ RZ_API int rz_core_bin_info(RzCore *core, int action, PJ *pj, int mode, int va,
43544310
ret &= binfile ? bin_dwarf(core, binfile, pj, mode) : false;
43554311
}
43564312
if ((action & RZ_CORE_BIN_ACC_PDB)) {
4357-
ret &= rz_core_pdb_info(core, core->bin->file, pj, mode);
4313+
ret &= rz_core_pdb_info(core, core->bin->file, mode);
43584314
}
43594315
if ((action & RZ_CORE_BIN_ACC_ENTRIES)) {
43604316
ret &= bin_entry(core, pj, mode, loadaddr, va, false);

librz/core/cmd_info.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,13 @@ RZ_IPI int rz_cmd_info(void *data, const char *input) {
947947
free(filename);
948948
break;
949949
}
950-
rz_core_pdb_info(core, filename, pj, mode);
950+
// sacrifice
951+
if (mode == RZ_MODE_JSON) {
952+
mode = RZ_OUTPUT_MODE_JSON;
953+
} else if (mode == RZ_MODE_PRINT) {
954+
mode = RZ_OUTPUT_MODE_STANDARD;
955+
}
956+
rz_core_pdb_info(core, filename, mode);
951957
free(filename);
952958
break;
953959
case '?':

librz/core/cpdb.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-FileCopyrightText: 2021 Basstorm <[email protected]>
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
#include <rz_core.h>
5+
#include "rz_util.h"
6+
#include "../bin/pdb/pdb.h"
7+
8+
RZ_API void rz_core_bin_pdb_types_print(RZ_NONNULL const RzTypeDB *db, RZ_NONNULL const RzPdb *pdb, const RzOutputMode mode) {
9+
rz_return_if_fail(db && pdb);
10+
char *str = rz_bin_pdb_types_as_string(db, pdb, mode);
11+
if (!str) {
12+
return;
13+
}
14+
rz_cons_print(str);
15+
}
16+
17+
RZ_API void rz_core_bin_pdb_gvars_print(RZ_NONNULL const RzPdb *pdb, const ut64 img_base, const RzOutputMode mode) {
18+
rz_return_if_fail(pdb);
19+
char *str = rz_bin_pdb_gvars_as_string(pdb, img_base, mode);
20+
if (!str) {
21+
return;
22+
}
23+
rz_cons_print(str);
24+
}
25+
26+
RZ_API bool rz_core_pdb_info(RzCore *core, const char *file, RzOutputMode mode) {
27+
rz_return_val_if_fail(core && file, false);
28+
29+
ut64 baddr = rz_config_get_i(core->config, "bin.baddr");
30+
if (core->bin->cur && core->bin->cur->o && core->bin->cur->o->opts.baseaddr) {
31+
baddr = core->bin->cur->o->opts.baseaddr;
32+
} else {
33+
eprintf("Warning: Cannot find base address, flags will probably be misplaced\n");
34+
}
35+
36+
RzPdb *pdb = rz_bin_pdb_parse_from_file(file);
37+
if (!pdb) {
38+
return false;
39+
}
40+
41+
// Save compound types into types database
42+
rz_parse_pdb_types(core->analysis->typedb, pdb);
43+
rz_core_bin_pdb_types_print(core->analysis->typedb, pdb, mode);
44+
rz_core_bin_pdb_gvars_print(pdb, baddr, mode);
45+
rz_bin_pdb_free(pdb);
46+
return true;
47+
}

librz/core/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rz_core_sources = [
1313
'cconfig.c',
1414
'cdebug.c',
1515
'cdwarf.c',
16+
'cpdb.c',
1617
'chash.c',
1718
'cheap.c',
1819
'cio.c',

librz/core/windows_heap.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <windows_heap.h>
77
#include "..\..\debug\p\native\maps\windows_maps.h"
88
#include "..\..\bin\pdb\pdb_downloader.h"
9+
#include "..\..\bin\pdb\pdb.h"
910

1011
/*
1112
* Viewer discretion advised: Spaghetti code ahead
@@ -333,14 +334,25 @@ static bool GetHeapGlobalsOffset(RzDebug *dbg, HANDLE h_proc) {
333334
}
334335

335336
// Get ntdll.dll PDB info and parse json output
336-
PJ *pj = pj_new();
337-
if (!rz_core_pdb_info(core, pdb_path, pj, RZ_MODE_JSON)) {
338-
pj_free(pj);
337+
RzPdb *pdb = rz_bin_pdb_parse_from_file(pdb_path);
338+
if (!pdb) {
339339
free(pdb_path);
340340
goto fail;
341341
}
342+
342343
free(pdb_path);
343-
char *j = pj_drain(pj);
344+
ut64 baddr = rz_config_get_i(core->config, "bin.baddr");
345+
if (core->bin->cur && core->bin->cur->o && core->bin->cur->o->opts.baseaddr) {
346+
baddr = core->bin->cur->o->opts.baseaddr;
347+
} else {
348+
eprintf("Warning: Cannot find base address, flags will probably be misplaced\n");
349+
}
350+
char *j = rz_bin_pdb_gvars_as_string(pdb, baddr, RZ_OUTPUT_MODE_JSON);
351+
if (!j) {
352+
rz_bin_pdb_free(pdb);
353+
goto fail;
354+
}
355+
rz_bin_pdb_free(pdb);
344356
RzJson *json = rz_json_parse(j);
345357
if (!json) {
346358
RZ_LOG_ERROR("rz_core_pdb_info returned invalid JSON");

librz/include/rz_core.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ RZ_API RZ_OWN char *rz_core_bin_super_build_flag_name(RZ_NONNULL RzBinClass *cls
850850
RZ_API RZ_OWN char *rz_core_bin_method_build_flag_name(RZ_NONNULL RzBinClass *cls, RZ_NONNULL RzBinSymbol *meth);
851851
RZ_API RZ_OWN char *rz_core_bin_field_build_flag_name(RZ_NONNULL RzBinClass *cls, RZ_NONNULL RzBinField *field);
852852
RZ_API char *rz_core_bin_method_flags_str(ut64 flags, int mode);
853-
RZ_API bool rz_core_pdb_info(RzCore *core, const char *file, PJ *pj, int mode);
853+
RZ_API bool rz_core_pdb_info(RzCore *core, const char *file, RzOutputMode mode);
854854
RZ_API RzCmdStatus rz_core_bin_plugins_print(RzBin *bin, RzCmdStateOutput *state);
855855

856856
typedef enum {

0 commit comments

Comments
 (0)