2004-04-06 00:21:27 +02:00
|
|
|
/*
|
|
|
|
* File source.c - source files management
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004, Eric Pouech.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2004-04-06 00:21:27 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "dbghelp_private.h"
|
|
|
|
#include "wine/debug.h"
|
2005-11-03 10:51:26 +01:00
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
# include <regex.h>
|
|
|
|
#endif
|
2004-04-06 00:21:27 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* source_find
|
|
|
|
*
|
|
|
|
* check whether a source file has already been stored
|
|
|
|
*/
|
|
|
|
static unsigned source_find(const struct module* module, const char* name)
|
|
|
|
{
|
|
|
|
char* ptr = module->sources;
|
|
|
|
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
if (strcmp(ptr, name) == 0) return ptr - module->sources;
|
|
|
|
ptr += strlen(ptr) + 1;
|
|
|
|
}
|
|
|
|
return (unsigned)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* source_new
|
|
|
|
*
|
|
|
|
* checks if source exists. if not, add it
|
|
|
|
*/
|
2006-06-18 21:32:20 +02:00
|
|
|
unsigned source_new(struct module* module, const char* base, const char* name)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
|
|
|
unsigned ret;
|
2006-06-18 21:32:20 +02:00
|
|
|
const char* full;
|
2006-10-28 18:27:26 +02:00
|
|
|
char* tmp = NULL;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
|
|
|
if (!name) return (unsigned)-1;
|
2006-06-18 21:32:20 +02:00
|
|
|
if (!base || *name == '/')
|
|
|
|
full = name;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned bsz = strlen(base);
|
|
|
|
|
2006-10-28 18:27:26 +02:00
|
|
|
tmp = HeapAlloc(GetProcessHeap(), 0, bsz + 1 + strlen(name) + 1);
|
2006-06-18 21:32:20 +02:00
|
|
|
if (!tmp) return (unsigned)-1;
|
|
|
|
full = tmp;
|
|
|
|
strcpy(tmp, base);
|
|
|
|
if (tmp[bsz - 1] != '/') tmp[bsz++] = '/';
|
|
|
|
strcpy(&tmp[bsz], name);
|
|
|
|
}
|
2006-11-05 17:51:35 +01:00
|
|
|
if (!module->sources || (ret = source_find(module, full)) == (unsigned)-1)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2006-11-05 17:51:35 +01:00
|
|
|
int len = strlen(full) + 1;
|
|
|
|
if (module->sources_used + len + 1 > module->sources_alloc)
|
|
|
|
{
|
|
|
|
/* Alloc by block of 256 bytes */
|
|
|
|
module->sources_alloc = (module->sources_used + len + 1 + 255) & ~255;
|
|
|
|
if (!module->sources)
|
|
|
|
module->sources = HeapAlloc(GetProcessHeap(), 0, module->sources_alloc);
|
|
|
|
else
|
|
|
|
module->sources = HeapReAlloc(GetProcessHeap(), 0, module->sources,
|
|
|
|
module->sources_alloc);
|
|
|
|
}
|
|
|
|
ret = module->sources_used;
|
|
|
|
memcpy(module->sources + module->sources_used, full, len);
|
|
|
|
module->sources_used += len;
|
|
|
|
module->sources[module->sources_used] = '\0';
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
2006-10-28 18:27:26 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, tmp);
|
2004-04-06 00:21:27 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* source_get
|
|
|
|
*
|
|
|
|
* returns a stored source file name
|
|
|
|
*/
|
|
|
|
const char* source_get(const struct module* module, unsigned idx)
|
|
|
|
{
|
|
|
|
if (idx == -1) return "";
|
|
|
|
assert(module->sources);
|
|
|
|
return module->sources + idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* SymEnumSourceFiles (DBGHELP.@)
|
|
|
|
*
|
|
|
|
*/
|
2006-01-23 16:37:48 +01:00
|
|
|
BOOL WINAPI SymEnumSourceFiles(HANDLE hProcess, ULONG64 ModBase, PCSTR Mask,
|
2005-11-04 12:16:48 +01:00
|
|
|
PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,
|
2004-10-21 21:57:56 +02:00
|
|
|
PVOID UserContext)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2006-05-07 14:29:49 +02:00
|
|
|
struct module_pair pair;
|
2004-04-06 00:21:27 +02:00
|
|
|
SOURCEFILE sf;
|
|
|
|
char* ptr;
|
|
|
|
|
|
|
|
if (!cbSrcFiles) return FALSE;
|
2006-11-24 22:17:10 +01:00
|
|
|
pair.pcs = process_find_by_handle(hProcess);
|
|
|
|
if (!pair.pcs) return FALSE;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
|
|
|
if (ModBase)
|
|
|
|
{
|
2006-11-24 22:17:10 +01:00
|
|
|
pair.requested = module_find_by_addr(pair.pcs, ModBase, DMT_UNKNOWN);
|
|
|
|
if (!module_get_debug(&pair)) return FALSE;
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Mask[0] == '!')
|
|
|
|
{
|
2007-03-13 17:33:02 +01:00
|
|
|
pair.requested = module_find_by_nameA(pair.pcs, Mask + 1);
|
2006-11-24 22:17:10 +01:00
|
|
|
if (!module_get_debug(&pair)) return FALSE;
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FIXME("Unsupported yet (should get info from current context)\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2006-05-07 14:29:49 +02:00
|
|
|
if (!pair.effective->sources) return FALSE;
|
|
|
|
for (ptr = pair.effective->sources; *ptr; ptr += strlen(ptr) + 1)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
|
|
|
/* FIXME: not using Mask */
|
|
|
|
sf.ModBase = ModBase;
|
|
|
|
sf.FileName = ptr;
|
|
|
|
if (!cbSrcFiles(&sf, UserContext)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2005-11-03 10:51:26 +01:00
|
|
|
|
2008-09-06 22:19:53 +02:00
|
|
|
static inline void re_append(char** mask, unsigned* len, char ch)
|
|
|
|
{
|
|
|
|
*mask = HeapReAlloc(GetProcessHeap(), 0, *mask, ++(*len));
|
|
|
|
(*mask)[*len - 2] = ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL compile_regex(regex_t* re, const char* srcfile)
|
|
|
|
{
|
|
|
|
char* mask;
|
|
|
|
unsigned len = 1;
|
|
|
|
|
|
|
|
mask = HeapAlloc(GetProcessHeap(), 0, 1);
|
|
|
|
re_append(&mask, &len, '^');
|
|
|
|
if (!srcfile || !*srcfile) re_append(&mask, &len, '*');
|
|
|
|
else while (*srcfile)
|
|
|
|
{
|
|
|
|
switch (*srcfile)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
case '/':
|
|
|
|
re_append(&mask, &len, '[');
|
|
|
|
re_append(&mask, &len, '\\');
|
|
|
|
re_append(&mask, &len, '\\');
|
|
|
|
re_append(&mask, &len, '/');
|
|
|
|
re_append(&mask, &len, ']');
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
re_append(&mask, &len, '\\');
|
|
|
|
re_append(&mask, &len, '.');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
re_append(&mask, &len, *srcfile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
srcfile++;
|
|
|
|
}
|
|
|
|
re_append(&mask, &len, '$');
|
|
|
|
mask[len - 1] = '\0';
|
|
|
|
len = regcomp(re, mask, REG_NOSUB);
|
|
|
|
HeapFree(GetProcessHeap(), 0, mask);
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
FIXME("Couldn't compile %s\n", mask);
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-11-03 10:51:26 +01:00
|
|
|
/******************************************************************
|
|
|
|
* SymEnumLines (DBGHELP.@)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
BOOL WINAPI SymEnumLines(HANDLE hProcess, ULONG64 base, PCSTR compiland,
|
|
|
|
PCSTR srcfile, PSYM_ENUMLINES_CALLBACK cb, PVOID user)
|
|
|
|
{
|
2006-05-07 14:29:49 +02:00
|
|
|
struct module_pair pair;
|
2005-11-03 10:51:26 +01:00
|
|
|
struct hash_table_iter hti;
|
|
|
|
struct symt_ht* sym;
|
|
|
|
regex_t re;
|
|
|
|
struct line_info* dli;
|
|
|
|
void* ptr;
|
|
|
|
SRCCODEINFO sci;
|
2006-06-01 21:02:44 +02:00
|
|
|
const char* file;
|
2005-11-03 10:51:26 +01:00
|
|
|
|
|
|
|
if (!cb) return FALSE;
|
|
|
|
if (!(dbghelp_options & SYMOPT_LOAD_LINES)) return TRUE;
|
2008-09-06 22:19:53 +02:00
|
|
|
|
2006-11-24 22:17:10 +01:00
|
|
|
pair.pcs = process_find_by_handle(hProcess);
|
|
|
|
if (!pair.pcs) return FALSE;
|
2005-11-03 10:51:26 +01:00
|
|
|
if (compiland) FIXME("Unsupported yet (filtering on compiland %s)\n", compiland);
|
2006-11-24 22:17:10 +01:00
|
|
|
pair.requested = module_find_by_addr(pair.pcs, base, DMT_UNKNOWN);
|
|
|
|
if (!module_get_debug(&pair)) return FALSE;
|
2008-09-06 22:19:53 +02:00
|
|
|
if (!compile_regex(&re, srcfile)) return FALSE;
|
2005-11-03 10:51:26 +01:00
|
|
|
|
|
|
|
sci.SizeOfStruct = sizeof(sci);
|
|
|
|
sci.ModBase = base;
|
|
|
|
|
2006-05-07 14:29:49 +02:00
|
|
|
hash_table_iter_init(&pair.effective->ht_symbols, &hti, NULL);
|
2005-11-03 10:51:26 +01:00
|
|
|
while ((ptr = hash_table_iter_up(&hti)))
|
|
|
|
{
|
2007-05-15 02:05:12 +02:00
|
|
|
int i;
|
|
|
|
|
2005-11-03 10:51:26 +01:00
|
|
|
sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
|
|
|
|
if (sym->symt.tag != SymTagFunction) continue;
|
|
|
|
|
|
|
|
sci.FileName[0] = '\0';
|
2007-05-15 02:05:12 +02:00
|
|
|
for (i=0; i<vector_length(&((struct symt_function*)sym)->vlines); i++)
|
2005-11-03 10:51:26 +01:00
|
|
|
{
|
2007-05-15 02:05:12 +02:00
|
|
|
dli = vector_at(&((struct symt_function*)sym)->vlines, i);
|
2005-11-03 10:51:26 +01:00
|
|
|
if (dli->is_source_file)
|
|
|
|
{
|
2006-06-01 21:02:44 +02:00
|
|
|
file = source_get(pair.effective, dli->u.source_file);
|
2005-11-03 10:51:26 +01:00
|
|
|
if (regexec(&re, file, 0, NULL, 0) != 0) file = "";
|
|
|
|
strcpy(sci.FileName, file);
|
|
|
|
}
|
|
|
|
else if (sci.FileName[0])
|
|
|
|
{
|
|
|
|
sci.Key = dli;
|
|
|
|
sci.Obj[0] = '\0'; /* FIXME */
|
|
|
|
sci.LineNumber = dli->line_number;
|
|
|
|
sci.Address = dli->u.pc_offset;
|
|
|
|
if (!cb(&sci, user)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-06 22:19:53 +02:00
|
|
|
regfree(&re);
|
2005-11-03 10:51:26 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
2006-05-07 14:30:12 +02:00
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* SymGetSourceFileToken (DBGHELP.@)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
BOOL WINAPI SymGetSourceFileToken(HANDLE hProcess, ULONG64 base,
|
|
|
|
PCSTR src, PVOID* token, DWORD* size)
|
|
|
|
{
|
|
|
|
FIXME("%p %s %s %p %p: stub!\n",
|
|
|
|
hProcess, wine_dbgstr_longlong(base), debugstr_a(src), token, size);
|
|
|
|
SetLastError(ERROR_NOT_SUPPORTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-05-10 21:35:30 +02:00
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* SymGetSourceFileTokenW (DBGHELP.@)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
BOOL WINAPI SymGetSourceFileTokenW(HANDLE hProcess, ULONG64 base,
|
|
|
|
PCWSTR src, PVOID* token, DWORD* size)
|
|
|
|
{
|
|
|
|
FIXME("%p %s %s %p %p: stub!\n",
|
|
|
|
hProcess, wine_dbgstr_longlong(base), debugstr_w(src), token, size);
|
|
|
|
SetLastError(ERROR_NOT_SUPPORTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|