2 * File path.c - managing path in debugging environments
4 * Copyright (C) 2004, 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
26 #include "dbghelp_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34 static inline BOOL is_sep(char ch) {return ch == '/' || ch == '\\';}
35 static inline BOOL is_sepW(WCHAR ch) {return ch == '/' || ch == '\\';}
37 static inline const char* file_name(const char* str)
41 for (p = str + strlen(str) - 1; p >= str && !is_sep(*p); p--);
45 static inline const WCHAR* file_nameW(const WCHAR* str)
49 for (p = str + strlenW(str) - 1; p >= str && !is_sepW(*p); p--);
53 /******************************************************************
54 * FindDebugInfoFile (DBGHELP.@)
57 HANDLE WINAPI FindDebugInfoFile(PCSTR FileName, PCSTR SymbolPath, PSTR DebugFilePath)
61 h = CreateFileA(DebugFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
62 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
63 if (h == INVALID_HANDLE_VALUE)
65 if (!SearchPathA(SymbolPath, file_name(FileName), NULL, MAX_PATH, DebugFilePath, NULL))
67 h = CreateFileA(DebugFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
68 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
70 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
73 /******************************************************************
74 * FindDebugInfoFileEx (DBGHELP.@)
77 HANDLE WINAPI FindDebugInfoFileEx(PCSTR FileName, PCSTR SymbolPath,
79 PFIND_DEBUG_FILE_CALLBACK Callback,
82 FIXME("(%s %s %p %p %p): stub\n",
83 FileName, SymbolPath, DebugFilePath, Callback, CallerData);
87 /******************************************************************
88 * FindExecutableImageExW (DBGHELP.@)
91 HANDLE WINAPI FindExecutableImageExW(PCWSTR FileName, PCWSTR SymbolPath, PWSTR ImageFilePath,
92 PFIND_EXE_FILE_CALLBACKW Callback, void* user)
96 if (Callback) FIXME("Unsupported callback yet\n");
97 if (!SearchPathW(SymbolPath, FileName, NULL, MAX_PATH, ImageFilePath, NULL))
99 h = CreateFileW(ImageFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
100 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
101 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
104 /******************************************************************
105 * FindExecutableImageEx (DBGHELP.@)
108 HANDLE WINAPI FindExecutableImageEx(PCSTR FileName, PCSTR SymbolPath, PSTR ImageFilePath,
109 PFIND_EXE_FILE_CALLBACK Callback, void* user)
113 if (Callback) FIXME("Unsupported callback yet\n");
114 if (!SearchPathA(SymbolPath, FileName, NULL, MAX_PATH, ImageFilePath, NULL))
116 h = CreateFileA(ImageFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
117 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
118 return (h == INVALID_HANDLE_VALUE) ? NULL : h;
121 /******************************************************************
122 * FindExecutableImage (DBGHELP.@)
125 HANDLE WINAPI FindExecutableImage(PCSTR FileName, PCSTR SymbolPath, PSTR ImageFilePath)
127 return FindExecutableImageEx(FileName, SymbolPath, ImageFilePath, NULL, NULL);
130 /***********************************************************************
131 * MakeSureDirectoryPathExists (DBGHELP.@)
133 BOOL WINAPI MakeSureDirectoryPathExists(LPCSTR DirPath)
136 const char *p = DirPath;
139 if (p[0] && p[1] == ':') p += 2;
140 while (*p == '\\') p++; /* skip drive root */
141 while ((p = strchr(p, '\\')) != NULL)
144 memcpy(path, DirPath, n);
146 if( !CreateDirectoryA(path, NULL) &&
147 (GetLastError() != ERROR_ALREADY_EXISTS))
151 if (GetLastError() == ERROR_ALREADY_EXISTS)
152 SetLastError(ERROR_SUCCESS);
157 /******************************************************************
158 * SymMatchFileNameW (DBGHELP.@)
161 BOOL WINAPI SymMatchFileNameW(WCHAR* file, WCHAR* match,
162 WCHAR** filestop, WCHAR** matchstop)
167 TRACE("(%s %s %p %p)\n",
168 debugstr_w(file), debugstr_w(match), filestop, matchstop);
170 fptr = file + strlenW(file) - 1;
171 mptr = match + strlenW(match) - 1;
173 while (fptr >= file && mptr >= match)
175 if (toupperW(*fptr) != toupperW(*mptr) && !(is_sepW(*fptr) && is_sepW(*mptr)))
179 if (filestop) *filestop = fptr;
180 if (matchstop) *matchstop = mptr;
182 return mptr == match - 1;
185 /******************************************************************
186 * SymMatchFileName (DBGHELP.@)
189 BOOL WINAPI SymMatchFileName(char* file, char* match,
190 char** filestop, char** matchstop)
195 TRACE("(%s %s %p %p)\n", file, match, filestop, matchstop);
197 fptr = file + strlen(file) - 1;
198 mptr = match + strlen(match) - 1;
200 while (fptr >= file && mptr >= match)
202 if (toupper(*fptr) != toupper(*mptr) && !(is_sep(*fptr) && is_sep(*mptr)))
206 if (filestop) *filestop = fptr;
207 if (matchstop) *matchstop = mptr;
209 return mptr == match - 1;
212 static BOOL do_searchW(const WCHAR* file, WCHAR* buffer, BOOL recurse,
213 PENUMDIRTREE_CALLBACKW cb, void* user)
219 static const WCHAR S_AllW[] = {'*','.','*','\0'};
220 static const WCHAR S_DotW[] = {'.','\0'};
221 static const WCHAR S_DotDotW[] = {'.','\0'};
223 pos = strlenW(buffer);
224 if (buffer[pos - 1] != '\\') buffer[pos++] = '\\';
225 strcpyW(buffer + pos, S_AllW);
226 if ((h = FindFirstFileW(buffer, &fd)) == INVALID_HANDLE_VALUE)
228 /* doc doesn't specify how the tree is enumerated...
229 * doing a depth first based on, but may be wrong
233 if (!strcmpW(fd.cFileName, S_DotW) || !strcmpW(fd.cFileName, S_DotDotW)) continue;
235 strcpyW(buffer + pos, fd.cFileName);
236 if (recurse && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
237 found = do_searchW(file, buffer, TRUE, cb, user);
238 else if (SymMatchFileNameW(buffer, (WCHAR*)file, NULL, NULL))
240 if (!cb || cb(buffer, user)) found = TRUE;
242 } while (!found && FindNextFileW(h, &fd));
243 if (!found) buffer[--pos] = '\0';
249 /***********************************************************************
250 * SearchTreeForFileW (DBGHELP.@)
252 BOOL WINAPI SearchTreeForFileW(PCWSTR root, PCWSTR file, PWSTR buffer)
254 TRACE("(%s, %s, %p)\n",
255 debugstr_w(root), debugstr_w(file), buffer);
256 strcpyW(buffer, root);
257 return do_searchW(file, buffer, TRUE, NULL, NULL);
260 /***********************************************************************
261 * SearchTreeForFile (DBGHELP.@)
263 BOOL WINAPI SearchTreeForFile(PCSTR root, PCSTR file, PSTR buffer)
265 WCHAR rootW[MAX_PATH];
266 WCHAR fileW[MAX_PATH];
267 WCHAR bufferW[MAX_PATH];
270 MultiByteToWideChar(CP_ACP, 0, root, -1, rootW, MAX_PATH);
271 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, MAX_PATH);
272 ret = SearchTreeForFileW(rootW, fileW, bufferW);
274 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
278 /******************************************************************
279 * EnumDirTreeW (DBGHELP.@)
283 BOOL WINAPI EnumDirTreeW(HANDLE hProcess, PCWSTR root, PCWSTR file,
284 LPWSTR buffer, PENUMDIRTREE_CALLBACKW cb, PVOID user)
286 TRACE("(%p %s %s %p %p %p)\n",
287 hProcess, debugstr_w(root), debugstr_w(file), buffer, cb, user);
289 strcpyW(buffer, root);
290 return do_searchW(file, buffer, TRUE, cb, user);
293 /******************************************************************
294 * EnumDirTree (DBGHELP.@)
298 struct enum_dir_treeWA
300 PENUMDIRTREE_CALLBACK cb;
305 static BOOL CALLBACK enum_dir_treeWA(LPCWSTR name, PVOID user)
307 struct enum_dir_treeWA* edt = user;
309 WideCharToMultiByte(CP_ACP, 0, name, -1, edt->name, MAX_PATH, NULL, NULL);
310 return edt->cb(edt->name, edt->user);
313 BOOL WINAPI EnumDirTree(HANDLE hProcess, PCSTR root, PCSTR file,
314 LPSTR buffer, PENUMDIRTREE_CALLBACK cb, PVOID user)
316 WCHAR rootW[MAX_PATH];
317 WCHAR fileW[MAX_PATH];
318 WCHAR bufferW[MAX_PATH];
319 struct enum_dir_treeWA edt;
324 MultiByteToWideChar(CP_ACP, 0, root, -1, rootW, MAX_PATH);
325 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, MAX_PATH);
326 if ((ret = EnumDirTreeW(hProcess, rootW, fileW, bufferW, enum_dir_treeWA, &edt)))
327 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
333 enum module_type kind;
334 /* pe: id -> DWORD:timestamp
335 * two -> size of image (from PE header)
336 * pdb: id -> PDB signature
337 * I think either DWORD:timestamp or GUID:guid depending on PDB version
339 * elf: id -> DWORD:CRC 32 of ELF image (Wine only)
345 PFINDFILEINPATHCALLBACKW cb;
349 /* checks that buffer (as found by matching the name) matches the info
350 * (information is based on file type)
351 * returns TRUE when file is found, FALSE to continue searching
352 * (NB this is the opposite conventions as for SymFindFileInPathProc)
354 static BOOL CALLBACK sffip_cb(LPCWSTR buffer, void* user)
356 struct sffip* s = (struct sffip*)user;
357 DWORD size, checksum;
359 /* FIXME: should check that id/two/three match the file pointed
370 timestamp = ~(DWORD_PTR)s->id;
372 hFile = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ, NULL,
373 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
374 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
375 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
377 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
379 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
380 timestamp = nth->FileHeader.TimeDateStamp;
381 size = nth->OptionalHeader.SizeOfImage;
382 UnmapViewOfFile(mapping);
387 if (timestamp != (DWORD_PTR)s->id || size != s->two)
389 WARN("Found %s, but wrong size or timestamp\n", debugstr_w(buffer));
395 if (elf_fetch_file_info(buffer, 0, &size, &checksum))
397 if (checksum != (DWORD_PTR)s->id)
399 WARN("Found %s, but wrong checksums: %08x %08lx\n",
400 debugstr_w(buffer), checksum, (DWORD_PTR)s->id);
406 WARN("Couldn't read %s\n", debugstr_w(buffer));
412 struct pdb_lookup pdb_lookup;
415 WideCharToMultiByte(CP_ACP, 0, buffer, -1, fn, MAX_PATH, NULL, NULL);
416 pdb_lookup.filename = fn;
418 if (!pdb_fetch_file_info(&pdb_lookup)) return FALSE;
419 switch (pdb_lookup.kind)
422 if (s->flags & SSRVOPT_GUIDPTR)
424 WARN("Found %s, but wrong PDB version\n", debugstr_w(buffer));
427 if (pdb_lookup.u.jg.timestamp != (DWORD_PTR)s->id)
429 WARN("Found %s, but wrong signature: %08x %08lx\n",
430 debugstr_w(buffer), pdb_lookup.u.jg.timestamp, (DWORD_PTR)s->id);
435 if (!(s->flags & SSRVOPT_GUIDPTR))
437 WARN("Found %s, but wrong PDB version\n", debugstr_w(buffer));
440 if (memcmp(&pdb_lookup.u.ds.guid, (GUID*)s->id, sizeof(GUID)))
442 WARN("Found %s, but wrong GUID: %s %s\n",
443 debugstr_w(buffer), debugstr_guid(&pdb_lookup.u.ds.guid),
444 debugstr_guid((GUID*)s->id));
449 if (pdb_lookup.age != s->two)
451 WARN("Found %s, but wrong age: %08x %08x\n",
452 debugstr_w(buffer), pdb_lookup.age, s->two);
458 FIXME("What the heck??\n");
461 /* yes, EnumDirTree/do_search and SymFindFileInPath callbacks use the opposite
462 * convention to stop/continue enumeration. sigh.
464 return !(s->cb)((WCHAR*)buffer, s->user);
467 /******************************************************************
468 * SymFindFileInPathW (DBGHELP.@)
471 BOOL WINAPI SymFindFileInPathW(HANDLE hProcess, PCWSTR searchPath, PCWSTR full_path,
472 PVOID id, DWORD two, DWORD three, DWORD flags,
473 LPWSTR buffer, PFINDFILEINPATHCALLBACKW cb,
477 struct process* pcs = process_find_by_handle(hProcess);
480 const WCHAR* filename;
482 TRACE("(%p %s %s %p %08x %08x %08x %p %p %p)\n",
483 hProcess, debugstr_w(searchPath), debugstr_w(full_path),
484 id, two, three, flags, buffer, cb, user);
486 if (!pcs) return FALSE;
487 if (!searchPath) searchPath = pcs->search_path;
496 filename = file_nameW(full_path);
497 s.kind = module_get_type_by_name(filename);
499 /* first check full path to file */
500 if (sffip_cb(full_path, &s))
502 strcpyW(buffer, full_path);
508 ptr = strchrW(searchPath, ';');
511 memcpy(tmp, searchPath, (ptr - searchPath) * sizeof(WCHAR));
512 tmp[ptr - searchPath] = 0;
513 searchPath = ptr + 1;
517 strcpyW(tmp, searchPath);
520 if (do_searchW(filename, tmp, FALSE, sffip_cb, &s))
522 strcpyW(buffer, tmp);
529 /******************************************************************
530 * SymFindFileInPath (DBGHELP.@)
533 BOOL WINAPI SymFindFileInPath(HANDLE hProcess, PCSTR searchPath, PCSTR full_path,
534 PVOID id, DWORD two, DWORD three, DWORD flags,
535 LPSTR buffer, PFINDFILEINPATHCALLBACK cb,
538 WCHAR searchPathW[MAX_PATH];
539 WCHAR full_pathW[MAX_PATH];
540 WCHAR bufferW[MAX_PATH];
541 struct enum_dir_treeWA edt;
544 /* a PFINDFILEINPATHCALLBACK and a PENUMDIRTREE_CALLBACK have actually the
545 * same signature & semantics, hence we can reuse the EnumDirTree W->A
551 MultiByteToWideChar(CP_ACP, 0, searchPath, -1, searchPathW, MAX_PATH);
552 MultiByteToWideChar(CP_ACP, 0, full_path, -1, full_pathW, MAX_PATH);
553 if ((ret = SymFindFileInPathW(hProcess, searchPath ? searchPathW : NULL, full_pathW,
554 id, two, three, flags,
555 bufferW, enum_dir_treeWA, &edt)))
556 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);