2 * File path.c - managing path in debugging environments
4 * Copyright (C) 2004,2008, 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 "dbghelp_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
25 static inline BOOL
is_sep(char ch
) {return ch
== '/' || ch
== '\\';}
26 static inline BOOL
is_sepW(WCHAR ch
) {return ch
== '/' || ch
== '\\';}
28 static inline const char* file_name(const char* str
)
32 for (p
= str
+ strlen(str
) - 1; p
>= str
&& !is_sep(*p
); p
--);
36 static inline const WCHAR
* file_nameW(const WCHAR
* str
)
40 for (p
= str
+ strlenW(str
) - 1; p
>= str
&& !is_sepW(*p
); p
--);
44 /******************************************************************
45 * FindDebugInfoFile (DBGHELP.@)
48 HANDLE WINAPI
FindDebugInfoFile(PCSTR FileName
, PCSTR SymbolPath
, PSTR DebugFilePath
)
52 h
= CreateFileA(FileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
53 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
54 if (h
== INVALID_HANDLE_VALUE
)
56 if (!SearchPathA(SymbolPath
, file_name(FileName
), NULL
, MAX_PATH
, DebugFilePath
, NULL
))
58 h
= CreateFileA(DebugFilePath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
59 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
61 return (h
== INVALID_HANDLE_VALUE
) ? NULL
: h
;
64 /******************************************************************
65 * FindDebugInfoFileEx (DBGHELP.@)
68 HANDLE WINAPI
FindDebugInfoFileEx(PCSTR FileName
, PCSTR SymbolPath
,
70 PFIND_DEBUG_FILE_CALLBACK Callback
,
73 FIXME("(%s %s %s %p %p): stub\n", debugstr_a(FileName
), debugstr_a(SymbolPath
),
74 debugstr_a(DebugFilePath
), Callback
, CallerData
);
78 /******************************************************************
79 * FindExecutableImageExW (DBGHELP.@)
82 HANDLE WINAPI
FindExecutableImageExW(PCWSTR FileName
, PCWSTR SymbolPath
, PWSTR ImageFilePath
,
83 PFIND_EXE_FILE_CALLBACKW Callback
, PVOID user
)
87 if (Callback
) FIXME("Unsupported callback yet\n");
88 if (!SearchPathW(SymbolPath
, FileName
, NULL
, MAX_PATH
, ImageFilePath
, NULL
))
90 h
= CreateFileW(ImageFilePath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
91 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
92 return (h
== INVALID_HANDLE_VALUE
) ? NULL
: h
;
95 /******************************************************************
96 * FindExecutableImageEx (DBGHELP.@)
99 HANDLE WINAPI
FindExecutableImageEx(PCSTR FileName
, PCSTR SymbolPath
, PSTR ImageFilePath
,
100 PFIND_EXE_FILE_CALLBACK Callback
, PVOID user
)
104 if (Callback
) FIXME("Unsupported callback yet\n");
105 if (!SearchPathA(SymbolPath
, FileName
, NULL
, MAX_PATH
, ImageFilePath
, NULL
))
107 h
= CreateFileA(ImageFilePath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
108 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
109 return (h
== INVALID_HANDLE_VALUE
) ? NULL
: h
;
112 /******************************************************************
113 * FindExecutableImage (DBGHELP.@)
116 HANDLE WINAPI
FindExecutableImage(PCSTR FileName
, PCSTR SymbolPath
, PSTR ImageFilePath
)
118 return FindExecutableImageEx(FileName
, SymbolPath
, ImageFilePath
, NULL
, NULL
);
121 /***********************************************************************
122 * MakeSureDirectoryPathExists (DBGHELP.@)
124 BOOL WINAPI
MakeSureDirectoryPathExists(PCSTR DirPath
)
127 const char *p
= DirPath
;
130 if (p
[0] && p
[1] == ':') p
+= 2;
131 while (*p
== '\\') p
++; /* skip drive root */
132 while ((p
= strchr(p
, '\\')) != NULL
)
135 memcpy(path
, DirPath
, n
);
137 if( !CreateDirectoryA(path
, NULL
) &&
138 (GetLastError() != ERROR_ALREADY_EXISTS
))
142 if (GetLastError() == ERROR_ALREADY_EXISTS
)
143 SetLastError(ERROR_SUCCESS
);
148 /******************************************************************
149 * SymMatchFileNameW (DBGHELP.@)
152 BOOL WINAPI
SymMatchFileNameW(PCWSTR file
, PCWSTR match
,
153 PWSTR
* filestop
, PWSTR
* matchstop
)
158 TRACE("(%s %s %p %p)\n",
159 debugstr_w(file
), debugstr_w(match
), filestop
, matchstop
);
161 fptr
= file
+ strlenW(file
) - 1;
162 mptr
= match
+ strlenW(match
) - 1;
164 while (fptr
>= file
&& mptr
>= match
)
166 if (toupperW(*fptr
) != toupperW(*mptr
) && !(is_sepW(*fptr
) && is_sepW(*mptr
)))
170 if (filestop
) *filestop
= (PWSTR
)fptr
;
171 if (matchstop
) *matchstop
= (PWSTR
)mptr
;
173 return mptr
== match
- 1;
176 /******************************************************************
177 * SymMatchFileName (DBGHELP.@)
180 BOOL WINAPI
SymMatchFileName(PCSTR file
, PCSTR match
,
181 PSTR
* filestop
, PSTR
* matchstop
)
186 TRACE("(%s %s %p %p)\n", debugstr_a(file
), debugstr_a(match
), filestop
, matchstop
);
188 fptr
= file
+ strlen(file
) - 1;
189 mptr
= match
+ strlen(match
) - 1;
191 while (fptr
>= file
&& mptr
>= match
)
193 if (toupper(*fptr
) != toupper(*mptr
) && !(is_sep(*fptr
) && is_sep(*mptr
)))
197 if (filestop
) *filestop
= (PSTR
)fptr
;
198 if (matchstop
) *matchstop
= (PSTR
)mptr
;
200 return mptr
== match
- 1;
203 static BOOL
do_searchW(PCWSTR file
, PWSTR buffer
, BOOL recurse
,
204 PENUMDIRTREE_CALLBACKW cb
, PVOID user
)
210 static const WCHAR S_AllW
[] = {'*','.','*','\0'};
211 static const WCHAR S_DotW
[] = {'.','\0'};
212 static const WCHAR S_DotDotW
[] = {'.','.','\0'};
214 pos
= strlenW(buffer
);
215 if (buffer
[pos
- 1] != '\\') buffer
[pos
++] = '\\';
216 strcpyW(buffer
+ pos
, S_AllW
);
217 if ((h
= FindFirstFileW(buffer
, &fd
)) == INVALID_HANDLE_VALUE
)
219 /* doc doesn't specify how the tree is enumerated...
220 * doing a depth first based on, but may be wrong
224 if (!strcmpW(fd
.cFileName
, S_DotW
) || !strcmpW(fd
.cFileName
, S_DotDotW
)) continue;
226 strcpyW(buffer
+ pos
, fd
.cFileName
);
227 if (recurse
&& (fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
228 found
= do_searchW(file
, buffer
, TRUE
, cb
, user
);
229 else if (SymMatchFileNameW(buffer
, file
, NULL
, NULL
))
231 if (!cb
|| cb(buffer
, user
)) found
= TRUE
;
233 } while (!found
&& FindNextFileW(h
, &fd
));
234 if (!found
) buffer
[--pos
] = '\0';
240 /***********************************************************************
241 * SearchTreeForFileW (DBGHELP.@)
243 BOOL WINAPI
SearchTreeForFileW(PCWSTR root
, PCWSTR file
, PWSTR buffer
)
245 TRACE("(%s, %s, %p)\n",
246 debugstr_w(root
), debugstr_w(file
), buffer
);
247 strcpyW(buffer
, root
);
248 return do_searchW(file
, buffer
, TRUE
, NULL
, NULL
);
251 /***********************************************************************
252 * SearchTreeForFile (DBGHELP.@)
254 BOOL WINAPI
SearchTreeForFile(PCSTR root
, PCSTR file
, PSTR buffer
)
256 WCHAR rootW
[MAX_PATH
];
257 WCHAR fileW
[MAX_PATH
];
258 WCHAR bufferW
[MAX_PATH
];
261 MultiByteToWideChar(CP_ACP
, 0, root
, -1, rootW
, MAX_PATH
);
262 MultiByteToWideChar(CP_ACP
, 0, file
, -1, fileW
, MAX_PATH
);
263 ret
= SearchTreeForFileW(rootW
, fileW
, bufferW
);
265 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, MAX_PATH
, NULL
, NULL
);
269 /******************************************************************
270 * EnumDirTreeW (DBGHELP.@)
274 BOOL WINAPI
EnumDirTreeW(HANDLE hProcess
, PCWSTR root
, PCWSTR file
,
275 PWSTR buffer
, PENUMDIRTREE_CALLBACKW cb
, PVOID user
)
277 TRACE("(%p %s %s %p %p %p)\n",
278 hProcess
, debugstr_w(root
), debugstr_w(file
), buffer
, cb
, user
);
280 strcpyW(buffer
, root
);
281 return do_searchW(file
, buffer
, TRUE
, cb
, user
);
284 /******************************************************************
285 * EnumDirTree (DBGHELP.@)
289 struct enum_dir_treeWA
291 PENUMDIRTREE_CALLBACK cb
;
296 static BOOL CALLBACK
enum_dir_treeWA(PCWSTR name
, PVOID user
)
298 struct enum_dir_treeWA
* edt
= user
;
300 WideCharToMultiByte(CP_ACP
, 0, name
, -1, edt
->name
, MAX_PATH
, NULL
, NULL
);
301 return edt
->cb(edt
->name
, edt
->user
);
304 BOOL WINAPI
EnumDirTree(HANDLE hProcess
, PCSTR root
, PCSTR file
,
305 PSTR buffer
, PENUMDIRTREE_CALLBACK cb
, PVOID user
)
307 WCHAR rootW
[MAX_PATH
];
308 WCHAR fileW
[MAX_PATH
];
309 WCHAR bufferW
[MAX_PATH
];
310 struct enum_dir_treeWA edt
;
315 MultiByteToWideChar(CP_ACP
, 0, root
, -1, rootW
, MAX_PATH
);
316 MultiByteToWideChar(CP_ACP
, 0, file
, -1, fileW
, MAX_PATH
);
317 if ((ret
= EnumDirTreeW(hProcess
, rootW
, fileW
, bufferW
, enum_dir_treeWA
, &edt
)))
318 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, MAX_PATH
, NULL
, NULL
);
324 PFINDFILEINPATHCALLBACKW cb
;
328 /* checks that buffer (as found by matching the name) matches the info
329 * (information is based on file type)
330 * returns TRUE when file is found, FALSE to continue searching
331 * (NB this is the opposite convention of SymFindFileInPathProc)
333 static BOOL CALLBACK
sffip_cb(PCWSTR buffer
, PVOID user
)
335 struct sffip
* s
= user
;
337 if (!s
->cb
) return TRUE
;
338 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
339 * convention to stop/continue enumeration. sigh.
341 return !(s
->cb
)(buffer
, s
->user
);
344 /******************************************************************
345 * SymFindFileInPathW (DBGHELP.@)
348 BOOL WINAPI
SymFindFileInPathW(HANDLE hProcess
, PCWSTR searchPath
, PCWSTR full_path
,
349 PVOID id
, DWORD two
, DWORD three
, DWORD flags
,
350 PWSTR buffer
, PFINDFILEINPATHCALLBACKW cb
,
354 struct process
* pcs
= process_find_by_handle(hProcess
);
357 const WCHAR
* filename
;
359 TRACE("(hProcess = %p, searchPath = %s, full_path = %s, id = %p, two = 0x%08x, three = 0x%08x, flags = 0x%08x, buffer = %p, cb = %p, user = %p)\n",
360 hProcess
, debugstr_w(searchPath
), debugstr_w(full_path
),
361 id
, two
, three
, flags
, buffer
, cb
, user
);
363 if (!pcs
) return FALSE
;
364 if (!searchPath
) searchPath
= pcs
->search_path
;
369 filename
= file_nameW(full_path
);
371 /* first check full path to file */
372 if (sffip_cb(full_path
, &s
))
374 strcpyW(buffer
, full_path
);
380 ptr
= strchrW(searchPath
, ';');
383 memcpy(tmp
, searchPath
, (ptr
- searchPath
) * sizeof(WCHAR
));
384 tmp
[ptr
- searchPath
] = 0;
385 searchPath
= ptr
+ 1;
389 strcpyW(tmp
, searchPath
);
392 if (do_searchW(filename
, tmp
, FALSE
, sffip_cb
, &s
))
394 strcpyW(buffer
, tmp
);
401 /******************************************************************
402 * SymFindFileInPath (DBGHELP.@)
405 BOOL WINAPI
SymFindFileInPath(HANDLE hProcess
, PCSTR searchPath
, PCSTR full_path
,
406 PVOID id
, DWORD two
, DWORD three
, DWORD flags
,
407 PSTR buffer
, PFINDFILEINPATHCALLBACK cb
,
410 WCHAR searchPathW
[MAX_PATH
];
411 WCHAR full_pathW
[MAX_PATH
];
412 WCHAR bufferW
[MAX_PATH
];
413 struct enum_dir_treeWA edt
;
416 /* a PFINDFILEINPATHCALLBACK and a PENUMDIRTREE_CALLBACK have actually the
417 * same signature & semantics, hence we can reuse the EnumDirTree W->A
423 MultiByteToWideChar(CP_ACP
, 0, searchPath
, -1, searchPathW
, MAX_PATH
);
424 MultiByteToWideChar(CP_ACP
, 0, full_path
, -1, full_pathW
, MAX_PATH
);
425 if ((ret
= SymFindFileInPathW(hProcess
, searchPath
? searchPathW
: NULL
, full_pathW
,
426 id
, two
, three
, flags
,
427 bufferW
, enum_dir_treeWA
, &edt
)))
428 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, MAX_PATH
, NULL
, NULL
);
434 enum module_type kind
;
435 /* pe: dw1 DWORD:timestamp
436 * dw2 size of image (from PE header)
437 * pdb: guid PDB guid (if DS PDB file)
438 * or dw1 PDB timestamp (if JG PDB file)
440 * elf: dw1 DWORD:CRC 32 of ELF image (Wine only)
445 WCHAR filename
[MAX_PATH
];
449 /* checks that buffer (as found by matching the name) matches the info
450 * (information is based on file type)
451 * returns TRUE when file is found, FALSE to continue searching
452 * (NB this is the opposite convention of SymFindFileInPathProc)
454 static BOOL CALLBACK
module_find_cb(PCWSTR buffer
, PVOID user
)
456 struct module_find
* mf
= user
;
457 DWORD size
, checksum
, timestamp
;
458 unsigned matched
= 0;
460 /* the matching weights:
461 * +1 if a file with same name is found and is a decent file of expected type
462 * +1 if first parameter and second parameter match
465 /* FIXME: should check that id/two match the file pointed
475 timestamp
= ~mf
->dw1
;
477 hFile
= CreateFileW(buffer
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
478 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
479 if (hFile
== INVALID_HANDLE_VALUE
) return FALSE
;
480 if ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != NULL
)
482 if ((mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
)
484 IMAGE_NT_HEADERS
* nth
= RtlImageNtHeader(mapping
);
487 UnmapViewOfFile(mapping
);
493 timestamp
= nth
->FileHeader
.TimeDateStamp
;
494 size
= nth
->OptionalHeader
.SizeOfImage
;
495 UnmapViewOfFile(mapping
);
500 if (timestamp
!= mf
->dw1
)
501 WARN("Found %s, but wrong timestamp\n", debugstr_w(buffer
));
503 WARN("Found %s, but wrong size\n", debugstr_w(buffer
));
504 if (timestamp
== mf
->dw1
&& size
== mf
->dw2
) matched
++;
508 if (elf_fetch_file_info(buffer
, 0, &size
, &checksum
))
511 if (checksum
== mf
->dw1
) matched
++;
513 WARN("Found %s, but wrong checksums: %08x %08x\n",
514 debugstr_w(buffer
), checksum
, mf
->dw1
);
518 WARN("Couldn't read %s\n", debugstr_w(buffer
));
523 if (macho_fetch_file_info(NULL
, buffer
, 0, 0, &size
, &checksum
))
526 if (checksum
== mf
->dw1
) matched
++;
528 WARN("Found %s, but wrong checksums: %08x %08x\n",
529 debugstr_w(buffer
), checksum
, mf
->dw1
);
533 WARN("Couldn't read %s\n", debugstr_w(buffer
));
539 struct pdb_lookup pdb_lookup
;
542 WideCharToMultiByte(CP_ACP
, 0, buffer
, -1, fn
, MAX_PATH
, NULL
, NULL
);
543 pdb_lookup
.filename
= fn
;
547 pdb_lookup
.kind
= PDB_DS
;
548 pdb_lookup
.timestamp
= 0;
549 pdb_lookup
.guid
= *mf
->guid
;
553 pdb_lookup
.kind
= PDB_JG
;
554 pdb_lookup
.timestamp
= mf
->dw1
;
555 /* pdb_loopkup.guid = */
557 pdb_lookup
.age
= mf
->dw2
;
559 if (!pdb_fetch_file_info(&pdb_lookup
, &matched
)) return FALSE
;
567 timestamp
= ~mf
->dw1
;
568 hFile
= CreateFileW(buffer
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
569 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
570 if (hFile
== INVALID_HANDLE_VALUE
) return FALSE
;
571 if ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != NULL
)
573 if ((mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
)
575 const IMAGE_SEPARATE_DEBUG_HEADER
* hdr
;
578 if (hdr
->Signature
== IMAGE_SEPARATE_DEBUG_SIGNATURE
)
581 timestamp
= hdr
->TimeDateStamp
;
583 UnmapViewOfFile(mapping
);
588 if (timestamp
== mf
->dw1
) matched
++;
589 else WARN("Found %s, but wrong timestamp\n", debugstr_w(buffer
));
593 FIXME("What the heck??\n");
596 if (matched
> mf
->matched
)
598 strcpyW(mf
->filename
, buffer
);
599 mf
->matched
= matched
;
601 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
602 * convention to stop/continue enumeration. sigh.
604 return mf
->matched
== 2;
607 BOOL
path_find_symbol_file(const struct process
* pcs
, PCSTR full_path
,
608 const GUID
* guid
, DWORD dw1
, DWORD dw2
, PSTR buffer
,
611 struct module_find mf
;
612 WCHAR full_pathW
[MAX_PATH
];
615 const WCHAR
* filename
;
616 WCHAR
* searchPath
= pcs
->search_path
;
618 TRACE("(pcs = %p, full_path = %s, guid = %s, dw1 = 0x%08x, dw2 = 0x%08x, buffer = %p)\n",
619 pcs
, debugstr_a(full_path
), debugstr_guid(guid
), dw1
, dw2
, buffer
);
626 MultiByteToWideChar(CP_ACP
, 0, full_path
, -1, full_pathW
, MAX_PATH
);
627 filename
= file_nameW(full_pathW
);
628 mf
.kind
= module_get_type_by_name(filename
);
629 *is_unmatched
= FALSE
;
631 /* first check full path to file */
632 if (module_find_cb(full_pathW
, &mf
))
634 WideCharToMultiByte(CP_ACP
, 0, full_pathW
, -1, buffer
, MAX_PATH
, NULL
, NULL
);
640 ptr
= strchrW(searchPath
, ';');
643 memcpy(tmp
, searchPath
, (ptr
- searchPath
) * sizeof(WCHAR
));
644 tmp
[ptr
- searchPath
] = '\0';
645 searchPath
= ptr
+ 1;
649 strcpyW(tmp
, searchPath
);
652 if (do_searchW(filename
, tmp
, FALSE
, module_find_cb
, &mf
))
654 /* return first fully matched file */
655 WideCharToMultiByte(CP_ACP
, 0, tmp
, -1, buffer
, MAX_PATH
, NULL
, NULL
);
659 /* if no fully matching file is found, return the best matching file if any */
660 if ((dbghelp_options
& SYMOPT_LOAD_ANYTHING
) && mf
.matched
)
662 WideCharToMultiByte(CP_ACP
, 0, mf
.filename
, -1, buffer
, MAX_PATH
, NULL
, NULL
);
663 *is_unmatched
= TRUE
;