d3d8: Toggle depth test state based on auto depth stencil.
[wine.git] / tools / winedump / dump.c
blobe84e473ea752942f518ec199500a292d314d2aa0
1 /*
2 * File dumping utility
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <time.h>
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
40 #include <fcntl.h>
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winedump.h"
46 static void* dump_base;
47 static unsigned long dump_total_len;
49 void dump_data( const unsigned char *ptr, unsigned int size, const char *prefix )
51 unsigned int i, j;
53 printf( "%s%08x: ", prefix, 0 );
54 if (!ptr)
56 printf("NULL\n");
57 return;
59 for (i = 0; i < size; i++)
61 printf( "%02x%c", ptr[i], (i % 16 == 7) ? '-' : ' ' );
62 if ((i % 16) == 15)
64 printf( " " );
65 for (j = 0; j < 16; j++)
66 printf( "%c", isprint(ptr[i-15+j]) ? ptr[i-15+j] : '.' );
67 if (i < size-1) printf( "\n%s%08x: ", prefix, i + 1 );
70 if (i % 16)
72 printf( "%*s ", 3 * (16-(i%16)), "" );
73 for (j = 0; j < i % 16; j++)
74 printf( "%c", isprint(ptr[i-(i%16)+j]) ? ptr[i-(i%16)+j] : '.' );
76 printf( "\n" );
79 static char* dump_want_n(unsigned sz)
81 static char buffer[4 * 1024];
82 static unsigned idx;
83 char* ret;
85 assert(sz < sizeof(buffer));
86 if (idx + sz >= sizeof(buffer)) idx = 0;
87 ret = &buffer[idx];
88 idx += sz;
89 return ret;
92 const char *get_time_str(unsigned long _t)
94 const time_t t = (const time_t)_t;
95 const char *str = ctime(&t);
96 size_t len;
97 char* buf;
99 if (!str) return "not valid time";
101 len = strlen(str);
102 /* FIXME: I don't get the same values from MS' pedump running under Wine...
103 * I wonder if Wine isn't broken wrt to GMT settings...
105 if (len && str[len-1] == '\n') len--;
106 buf = dump_want_n(len + 1);
107 if (buf)
109 memcpy( buf, str, len );
110 buf[len] = 0;
112 return buf;
115 unsigned int strlenW( const WCHAR *str )
117 const WCHAR *s = str;
118 while (*s) s++;
119 return s - str;
122 void dump_unicode_str( const WCHAR *str, int len )
124 if (len == -1) len = strlenW( str );
125 printf( "L\"");
126 while (len-- > 0 && *str)
128 WCHAR c = *str++;
129 switch (c)
131 case '\n': printf( "\\n" ); break;
132 case '\r': printf( "\\r" ); break;
133 case '\t': printf( "\\t" ); break;
134 case '"': printf( "\\\"" ); break;
135 case '\\': printf( "\\\\" ); break;
136 default:
137 if (c >= ' ' && c <= 126) putchar(c);
138 else printf( "\\u%04x",c);
141 printf( "\"" );
144 const char* get_symbol_str(const char* symname)
146 char* tmp;
147 const char* ret;
149 if (!symname) return "(nil)";
150 if (globals.do_demangle)
152 parsed_symbol symbol;
154 symbol_init(&symbol, symname);
155 if (!symbol_demangle(&symbol))
156 ret = symname;
157 else if (symbol.flags & SYM_DATA)
159 ret = tmp = dump_want_n(strlen(symbol.arg_text[0]) + 1);
160 if (tmp) strcpy(tmp, symbol.arg_text[0]);
162 else
164 unsigned int i, len, start = symbol.flags & SYM_THISCALL ? 1 : 0;
166 len = strlen(symbol.return_text) + 3 /* ' __' */ +
167 strlen(symbol_get_call_convention(&symbol)) + 1 /* ' ' */+
168 strlen(symbol.function_name) + 1 /* ')' */;
169 if (!symbol.argc || (symbol.argc == 1 && symbol.flags & SYM_THISCALL))
170 len += 4 /* "void" */;
171 else for (i = start; i < symbol.argc; i++)
172 len += (i > start ? 2 /* ", " */ : 0 /* "" */) + strlen(symbol.arg_text[i]);
173 if (symbol.varargs) len += 5 /* ", ..." */;
174 len += 2; /* ")\0" */
176 ret = tmp = dump_want_n(len);
177 if (tmp)
179 sprintf(tmp, "%s __%s %s(",
180 symbol.return_text,
181 symbol_get_call_convention(&symbol),
182 symbol.function_name);
183 if (!symbol.argc || (symbol.argc == 1 && symbol.flags & SYM_THISCALL))
184 strcat(tmp, "void");
185 else for (i = start; i < symbol.argc; i++)
187 if (i > start) strcat(tmp, ", ");
188 strcat(tmp, symbol.arg_text[i]);
190 if (symbol.varargs) strcat(tmp, ", ...");
191 strcat(tmp, ")");
194 symbol_clear(&symbol);
196 else ret = symname;
197 return ret;
200 const char* get_guid_str(const GUID* guid)
202 char* str;
204 str = dump_want_n(39);
205 if (str)
206 sprintf(str, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
207 guid->Data1, guid->Data2, guid->Data3,
208 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
209 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
210 return str;
213 const void* PRD(unsigned long prd, unsigned long len)
215 return (prd + len > dump_total_len) ? NULL : (const char*)dump_base + prd;
218 unsigned long Offset(const void* ptr)
220 if (ptr < dump_base) {printf("<<<<<ptr below\n");return 0;}
221 if ((const char *)ptr >= (const char*)dump_base + dump_total_len) {printf("<<<<<ptr above\n");return 0;}
222 return (const char *)ptr - (const char *)dump_base;
225 static const struct dumper
227 enum FileSig kind;
228 enum FileSig (*get_kind)(void);
229 file_dumper dumper; /* default dump tool */
231 dumpers[] =
233 {SIG_DOS, get_kind_exec, dos_dump},
234 {SIG_PE, get_kind_exec, pe_dump},
235 {SIG_DBG, get_kind_dbg, dbg_dump},
236 {SIG_PDB, get_kind_pdb, pdb_dump},
237 {SIG_NE, get_kind_exec, ne_dump},
238 {SIG_LE, get_kind_exec, le_dump},
239 {SIG_COFFLIB, get_kind_lib, lib_dump},
240 {SIG_MDMP, get_kind_mdmp, mdmp_dump},
241 {SIG_LNK, get_kind_lnk, lnk_dump},
242 {SIG_EMF, get_kind_emf, emf_dump},
243 {SIG_FNT, get_kind_fnt, fnt_dump},
244 {SIG_MSFT, get_kind_msft, msft_dump},
245 {SIG_UNKNOWN, NULL, NULL} /* sentinel */
248 BOOL dump_analysis(const char *name, file_dumper fn, enum FileSig wanted_sig)
250 int fd;
251 BOOL ret = TRUE;
252 struct stat s;
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;
263 #ifdef HAVE_MMAP
264 if ((dump_base = mmap(NULL, dump_total_len, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1)
265 #endif
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();
279 break;
282 if (dpr->kind == SIG_UNKNOWN)
284 printf("Can't get a suitable file signature, aborting\n");
285 ret = FALSE;
288 if (ret) printf("Done dumping %s\n", name);
289 #ifdef HAVE_MMAP
290 if (munmap(dump_base, dump_total_len) == -1)
291 #endif
293 free( dump_base );
295 close(fd);
297 return ret;
300 void dump_file(const char* name)
302 dump_analysis(name, NULL, SIG_UNKNOWN);