Rationale.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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: Rationale</title>
  16. <meta name="description" content="Debugging with GDB: Rationale">
  17. <meta name="keywords" content="Debugging with GDB: Rationale">
  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="Agent-Expressions.html#Agent-Expressions" rel="up" title="Agent Expressions">
  26. <link href="Target-Descriptions.html#Target-Descriptions" rel="next" title="Target Descriptions">
  27. <link href="Varying-Target-Capabilities.html#Varying-Target-Capabilities" rel="previous" title="Varying Target Capabilities">
  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="Rationale"></a>
  59. <div class="header">
  60. <p>
  61. Previous: <a href="Varying-Target-Capabilities.html#Varying-Target-Capabilities" accesskey="p" rel="previous">Varying Target Capabilities</a>, Up: <a href="Agent-Expressions.html#Agent-Expressions" accesskey="u" rel="up">Agent Expressions</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="Rationale-1"></a>
  65. <h3 class="section">F.5 Rationale</h3>
  66. <p>Some of the design decisions apparent above are arguable.
  67. </p>
  68. <dl compact="compact">
  69. <dt><b>What about stack overflow/underflow?</b></dt>
  70. <dd><p>GDB should be able to query the target to discover its stack size.
  71. Given that information, GDB can determine at translation time whether a
  72. given expression will overflow the stack. But this spec isn&rsquo;t about
  73. what kinds of error-checking GDB ought to do.
  74. </p>
  75. </dd>
  76. <dt><b>Why are you doing everything in LONGEST?</b></dt>
  77. <dd>
  78. <p>Speed isn&rsquo;t important, but agent code size is; using LONGEST brings in a
  79. bunch of support code to do things like division, etc. So this is a
  80. serious concern.
  81. </p>
  82. <p>First, note that you don&rsquo;t need different bytecodes for different
  83. operand sizes. You can generate code without <em>knowing</em> how big the
  84. stack elements actually are on the target. If the target only supports
  85. 32-bit ints, and you don&rsquo;t send any 64-bit bytecodes, everything just
  86. works. The observation here is that the MIPS and the Alpha have only
  87. fixed-size registers, and you can still get C&rsquo;s semantics even though
  88. most instructions only operate on full-sized words. You just need to
  89. make sure everything is properly sign-extended at the right times. So
  90. there is no need for 32- and 64-bit variants of the bytecodes. Just
  91. implement everything using the largest size you support.
  92. </p>
  93. <p>GDB should certainly check to see what sizes the target supports, so the
  94. user can get an error earlier, rather than later. But this information
  95. is not necessary for correctness.
  96. </p>
  97. </dd>
  98. <dt><b>Why don&rsquo;t you have <code>&gt;</code> or <code>&lt;=</code> operators?</b></dt>
  99. <dd><p>I want to keep the interpreter small, and we don&rsquo;t need them. We can
  100. combine the <code>less_</code> opcodes with <code>log_not</code>, and swap the order
  101. of the operands, yielding all four asymmetrical comparison operators.
  102. For example, <code>(x &lt;= y)</code> is <code>! (x &gt; y)</code>, which is <code>! (y &lt;
  103. x)</code>.
  104. </p>
  105. </dd>
  106. <dt><b>Why do you have <code>log_not</code>?</b></dt>
  107. <dt><b>Why do you have <code>ext</code>?</b></dt>
  108. <dt><b>Why do you have <code>zero_ext</code>?</b></dt>
  109. <dd><p>These are all easily synthesized from other instructions, but I expect
  110. them to be used frequently, and they&rsquo;re simple, so I include them to
  111. keep bytecode strings short.
  112. </p>
  113. <p><code>log_not</code> is equivalent to <code>const8 0 equal</code>; it&rsquo;s used in half
  114. the relational operators.
  115. </p>
  116. <p><code>ext <var>n</var></code> is equivalent to <code>const8 <var>s-n</var> lsh const8
  117. <var>s-n</var> rsh_signed</code>, where <var>s</var> is the size of the stack elements;
  118. it follows <code>ref<var>m</var></code> and <var>reg</var> bytecodes when the value
  119. should be signed. See the next bulleted item.
  120. </p>
  121. <p><code>zero_ext <var>n</var></code> is equivalent to <code>const<var>m</var> <var>mask</var>
  122. log_and</code>; it&rsquo;s used whenever we push the value of a register, because we
  123. can&rsquo;t assume the upper bits of the register aren&rsquo;t garbage.
  124. </p>
  125. </dd>
  126. <dt><b>Why not have sign-extending variants of the <code>ref</code> operators?</b></dt>
  127. <dd><p>Because that would double the number of <code>ref</code> operators, and we
  128. need the <code>ext</code> bytecode anyway for accessing bitfields.
  129. </p>
  130. </dd>
  131. <dt><b>Why not have constant-address variants of the <code>ref</code> operators?</b></dt>
  132. <dd><p>Because that would double the number of <code>ref</code> operators again, and
  133. <code>const32 <var>address</var> ref32</code> is only one byte longer.
  134. </p>
  135. </dd>
  136. <dt><b>Why do the <code>ref<var>n</var></code> operators have to support unaligned fetches?</b></dt>
  137. <dd><p>GDB will generate bytecode that fetches multi-byte values at unaligned
  138. addresses whenever the executable&rsquo;s debugging information tells it to.
  139. Furthermore, GDB does not know the value the pointer will have when GDB
  140. generates the bytecode, so it cannot determine whether a particular
  141. fetch will be aligned or not.
  142. </p>
  143. <p>In particular, structure bitfields may be several bytes long, but follow
  144. no alignment rules; members of packed structures are not necessarily
  145. aligned either.
  146. </p>
  147. <p>In general, there are many cases where unaligned references occur in
  148. correct C code, either at the programmer&rsquo;s explicit request, or at the
  149. compiler&rsquo;s discretion. Thus, it is simpler to make the GDB agent
  150. bytecodes work correctly in all circumstances than to make GDB guess in
  151. each case whether the compiler did the usual thing.
  152. </p>
  153. </dd>
  154. <dt><b>Why are there no side-effecting operators?</b></dt>
  155. <dd><p>Because our current client doesn&rsquo;t want them? That&rsquo;s a cheap answer. I
  156. think the real answer is that I&rsquo;m afraid of implementing function
  157. calls. We should re-visit this issue after the present contract is
  158. delivered.
  159. </p>
  160. </dd>
  161. <dt><b>Why aren&rsquo;t the <code>goto</code> ops PC-relative?</b></dt>
  162. <dd><p>The interpreter has the base address around anyway for PC bounds
  163. checking, and it seemed simpler.
  164. </p>
  165. </dd>
  166. <dt><b>Why is there only one offset size for the <code>goto</code> ops?</b></dt>
  167. <dd><p>Offsets are currently sixteen bits. I&rsquo;m not happy with this situation
  168. either:
  169. </p>
  170. <p>Suppose we have multiple branch ops with different offset sizes. As I
  171. generate code left-to-right, all my jumps are forward jumps (there are
  172. no loops in expressions), so I never know the target when I emit the
  173. jump opcode. Thus, I have to either always assume the largest offset
  174. size, or do jump relaxation on the code after I generate it, which seems
  175. like a big waste of time.
  176. </p>
  177. <p>I can imagine a reasonable expression being longer than 256 bytes. I
  178. can&rsquo;t imagine one being longer than 64k. Thus, we need 16-bit offsets.
  179. This kind of reasoning is so bogus, but relaxation is pathetic.
  180. </p>
  181. <p>The other approach would be to generate code right-to-left. Then I&rsquo;d
  182. always know my offset size. That might be fun.
  183. </p>
  184. </dd>
  185. <dt><b>Where is the function call bytecode?</b></dt>
  186. <dd>
  187. <p>When we add side-effects, we should add this.
  188. </p>
  189. </dd>
  190. <dt><b>Why does the <code>reg</code> bytecode take a 16-bit register number?</b></dt>
  191. <dd>
  192. <p>Intel&rsquo;s IA-64 architecture has 128 general-purpose registers,
  193. and 128 floating-point registers, and I&rsquo;m sure it has some random
  194. control registers.
  195. </p>
  196. </dd>
  197. <dt><b>Why do we need <code>trace</code> and <code>trace_quick</code>?</b></dt>
  198. <dd><p>Because GDB needs to record all the memory contents and registers an
  199. expression touches. If the user wants to evaluate an expression
  200. <code>x-&gt;y-&gt;z</code>, the agent must record the values of <code>x</code> and
  201. <code>x-&gt;y</code> as well as the value of <code>x-&gt;y-&gt;z</code>.
  202. </p>
  203. </dd>
  204. <dt><b>Don&rsquo;t the <code>trace</code> bytecodes make the interpreter less general?</b></dt>
  205. <dd><p>They do mean that the interpreter contains special-purpose code, but
  206. that doesn&rsquo;t mean the interpreter can only be used for that purpose. If
  207. an expression doesn&rsquo;t use the <code>trace</code> bytecodes, they don&rsquo;t get in
  208. its way.
  209. </p>
  210. </dd>
  211. <dt><b>Why doesn&rsquo;t <code>trace_quick</code> consume its arguments the way everything else does?</b></dt>
  212. <dd><p>In general, you do want your operators to consume their arguments; it&rsquo;s
  213. consistent, and generally reduces the amount of stack rearrangement
  214. necessary. However, <code>trace_quick</code> is a kludge to save space; it
  215. only exists so we needn&rsquo;t write <code>dup const8 <var>SIZE</var> trace</code>
  216. before every memory reference. Therefore, it&rsquo;s okay for it not to
  217. consume its arguments; it&rsquo;s meant for a specific context in which we
  218. know exactly what it should do with the stack. If we&rsquo;re going to have a
  219. kludge, it should be an effective kludge.
  220. </p>
  221. </dd>
  222. <dt><b>Why does <code>trace16</code> exist?</b></dt>
  223. <dd><p>That opcode was added by the customer that contracted Cygnus for the
  224. data tracing work. I personally think it is unnecessary; objects that
  225. large will be quite rare, so it is okay to use <code>dup const16
  226. <var>size</var> trace</code> in those cases.
  227. </p>
  228. <p>Whatever we decide to do with <code>trace16</code>, we should at least leave
  229. opcode 0x30 reserved, to remain compatible with the customer who added
  230. it.
  231. </p>
  232. </dd>
  233. </dl>
  234. <hr>
  235. <div class="header">
  236. <p>
  237. Previous: <a href="Varying-Target-Capabilities.html#Varying-Target-Capabilities" accesskey="p" rel="previous">Varying Target Capabilities</a>, Up: <a href="Agent-Expressions.html#Agent-Expressions" accesskey="u" rel="up">Agent Expressions</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>
  238. </div>
  239. </body>
  240. </html>