You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Source-Path.html 18KB

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