psrcgen: make new tool derived from pdwtags
This commit is contained in:
parent
8eafd61d4d
commit
6fc3366b3e
@ -155,6 +155,10 @@ set(pdwtags_SRCS pdwtags.c)
|
||||
add_executable(pdwtags ${pdwtags_SRCS})
|
||||
target_link_libraries(pdwtags dwarves)
|
||||
|
||||
set(psrcgen_SRCS psrcgen.c)
|
||||
add_executable(psrcgen ${psrcgen_SRCS})
|
||||
target_link_libraries(psrcgen dwarves)
|
||||
|
||||
set(pglobal_SRCS pglobal.c)
|
||||
add_executable(pglobal ${pglobal_SRCS})
|
||||
target_link_libraries(pglobal dwarves)
|
||||
|
437
pdwtags.c
437
pdwtags.c
@ -2,428 +2,71 @@
|
||||
SPDX-License-Identifier: GPL-2.0-only
|
||||
|
||||
Copyright (C) 2007-2016 Arnaldo Carvalho de Melo <acme@kernel.org>
|
||||
Copyright (C) 2022 Alibek Omarov <a1ba.omarov@gmail.com>
|
||||
*/
|
||||
|
||||
#include <argp.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dwarves.h"
|
||||
#include "dutil.h"
|
||||
|
||||
#define IGNORE_UPPER_DIRECTORY 1
|
||||
#define NO_WRITE_FILES 0
|
||||
|
||||
char *strncpy2(char *dest, const char *src, size_t n)
|
||||
{
|
||||
char *ret = strncpy(dest, src, n);
|
||||
dest[n] = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool endswith(const char *s1, const char *s2)
|
||||
{
|
||||
int len1 = strlen(s1);
|
||||
int len2 = strlen(s2);
|
||||
|
||||
if( len1 < len2 )
|
||||
return false;
|
||||
|
||||
return !strncmp(s1 + len1 - len2, s2, len2);
|
||||
}
|
||||
|
||||
static bool filename_looks_like_header(const char *s1)
|
||||
{
|
||||
return endswith(s1, ".h") || endswith(s1, ".H");
|
||||
}
|
||||
|
||||
static int startswith( const char *s1, const char *s2 )
|
||||
{
|
||||
int len2 = strlen(s2);
|
||||
|
||||
if( !strncmp(s1, s2, len2))
|
||||
return len2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mkdir_p(const char *path)
|
||||
{
|
||||
#if NO_WRITE_FILES
|
||||
return;
|
||||
#else
|
||||
char orig_dir[2048];
|
||||
getcwd(orig_dir, sizeof( orig_dir ));
|
||||
|
||||
for(const char *path2 = path; (path2 = strchr(path, '/')); path = path2 + 1)
|
||||
{
|
||||
char dir[128];
|
||||
|
||||
strncpy2(dir, path, path2 - path);
|
||||
|
||||
mkdir(dir, 0777);
|
||||
|
||||
chdir(dir);
|
||||
}
|
||||
|
||||
chdir(orig_dir);
|
||||
#endif
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
|
||||
typedef struct
|
||||
{
|
||||
struct list_head node;
|
||||
int linenum;
|
||||
char *include;
|
||||
struct tag *tag;
|
||||
struct cu *cu;
|
||||
} srcfile_line_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
struct list_head node;
|
||||
const char *filename;
|
||||
struct list_head lines;
|
||||
} srcfile_t;
|
||||
|
||||
LIST_HEAD(g_SourceFiles);
|
||||
|
||||
typedef struct include_t
|
||||
{
|
||||
struct list_head node;
|
||||
char *filename;
|
||||
struct include_t *parent;
|
||||
} include_t;
|
||||
|
||||
LIST_HEAD(g_Includes);
|
||||
|
||||
static srcfile_t *find_or_create_srcfile(const char *filename);
|
||||
static bool find_or_insert_line(srcfile_t *sourceFile, int linenum, struct tag *tag, struct cu *cu, const char *include);
|
||||
|
||||
static void include_detector_init( void )
|
||||
{
|
||||
include_t *ptr, *next;
|
||||
|
||||
list_for_each_entry_safe(ptr, next, &g_Includes, node)
|
||||
{
|
||||
list_del(&ptr->node);
|
||||
free(ptr->filename);
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
INIT_LIST_HEAD(&g_Includes);
|
||||
}
|
||||
|
||||
static void include_detector_push(const char *filename)
|
||||
{
|
||||
include_t *include;
|
||||
|
||||
include = malloc( sizeof( include_t ));
|
||||
include->filename = strdup( filename );
|
||||
include->parent = NULL;
|
||||
list_add_tail(&include->node, &g_Includes);
|
||||
}
|
||||
|
||||
static void include_detector_detect1(struct cu *cu)
|
||||
{
|
||||
include_t *inc1;
|
||||
|
||||
// for each include we find next duplicate
|
||||
// it PROBABLY means that everything in between
|
||||
// belongs to that include
|
||||
// list_for_each_entry(inc1, &g_Includes, node)
|
||||
// {
|
||||
// include_t *inc2;
|
||||
//
|
||||
// list_for_each_entry(inc2, inc1->node.next, node)
|
||||
// {
|
||||
// include_t *inc3;
|
||||
//
|
||||
// if( strcmp( inc1->filename, inc2->filename ))
|
||||
// continue;
|
||||
//
|
||||
// list_for_each_entry_reverse(inc3, inc2->node.prev, node)
|
||||
// {
|
||||
// if( inc1 == inc3 )
|
||||
// break;
|
||||
//
|
||||
// inc3->parent = inc1;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
list_for_each_entry(inc1, &g_Includes, node)
|
||||
{
|
||||
if( filename_looks_like_header( inc1->filename ))
|
||||
{
|
||||
srcfile_t *srcfile = find_or_create_srcfile(cu->name);
|
||||
|
||||
if( srcfile )
|
||||
find_or_insert_line(srcfile, -1, NULL, NULL, inc1->filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static srcfile_t *find_or_create_srcfile(const char *filename)
|
||||
{
|
||||
srcfile_t *srcfile;
|
||||
|
||||
filename += startswith( filename, "/" );
|
||||
|
||||
#if IGNORE_UPPER_DIRECTORY
|
||||
if( startswith( filename, "../" ))
|
||||
return NULL;
|
||||
#endif
|
||||
|
||||
if( startswith( filename, "usr/" ))
|
||||
return NULL;
|
||||
|
||||
if( startswith( filename, "fs/root/build/host/glibc-2.29/" ))
|
||||
return NULL;
|
||||
|
||||
#if !IGNORE_UPPER_DIRECTORY
|
||||
filename += startswith( filename, "../" );
|
||||
#endif
|
||||
filename += startswith( filename, "/fs/root/build/x86_64/lccrt/" );
|
||||
filename += startswith( filename, "fs/root/build/x86_64/lccrt/" );
|
||||
filename += startswith( filename, "./" );
|
||||
filename += startswith( filename, ".obj/" );
|
||||
|
||||
include_detector_push(filename);
|
||||
|
||||
list_for_each_entry(srcfile, &g_SourceFiles, node)
|
||||
{
|
||||
if( !strcmp( srcfile->filename, filename ))
|
||||
{
|
||||
return srcfile;
|
||||
}
|
||||
}
|
||||
|
||||
srcfile = malloc( sizeof( srcfile_t ));
|
||||
srcfile->filename = strdup(filename);
|
||||
INIT_LIST_HEAD(&srcfile->lines);
|
||||
|
||||
list_add_tail(&srcfile->node, &g_SourceFiles);
|
||||
|
||||
return srcfile;
|
||||
}
|
||||
|
||||
static bool find_or_insert_line(srcfile_t *sourceFile, int linenum, struct tag *tag, struct cu *cu, const char *include)
|
||||
{
|
||||
srcfile_line_t *line, *new;
|
||||
|
||||
list_for_each_entry_reverse(line, &sourceFile->lines, node)
|
||||
{
|
||||
if( line->linenum == linenum ) // && tag__orig_id(line->tag, line->cu) == tag__orig_id(tag, cu))
|
||||
{
|
||||
// special case for include insert
|
||||
if( line->include )
|
||||
break;
|
||||
|
||||
line->tag = tag;
|
||||
line->cu = cu;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( line->linenum <= linenum )
|
||||
break;
|
||||
}
|
||||
|
||||
new = malloc( sizeof( srcfile_line_t ));
|
||||
new->linenum = linenum;
|
||||
new->tag = tag;
|
||||
new->cu = cu;
|
||||
new->include = include ? strdup(include) : 0;
|
||||
list_add(&new->node, &line->node);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void add_srcfile_tag(struct tag *tag, struct cu *cu)
|
||||
{
|
||||
const char *file = tag__decl_file(tag, cu);
|
||||
srcfile_t *srcfile;
|
||||
|
||||
if( !file )
|
||||
return;
|
||||
|
||||
srcfile = find_or_create_srcfile(file);
|
||||
|
||||
if( srcfile )
|
||||
find_or_insert_line(srcfile, tag__decl_line(tag, cu), tag, cu, NULL);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
|
||||
static struct conf_fprintf conf = {
|
||||
.emit_stats = 0,
|
||||
.emit_stats = 1,
|
||||
};
|
||||
|
||||
static void print_type(struct tag *tag, struct cu *cu, FILE *fp)
|
||||
static void emit_tag(struct tag *tag, uint32_t tag_id, struct cu *cu)
|
||||
{
|
||||
fprintf(fp, "/* line: %i */\n", tag__decl_line(tag, cu));
|
||||
printf("/* %d */\n", tag_id);
|
||||
|
||||
if (tag__is_struct(tag))
|
||||
class__find_holes(tag__class(tag));
|
||||
|
||||
tag__fprintf(tag, cu, &conf, fp);
|
||||
if (tag->tag == DW_TAG_base_type) {
|
||||
char bf[64];
|
||||
const char *name = base_type__name(tag__base_type(tag), bf, sizeof(bf));
|
||||
|
||||
fprintf(fp, " /* size: %zd */\n\n", tag__size(tag, cu));
|
||||
}
|
||||
|
||||
static void print_function(struct tag *tag, struct cu *cu, FILE *fp)
|
||||
{
|
||||
conf.no_semicolon = true;
|
||||
{
|
||||
struct function *f = tag__function(tag);
|
||||
int c = tag__fprintf(tag, cu, &conf, fp);
|
||||
if( c >= 70 ) c = 69; // nice
|
||||
|
||||
fprintf(fp, "%-*.*s// %5u\n", 70 - c, 70 - c, " ",
|
||||
tag__decl_line(tag, cu));
|
||||
lexblock__fprintf(&f->lexblock, cu, f, 0, &conf, fp);
|
||||
fputs("\n\n", fp);
|
||||
}
|
||||
conf.no_semicolon = false;
|
||||
}
|
||||
|
||||
static void print_variable(struct tag *tag, struct cu *cu, FILE *fp)
|
||||
{
|
||||
tag__fprintf(tag, cu, NULL, fp);
|
||||
fprintf(fp, " /* line: %i, size: %zd, scope: %s */\n\n", tag__decl_line(tag, cu), tag__size(tag, cu), variable__scope_str(tag__variable(tag)));
|
||||
}
|
||||
|
||||
static void source_files_print( void )
|
||||
{
|
||||
srcfile_t *srcfile;
|
||||
|
||||
list_for_each_entry(srcfile, &g_SourceFiles, node)
|
||||
{
|
||||
printf( "SOURCE FILE: %s\n", srcfile->filename );
|
||||
|
||||
mkdir_p( srcfile->filename );
|
||||
|
||||
FILE *fp = fopen( srcfile->filename, "w+" );
|
||||
|
||||
srcfile_line_t *line;
|
||||
bool header = filename_looks_like_header( srcfile->filename );
|
||||
|
||||
fprintf( fp, "// Generated by psource tool\n" );
|
||||
|
||||
if( header )
|
||||
{
|
||||
fprintf( fp, "#pragma once\n" );
|
||||
}
|
||||
if (name == NULL)
|
||||
printf("anonymous base_type\n");
|
||||
else
|
||||
{
|
||||
fprintf( fp, "#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n" );
|
||||
}
|
||||
|
||||
list_for_each_entry(line, &srcfile->lines, node)
|
||||
{
|
||||
if( line->include )
|
||||
{
|
||||
fprintf(fp, "#include \"%s\"\n", line->include );
|
||||
}
|
||||
else if( tag__is_type(line->tag))
|
||||
{
|
||||
print_type(line->tag, line->cu, fp);
|
||||
}
|
||||
else if( tag__is_variable(line->tag))
|
||||
{
|
||||
print_variable(line->tag, line->cu, fp );
|
||||
}
|
||||
else if( tag__is_function(line->tag))
|
||||
{
|
||||
print_function(line->tag, line->cu, fp );
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
puts(name);
|
||||
} else if (tag__is_pointer(tag))
|
||||
printf(" /* pointer to %lld */\n", (unsigned long long)tag->type);
|
||||
else
|
||||
tag__fprintf(tag, cu, &conf, stdout);
|
||||
|
||||
// ========================================================================
|
||||
|
||||
static void scan_type(struct tag *tag, struct cu *cu)
|
||||
{
|
||||
const char *name;
|
||||
|
||||
switch(tag->tag)
|
||||
{
|
||||
case DW_TAG_base_type:
|
||||
case DW_TAG_const_type:
|
||||
case DW_TAG_array_type:
|
||||
case DW_TAG_pointer_type:
|
||||
case DW_TAG_reference_type:
|
||||
case DW_TAG_subroutine_type:
|
||||
case DW_TAG_unspecified_type:
|
||||
case DW_TAG_rvalue_reference_type:
|
||||
case DW_TAG_restrict_type:
|
||||
case DW_TAG_volatile_type:
|
||||
return;
|
||||
}
|
||||
|
||||
name = type__name(tag__type(tag));
|
||||
// probably included
|
||||
if( name == NULL )
|
||||
return;
|
||||
|
||||
if( name[0] == '\0' )
|
||||
return;
|
||||
|
||||
// probably a forward declaration, skip for now
|
||||
if( tag__size(tag, cu) == 0 )
|
||||
return;
|
||||
|
||||
add_srcfile_tag(tag, cu);
|
||||
}
|
||||
|
||||
static void scan_variable(struct tag *tag, struct cu *cu)
|
||||
{
|
||||
struct variable *variable = tag__variable(tag);
|
||||
|
||||
if( variable->scope == VSCOPE_LOCAL )
|
||||
return;
|
||||
|
||||
add_srcfile_tag(tag, cu);
|
||||
printf(" /* size: %zd */\n\n", tag__size(tag, cu));
|
||||
}
|
||||
|
||||
static int cu__emit_tags(struct cu *cu)
|
||||
{
|
||||
uint32_t i;
|
||||
struct tag *tag;
|
||||
|
||||
puts("/* Types: */\n");
|
||||
cu__for_each_type(cu, i, tag)
|
||||
emit_tag(tag, i, cu);
|
||||
|
||||
puts("/* Functions: */\n");
|
||||
conf.no_semicolon = true;
|
||||
struct function *function;
|
||||
const char *name = cu->name;
|
||||
|
||||
#if IGNORE_UPPER_DIRECTORY
|
||||
if( startswith(name, "../" ))
|
||||
return 0;
|
||||
#else
|
||||
name += startswith(name, "../" );
|
||||
#endif
|
||||
|
||||
include_detector_init();
|
||||
|
||||
cu__for_each_type(cu, i, tag) {
|
||||
scan_type(tag, cu);
|
||||
}
|
||||
|
||||
cu__for_each_variable(cu, i, tag) {
|
||||
scan_variable(tag, cu);
|
||||
}
|
||||
|
||||
cu__for_each_function(cu, i, function) {
|
||||
add_srcfile_tag(function__tag(function), cu);
|
||||
tag__fprintf(function__tag(function), cu, &conf, stdout);
|
||||
putchar('\n');
|
||||
lexblock__fprintf(&function->lexblock, cu, function, 0,
|
||||
&conf, stdout);
|
||||
printf(" /* size: %zd */\n\n",
|
||||
tag__size(function__tag(function), cu));
|
||||
}
|
||||
|
||||
include_detector_detect1(cu);
|
||||
conf.no_semicolon = false;
|
||||
|
||||
puts("\n\n/* Variables: */\n");
|
||||
cu__for_each_variable(cu, i, tag) {
|
||||
tag__fprintf(tag, cu, NULL, stdout);
|
||||
printf(" /* size: %zd */\n\n", tag__size(tag, cu));
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -433,15 +76,12 @@ static enum load_steal_kind pdwtags_stealer(struct cu *cu,
|
||||
void *thr_data __maybe_unused)
|
||||
{
|
||||
cu__emit_tags(cu);
|
||||
|
||||
return LSK__KEEPIT;
|
||||
return LSK__DELETE;
|
||||
}
|
||||
|
||||
static struct conf_load pdwtags_conf_load = {
|
||||
.steal = pdwtags_stealer,
|
||||
.conf_fprintf = &conf,
|
||||
.extra_dbg_info = 1,
|
||||
.get_addr_info = 1,
|
||||
};
|
||||
|
||||
/* Name and version of program. */
|
||||
@ -505,10 +145,9 @@ int main(int argc, char *argv[])
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = cus__load_file(cus, &pdwtags_conf_load, argv[remaining]);
|
||||
err = cus__load_files(cus, &pdwtags_conf_load, argv + remaining);
|
||||
if (err == 0) {
|
||||
rc = EXIT_SUCCESS;
|
||||
source_files_print();
|
||||
goto out;
|
||||
}
|
||||
|
||||
|
520
psrcgen.c
Normal file
520
psrcgen.c
Normal file
@ -0,0 +1,520 @@
|
||||
/*
|
||||
SPDX-License-Identifier: GPL-2.0-only
|
||||
|
||||
Copyright (C) 2007-2016 Arnaldo Carvalho de Melo <acme@kernel.org>
|
||||
Copyright (C) 2022 Alibek Omarov <a1ba.omarov@gmail.com>
|
||||
*/
|
||||
|
||||
#include <argp.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include "dwarves.h"
|
||||
#include "dutil.h"
|
||||
|
||||
#define IGNORE_UPPER_DIRECTORY 1
|
||||
#define NO_WRITE_FILES 0
|
||||
|
||||
char *strncpy2(char *dest, const char *src, size_t n)
|
||||
{
|
||||
char *ret = strncpy(dest, src, n);
|
||||
dest[n] = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool endswith(const char *s1, const char *s2)
|
||||
{
|
||||
int len1 = strlen(s1);
|
||||
int len2 = strlen(s2);
|
||||
|
||||
if( len1 < len2 )
|
||||
return false;
|
||||
|
||||
return !strncmp(s1 + len1 - len2, s2, len2);
|
||||
}
|
||||
|
||||
static bool filename_looks_like_header(const char *s1)
|
||||
{
|
||||
return endswith(s1, ".h") || endswith(s1, ".H");
|
||||
}
|
||||
|
||||
static int startswith( const char *s1, const char *s2 )
|
||||
{
|
||||
int len2 = strlen(s2);
|
||||
|
||||
if( !strncmp(s1, s2, len2))
|
||||
return len2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mkdir_p(const char *path)
|
||||
{
|
||||
#if NO_WRITE_FILES
|
||||
return;
|
||||
#else
|
||||
char orig_dir[2048];
|
||||
getcwd(orig_dir, sizeof( orig_dir ));
|
||||
|
||||
for(const char *path2 = path; (path2 = strchr(path, '/')); path = path2 + 1)
|
||||
{
|
||||
char dir[128];
|
||||
|
||||
strncpy2(dir, path, path2 - path);
|
||||
|
||||
mkdir(dir, 0777);
|
||||
|
||||
chdir(dir);
|
||||
}
|
||||
|
||||
chdir(orig_dir);
|
||||
#endif
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
|
||||
typedef struct
|
||||
{
|
||||
struct list_head node;
|
||||
int linenum;
|
||||
char *include;
|
||||
struct tag *tag;
|
||||
struct cu *cu;
|
||||
} srcfile_line_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
struct list_head node;
|
||||
const char *filename;
|
||||
struct list_head lines;
|
||||
} srcfile_t;
|
||||
|
||||
LIST_HEAD(g_SourceFiles);
|
||||
|
||||
typedef struct include_t
|
||||
{
|
||||
struct list_head node;
|
||||
char *filename;
|
||||
struct include_t *parent;
|
||||
} include_t;
|
||||
|
||||
LIST_HEAD(g_Includes);
|
||||
|
||||
static srcfile_t *find_or_create_srcfile(const char *filename);
|
||||
static bool find_or_insert_line(srcfile_t *sourceFile, int linenum, struct tag *tag, struct cu *cu, const char *include);
|
||||
|
||||
static void include_detector_init( void )
|
||||
{
|
||||
include_t *ptr, *next;
|
||||
|
||||
list_for_each_entry_safe(ptr, next, &g_Includes, node)
|
||||
{
|
||||
list_del(&ptr->node);
|
||||
free(ptr->filename);
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
INIT_LIST_HEAD(&g_Includes);
|
||||
}
|
||||
|
||||
static void include_detector_push(const char *filename)
|
||||
{
|
||||
include_t *include;
|
||||
|
||||
include = malloc( sizeof( include_t ));
|
||||
include->filename = strdup( filename );
|
||||
include->parent = NULL;
|
||||
list_add_tail(&include->node, &g_Includes);
|
||||
}
|
||||
|
||||
static void include_detector_detect1(struct cu *cu)
|
||||
{
|
||||
include_t *inc1;
|
||||
|
||||
// for each include we find next duplicate
|
||||
// it PROBABLY means that everything in between
|
||||
// belongs to that include
|
||||
// list_for_each_entry(inc1, &g_Includes, node)
|
||||
// {
|
||||
// include_t *inc2;
|
||||
//
|
||||
// list_for_each_entry(inc2, inc1->node.next, node)
|
||||
// {
|
||||
// include_t *inc3;
|
||||
//
|
||||
// if( strcmp( inc1->filename, inc2->filename ))
|
||||
// continue;
|
||||
//
|
||||
// list_for_each_entry_reverse(inc3, inc2->node.prev, node)
|
||||
// {
|
||||
// if( inc1 == inc3 )
|
||||
// break;
|
||||
//
|
||||
// inc3->parent = inc1;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
list_for_each_entry(inc1, &g_Includes, node)
|
||||
{
|
||||
if( filename_looks_like_header( inc1->filename ))
|
||||
{
|
||||
srcfile_t *srcfile = find_or_create_srcfile(cu->name);
|
||||
|
||||
if( srcfile )
|
||||
find_or_insert_line(srcfile, -1, NULL, NULL, inc1->filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static srcfile_t *find_or_create_srcfile(const char *filename)
|
||||
{
|
||||
srcfile_t *srcfile;
|
||||
|
||||
filename += startswith( filename, "/" );
|
||||
|
||||
#if IGNORE_UPPER_DIRECTORY
|
||||
if( startswith( filename, "../" ))
|
||||
return NULL;
|
||||
#endif
|
||||
|
||||
if( startswith( filename, "usr/" ))
|
||||
return NULL;
|
||||
|
||||
if( startswith( filename, "fs/root/build/host/glibc-2.29/" ))
|
||||
return NULL;
|
||||
|
||||
#if !IGNORE_UPPER_DIRECTORY
|
||||
filename += startswith( filename, "../" );
|
||||
#endif
|
||||
filename += startswith( filename, "/fs/root/build/x86_64/lccrt/" );
|
||||
filename += startswith( filename, "fs/root/build/x86_64/lccrt/" );
|
||||
filename += startswith( filename, "./" );
|
||||
filename += startswith( filename, ".obj/" );
|
||||
|
||||
include_detector_push(filename);
|
||||
|
||||
list_for_each_entry(srcfile, &g_SourceFiles, node)
|
||||
{
|
||||
if( !strcmp( srcfile->filename, filename ))
|
||||
{
|
||||
return srcfile;
|
||||
}
|
||||
}
|
||||
|
||||
srcfile = malloc( sizeof( srcfile_t ));
|
||||
srcfile->filename = strdup(filename);
|
||||
INIT_LIST_HEAD(&srcfile->lines);
|
||||
|
||||
list_add_tail(&srcfile->node, &g_SourceFiles);
|
||||
|
||||
return srcfile;
|
||||
}
|
||||
|
||||
static bool find_or_insert_line(srcfile_t *sourceFile, int linenum, struct tag *tag, struct cu *cu, const char *include)
|
||||
{
|
||||
srcfile_line_t *line, *new;
|
||||
|
||||
list_for_each_entry_reverse(line, &sourceFile->lines, node)
|
||||
{
|
||||
if( line->linenum == linenum ) // && tag__orig_id(line->tag, line->cu) == tag__orig_id(tag, cu))
|
||||
{
|
||||
// special case for include insert
|
||||
if( line->include )
|
||||
break;
|
||||
|
||||
line->tag = tag;
|
||||
line->cu = cu;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( line->linenum <= linenum )
|
||||
break;
|
||||
}
|
||||
|
||||
new = malloc( sizeof( srcfile_line_t ));
|
||||
new->linenum = linenum;
|
||||
new->tag = tag;
|
||||
new->cu = cu;
|
||||
new->include = include ? strdup(include) : 0;
|
||||
list_add(&new->node, &line->node);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void add_srcfile_tag(struct tag *tag, struct cu *cu)
|
||||
{
|
||||
const char *file = tag__decl_file(tag, cu);
|
||||
srcfile_t *srcfile;
|
||||
|
||||
if( !file )
|
||||
return;
|
||||
|
||||
srcfile = find_or_create_srcfile(file);
|
||||
|
||||
if( srcfile )
|
||||
find_or_insert_line(srcfile, tag__decl_line(tag, cu), tag, cu, NULL);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
|
||||
static struct conf_fprintf conf = {
|
||||
.emit_stats = 0,
|
||||
};
|
||||
|
||||
static void print_type(struct tag *tag, struct cu *cu, FILE *fp)
|
||||
{
|
||||
fprintf(fp, "/* line: %i */\n", tag__decl_line(tag, cu));
|
||||
|
||||
if (tag__is_struct(tag))
|
||||
class__find_holes(tag__class(tag));
|
||||
|
||||
tag__fprintf(tag, cu, &conf, fp);
|
||||
|
||||
fprintf(fp, " /* size: %zd */\n\n", tag__size(tag, cu));
|
||||
}
|
||||
|
||||
static void print_function(struct tag *tag, struct cu *cu, FILE *fp)
|
||||
{
|
||||
conf.no_semicolon = true;
|
||||
{
|
||||
struct function *f = tag__function(tag);
|
||||
int c = tag__fprintf(tag, cu, &conf, fp);
|
||||
if( c >= 70 ) c = 69; // nice
|
||||
|
||||
fprintf(fp, "%-*.*s// %5u\n", 70 - c, 70 - c, " ",
|
||||
tag__decl_line(tag, cu));
|
||||
lexblock__fprintf(&f->lexblock, cu, f, 0, &conf, fp);
|
||||
fputs("\n\n", fp);
|
||||
}
|
||||
conf.no_semicolon = false;
|
||||
}
|
||||
|
||||
static void print_variable(struct tag *tag, struct cu *cu, FILE *fp)
|
||||
{
|
||||
tag__fprintf(tag, cu, NULL, fp);
|
||||
fprintf(fp, " /* line: %i, size: %zd, scope: %s */\n\n", tag__decl_line(tag, cu), tag__size(tag, cu), variable__scope_str(tag__variable(tag)));
|
||||
}
|
||||
|
||||
static void source_files_print( void )
|
||||
{
|
||||
srcfile_t *srcfile;
|
||||
|
||||
list_for_each_entry(srcfile, &g_SourceFiles, node)
|
||||
{
|
||||
printf( "SOURCE FILE: %s\n", srcfile->filename );
|
||||
|
||||
mkdir_p( srcfile->filename );
|
||||
|
||||
FILE *fp = fopen( srcfile->filename, "w+" );
|
||||
|
||||
srcfile_line_t *line;
|
||||
bool header = filename_looks_like_header( srcfile->filename );
|
||||
|
||||
fprintf( fp, "// Generated by psrcgen tool\n" );
|
||||
|
||||
if( header )
|
||||
{
|
||||
fprintf( fp, "#pragma once\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf( fp, "#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n" );
|
||||
}
|
||||
|
||||
list_for_each_entry(line, &srcfile->lines, node)
|
||||
{
|
||||
if( line->include )
|
||||
{
|
||||
fprintf(fp, "#include \"%s\"\n", line->include );
|
||||
}
|
||||
else if( tag__is_type(line->tag))
|
||||
{
|
||||
print_type(line->tag, line->cu, fp);
|
||||
}
|
||||
else if( tag__is_variable(line->tag))
|
||||
{
|
||||
print_variable(line->tag, line->cu, fp );
|
||||
}
|
||||
else if( tag__is_function(line->tag))
|
||||
{
|
||||
print_function(line->tag, line->cu, fp );
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
|
||||
static void scan_type(struct tag *tag, struct cu *cu)
|
||||
{
|
||||
const char *name;
|
||||
|
||||
switch(tag->tag)
|
||||
{
|
||||
case DW_TAG_base_type:
|
||||
case DW_TAG_const_type:
|
||||
case DW_TAG_array_type:
|
||||
case DW_TAG_pointer_type:
|
||||
case DW_TAG_reference_type:
|
||||
case DW_TAG_subroutine_type:
|
||||
case DW_TAG_unspecified_type:
|
||||
case DW_TAG_rvalue_reference_type:
|
||||
case DW_TAG_restrict_type:
|
||||
case DW_TAG_volatile_type:
|
||||
return;
|
||||
}
|
||||
|
||||
name = type__name(tag__type(tag));
|
||||
// probably included
|
||||
if( name == NULL )
|
||||
return;
|
||||
|
||||
if( name[0] == '\0' )
|
||||
return;
|
||||
|
||||
// probably a forward declaration, skip for now
|
||||
if( tag__size(tag, cu) == 0 )
|
||||
return;
|
||||
|
||||
add_srcfile_tag(tag, cu);
|
||||
}
|
||||
|
||||
static void scan_variable(struct tag *tag, struct cu *cu)
|
||||
{
|
||||
struct variable *variable = tag__variable(tag);
|
||||
|
||||
if( variable->scope == VSCOPE_LOCAL )
|
||||
return;
|
||||
|
||||
add_srcfile_tag(tag, cu);
|
||||
}
|
||||
|
||||
static int cu__emit_tags(struct cu *cu)
|
||||
{
|
||||
uint32_t i;
|
||||
struct tag *tag;
|
||||
struct function *function;
|
||||
const char *name = cu->name;
|
||||
|
||||
#if IGNORE_UPPER_DIRECTORY
|
||||
if( startswith(name, "../" ))
|
||||
return 0;
|
||||
#else
|
||||
name += startswith(name, "../" );
|
||||
#endif
|
||||
|
||||
include_detector_init();
|
||||
|
||||
cu__for_each_type(cu, i, tag) {
|
||||
scan_type(tag, cu);
|
||||
}
|
||||
|
||||
cu__for_each_variable(cu, i, tag) {
|
||||
scan_variable(tag, cu);
|
||||
}
|
||||
|
||||
cu__for_each_function(cu, i, function) {
|
||||
add_srcfile_tag(function__tag(function), cu);
|
||||
}
|
||||
|
||||
include_detector_detect1(cu);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static enum load_steal_kind pdwtags_stealer(struct cu *cu,
|
||||
struct conf_load *conf_load __maybe_unused,
|
||||
void *thr_data __maybe_unused)
|
||||
{
|
||||
cu__emit_tags(cu);
|
||||
|
||||
return LSK__KEEPIT;
|
||||
}
|
||||
|
||||
static struct conf_load pdwtags_conf_load = {
|
||||
.steal = pdwtags_stealer,
|
||||
.conf_fprintf = &conf,
|
||||
.extra_dbg_info = 1,
|
||||
.get_addr_info = 1,
|
||||
};
|
||||
|
||||
/* Name and version of program. */
|
||||
ARGP_PROGRAM_VERSION_HOOK_DEF = dwarves_print_version;
|
||||
|
||||
static const struct argp_option pdwtags__options[] = {
|
||||
{
|
||||
.name = "format_path",
|
||||
.key = 'F',
|
||||
.arg = "FORMAT_LIST",
|
||||
.doc = "List of debugging formats to try"
|
||||
},
|
||||
{
|
||||
.key = 'V',
|
||||
.name = "verbose",
|
||||
.doc = "show details",
|
||||
},
|
||||
{
|
||||
.name = NULL,
|
||||
}
|
||||
};
|
||||
|
||||
static error_t pdwtags__options_parser(int key, char *arg __maybe_unused,
|
||||
struct argp_state *state)
|
||||
{
|
||||
switch (key) {
|
||||
case ARGP_KEY_INIT:
|
||||
if (state->child_inputs != NULL)
|
||||
state->child_inputs[0] = state->input;
|
||||
break;
|
||||
case 'F': pdwtags_conf_load.format_path = arg; break;
|
||||
case 'V': conf.show_decl_info = 1; break;
|
||||
default: return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const char pdwtags__args_doc[] = "FILE";
|
||||
|
||||
static struct argp pdwtags__argp = {
|
||||
.options = pdwtags__options,
|
||||
.parser = pdwtags__options_parser,
|
||||
.args_doc = pdwtags__args_doc,
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int remaining, rc = EXIT_FAILURE, err;
|
||||
struct cus *cus = cus__new();
|
||||
|
||||
if (dwarves__init() || cus == NULL) {
|
||||
fputs("pwdtags: insufficient memory\n", stderr);
|
||||
goto out;
|
||||
}
|
||||
|
||||
dwarves__resolve_cacheline_size(&pdwtags_conf_load, 0);
|
||||
|
||||
if (argp_parse(&pdwtags__argp, argc, argv, 0, &remaining, NULL) ||
|
||||
remaining == argc) {
|
||||
argp_help(&pdwtags__argp, stderr, ARGP_HELP_SEE, argv[0]);
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = cus__load_file(cus, &pdwtags_conf_load, argv[remaining]);
|
||||
if (err == 0) {
|
||||
rc = EXIT_SUCCESS;
|
||||
source_files_print();
|
||||
goto out;
|
||||
}
|
||||
|
||||
cus__fprintf_load_files_err(cus, "pdwtags", argv + remaining, err, stderr);
|
||||
out:
|
||||
cus__delete(cus);
|
||||
dwarves__exit();
|
||||
return rc;
|
||||
}
|
Loading…
Reference in New Issue
Block a user