xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/guile/README (revision bb16d22702ff57c46e117881dd16b08ca16721cc)
1*bb16d227SchristosREADME for gdb/guile
2*bb16d227Schristos====================
3*bb16d227Schristos
4*bb16d227SchristosThis file contains important notes for gdb/guile developers.
5*bb16d227Schristos["gdb/guile" refers to the directory you found this file in]
6*bb16d227Schristos
7*bb16d227SchristosNomenclature:
8*bb16d227Schristos
9*bb16d227Schristos  In the implementation we use "Scheme" or "Guile" depending on context.
10*bb16d227Schristos  And sometimes it doesn't matter.
11*bb16d227Schristos  Guile is Scheme, and for the most part this is what we present to the user
12*bb16d227Schristos  as well.  However, to highlight the fact that it is Guile, the GDB commands
13*bb16d227Schristos  that invoke Scheme functions are named "guile" and "guile-repl",
14*bb16d227Schristos  abbreviated "gu" and "gr" respectively.
15*bb16d227Schristos
16*bb16d227SchristosCo-existence with Python:
17*bb16d227Schristos
18*bb16d227Schristos  Keep the user interfaces reasonably consistent, but don't shy away from
19*bb16d227Schristos  providing a clearer (or more Scheme-friendly/consistent) user interface
20*bb16d227Schristos  where appropriate.
21*bb16d227Schristos
22*bb16d227Schristos  Additions to Python support or Scheme support don't require corresponding
23*bb16d227Schristos  changes in the other scripting language.
24*bb16d227Schristos
25*bb16d227Schristos  Scheme-wrapped breakpoints are created lazily so that if the user
26*bb16d227Schristos  doesn't use Scheme s/he doesn't pay any cost.
27*bb16d227Schristos
28*bb16d227SchristosImporting the gdb module into Scheme:
29*bb16d227Schristos
30*bb16d227Schristos  To import the gdb module:
31*bb16d227Schristos  (gdb) guile (use-modules (gdb))
32*bb16d227Schristos
33*bb16d227Schristos  If you want to add a prefix to gdb module symbols:
34*bb16d227Schristos  (gdb) guile (use-modules ((gdb) #:renamer (symbol-prefix-proc 'gdb:)))
35*bb16d227Schristos  This gives every symbol a "gdb:" prefix which is a common convention.
36*bb16d227Schristos  OTOH it's more to type.
37*bb16d227Schristos
38*bb16d227SchristosImplementation/Hacking notes:
39*bb16d227Schristos
40*bb16d227Schristos  Don't use scm_is_false.
41*bb16d227Schristos  For this C function, () == #f (a la Lisp) and it's not clear how treating
42*bb16d227Schristos  them as equivalent for truth values will affect the GDB interface.
43*bb16d227Schristos  Until the effect is clear avoid them.
44*bb16d227Schristos  Instead use gdbscm_is_false, gdbscm_is_true, gdbscm_is_bool.
45*bb16d227Schristos  There are macros in guile-internal.h to enforce this.
46*bb16d227Schristos
47*bb16d227Schristos  Use gdbscm_foo as the name of functions that implement Scheme procedures
48*bb16d227Schristos  to provide consistent naming in error messages.  The user can see "gdbscm"
49*bb16d227Schristos  in the name and immediately know where the function came from.
50*bb16d227Schristos
51*bb16d227Schristos  All smobs contain gdb_smob or chained_gdb_smob as the first member.
52*bb16d227Schristos  This provides a mechanism for extending them in the Scheme side without
53*bb16d227Schristos  tying GDB to the details.
54*bb16d227Schristos
55*bb16d227Schristos  The lifetime of a smob, AIUI, is decided by the containing SCM.
56*bb16d227Schristos  When there is no longer a reference to the containing SCM then the
57*bb16d227Schristos  smob can be GC'd.  Objects that have references from outside of Scheme,
58*bb16d227Schristos  e.g., breakpoints, need to be protected from GC.
59*bb16d227Schristos
60*bb16d227Schristos  Don't do something that can cause a Scheme exception inside a TRY_CATCH,
61*bb16d227Schristos  and, in code that can be called from Scheme, don't do something that can
62*bb16d227Schristos  cause a GDB exception outside a TRY_CATCH.
63*bb16d227Schristos  This makes the code a little tricky to write sometimes, but it is a
64*bb16d227Schristos  rule imposed by the programming environment.  Bugs often happen because
65*bb16d227Schristos  this rule is broken.  Learn it, follow it.
66*bb16d227Schristos
67*bb16d227SchristosCoding style notes:
68*bb16d227Schristos
69*bb16d227Schristos  - If you find violations to these rules, let's fix the code.
70*bb16d227Schristos    Some attempt has been made to be consistent, but it's early.
71*bb16d227Schristos    Over time we want things to be more consistent, not less.
72*bb16d227Schristos
73*bb16d227Schristos  - None of this really needs to be read.  Instead, do not be creative:
74*bb16d227Schristos    Monkey-See-Monkey-Do hacking should generally Just Work.
75*bb16d227Schristos
76*bb16d227Schristos  - Absence of the word "typically" means the rule is reasonably strict.
77*bb16d227Schristos
78*bb16d227Schristos  - The gdbscm_initialize_foo function (e.g., gdbscm_initialize_values)
79*bb16d227Schristos    is the last thing to appear in the file, immediately preceded by any
80*bb16d227Schristos    tables of exported variables and functions.
81*bb16d227Schristos
82*bb16d227Schristos  - In addition to these of course, follow GDB coding conventions.
83*bb16d227Schristos
84*bb16d227SchristosGeneral naming rules:
85*bb16d227Schristos
86*bb16d227Schristos  - The word "object" absent any modifier (like "GOOPS object") means a
87*bb16d227Schristos    Scheme object (of any type), and is never used otherwise.
88*bb16d227Schristos    If you want to refer to, e.g., a GOOPS object, say "GOOPS object".
89*bb16d227Schristos
90*bb16d227Schristos  - Do not begin any function, global variable, etc. name with scm_.
91*bb16d227Schristos    That's what the Guile implementation uses.
92*bb16d227Schristos    (kinda obvious, just being complete).
93*bb16d227Schristos
94*bb16d227Schristos  - The word "invalid" carries a specific connotation.  Try not to use it
95*bb16d227Schristos    in a different way.  It means the underlying GDB object has disappeared.
96*bb16d227Schristos    For example, a <gdb:objfile> smob becomes "invalid" when the underlying
97*bb16d227Schristos    objfile is removed from GDB.
98*bb16d227Schristos
99*bb16d227Schristos  - We typically use the word "exception" to mean Scheme exceptions,
100*bb16d227Schristos    and we typically use the word "error" to mean GDB errors.
101*bb16d227Schristos
102*bb16d227SchristosComments:
103*bb16d227Schristos
104*bb16d227Schristos  - function comments for functions implementing Scheme procedures begin with
105*bb16d227Schristos    a description of the Scheme usage.  Example:
106*bb16d227Schristos    /* (gsmob-aux gsmob) -> object */
107*bb16d227Schristos
108*bb16d227Schristos  - the following comment appears after the copyright header:
109*bb16d227Schristos    /* See README file in this directory for implementation notes, coding
110*bb16d227Schristos       conventions, et.al.  */
111*bb16d227Schristos
112*bb16d227SchristosSmob naming:
113*bb16d227Schristos
114*bb16d227Schristos  - gdb smobs are named, internally, "gdb:foo"
115*bb16d227Schristos  - in Guile they become <gdb:foo>, that is the convention for naming classes
116*bb16d227Schristos    and smobs have rudimentary GOOPS support (they can't be inherited from,
117*bb16d227Schristos    but generics can work with them)
118*bb16d227Schristos  - in comments use the Guile naming for smobs,
119*bb16d227Schristos    i.e., <gdb:foo> instead of gdb:foo.
120*bb16d227Schristos    Note: This only applies to smobs.  Exceptions are also named gdb:foo,
121*bb16d227Schristos    but since they are not "classes" they are not wrapped in <>.
122*bb16d227Schristos  - smob names are stored in a global, and for simplicity we pass this
123*bb16d227Schristos    global as the "expected type" parameter to SCM_ASSERT_TYPE, thus in
124*bb16d227Schristos    this instance smob types are printed without the <>.
125*bb16d227Schristos    [Hmmm, this rule seems dated now.  Plus I18N rules in GDB are not always
126*bb16d227Schristos    clear, sometimes we pass the smob name through _(), however it's not
127*bb16d227Schristos    clear that's actually a good idea.]
128*bb16d227Schristos
129*bb16d227SchristosType naming:
130*bb16d227Schristos
131*bb16d227Schristos  - smob structs are typedefs named foo_smob
132*bb16d227Schristos
133*bb16d227SchristosVariable naming:
134*bb16d227Schristos
135*bb16d227Schristos  - "scm" by itself is reserved for arbitrary Scheme objects
136*bb16d227Schristos
137*bb16d227Schristos  - variables that are pointers to smob structs are named <char>_smob or
138*bb16d227Schristos    <char><char>_smob, e.g., f_smob for a pointer to a frame smob
139*bb16d227Schristos
140*bb16d227Schristos  - variables that are gdb smob objects are typically named <char>_scm or
141*bb16d227Schristos    <char><char>_scm, e.g., f_scm for a <gdb:frame> object
142*bb16d227Schristos
143*bb16d227Schristos  - the name of the first argument for method-like functions is "self"
144*bb16d227Schristos
145*bb16d227SchristosFunction naming:
146*bb16d227Schristos
147*bb16d227Schristos  General:
148*bb16d227Schristos
149*bb16d227Schristos  - all non-static functions have a prefix,
150*bb16d227Schristos    either gdbscm_ or <char><char>scm_ [or <char><char><char>scm_]
151*bb16d227Schristos
152*bb16d227Schristos  - all functions that implement Scheme procedures have a gdbscm_ prefix,
153*bb16d227Schristos    this is for consistency and readability of Scheme exception text
154*bb16d227Schristos
155*bb16d227Schristos  - static functions typically have a prefix
156*bb16d227Schristos    - the prefix is typically <char><char>scm_ where the first two letters
157*bb16d227Schristos      are unique to the file or class the function works with.
158*bb16d227Schristos      E.g., the scm-arch.c prefix is arscm_.
159*bb16d227Schristos      This follows something used in gdb/python in some places,
160*bb16d227Schristos      we make it formal.
161*bb16d227Schristos
162*bb16d227Schristos  - if the function is of a general nature, or no other prefix works,
163*bb16d227Schristos    use gdbscm_
164*bb16d227Schristos
165*bb16d227Schristos  Conversion functions:
166*bb16d227Schristos
167*bb16d227Schristos  - the from/to in function names follows from libguile's existing style
168*bb16d227Schristos  - conversions from/to Scheme objects are named:
169*bb16d227Schristos      prefix_scm_from_foo: converts from foo to scm
170*bb16d227Schristos      prefix_scm_to_foo: converts from scm to foo
171*bb16d227Schristos
172*bb16d227Schristos  Exception handling:
173*bb16d227Schristos
174*bb16d227Schristos  - functions that may throw a Scheme exception have an _unsafe suffix
175*bb16d227Schristos    - This does not apply to functions that implement Scheme procedures.
176*bb16d227Schristos    - This does not apply to functions whose explicit job is to throw
177*bb16d227Schristos      an exception.  Adding _unsafe to gdbscm_throw is kinda superfluous. :-)
178*bb16d227Schristos  - functions that can throw a GDB error aren't adorned with _unsafe
179*bb16d227Schristos
180*bb16d227Schristos  - "_safe" in a function name means it will never throw an exception
181*bb16d227Schristos    - Generally unnecessary, since the convention is to mark the ones that
182*bb16d227Schristos      *can* throw an exception.  But sometimes it's useful to highlight the
183*bb16d227Schristos      fact that the function is safe to call without worrying about exception
184*bb16d227Schristos      handling.
185*bb16d227Schristos
186*bb16d227Schristos  - except for functions that implement Scheme procedures, all functions
187*bb16d227Schristos    that can throw exceptions (GDB or Scheme) say so in their function comment
188*bb16d227Schristos
189*bb16d227Schristos  - functions that don't throw an exception, but still need to indicate to
190*bb16d227Schristos    the caller that one happened (i.e., "safe" functions), either return
191*bb16d227Schristos    a <gdb:exception> smob as a result or pass it back via a parameter.
192*bb16d227Schristos    For this reason don't pass back <gdb:exception> smobs for any other
193*bb16d227Schristos    reason.  There are functions that explicitly construct <gdb:exception>
194*bb16d227Schristos    smobs.  They're obviously the, umm, exception.
195*bb16d227Schristos
196*bb16d227Schristos  Internal functions:
197*bb16d227Schristos
198*bb16d227Schristos  - internal Scheme functions begin with "%" and are intentionally undocumented
199*bb16d227Schristos    in the manual
200*bb16d227Schristos
201*bb16d227Schristos  Standard Guile/Scheme conventions:
202*bb16d227Schristos
203*bb16d227Schristos  - predicates that return Scheme values have the suffix _p and have suffix "?"
204*bb16d227Schristos    in the Scheme procedure's name
205*bb16d227Schristos  - functions that implement Scheme procedures that modify state have the
206*bb16d227Schristos    suffix _x and have suffix "!" in the Scheme procedure's name
207*bb16d227Schristos  - object predicates that return a C truth value are named prefix_is_foo
208*bb16d227Schristos  - functions that set something have "set" at the front (except for a prefix)
209*bb16d227Schristos    write this: gdbscm_set_gsmob_aux_x implements (set-gsmob-aux! ...)
210*bb16d227Schristos    not this: gdbscm_gsmob_set_aux_x implements (gsmob-set-aux! ...)
211*bb16d227Schristos
212*bb16d227SchristosDoc strings:
213*bb16d227Schristos
214*bb16d227Schristos  - there are lots of existing examples, they should be pretty consistent,
215*bb16d227Schristos    use them as boilerplate/examples
216*bb16d227Schristos  - begin with a one line summary (can be multiple lines if necessary)
217*bb16d227Schristos  - if the arguments need description:
218*bb16d227Schristos    - blank line
219*bb16d227Schristos    - "  Arguments: arg1 arg2"
220*bb16d227Schristos      "    arg1: blah ..."
221*bb16d227Schristos      "    arg2: blah ..."
222*bb16d227Schristos  - if the result requires more description:
223*bb16d227Schristos    - blank line
224*bb16d227Schristos    - "  Returns:"
225*bb16d227Schristos      "    Blah ..."
226*bb16d227Schristos  - if it's important to list exceptions that can be thrown:
227*bb16d227Schristos    - blank line
228*bb16d227Schristos    - "  Throws:"
229*bb16d227Schristos      "    exception-name: blah ..."
230