4 * Copyright 2001,2007 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
37 #ifdef HAVE_SYS_MMAN_H
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
48 static void* dump_base
;
49 static unsigned long dump_total_len
;
51 void dump_data( const unsigned char *ptr
, unsigned int size
, const char *prefix
)
55 printf( "%s%08x: ", prefix
, 0 );
61 for (i
= 0; i
< size
; i
++)
63 printf( "%02x%c", ptr
[i
], (i
% 16 == 7) ? '-' : ' ' );
67 for (j
= 0; j
< 16; j
++)
68 printf( "%c", isprint(ptr
[i
-15+j
]) ? ptr
[i
-15+j
] : '.' );
69 if (i
< size
-1) printf( "\n%s%08x: ", prefix
, i
+ 1 );
74 printf( "%*s ", 3 * (16-(i
%16)), "" );
75 for (j
= 0; j
< i
% 16; j
++)
76 printf( "%c", isprint(ptr
[i
-(i
%16)+j
]) ? ptr
[i
-(i
%16)+j
] : '.' );
81 static char* dump_want_n(unsigned sz
)
83 static char buffer
[4 * 1024];
87 assert(sz
< sizeof(buffer
));
88 if (idx
+ sz
>= sizeof(buffer
)) idx
= 0;
94 const char *get_time_str(unsigned long _t
)
96 const time_t t
= (const time_t)_t
;
97 const char *str
= ctime(&t
);
101 if (!str
) return "not valid time";
104 /* FIXME: I don't get the same values from MS' pedump running under Wine...
105 * I wonder if Wine isn't broken wrt to GMT settings...
107 if (len
&& str
[len
-1] == '\n') len
--;
108 buf
= dump_want_n(len
+ 1);
111 memcpy( buf
, str
, len
);
117 unsigned int strlenW( const WCHAR
*str
)
119 const WCHAR
*s
= str
;
124 void dump_unicode_str( const WCHAR
*str
, int len
)
126 if (len
== -1) len
= strlenW( str
);
128 while (len
-- > 0 && *str
)
133 case '\n': printf( "\\n" ); break;
134 case '\r': printf( "\\r" ); break;
135 case '\t': printf( "\\t" ); break;
136 case '"': printf( "\\\"" ); break;
137 case '\\': printf( "\\\\" ); break;
139 if (c
>= ' ' && c
<= 126) putchar(c
);
140 else printf( "\\u%04x",c
);
146 const char* get_symbol_str(const char* symname
)
151 if (!symname
) return "(nil)";
152 if (globals
.do_demangle
)
154 parsed_symbol symbol
;
156 symbol_init(&symbol
, symname
);
157 if (symbol_demangle(&symbol
) == -1)
159 else if (symbol
.flags
& SYM_DATA
)
161 ret
= tmp
= dump_want_n(strlen(symbol
.arg_text
[0]) + 1);
162 if (tmp
) strcpy(tmp
, symbol
.arg_text
[0]);
166 unsigned int i
, len
, start
= symbol
.flags
& SYM_THISCALL
? 1 : 0;
168 len
= strlen(symbol
.return_text
) + 3 /* ' __' */ +
169 strlen(symbol_get_call_convention(&symbol
)) + 1 /* ' ' */+
170 strlen(symbol
.function_name
) + 1 /* ')' */;
171 if (!symbol
.argc
|| (symbol
.argc
== 1 && symbol
.flags
& SYM_THISCALL
))
172 len
+= 4 /* "void" */;
173 else for (i
= start
; i
< symbol
.argc
; i
++)
174 len
+= (i
> start
? 2 /* ", " */ : 0 /* "" */) + strlen(symbol
.arg_text
[i
]);
175 if (symbol
.varargs
) len
+= 5 /* ", ..." */;
176 len
+= 2; /* ")\0" */
178 ret
= tmp
= dump_want_n(len
);
181 sprintf(tmp
, "%s __%s %s(",
183 symbol_get_call_convention(&symbol
),
184 symbol
.function_name
);
185 if (!symbol
.argc
|| (symbol
.argc
== 1 && symbol
.flags
& SYM_THISCALL
))
187 else for (i
= start
; i
< symbol
.argc
; i
++)
189 if (i
> start
) strcat(tmp
, ", ");
190 strcat(tmp
, symbol
.arg_text
[i
]);
192 if (symbol
.varargs
) strcat(tmp
, ", ...");
196 symbol_clear(&symbol
);
202 const char* get_guid_str(const GUID
* guid
)
206 str
= dump_want_n(39);
208 sprintf(str
, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
209 guid
->Data1
, guid
->Data2
, guid
->Data3
,
210 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
211 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
215 const void* PRD(unsigned long prd
, unsigned long len
)
217 return (prd
+ len
> dump_total_len
) ? NULL
: (const char*)dump_base
+ prd
;
220 unsigned long Offset(const void* ptr
)
222 if (ptr
< dump_base
) {printf("<<<<<ptr below\n");return 0;}
223 if ((const char *)ptr
>= (const char*)dump_base
+ dump_total_len
) {printf("<<<<<ptr above\n");return 0;}
224 return (const char *)ptr
- (const char *)dump_base
;
227 static const struct dumper
230 enum FileSig (*get_kind
)(void);
231 file_dumper dumper
; /* default dump tool */
235 {SIG_DOS
, get_kind_exec
, dos_dump
},
236 {SIG_PE
, get_kind_exec
, pe_dump
},
237 {SIG_DBG
, get_kind_dbg
, dbg_dump
},
238 {SIG_PDB
, get_kind_pdb
, pdb_dump
},
239 {SIG_NE
, get_kind_exec
, ne_dump
},
240 {SIG_LE
, get_kind_exec
, le_dump
},
241 {SIG_COFFLIB
, get_kind_lib
, lib_dump
},
242 {SIG_MDMP
, get_kind_mdmp
, mdmp_dump
},
243 {SIG_LNK
, get_kind_lnk
, lnk_dump
},
244 {SIG_EMF
, get_kind_emf
, emf_dump
},
245 {SIG_UNKNOWN
, NULL
, NULL
} /* sentinel */
248 int dump_analysis(const char *name
, file_dumper fn
, enum FileSig wanted_sig
)
253 const struct dumper
*dpr
;
255 setbuf(stdout
, NULL
);
257 fd
= open(name
, O_RDONLY
| O_BINARY
);
258 if (fd
== -1) fatal("Can't open file");
260 if (fstat(fd
, &s
) < 0) fatal("Can't get size");
261 dump_total_len
= s
.st_size
;
264 if ((dump_base
= mmap(NULL
, dump_total_len
, PROT_READ
, MAP_PRIVATE
, fd
, 0)) == (void *)-1)
267 if (!(dump_base
= malloc( dump_total_len
))) fatal( "Out of memory" );
268 if ((unsigned long)read( fd
, dump_base
, dump_total_len
) != dump_total_len
) fatal( "Cannot read file" );
271 printf("Contents of %s: %ld bytes\n\n", name
, dump_total_len
);
273 for (dpr
= dumpers
; dpr
->kind
!= SIG_UNKNOWN
; dpr
++)
275 if (dpr
->get_kind() == dpr
->kind
&&
276 (wanted_sig
== SIG_UNKNOWN
|| wanted_sig
== dpr
->kind
))
278 if (fn
) fn(); else dpr
->dumper();
282 if (dpr
->kind
== SIG_UNKNOWN
)
284 printf("Can't get a suitable file signature, aborting\n");
288 if (ret
) printf("Done dumping %s\n", name
);
290 if (munmap(dump_base
, dump_total_len
) == -1)
300 void dump_file(const char* name
)
302 dump_analysis(name
, NULL
, SIG_UNKNOWN
);