source.c 11 KB

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