Line data Source code
1 : // --file d2.bin --type vote_state_versioned
2 :
3 : #define _GNU_SOURCE
4 : #include <dlfcn.h>
5 :
6 : #include "../fd_flamenco.h"
7 : #include "../runtime/fd_runtime.h"
8 : #include "../types/fd_types_yaml.h"
9 :
10 : #include <errno.h>
11 : #include <stdio.h>
12 : #include <sys/stat.h> /* mkdir(2) */
13 : #include <fcntl.h> /* open(2) */
14 : #include <unistd.h> /* close(2) */
15 :
16 : static void
17 0 : usage( void ) {
18 0 : fprintf( stderr,
19 0 : "Usage: fd_solcap_dump --type <type> --file {FILE}\n"
20 0 : "\n"
21 0 : "dumps the contents of an account.\n"
22 0 : "\n"
23 0 : "Options:\n"
24 0 : " --file name filename to read\n"
25 0 : " --type <type> type of the data\n"
26 0 : "\n" );
27 0 : }
28 :
29 0 : ulong foo_lkasjdf( void ) {
30 0 : return fd_vote_state_versioned_footprint();
31 0 : }
32 :
33 0 : int fd_flamenco_type_lookup(const char *type, fd_types_funcs_t * t) {
34 0 : char fp[255];
35 :
36 0 : #pragma GCC diagnostic ignored "-Wpedantic"
37 0 : sprintf(fp, "%s_footprint", type);
38 0 : t->footprint_fun = dlsym(RTLD_DEFAULT, fp);
39 :
40 0 : sprintf(fp, "%s_align", type);
41 0 : t->align_fun = dlsym(RTLD_DEFAULT, fp);
42 :
43 0 : sprintf(fp, "%s_new", type);
44 0 : t->new_fun = dlsym(RTLD_DEFAULT, fp);
45 :
46 0 : sprintf(fp, "%s_decode", type);
47 0 : t->decode_fun = dlsym(RTLD_DEFAULT, fp);
48 :
49 0 : sprintf(fp, "%s_decode_footprint", type);
50 0 : t->decode_footprint_fun = dlsym(RTLD_DEFAULT, fp);
51 :
52 0 : sprintf(fp, "%s_walk", type);
53 0 : t->walk_fun = dlsym(RTLD_DEFAULT, fp);
54 :
55 0 : sprintf(fp, "%s_encode", type);
56 0 : t->encode_fun = dlsym(RTLD_DEFAULT, fp);
57 :
58 0 : sprintf(fp, "%s_destroy", type);
59 0 : t->destroy_fun = dlsym(RTLD_DEFAULT, fp);
60 :
61 0 : sprintf(fp, "%s_size", type);
62 0 : t->size_fun = dlsym(RTLD_DEFAULT, fp);
63 :
64 0 : if (( t->footprint_fun == NULL) ||
65 0 : ( t->align_fun == NULL) ||
66 0 : ( t->new_fun == NULL) ||
67 0 : ( t->decode_fun == NULL) ||
68 0 : ( t->decode_footprint_fun == NULL) ||
69 0 : ( t->walk_fun == NULL) ||
70 0 : ( t->encode_fun == NULL) ||
71 0 : ( t->destroy_fun == NULL) ||
72 0 : ( t->size_fun == NULL))
73 0 : return -1;
74 0 : return 0;
75 0 : }
76 :
77 : int
78 : main( int argc,
79 : char ** argv ) {
80 : fd_boot( &argc, &argv );
81 : fd_flamenco_boot( &argc, &argv );
82 :
83 : /* Command line handling */
84 :
85 : for( int i=1; i<argc; i++ ) {
86 : if( 0==strcmp( argv[i], "--help" ) ) {
87 : usage();
88 : return 0;
89 : }
90 : }
91 :
92 : char const * _page_sz = fd_env_strip_cmdline_cstr ( &argc, &argv, "--page-sz", NULL, "gigantic" );
93 : ulong page_cnt = fd_env_strip_cmdline_ulong( &argc, &argv, "--page-cnt", NULL, 2UL );
94 : ulong scratch_mb = fd_env_strip_cmdline_ulong( &argc, &argv, "--scratch-mb", NULL, 1024UL );
95 : char const * type = fd_env_strip_cmdline_cstr ( &argc, &argv, "--type", NULL, NULL );
96 : char const * file = fd_env_strip_cmdline_cstr ( &argc, &argv, "--file", NULL, NULL );
97 :
98 : if ((NULL == type) || (NULL == file)) {
99 : usage();
100 : return 0;
101 : }
102 :
103 : ulong page_sz = fd_cstr_to_shmem_page_sz( _page_sz );
104 :
105 : fd_wksp_t * wksp = fd_wksp_new_anonymous( page_sz, page_cnt, fd_log_cpu_id(), "wksp", 0UL );
106 : if( FD_UNLIKELY( !wksp ) ) FD_LOG_ERR(( "fd_wksp_new_anonymous() failed" ));
107 :
108 : /* Create scratch allocator */
109 :
110 : ulong smax = scratch_mb << 20;
111 : void * smem = fd_wksp_alloc_laddr( wksp, fd_scratch_smem_align(), smax, 1UL );
112 : if( FD_UNLIKELY( !smem ) ) FD_LOG_ERR(( "Failed to alloc scratch mem" ));
113 : # define SCRATCH_DEPTH (4UL)
114 : ulong fmem[ SCRATCH_DEPTH ] __attribute((aligned(FD_SCRATCH_FMEM_ALIGN)));
115 : fd_scratch_attach( smem, fmem, smax, SCRATCH_DEPTH );
116 : fd_scratch_push();
117 :
118 : /* Read file */
119 :
120 : uchar * data;
121 : ulong data_sz;
122 : do {
123 : /* Open and stat file */
124 : int fd = open( file, O_RDONLY );
125 : FD_TEST( fd>=0 );
126 : struct stat statbuf[1];
127 : FD_TEST( 0==fstat( fd, statbuf ) );
128 : data_sz = (ulong)statbuf->st_size;
129 :
130 : /* Allocate scratch buffer for file */
131 : FD_TEST( fd_scratch_alloc_is_safe( /* align */ 1UL, data_sz ) );
132 : data = fd_scratch_alloc( /* align */ 1UL, data_sz );
133 :
134 : /* Copy file into memory */
135 : FD_TEST( (ssize_t)data_sz == read( fd, data, data_sz ) );
136 : FD_TEST( 0==close( fd ) );
137 : } while(0);
138 :
139 : /* Decode file */
140 :
141 : fd_bincode_decode_ctx_t decode = {
142 : .data = data,
143 : .dataend = data + data_sz,
144 : };
145 :
146 : fd_flamenco_yaml_t * yaml =
147 : fd_flamenco_yaml_init( fd_flamenco_yaml_new(
148 : fd_scratch_alloc( fd_flamenco_yaml_align(), fd_flamenco_yaml_footprint() ) ),
149 : stdout );
150 :
151 : fd_types_funcs_t f;
152 : if (fd_flamenco_type_lookup(type, &f) != 0)
153 : FD_LOG_ERR (( "lookup for %s failed", type));
154 :
155 : ulong total_sz = 0UL;
156 : int err = f.decode_footprint_fun( &decode, &total_sz );
157 : if( FD_UNLIKELY( err!=0 ) ) return err;
158 :
159 : uchar * d = fd_scratch_alloc( f.align_fun(), total_sz );
160 :
161 : f.decode_fun( d, &decode );
162 :
163 : f.walk_fun(yaml, d, fd_flamenco_yaml_walk, NULL, 0U );
164 :
165 : fd_scratch_pop();
166 : fd_scratch_detach( NULL );
167 : fd_flamenco_halt();
168 : fd_halt();
169 : return 0;
170 : }
|