ole32: Allocate memory for the generic composite moniker in CompositeMonikerImpl_Cons...
[wine/multimedia.git] / dlls / dbghelp / source.c
bloba56b7e91e877e953db78718b1a68a3f5ccfde957
1 /*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "dbghelp_private.h"
28 #include "wine/debug.h"
29 #ifdef HAVE_REGEX_H
30 # include <regex.h>
31 #endif
33 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
35 /******************************************************************
36 * source_find
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;
44 while (*ptr)
46 if (strcmp(ptr, name) == 0) return ptr - module->sources;
47 ptr += strlen(ptr) + 1;
49 return (unsigned)-1;
52 /******************************************************************
53 * source_new
55 * checks if source exists. if not, add it
57 unsigned source_new(struct module* module, const char* name)
59 int len;
60 unsigned ret;
62 if (!name) return (unsigned)-1;
63 if (module->sources && (ret = source_find(module, name)) != (unsigned)-1)
64 return ret;
66 len = strlen(name) + 1;
67 if (module->sources_used + len + 1 > module->sources_alloc)
69 /* Alloc by block of 256 bytes */
70 module->sources_alloc = (module->sources_used + len + 1 + 255) & ~255;
71 if (!module->sources)
72 module->sources = HeapAlloc(GetProcessHeap(), 0, module->sources_alloc);
73 else
74 module->sources = HeapReAlloc(GetProcessHeap(), 0, module->sources,
75 module->sources_alloc);
77 ret = module->sources_used;
78 strcpy(module->sources + module->sources_used, name);
79 module->sources_used += len;
80 module->sources[module->sources_used] = '\0';
81 return ret;
84 /******************************************************************
85 * source_get
87 * returns a stored source file name
89 const char* source_get(const struct module* module, unsigned idx)
91 if (idx == -1) return "";
92 assert(module->sources);
93 return module->sources + idx;
96 /******************************************************************
97 * SymEnumSourceFiles (DBGHELP.@)
100 BOOL WINAPI SymEnumSourceFiles(HANDLE hProcess, ULONG64 ModBase, PCSTR Mask,
101 PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,
102 PVOID UserContext)
104 struct process* pcs;
105 struct module_pair pair;
106 SOURCEFILE sf;
107 char* ptr;
109 if (!cbSrcFiles) return FALSE;
110 pcs = process_find_by_handle(hProcess);
111 if (!pcs) return FALSE;
113 if (ModBase)
115 pair.requested = module_find_by_addr(pcs, ModBase, DMT_UNKNOWN);
116 if (!module_get_debug(pcs, &pair)) return FALSE;
118 else
120 if (Mask[0] == '!')
122 pair.requested = module_find_by_name(pcs, Mask + 1, DMT_UNKNOWN);
123 if (!module_get_debug(pcs, &pair)) return FALSE;
125 else
127 FIXME("Unsupported yet (should get info from current context)\n");
128 return FALSE;
131 if (!pair.effective->sources) return FALSE;
132 for (ptr = pair.effective->sources; *ptr; ptr += strlen(ptr) + 1)
134 /* FIXME: not using Mask */
135 sf.ModBase = ModBase;
136 sf.FileName = ptr;
137 if (!cbSrcFiles(&sf, UserContext)) break;
140 return TRUE;
143 /******************************************************************
144 * SymEnumLines (DBGHELP.@)
147 BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
148 PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
150 struct process* pcs;
151 struct module_pair pair;
152 struct hash_table_iter hti;
153 struct symt_ht* sym;
154 regex_t re;
155 struct line_info* dli;
156 void* ptr;
157 SRCCODEINFO sci;
158 char* file;
160 if (!cb) return FALSE;
161 pcs = process_find_by_handle(hProcess);
162 if (!pcs) return FALSE;
163 if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
164 if (regcomp(&re, srcfile, REG_NOSUB))
166 FIXME("Couldn't compile %s\n", srcfile);
167 SetLastError(ERROR_INVALID_PARAMETER);
168 return FALSE;
170 if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
171 pair.requested = module_find_by_addr(pcs, base, DMT_UNKNOWN);
172 if (!module_get_debug(pcs, &pair)) return FALSE;
174 sci.SizeOfStruct = sizeof(sci);
175 sci.ModBase = base;
177 hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
178 while ((ptr = hash_table_iter_up(&hti)))
180 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
181 if (sym->symt.tag != SymTagFunction) continue;
183 dli = NULL;
184 sci.FileName[0] = '\0';
185 while ((dli = vector_iter_up(&((struct symt_function*)sym)->vlines, dli)))
187 if (dli->is_source_file)
189 file = (char*)source_get(pair.effective, dli->u.source_file);
190 if (regexec(&re, file, 0, NULL, 0) != 0) file = "";
191 strcpy(sci.FileName, file);
193 else if (sci.FileName[0])
195 sci.Key = dli;
196 sci.Obj[0] = '\0'; /* FIXME */
197 sci.LineNumber = dli->line_number;
198 sci.Address = dli->u.pc_offset;
199 if (!cb(&sci, user)) break;
203 return TRUE;
206 /******************************************************************
207 * SymGetSourceFileToken (DBGHELP.@)
210 BOOL WINAPI SymGetSourceFileToken(HANDLE hProcess, ULONG64 base,
211 PCSTR src, PVOID* token, DWORD* size)
213 FIXME("%p %s %s %p %p: stub!\n",
214 hProcess, wine_dbgstr_longlong(base), debugstr_a(src), token, size);
215 SetLastError(ERROR_NOT_SUPPORTED);
216 return FALSE;