source.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * File source.c - source file handling for internal debugger.
  3. *
  4. * Copyright (C) 1997, Eric Youngdale.
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include "config.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "debugger.h"
  24. struct open_file_list
  25. {
  26. char* path;
  27. char* real_path;
  28. struct open_file_list* next;
  29. unsigned int size;
  30. signed int nlines;
  31. unsigned int* linelist;
  32. };
  33. void source_show_path(void)
  34. {
  35. const char* ptr;
  36. const char* next;
  37. dbg_printf("Search list:\n");
  38. for (ptr = dbg_curr_process->search_path; ptr; ptr = next)
  39. {
  40. next = strchr(ptr, ';');
  41. if (next)
  42. dbg_printf("\t%.*s\n", (int)(next++ - ptr), ptr);
  43. else
  44. dbg_printf("\t%s\n", ptr);
  45. }
  46. dbg_printf("\n");
  47. }
  48. void source_add_path(const char* path)
  49. {
  50. char* new;
  51. unsigned size;
  52. size = strlen(path) + 1;
  53. if (dbg_curr_process->search_path)
  54. {
  55. unsigned pos = strlen(dbg_curr_process->search_path) + 1;
  56. new = HeapReAlloc(GetProcessHeap(), 0, dbg_curr_process->search_path, pos + size);
  57. if (!new) return;
  58. new[pos - 1] = ';';
  59. strcpy(&new[pos], path);
  60. }
  61. else
  62. {
  63. new = HeapAlloc(GetProcessHeap(), 0, size);
  64. if (!new) return;
  65. strcpy(new, path);
  66. }
  67. dbg_curr_process->search_path = new;
  68. }
  69. void source_nuke_path(struct dbg_process* p)
  70. {
  71. HeapFree(GetProcessHeap(), 0, p->search_path);
  72. p->search_path = NULL;
  73. }
  74. static void* source_map_file(const char* name, HANDLE* hMap, unsigned* size)
  75. {
  76. HANDLE hFile;
  77. hFile = CreateFileA(name, GENERIC_READ, FILE_SHARE_READ, NULL,
  78. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  79. if (hFile == INVALID_HANDLE_VALUE) return (void*)-1;
  80. if (size != NULL && (*size = GetFileSize(hFile, NULL)) == INVALID_FILE_SIZE) {
  81. CloseHandle(hFile);
  82. return (void*)-1;
  83. }
  84. *hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  85. CloseHandle(hFile);
  86. if (!*hMap) return (void*)-1;
  87. return MapViewOfFile(*hMap, FILE_MAP_READ, 0, 0, 0);
  88. }
  89. static void source_unmap_file(void* addr, HANDLE hMap)
  90. {
  91. UnmapViewOfFile(addr);
  92. CloseHandle(hMap);
  93. }
  94. static struct open_file_list* source_search_open_file(const char* name)
  95. {
  96. struct open_file_list* ol;
  97. for (ol = dbg_curr_process->source_ofiles; ol; ol = ol->next)
  98. {
  99. if (strcmp(ol->path, name) == 0) break;
  100. }
  101. return ol;
  102. }
  103. static BOOL source_locate_file(const char* srcfile, char* path)
  104. {
  105. BOOL found = FALSE;
  106. if (GetFileAttributesA(srcfile) != INVALID_FILE_ATTRIBUTES)
  107. {
  108. strcpy(path, srcfile);
  109. found = TRUE;
  110. }
  111. else if (dbg_curr_process->search_path)
  112. {
  113. const char* spath;
  114. spath = srcfile;
  115. while (!found)
  116. {
  117. while (*spath && *spath != '/' && *spath != '\\') spath++;
  118. if (!*spath) break;
  119. spath++;
  120. found = SearchPathA(dbg_curr_process->search_path, spath, NULL, MAX_PATH, path, NULL);
  121. }
  122. }
  123. return found;
  124. }
  125. static struct open_file_list* source_add_file(const char* name, const char* realpath)
  126. {
  127. struct open_file_list* ol;
  128. size_t sz, nlen;
  129. sz = sizeof(*ol);
  130. nlen = strlen(name) + 1;
  131. if (realpath) sz += strlen(realpath) + 1;
  132. ol = HeapAlloc(GetProcessHeap(), 0, sz + nlen);
  133. if (!ol) return NULL;
  134. strcpy(ol->path = (char*)(ol + 1), name);
  135. if (realpath)
  136. strcpy(ol->real_path = ol->path + nlen, realpath);
  137. else
  138. ol->real_path = NULL;
  139. ol->next = dbg_curr_process->source_ofiles;
  140. ol->nlines = 0;
  141. ol->linelist = NULL;
  142. ol->size = 0;
  143. return dbg_curr_process->source_ofiles = ol;
  144. }
  145. static int source_display(const char* sourcefile, int start, int end)
  146. {
  147. char* addr;
  148. int i;
  149. struct open_file_list* ol;
  150. int nlines;
  151. const char* basename = NULL;
  152. char* pnt;
  153. int rtn;
  154. HANDLE hMap;
  155. char tmppath[MAX_PATH];
  156. /*
  157. * First see whether we have the file open already. If so, then
  158. * use that, otherwise we have to try and open it.
  159. */
  160. ol = source_search_open_file(sourcefile);
  161. if (ol == NULL)
  162. {
  163. /*
  164. * Try again, stripping the path from the opened file.
  165. */
  166. basename = strrchr(sourcefile, '\\');
  167. if (!basename) basename = strrchr(sourcefile, '/');
  168. if (!basename) basename = sourcefile;
  169. else basename++;
  170. ol = source_search_open_file(basename);
  171. }
  172. if (ol == NULL)
  173. {
  174. /*
  175. * Crapola. We need to try and open the file.
  176. */
  177. if (!source_locate_file(sourcefile, tmppath))
  178. {
  179. if (dbg_interactiveP)
  180. {
  181. char zbuf[256];
  182. for (;;)
  183. {
  184. size_t len;
  185. /*
  186. * Still couldn't find it. Ask user for path to add.
  187. */
  188. snprintf(zbuf, sizeof(zbuf), "Enter path to file '%s' (<cr> to end search): ", sourcefile);
  189. input_read_line(zbuf, tmppath, sizeof(tmppath));
  190. if (!(len = strlen(tmppath))) break;
  191. /* append '/' if missing at the end */
  192. if (tmppath[len - 1] != '/' && tmppath[len - 1] != '\\')
  193. tmppath[len++] = '/';
  194. strcpy(&tmppath[len], basename);
  195. if (GetFileAttributesA(tmppath) != INVALID_FILE_ATTRIBUTES)
  196. break;
  197. dbg_printf("Unable to access file '%s'\n", tmppath);
  198. }
  199. }
  200. else
  201. {
  202. dbg_printf("Unable to access file '%s'\n", sourcefile);
  203. tmppath[0] = '\0';
  204. }
  205. if (!tmppath[0])
  206. {
  207. /*
  208. * OK, I guess the user doesn't really want to see it
  209. * after all.
  210. */
  211. source_add_file(sourcefile, NULL);
  212. return FALSE;
  213. }
  214. }
  215. /*
  216. * Create header for file.
  217. */
  218. ol = source_add_file(sourcefile, tmppath);
  219. addr = source_map_file(tmppath, &hMap, &ol->size);
  220. if (addr == (char*)-1) return FALSE;
  221. /*
  222. * Now build up the line number mapping table.
  223. */
  224. ol->nlines = 1;
  225. pnt = addr;
  226. while (pnt < addr + ol->size)
  227. {
  228. if (*pnt++ == '\n') ol->nlines++;
  229. }
  230. ol->nlines++;
  231. ol->linelist = HeapAlloc(GetProcessHeap(), 0, ol->nlines * sizeof(unsigned int));
  232. nlines = 0;
  233. pnt = addr;
  234. ol->linelist[nlines++] = 0;
  235. while (pnt < addr + ol->size)
  236. {
  237. if (*pnt++ == '\n') ol->linelist[nlines++] = pnt - addr;
  238. }
  239. ol->linelist[nlines++] = pnt - addr;
  240. }
  241. else
  242. {
  243. addr = source_map_file(ol->real_path, &hMap, NULL);
  244. if (addr == (char*)-1) return FALSE;
  245. }
  246. /*
  247. * All we need to do is to display the source lines here.
  248. */
  249. rtn = FALSE;
  250. for (i = start - 1; i <= end - 1; i++)
  251. {
  252. char buffer[1024];
  253. if (i < 0 || i >= ol->nlines - 1) continue;
  254. rtn = TRUE;
  255. memset(&buffer, 0, sizeof(buffer));
  256. if (ol->linelist[i+1] != ol->linelist[i])
  257. {
  258. memcpy(&buffer, addr + ol->linelist[i],
  259. (ol->linelist[i+1] - ol->linelist[i]) - 1);
  260. }
  261. dbg_printf("%d\t%s\n", i + 1, buffer);
  262. }
  263. source_unmap_file(addr, hMap);
  264. return rtn;
  265. }
  266. void source_list(IMAGEHLP_LINE64* src1, IMAGEHLP_LINE64* src2, int delta)
  267. {
  268. int end;
  269. int start;
  270. const char* sourcefile;
  271. /*
  272. * We need to see what source file we need. Hopefully we only have
  273. * one specified, otherwise we might as well punt.
  274. */
  275. if (src1 && src2 && src1->FileName && src2->FileName &&
  276. strcmp(src1->FileName, src2->FileName) != 0)
  277. {
  278. dbg_printf("Ambiguous source file specification.\n");
  279. return;
  280. }
  281. sourcefile = NULL;
  282. if (src1 && src1->FileName) sourcefile = src1->FileName;
  283. if (!sourcefile && src2 && src2->FileName) sourcefile = src2->FileName;
  284. if (!sourcefile) sourcefile = dbg_curr_process->source_current_file;
  285. /*
  286. * Now figure out the line number range to be listed.
  287. */
  288. start = end = -1;
  289. if (src1) start = src1->LineNumber;
  290. if (src2) end = src2->LineNumber;
  291. if (start == -1 && end == -1)
  292. {
  293. if (delta < 0)
  294. {
  295. end = dbg_curr_process->source_start_line;
  296. start = end + delta;
  297. }
  298. else
  299. {
  300. start = dbg_curr_process->source_end_line;
  301. end = start + delta;
  302. }
  303. }
  304. else if (start == -1) start = end + delta;
  305. else if (end == -1) end = start + delta;
  306. /*
  307. * Now call this function to do the dirty work.
  308. */
  309. source_display(sourcefile, start, end);
  310. if (sourcefile != dbg_curr_process->source_current_file)
  311. strcpy(dbg_curr_process->source_current_file, sourcefile);
  312. dbg_curr_process->source_start_line = start;
  313. dbg_curr_process->source_end_line = end;
  314. }
  315. void source_list_from_addr(const ADDRESS64* addr, int nlines)
  316. {
  317. IMAGEHLP_LINE64 il;
  318. ADDRESS64 la;
  319. DWORD disp;
  320. if (!addr)
  321. {
  322. memory_get_current_pc(&la);
  323. addr = &la;
  324. }
  325. il.SizeOfStruct = sizeof(il);
  326. if (SymGetLineFromAddr64(dbg_curr_process->handle,
  327. (DWORD_PTR)memory_to_linear_addr(addr),
  328. &disp, &il))
  329. source_list(&il, NULL, nlines);
  330. }
  331. void source_free_files(struct dbg_process* p)
  332. {
  333. struct open_file_list* ofile;
  334. struct open_file_list* ofile_next;
  335. for (ofile = p->source_ofiles; ofile; ofile = ofile_next)
  336. {
  337. ofile_next = ofile->next;
  338. HeapFree(GetProcessHeap(), 0, ofile->linelist);
  339. HeapFree(GetProcessHeap(), 0, ofile);
  340. }
  341. }