Source-Path.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Source Path</title>
  16. <meta name="description" content="Debugging with GDB: Source Path">
  17. <meta name="keywords" content="Debugging with GDB: Source Path">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Source.html#Source" rel="up" title="Source">
  26. <link href="Machine-Code.html#Machine-Code" rel="next" title="Machine Code">
  27. <link href="Search.html#Search" rel="previous" title="Search">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Source-Path"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Machine-Code.html#Machine-Code" accesskey="n" rel="next">Machine Code</a>, Previous: <a href="Search.html#Search" accesskey="p" rel="previous">Search</a>, Up: <a href="Source.html#Source" accesskey="u" rel="up">Source</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  62. </div>
  63. <hr>
  64. <a name="Specifying-Source-Directories"></a>
  65. <h3 class="section">9.5 Specifying Source Directories</h3>
  66. <a name="index-source-path"></a>
  67. <a name="index-directories-for-source-files"></a>
  68. <p>Executable programs sometimes do not record the directories of the source
  69. files from which they were compiled, just the names. Even when they do,
  70. the directories could be moved between the compilation and your debugging
  71. session. <small>GDB</small> has a list of directories to search for source files;
  72. this is called the <em>source path</em>. Each time <small>GDB</small> wants a source file,
  73. it tries all the directories in the list, in the order they are present
  74. in the list, until it finds a file with the desired name.
  75. </p>
  76. <p>For example, suppose an executable references the file
  77. <samp>/usr/src/foo-1.0/lib/foo.c</samp>, does not record a compilation
  78. directory, and the <em>source path</em> is <samp>/mnt/cross</samp>.
  79. <small>GDB</small> would look for the source file in the following
  80. locations:
  81. </p>
  82. <ol>
  83. <li> <samp>/usr/src/foo-1.0/lib/foo.c</samp>
  84. </li><li> <samp>/mnt/cross/usr/src/foo-1.0/lib/foo.c</samp>
  85. </li><li> <samp>/mnt/cross/foo.c</samp>
  86. </li></ol>
  87. <p>If the source file is not present at any of the above locations then
  88. an error is printed. <small>GDB</small> does not look up the parts of the
  89. source file name, such as <samp>/mnt/cross/src/foo-1.0/lib/foo.c</samp>.
  90. Likewise, the subdirectories of the source path are not searched: if
  91. the source path is <samp>/mnt/cross</samp>, and the binary refers to
  92. <samp>foo.c</samp>, <small>GDB</small> would not find it under
  93. <samp>/mnt/cross/usr/src/foo-1.0/lib</samp>.
  94. </p>
  95. <p>Plain file names, relative file names with leading directories, file
  96. names containing dots, etc. are all treated as described above,
  97. except that non-absolute file names are not looked up literally. If
  98. the <em>source path</em> is <samp>/mnt/cross</samp>, the source file is
  99. recorded as <samp>../lib/foo.c</samp>, and no compilation directory is
  100. recorded, then <small>GDB</small> will search in the following locations:
  101. </p>
  102. <ol>
  103. <li> <samp>/mnt/cross/../lib/foo.c</samp>
  104. </li><li> <samp>/mnt/cross/foo.c</samp>
  105. </li></ol>
  106. <a name="index-cdir"></a>
  107. <a name="index-cwd"></a>
  108. <a name="index-_0024cdir_002c-convenience-variable"></a>
  109. <a name="index-_0024cwd_002c-convenience-variable"></a>
  110. <a name="index-compilation-directory"></a>
  111. <a name="index-current-directory"></a>
  112. <a name="index-working-directory"></a>
  113. <a name="index-directory_002c-current"></a>
  114. <a name="index-directory_002c-compilation"></a>
  115. <p>The <em>source path</em> will always include two special entries
  116. &lsquo;<samp>$cdir</samp>&rsquo; and &lsquo;<samp>$cwd</samp>&rsquo;, these refer to the compilation directory
  117. (if one is recorded) and the current working directory respectively.
  118. </p>
  119. <p>&lsquo;<samp>$cdir</samp>&rsquo; causes <small>GDB</small> to search within the compilation
  120. directory, if one is recorded in the debug information. If no
  121. compilation directory is recorded in the debug information then
  122. &lsquo;<samp>$cdir</samp>&rsquo; is ignored.
  123. </p>
  124. <p>&lsquo;<samp>$cwd</samp>&rsquo; is not the same as &lsquo;<samp>.</samp>&rsquo;&mdash;the former tracks the
  125. current working directory as it changes during your <small>GDB</small>
  126. session, while the latter is immediately expanded to the current
  127. directory at the time you add an entry to the source path.
  128. </p>
  129. <p>If a compilation directory is recorded in the debug information, and
  130. <small>GDB</small> has not found the source file after the first search
  131. using <em>source path</em>, then <small>GDB</small> will combine the
  132. compilation directory and the filename, and then search for the source
  133. file again using the <em>source path</em>.
  134. </p>
  135. <p>For example, if the executable records the source file as
  136. <samp>/usr/src/foo-1.0/lib/foo.c</samp>, the compilation directory is
  137. recorded as <samp>/project/build</samp>, and the <em>source path</em> is
  138. <samp>/mnt/cross:$cdir:$cwd</samp> while the current working directory of
  139. the <small>GDB</small> session is <samp>/home/user</samp>, then <small>GDB</small> will
  140. search for the source file in the following locations:
  141. </p>
  142. <ol>
  143. <li> <samp>/usr/src/foo-1.0/lib/foo.c</samp>
  144. </li><li> <samp>/mnt/cross/usr/src/foo-1.0/lib/foo.c</samp>
  145. </li><li> <samp>/project/build/usr/src/foo-1.0/lib/foo.c</samp>
  146. </li><li> <samp>/home/user/usr/src/foo-1.0/lib/foo.c</samp>
  147. </li><li> <samp>/mnt/cross/project/build/usr/src/foo-1.0/lib/foo.c</samp>
  148. </li><li> <samp>/project/build/project/build/usr/src/foo-1.0/lib/foo.c</samp>
  149. </li><li> <samp>/home/user/project/build/usr/src/foo-1.0/lib/foo.c</samp>
  150. </li><li> <samp>/mnt/cross/foo.c</samp>
  151. </li><li> <samp>/project/build/foo.c</samp>
  152. </li><li> <samp>/home/user/foo.c</samp>
  153. </li></ol>
  154. <p>If the file name in the previous example had been recorded in the
  155. executable as a relative path rather than an absolute path, then the
  156. first look up would not have occurred, but all of the remaining steps
  157. would be similar.
  158. </p>
  159. <p>When searching for source files on MS-DOS and MS-Windows, where
  160. absolute paths start with a drive letter (e.g.
  161. <samp>C:/project/foo.c</samp>), <small>GDB</small> will remove the drive letter
  162. from the file name before appending it to a search directory from
  163. <em>source path</em>; for instance if the executable references the
  164. source file <samp>C:/project/foo.c</samp> and <em>source path</em> is set to
  165. <samp>D:/mnt/cross</samp>, then <small>GDB</small> will search in the following
  166. locations for the source file:
  167. </p>
  168. <ol>
  169. <li> <samp>C:/project/foo.c</samp>
  170. </li><li> <samp>D:/mnt/cross/project/foo.c</samp>
  171. </li><li> <samp>D:/mnt/cross/foo.c</samp>
  172. </li></ol>
  173. <p>Note that the executable search path is <em>not</em> used to locate the
  174. source files.
  175. </p>
  176. <p>Whenever you reset or rearrange the source path, <small>GDB</small> clears out
  177. any information it has cached about where source files are found and where
  178. each line is in the file.
  179. </p>
  180. <a name="index-directory"></a>
  181. <a name="index-dir"></a>
  182. <p>When you start <small>GDB</small>, its source path includes only &lsquo;<samp>$cdir</samp>&rsquo;
  183. and &lsquo;<samp>$cwd</samp>&rsquo;, in that order.
  184. To add other directories, use the <code>directory</code> command.
  185. </p>
  186. <p>The search path is used to find both program source files and <small>GDB</small>
  187. script files (read using the &lsquo;<samp>-command</samp>&rsquo; option and &lsquo;<samp>source</samp>&rsquo; command).
  188. </p>
  189. <p>In addition to the source path, <small>GDB</small> provides a set of commands
  190. that manage a list of source path substitution rules. A <em>substitution
  191. rule</em> specifies how to rewrite source directories stored in the program&rsquo;s
  192. debug information in case the sources were moved to a different
  193. directory between compilation and debugging. A rule is made of
  194. two strings, the first specifying what needs to be rewritten in
  195. the path, and the second specifying how it should be rewritten.
  196. In <a href="#set-substitute_002dpath">set substitute-path</a>, we name these two parts <var>from</var> and
  197. <var>to</var> respectively. <small>GDB</small> does a simple string replacement
  198. of <var>from</var> with <var>to</var> at the start of the directory part of the
  199. source file name, and uses that result instead of the original file
  200. name to look up the sources.
  201. </p>
  202. <p>Using the previous example, suppose the <samp>foo-1.0</samp> tree has been
  203. moved from <samp>/usr/src</samp> to <samp>/mnt/cross</samp>, then you can tell
  204. <small>GDB</small> to replace <samp>/usr/src</samp> in all source path names with
  205. <samp>/mnt/cross</samp>. The first lookup will then be
  206. <samp>/mnt/cross/foo-1.0/lib/foo.c</samp> in place of the original location
  207. of <samp>/usr/src/foo-1.0/lib/foo.c</samp>. To define a source path
  208. substitution rule, use the <code>set substitute-path</code> command
  209. (see <a href="#set-substitute_002dpath">set substitute-path</a>).
  210. </p>
  211. <p>To avoid unexpected substitution results, a rule is applied only if the
  212. <var>from</var> part of the directory name ends at a directory separator.
  213. For instance, a rule substituting <samp>/usr/source</samp> into
  214. <samp>/mnt/cross</samp> will be applied to <samp>/usr/source/foo-1.0</samp> but
  215. not to <samp>/usr/sourceware/foo-2.0</samp>. And because the substitution
  216. is applied only at the beginning of the directory name, this rule will
  217. not be applied to <samp>/root/usr/source/baz.c</samp> either.
  218. </p>
  219. <p>In many cases, you can achieve the same result using the <code>directory</code>
  220. command. However, <code>set substitute-path</code> can be more efficient in
  221. the case where the sources are organized in a complex tree with multiple
  222. subdirectories. With the <code>directory</code> command, you need to add each
  223. subdirectory of your project. If you moved the entire tree while
  224. preserving its internal organization, then <code>set substitute-path</code>
  225. allows you to direct the debugger to all the sources with one single
  226. command.
  227. </p>
  228. <p><code>set substitute-path</code> is also more than just a shortcut command.
  229. The source path is only used if the file at the original location no
  230. longer exists. On the other hand, <code>set substitute-path</code> modifies
  231. the debugger behavior to look at the rewritten location instead. So, if
  232. for any reason a source file that is not relevant to your executable is
  233. located at the original location, a substitution rule is the only
  234. method available to point <small>GDB</small> at the new location.
  235. </p>
  236. <a name="index-_002d_002dwith_002drelocated_002dsources"></a>
  237. <a name="index-default-source-path-substitution"></a>
  238. <p>You can configure a default source path substitution rule by
  239. configuring <small>GDB</small> with the
  240. &lsquo;<samp>--with-relocated-sources=<var>dir</var></samp>&rsquo; option. The <var>dir</var>
  241. should be the name of a directory under <small>GDB</small>&rsquo;s configured
  242. prefix (set with &lsquo;<samp>--prefix</samp>&rsquo; or &lsquo;<samp>--exec-prefix</samp>&rsquo;), and
  243. directory names in debug information under <var>dir</var> will be adjusted
  244. automatically if the installed <small>GDB</small> is moved to a new
  245. location. This is useful if <small>GDB</small>, libraries or executables
  246. with debug information and corresponding source code are being moved
  247. together.
  248. </p>
  249. <dl compact="compact">
  250. <dt><code>directory <var>dirname</var> &hellip;</code></dt>
  251. <dt><code>dir <var>dirname</var> &hellip;</code></dt>
  252. <dd><p>Add directory <var>dirname</var> to the front of the source path. Several
  253. directory names may be given to this command, separated by &lsquo;<samp>:</samp>&rsquo;
  254. (&lsquo;<samp>;</samp>&rsquo; on MS-DOS and MS-Windows, where &lsquo;<samp>:</samp>&rsquo; usually appears as
  255. part of absolute file names) or
  256. whitespace. You may specify a directory that is already in the source
  257. path; this moves it forward, so <small>GDB</small> searches it sooner.
  258. </p>
  259. <p>The special strings &lsquo;<samp>$cdir</samp>&rsquo; (to refer to the compilation
  260. directory, if one is recorded), and &lsquo;<samp>$cwd</samp>&rsquo; (to refer to the
  261. current working directory) can also be included in the list of
  262. directories <var>dirname</var>. Though these will already be in the source
  263. path they will be moved forward in the list so <small>GDB</small> searches
  264. them sooner.
  265. </p>
  266. </dd>
  267. <dt><code>directory</code></dt>
  268. <dd><p>Reset the source path to its default value (&lsquo;<samp>$cdir:$cwd</samp>&rsquo; on Unix systems). This requires confirmation.
  269. </p>
  270. </dd>
  271. <dt><code>set directories <var>path-list</var></code></dt>
  272. <dd><a name="index-set-directories"></a>
  273. <p>Set the source path to <var>path-list</var>.
  274. &lsquo;<samp>$cdir:$cwd</samp>&rsquo; are added if missing.
  275. </p>
  276. </dd>
  277. <dt><code>show directories</code></dt>
  278. <dd><a name="index-show-directories"></a>
  279. <p>Print the source path: show which directories it contains.
  280. </p>
  281. <a name="set-substitute_002dpath"></a></dd>
  282. <dt><code>set substitute-path <var>from</var> <var>to</var></code></dt>
  283. <dd><a name="index-set-substitute_002dpath"></a>
  284. <p>Define a source path substitution rule, and add it at the end of the
  285. current list of existing substitution rules. If a rule with the same
  286. <var>from</var> was already defined, then the old rule is also deleted.
  287. </p>
  288. <p>For example, if the file <samp>/foo/bar/baz.c</samp> was moved to
  289. <samp>/mnt/cross/baz.c</samp>, then the command
  290. </p>
  291. <div class="smallexample">
  292. <pre class="smallexample">(gdb) set substitute-path /foo/bar /mnt/cross
  293. </pre></div>
  294. <p>will tell <small>GDB</small> to replace &lsquo;<samp>/foo/bar</samp>&rsquo; with
  295. &lsquo;<samp>/mnt/cross</samp>&rsquo;, which will allow <small>GDB</small> to find the file
  296. <samp>baz.c</samp> even though it was moved.
  297. </p>
  298. <p>In the case when more than one substitution rule have been defined,
  299. the rules are evaluated one by one in the order where they have been
  300. defined. The first one matching, if any, is selected to perform
  301. the substitution.
  302. </p>
  303. <p>For instance, if we had entered the following commands:
  304. </p>
  305. <div class="smallexample">
  306. <pre class="smallexample">(gdb) set substitute-path /usr/src/include /mnt/include
  307. (gdb) set substitute-path /usr/src /mnt/src
  308. </pre></div>
  309. <p><small>GDB</small> would then rewrite <samp>/usr/src/include/defs.h</samp> into
  310. <samp>/mnt/include/defs.h</samp> by using the first rule. However, it would
  311. use the second rule to rewrite <samp>/usr/src/lib/foo.c</samp> into
  312. <samp>/mnt/src/lib/foo.c</samp>.
  313. </p>
  314. </dd>
  315. <dt><code>unset substitute-path [path]</code></dt>
  316. <dd><a name="index-unset-substitute_002dpath"></a>
  317. <p>If a path is specified, search the current list of substitution rules
  318. for a rule that would rewrite that path. Delete that rule if found.
  319. A warning is emitted by the debugger if no rule could be found.
  320. </p>
  321. <p>If no path is specified, then all substitution rules are deleted.
  322. </p>
  323. </dd>
  324. <dt><code>show substitute-path [path]</code></dt>
  325. <dd><a name="index-show-substitute_002dpath"></a>
  326. <p>If a path is specified, then print the source path substitution rule
  327. which would rewrite that path, if any.
  328. </p>
  329. <p>If no path is specified, then print all existing source path substitution
  330. rules.
  331. </p>
  332. </dd>
  333. </dl>
  334. <p>If your source path is cluttered with directories that are no longer of
  335. interest, <small>GDB</small> may sometimes cause confusion by finding the wrong
  336. versions of source. You can correct the situation as follows:
  337. </p>
  338. <ol>
  339. <li> Use <code>directory</code> with no argument to reset the source path to its default value.
  340. </li><li> Use <code>directory</code> with suitable arguments to reinstall the
  341. directories you want in the source path. You can add all the
  342. directories in one command.
  343. </li></ol>
  344. <hr>
  345. <div class="header">
  346. <p>
  347. Next: <a href="Machine-Code.html#Machine-Code" accesskey="n" rel="next">Machine Code</a>, Previous: <a href="Search.html#Search" accesskey="p" rel="previous">Search</a>, Up: <a href="Source.html#Source" accesskey="u" rel="up">Source</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  348. </div>
  349. </body>
  350. </html>