2 * File source.c - source files management
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
27 #include "dbghelp_private.h"
28 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
35 /******************************************************************
38 * check whether a source file has already been stored
40 static unsigned source_find(const struct module* module, const char* name)
42 char* ptr = module->sources;
46 if (strcmp(ptr, name) == 0) return ptr - module->sources;
47 ptr += strlen(ptr) + 1;
52 /******************************************************************
55 * checks if source exists. if not, add it
57 unsigned source_new(struct module* module, const char* base, const char* name)
63 if (!name) return (unsigned)-1;
64 if (!base || *name == '/')
68 unsigned bsz = strlen(base);
69 char* tmp = HeapAlloc(GetProcessHeap(), 0, bsz + 1 + strlen(name) + 1);
71 if (!tmp) return (unsigned)-1;
74 if (tmp[bsz - 1] != '/') tmp[bsz++] = '/';
75 strcpy(&tmp[bsz], name);
77 if (module->sources && (ret = source_find(module, full)) != (unsigned)-1)
80 len = strlen(full) + 1;
81 if (module->sources_used + len + 1 > module->sources_alloc)
83 /* Alloc by block of 256 bytes */
84 module->sources_alloc = (module->sources_used + len + 1 + 255) & ~255;
86 module->sources = HeapAlloc(GetProcessHeap(), 0, module->sources_alloc);
88 module->sources = HeapReAlloc(GetProcessHeap(), 0, module->sources,
89 module->sources_alloc);
91 ret = module->sources_used;
92 strcpy(module->sources + module->sources_used, full);
93 module->sources_used += len;
94 module->sources[module->sources_used] = '\0';
95 if (full != name) HeapFree(GetProcessHeap(), 0, (char*)full);
99 /******************************************************************
102 * returns a stored source file name
104 const char* source_get(const struct module* module, unsigned idx)
106 if (idx == -1) return "";
107 assert(module->sources);
108 return module->sources + idx;
111 /******************************************************************
112 * SymEnumSourceFiles (DBGHELP.@)
115 BOOL WINAPI SymEnumSourceFiles(HANDLE hProcess, ULONG64 ModBase, PCSTR Mask,
116 PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,
120 struct module_pair pair;
124 if (!cbSrcFiles) return FALSE;
125 pcs = process_find_by_handle(hProcess);
126 if (!pcs) return FALSE;
130 pair.requested = module_find_by_addr(pcs, ModBase, DMT_UNKNOWN);
131 if (!module_get_debug(pcs, &pair)) return FALSE;
137 pair.requested = module_find_by_name(pcs, Mask + 1, DMT_UNKNOWN);
138 if (!module_get_debug(pcs, &pair)) return FALSE;
142 FIXME("Unsupported yet (should get info from current context)\n");
146 if (!pair.effective->sources) return FALSE;
147 for (ptr = pair.effective->sources; *ptr; ptr += strlen(ptr) + 1)
149 /* FIXME: not using Mask */
150 sf.ModBase = ModBase;
152 if (!cbSrcFiles(&sf, UserContext)) break;
158 /******************************************************************
159 * SymEnumLines (DBGHELP.@)
162 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
163 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
166 struct module_pair pair;
167 struct hash_table_iter hti;
170 struct line_info* dli;
175 if (!cb) return FALSE;
176 pcs = process_find_by_handle(hProcess);
177 if (!pcs) return FALSE;
178 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
179 if (regcomp(&re, srcfile, REG_NOSUB))
181 FIXME("Couldn't compile %s\n", srcfile);
182 SetLastError(ERROR_INVALID_PARAMETER);
185 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
186 pair.requested = module_find_by_addr(pcs, base, DMT_UNKNOWN);
187 if (!module_get_debug(pcs, &pair)) return FALSE;
189 sci.SizeOfStruct = sizeof(sci);
192 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
193 while ((ptr = hash_table_iter_up(&hti)))
195 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
196 if (sym->symt.tag != SymTagFunction) continue;
199 sci.FileName[0] = '\0';
200 while ((dli = vector_iter_up(&((struct symt_function*)sym)->vlines, dli)))
202 if (dli->is_source_file)
204 file = source_get(pair.effective, dli->u.source_file);
205 if (regexec(&re, file, 0, NULL, 0) != 0) file = "";
206 strcpy(sci.FileName, file);
208 else if (sci.FileName[0])
211 sci.Obj[0] = '\0'; /* FIXME */
212 sci.LineNumber = dli->line_number;
213 sci.Address = dli->u.pc_offset;
214 if (!cb(&sci, user)) break;
221 /******************************************************************
222 * SymGetSourceFileToken (DBGHELP.@)
225 BOOL WINAPI SymGetSourceFileToken(HANDLE hProcess, ULONG64 base,
226 PCSTR src, PVOID* token, DWORD* size)
228 FIXME("%p %s %s %p %p: stub!\n",
229 hProcess, wine_dbgstr_longlong(base), debugstr_a(src), token, size);
230 SetLastError(ERROR_NOT_SUPPORTED);
234 /******************************************************************
235 * SymGetSourceFileTokenW (DBGHELP.@)
238 BOOL WINAPI SymGetSourceFileTokenW(HANDLE hProcess, ULONG64 base,
239 PCWSTR src, PVOID* token, DWORD* size)
241 FIXME("%p %s %s %p %p: stub!\n",
242 hProcess, wine_dbgstr_longlong(base), debugstr_w(src), token, size);
243 SetLastError(ERROR_NOT_SUPPORTED);