]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-mi.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / progmodes / gdb-mi.el
1 ;;; gdb-mi.el --- User Interface for running GDB
2
3 ;; Copyright (C) 2007-2011 Free Software Foundation, Inc.
4
5 ;; Author: Nick Roberts <nickrob@gnu.org>
6 ;; Maintainer: FSF
7 ;; Keywords: unix, tools
8
9 ;; This file is part of GNU Emacs.
10
11 ;; Homepage: http://www.emacswiki.org/emacs/GDB-MI
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Credits:
27
28 ;; This file was written by by Nick Roberts following the general design
29 ;; used in gdb-ui.el for Emacs 22.1 - 23.1. It is currently being developed
30 ;; by Dmitry Dzhus <dima@sphinx.net.ru> as part of the Google Summer
31 ;; of Code 2009 Project "Emacs GDB/MI migration".
32
33 ;;; Commentary:
34
35 ;; This mode acts as a graphical user interface to GDB. You can interact with
36 ;; GDB through the GUD buffer in the usual way, but there are also further
37 ;; buffers which control the execution and describe the state of your program.
38 ;; It separates the input/output of your program from that of GDB and displays
39 ;; expressions and their current values in their own buffers. It also uses
40 ;; features of Emacs 21 such as the fringe/display margin for breakpoints, and
41 ;; the toolbar (see the GDB Graphical Interface section in the Emacs info
42 ;; manual).
43
44 ;; M-x gdb will start the debugger.
45
46 ;; This file uses GDB/MI as the primary interface to GDB. It is still under
47 ;; development and is part of a process to migrate Emacs from annotations (as
48 ;; used in gdb-ui.el) to GDB/MI. It runs gdb with GDB/MI (-interp=mi) and
49 ;; access CLI using "-interpreter-exec console cli-command". This code works
50 ;; without gdb-ui.el and uses MI tokens instead of queues. Eventually MI
51 ;; should be asynchronous.
52
53 ;; This mode will PARTLY WORK WITH RECENT GDB RELEASES (status in modeline
54 ;; doesn't update properly when execution commands are issued from GUD buffer)
55 ;; and WORKS BEST when GDB runs asynchronously: maint set linux-async on.
56 ;;
57 ;; You need development version of GDB 7.0 for the thread buffer to work.
58
59 ;; This file replaces gdb-ui.el and is for development with GDB. Use the
60 ;; release branch of Emacs 22 for the latest version of gdb-ui.el.
61
62 ;; Windows Platforms:
63
64 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
65 ;; explicitly in your program if you want timely display of I/O in Emacs.
66 ;; Alternatively you can make the output stream unbuffered, for example, by
67 ;; using a macro:
68
69 ;; #ifdef UNBUFFERED
70 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
71 ;; #endif
72
73 ;; and compiling with -DUNBUFFERED while debugging.
74
75 ;; If you are using Cygwin GDB and find that the source is not being displayed
76 ;; in Emacs when you step through it, possible solutions are to:
77
78 ;; 1) Use Cygwin X Windows and Cygwin Emacs.
79 ;; (Since 22.1 Emacs builds under Cygwin.)
80 ;; 2) Use MinGW GDB instead.
81 ;; 3) Use cygwin-mount.el
82
83 ;;; Mac OSX:
84
85 ;; GDB in Emacs on Mac OSX works best with FSF GDB as Apple have made
86 ;; some changes to the version that they include as part of Mac OSX.
87 ;; This requires GDB version 7.0 or later (estimated release date Aug 2009)
88 ;; as earlier versions don not compile on Mac OSX.
89
90 ;;; Known Bugs:
91
92 ;; 1) Stack buffer doesn't parse MI output if you stop in a routine without
93 ;; line information, e.g., a routine in libc (just a TODO item).
94
95 ;; TODO:
96 ;; 2) Watch windows to work with threads.
97 ;; 3) Use treebuffer.el instead of the speedbar for watch-expressions?
98 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
99
100 ;;; Code:
101
102 (require 'gud)
103 (require 'json)
104 (require 'bindat)
105 (eval-when-compile (require 'cl))
106
107 (defvar tool-bar-map)
108 (defvar speedbar-initial-expansion-list-name)
109 (defvar speedbar-frame)
110
111 (defvar gdb-memory-address "main")
112 (defvar gdb-memory-last-address nil
113 "Last successfully accessed memory address.")
114 (defvar gdb-memory-next-page nil
115 "Address of next memory page for program memory buffer.")
116 (defvar gdb-memory-prev-page nil
117 "Address of previous memory page for program memory buffer.")
118
119 (defvar gdb-thread-number nil
120 "Main current thread.
121
122 Invalidation triggers use this variable to query GDB for
123 information on the specified thread by wrapping GDB/MI commands
124 in `gdb-current-context-command'.
125
126 This variable may be updated implicitly by GDB via `gdb-stopped'
127 or explicitly by `gdb-select-thread'.
128
129 Only `gdb-setq-thread-number' should be used to change this
130 value.")
131
132 (defvar gdb-frame-number nil
133 "Selected frame level for main current thread.
134
135 Updated according to the following rules:
136
137 When a thread is selected or current thread stops, set to \"0\".
138
139 When current thread goes running (and possibly exits eventually),
140 set to nil.
141
142 May be manually changed by user with `gdb-select-frame'.")
143
144 (defvar gdb-frame-address nil "Identity of frame for watch expression.")
145
146 ;; Used to show overlay arrow in source buffer. All set in
147 ;; gdb-get-main-selected-frame. Disassembly buffer should not use
148 ;; these but rely on buffer-local thread information instead.
149 (defvar gdb-selected-frame nil
150 "Name of selected function for main current thread.")
151 (defvar gdb-selected-file nil
152 "Name of selected file for main current thread.")
153 (defvar gdb-selected-line nil
154 "Number of selected line for main current thread.")
155
156 (defvar gdb-threads-list nil
157 "Associative list of threads provided by \"-thread-info\" MI command.
158
159 Keys are thread numbers (in strings) and values are structures as
160 returned from -thread-info by `gdb-json-partial-output'. Updated in
161 `gdb-thread-list-handler-custom'.")
162
163 (defvar gdb-running-threads-count nil
164 "Number of currently running threads.
165
166 If nil, no information is available.
167
168 Updated in `gdb-thread-list-handler-custom'.")
169
170 (defvar gdb-stopped-threads-count nil
171 "Number of currently stopped threads.
172
173 See also `gdb-running-threads-count'.")
174
175 (defvar gdb-breakpoints-list nil
176 "Associative list of breakpoints provided by \"-break-list\" MI command.
177
178 Keys are breakpoint numbers (in string) and values are structures
179 as returned from \"-break-list\" by `gdb-json-partial-output'
180 \(\"body\" field is used). Updated in
181 `gdb-breakpoints-list-handler-custom'.")
182
183 (defvar gdb-current-language nil)
184 (defvar gdb-var-list nil
185 "List of variables in watch window.
186 Each element has the form (VARNUM EXPRESSION NUMCHILD TYPE VALUE STATUS HAS_MORE FP)
187 where STATUS is nil (`unchanged'), `changed' or `out-of-scope', FP the frame
188 address for root variables.")
189 (defvar gdb-main-file nil "Source file from which program execution begins.")
190
191 ;; Overlay arrow markers
192 (defvar gdb-stack-position nil)
193 (defvar gdb-thread-position nil)
194 (defvar gdb-disassembly-position nil)
195
196 (defvar gdb-location-alist nil
197 "Alist of breakpoint numbers and full filenames. Only used for files that
198 Emacs can't find.")
199 (defvar gdb-active-process nil
200 "GUD tooltips display variable values when t, and macro definitions otherwise.")
201 (defvar gdb-error "Non-nil when GDB is reporting an error.")
202 (defvar gdb-macro-info nil
203 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
204 (defvar gdb-register-names nil "List of register names.")
205 (defvar gdb-changed-registers nil
206 "List of changed register numbers (strings).")
207 (defvar gdb-buffer-fringe-width nil)
208 (defvar gdb-last-command nil)
209 (defvar gdb-prompt-name nil)
210 (defvar gdb-token-number 0)
211 (defvar gdb-handler-alist '())
212 (defvar gdb-handler-number nil)
213 (defvar gdb-source-file-list nil
214 "List of source files for the current executable.")
215 (defvar gdb-first-done-or-error t)
216 (defvar gdb-source-window nil)
217 (defvar gdb-inferior-status nil)
218 (defvar gdb-continuation nil)
219 (defvar gdb-version nil)
220 (defvar gdb-filter-output nil
221 "Message to be shown in GUD console.
222
223 This variable is updated in `gdb-done-or-error' and returned by
224 `gud-gdbmi-marker-filter'.")
225
226 (defvar gdb-non-stop nil
227 "Indicates whether current GDB session is using non-stop mode.
228
229 It is initialized to `gdb-non-stop-setting' at the beginning of
230 every GDB session.")
231
232 (defvar gdb-buffer-type nil
233 "One of the symbols bound in `gdb-buffer-rules'.")
234 (make-variable-buffer-local 'gdb-buffer-type)
235
236 (defvar gdb-output-sink 'nil
237 "The disposition of the output of the current gdb command.
238 Possible values are these symbols:
239
240 `user' -- gdb output should be copied to the GUD buffer
241 for the user to see.
242
243 `emacs' -- output should be collected in the partial-output-buffer
244 for subsequent processing by a command. This is the
245 disposition of output generated by commands that
246 gdb mode sends to gdb on its own behalf.")
247
248 ;; Pending triggers prevent congestion: Emacs won't send two similar
249 ;; consecutive requests.
250
251 (defvar gdb-pending-triggers '()
252 "A list of trigger functions which have not yet been handled.
253
254 Elements are either function names or pairs (buffer . function)")
255
256 (defmacro gdb-add-pending (item)
257 `(push ,item gdb-pending-triggers))
258 (defmacro gdb-pending-p (item)
259 `(member ,item gdb-pending-triggers))
260 (defmacro gdb-delete-pending (item)
261 `(setq gdb-pending-triggers
262 (delete ,item gdb-pending-triggers)))
263
264 (defmacro gdb-wait-for-pending (&rest body)
265 "Wait until `gdb-pending-triggers' is empty and evaluate FORM.
266
267 This function checks `gdb-pending-triggers' value every
268 `gdb-wait-for-pending' seconds."
269 (run-with-timer
270 0.5 nil
271 `(lambda ()
272 (if (not gdb-pending-triggers)
273 (progn ,@body)
274 (gdb-wait-for-pending ,@body)))))
275
276 ;; Publish-subscribe
277
278 (defmacro gdb-add-subscriber (publisher subscriber)
279 "Register new PUBLISHER's SUBSCRIBER.
280
281 SUBSCRIBER must be a pair, where cdr is a function of one
282 argument (see `gdb-emit-signal')."
283 `(add-to-list ',publisher ,subscriber t))
284
285 (defmacro gdb-delete-subscriber (publisher subscriber)
286 "Unregister SUBSCRIBER from PUBLISHER."
287 `(setq ,publisher (delete ,subscriber
288 ,publisher)))
289
290 (defun gdb-get-subscribers (publisher)
291 publisher)
292
293 (defun gdb-emit-signal (publisher &optional signal)
294 "Call cdr for each subscriber of PUBLISHER with SIGNAL as argument."
295 (dolist (subscriber (gdb-get-subscribers publisher))
296 (funcall (cdr subscriber) signal)))
297
298 (defvar gdb-buf-publisher '()
299 "Used to invalidate GDB buffers by emitting a signal in
300 `gdb-update'.
301
302 Must be a list of pairs with cars being buffers and cdr's being
303 valid signal handlers.")
304
305 (defgroup gdb nil
306 "GDB graphical interface"
307 :group 'tools
308 :link '(info-link "(emacs)GDB Graphical Interface")
309 :version "23.2")
310
311 (defgroup gdb-non-stop nil
312 "GDB non-stop debugging settings"
313 :group 'gdb
314 :version "23.2")
315
316 (defgroup gdb-buffers nil
317 "GDB buffers"
318 :group 'gdb
319 :version "23.2")
320
321 (defcustom gdb-debug-log-max 128
322 "Maximum size of `gdb-debug-log'. If nil, size is unlimited."
323 :group 'gdb
324 :type '(choice (integer :tag "Number of elements")
325 (const :tag "Unlimited" nil))
326 :version "22.1")
327
328 (defcustom gdb-non-stop-setting t
329 "When in non-stop mode, stopped threads can be examined while
330 other threads continue to execute.
331
332 GDB session needs to be restarted for this setting to take
333 effect."
334 :type 'boolean
335 :group 'gdb-non-stop
336 :version "23.2")
337
338 ;; TODO Some commands can't be called with --all (give a notice about
339 ;; it in setting doc)
340 (defcustom gdb-gud-control-all-threads t
341 "When enabled, GUD execution commands affect all threads when
342 in non-stop mode. Otherwise, only current thread is affected."
343 :type 'boolean
344 :group 'gdb-non-stop
345 :version "23.2")
346
347 (defcustom gdb-switch-reasons t
348 "List of stop reasons which cause Emacs to switch to the thread
349 which caused the stop. When t, switch to stopped thread no matter
350 what the reason was. When nil, never switch to stopped thread
351 automatically.
352
353 This setting is used in non-stop mode only. In all-stop mode,
354 Emacs always switches to the thread which caused the stop."
355 ;; exited, exited-normally and exited-signalled are not
356 ;; thread-specific stop reasons and therefore are not included in
357 ;; this list
358 :type '(choice
359 (const :tag "All reasons" t)
360 (set :tag "Selection of reasons..."
361 (const :tag "A breakpoint was reached." "breakpoint-hit")
362 (const :tag "A watchpoint was triggered." "watchpoint-trigger")
363 (const :tag "A read watchpoint was triggered." "read-watchpoint-trigger")
364 (const :tag "An access watchpoint was triggered." "access-watchpoint-trigger")
365 (const :tag "Function finished execution." "function-finished")
366 (const :tag "Location reached." "location-reached")
367 (const :tag "Watchpoint has gone out of scope" "watchpoint-scope")
368 (const :tag "End of stepping range reached." "end-stepping-range")
369 (const :tag "Signal received (like interruption)." "signal-received"))
370 (const :tag "None" nil))
371 :group 'gdb-non-stop
372 :version "23.2"
373 :link '(info-link "(gdb)GDB/MI Async Records"))
374
375 (defcustom gdb-stopped-hooks nil
376 "This variable holds a list of functions to be called whenever
377 GDB stops.
378
379 Each function takes one argument, a parsed MI response, which
380 contains fields of corresponding MI *stopped async record:
381
382 ((stopped-threads . \"all\")
383 (thread-id . \"1\")
384 (frame (line . \"38\")
385 (fullname . \"/home/sphinx/projects/gsoc/server.c\")
386 (file . \"server.c\")
387 (args ((value . \"0x804b038\")
388 (name . \"arg\")))
389 (func . \"hello\")
390 (addr . \"0x0804869e\"))
391 (reason . \"end-stepping-range\"))
392
393 Note that \"reason\" is only present in non-stop debugging mode.
394
395 `bindat-get-field' may be used to access the fields of response.
396
397 Each function is called after the new current thread was selected
398 and GDB buffers were updated in `gdb-stopped'."
399 :type '(repeat function)
400 :group 'gdb
401 :version "23.2"
402 :link '(info-link "(gdb)GDB/MI Async Records"))
403
404 (defcustom gdb-switch-when-another-stopped t
405 "When nil, Emacs won't switch to stopped thread if some other
406 stopped thread is already selected."
407 :type 'boolean
408 :group 'gdb-non-stop
409 :version "23.2")
410
411 (defcustom gdb-stack-buffer-locations t
412 "Show file information or library names in stack buffers."
413 :type 'boolean
414 :group 'gdb-buffers
415 :version "23.2")
416
417 (defcustom gdb-stack-buffer-addresses nil
418 "Show frame addresses in stack buffers."
419 :type 'boolean
420 :group 'gdb-buffers
421 :version "23.2")
422
423 (defcustom gdb-thread-buffer-verbose-names t
424 "Show long thread names in threads buffer."
425 :type 'boolean
426 :group 'gdb-buffers
427 :version "23.2")
428
429 (defcustom gdb-thread-buffer-arguments t
430 "Show function arguments in threads buffer."
431 :type 'boolean
432 :group 'gdb-buffers
433 :version "23.2")
434
435 (defcustom gdb-thread-buffer-locations t
436 "Show file information or library names in threads buffer."
437 :type 'boolean
438 :group 'gdb-buffers
439 :version "23.2")
440
441 (defcustom gdb-thread-buffer-addresses nil
442 "Show addresses for thread frames in threads buffer."
443 :type 'boolean
444 :group 'gdb-buffers
445 :version "23.2")
446
447 (defcustom gdb-show-threads-by-default nil
448 "Show threads list buffer instead of breakpoints list by
449 default."
450 :type 'boolean
451 :group 'gdb-buffers
452 :version "23.2")
453
454 (defvar gdb-debug-log nil
455 "List of commands sent to and replies received from GDB.
456 Most recent commands are listed first. This list stores only the last
457 `gdb-debug-log-max' values. This variable is used to debug GDB-MI.")
458
459 ;;;###autoload
460 (defcustom gdb-enable-debug nil
461 "Non-nil means record the process input and output in `gdb-debug-log'."
462 :type 'boolean
463 :group 'gdb
464 :version "22.1")
465
466 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
467 "Shell command for generating a list of defined macros in a source file.
468 This list is used to display the #define directive associated
469 with an identifier as a tooltip. It works in a debug session with
470 GDB, when `gud-tooltip-mode' is t.
471
472 Set `gdb-cpp-define-alist-flags' for any include paths or
473 predefined macros."
474 :type 'string
475 :group 'gdb
476 :version "22.1")
477
478 (defcustom gdb-cpp-define-alist-flags ""
479 "Preprocessor flags for `gdb-cpp-define-alist-program'."
480 :type 'string
481 :group 'gdb
482 :version "22.1")
483
484 (defcustom gdb-create-source-file-list t
485 "Non-nil means create a list of files from which the executable was built.
486 Set this to nil if the GUD buffer displays \"initializing...\" in the mode
487 line for a long time when starting, possibly because your executable was
488 built from a large number of files. This allows quicker initialization
489 but means that these files are not automatically enabled for debugging,
490 e.g., you won't be able to click in the fringe to set a breakpoint until
491 execution has already stopped there."
492 :type 'boolean
493 :group 'gdb
494 :version "23.1")
495
496 (defcustom gdb-show-main nil
497 "Non-nil means display source file containing the main routine at startup.
498 Also display the main routine in the disassembly buffer if present."
499 :type 'boolean
500 :group 'gdb
501 :version "22.1")
502
503 (defun gdb-force-mode-line-update (status)
504 (let ((buffer gud-comint-buffer))
505 (if (and buffer (buffer-name buffer))
506 (with-current-buffer buffer
507 (setq mode-line-process
508 (format ":%s [%s]"
509 (process-status (get-buffer-process buffer)) status))
510 ;; Force mode line redisplay soon.
511 (force-mode-line-update)))))
512
513 (defun gdb-enable-debug (arg)
514 "Toggle logging of transaction between Emacs and Gdb.
515 The log is stored in `gdb-debug-log' as an alist with elements
516 whose cons is send, send-item or recv and whose cdr is the string
517 being transferred. This list may grow up to a size of
518 `gdb-debug-log-max' after which the oldest element (at the end of
519 the list) is deleted every time a new one is added (at the front)."
520 (interactive "P")
521 (setq gdb-enable-debug
522 (if (null arg)
523 (not gdb-enable-debug)
524 (> (prefix-numeric-value arg) 0)))
525 (message (format "Logging of transaction %sabled"
526 (if gdb-enable-debug "en" "dis"))))
527
528 ;; These two are used for menu and toolbar
529 (defun gdb-control-all-threads ()
530 "Switch to non-stop/A mode."
531 (interactive)
532 (setq gdb-gud-control-all-threads t)
533 ;; Actually forcing the tool-bar to update.
534 (force-mode-line-update)
535 (message "Now in non-stop/A mode."))
536
537 (defun gdb-control-current-thread ()
538 "Switch to non-stop/T mode."
539 (interactive)
540 (setq gdb-gud-control-all-threads nil)
541 ;; Actually forcing the tool-bar to update.
542 (force-mode-line-update)
543 (message "Now in non-stop/T mode."))
544
545 (defun gdb-find-watch-expression ()
546 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
547 (varnum (car var)) expr array)
548 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varnum)
549 (let ((var1 (assoc (match-string 1 varnum) gdb-var-list)) var2 varnumlet
550 (component-list (split-string (match-string 2 varnum) "\\." t)))
551 (setq expr (nth 1 var1))
552 (setq varnumlet (car var1))
553 (dolist (component component-list)
554 (setq var2 (assoc varnumlet gdb-var-list))
555 (setq expr (concat expr
556 (if (string-match ".*\\[[0-9]+\\]$" (nth 3 var2))
557 (concat "[" component "]")
558 (concat "." component))))
559 (setq varnumlet (concat varnumlet "." component)))
560 expr)))
561
562 ;; noall is used for commands which don't take --all, but only
563 ;; --thread.
564 (defun gdb-gud-context-command (command &optional noall)
565 "When `gdb-non-stop' is t, add --thread option to COMMAND if
566 `gdb-gud-control-all-threads' is nil and --all option otherwise.
567 If NOALL is t, always add --thread option no matter what
568 `gdb-gud-control-all-threads' value is.
569
570 When `gdb-non-stop' is nil, return COMMAND unchanged."
571 (if gdb-non-stop
572 (if (and gdb-gud-control-all-threads
573 (not noall)
574 (string-equal gdb-version "7.0+"))
575 (concat command " --all ")
576 (gdb-current-context-command command))
577 command))
578
579 (defmacro gdb-gud-context-call (cmd1 &optional cmd2 noall noarg)
580 "`gud-call' wrapper which adds --thread/--all options between
581 CMD1 and CMD2. NOALL is the same as in `gdb-gud-context-command'.
582
583 NOARG must be t when this macro is used outside `gud-def'"
584 `(gud-call
585 (concat (gdb-gud-context-command ,cmd1 ,noall) " " ,cmd2)
586 ,(when (not noarg) 'arg)))
587
588 ;;;###autoload
589 (defun gdb (command-line)
590 "Run gdb on program FILE in buffer *gud-FILE*.
591 The directory containing FILE becomes the initial working directory
592 and source-file directory for your debugger.
593
594 If `gdb-many-windows' is nil (the default value) then gdb just
595 pops up the GUD buffer unless `gdb-show-main' is t. In this case
596 it starts with two windows: one displaying the GUD buffer and the
597 other with the source file with the main routine of the inferior.
598
599 If `gdb-many-windows' is t, regardless of the value of
600 `gdb-show-main', the layout below will appear. Keybindings are
601 shown in some of the buffers.
602
603 Watch expressions appear in the speedbar/slowbar.
604
605 The following commands help control operation :
606
607 `gdb-many-windows' - Toggle the number of windows gdb uses.
608 `gdb-restore-windows' - To restore the window layout.
609
610 See Info node `(emacs)GDB Graphical Interface' for a more
611 detailed description of this mode.
612
613
614 +----------------------------------------------------------------------+
615 | GDB Toolbar |
616 +-----------------------------------+----------------------------------+
617 | GUD buffer (I/O of GDB) | Locals buffer |
618 | | |
619 | | |
620 | | |
621 +-----------------------------------+----------------------------------+
622 | Source buffer | I/O buffer (of debugged program) |
623 | | (comint-mode) |
624 | | |
625 | | |
626 | | |
627 | | |
628 | | |
629 | | |
630 +-----------------------------------+----------------------------------+
631 | Stack buffer | Breakpoints buffer |
632 | RET gdb-select-frame | SPC gdb-toggle-breakpoint |
633 | | RET gdb-goto-breakpoint |
634 | | D gdb-delete-breakpoint |
635 +-----------------------------------+----------------------------------+"
636 ;;
637 (interactive (list (gud-query-cmdline 'gdb)))
638
639 (when (and gud-comint-buffer
640 (buffer-name gud-comint-buffer)
641 (get-buffer-process gud-comint-buffer)
642 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
643 (gdb-restore-windows)
644 (error
645 "Multiple debugging requires restarting in text command mode"))
646 ;;
647 (gud-common-init command-line nil 'gud-gdbmi-marker-filter)
648 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
649 (setq comint-input-sender 'gdb-send)
650 (when (ring-empty-p comint-input-ring) ; cf shell-mode
651 (let (hfile)
652 (when (catch 'done
653 (dolist (file '(".gdbinit" "~/.gdbinit"))
654 (if (file-readable-p (setq file (expand-file-name file)))
655 (with-temp-buffer
656 (insert-file-contents file)
657 (and (re-search-forward
658 "^ *set history filename *\\(.*\\)" nil t)
659 (file-readable-p
660 (setq hfile (expand-file-name
661 (match-string 1)
662 (file-name-directory file))))
663 (throw 'done t))))))
664 (set (make-local-variable 'comint-input-ring-file-name) hfile)
665 (comint-read-input-ring t))))
666 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
667 "Set temporary breakpoint at current line.")
668 (gud-def gud-jump
669 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
670 "\C-j" "Set execution address to current line.")
671
672 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
673 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
674 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
675 (gud-def gud-pstar "print* %e" nil
676 "Evaluate C dereferenced pointer expression at point.")
677
678 (gud-def gud-step (gdb-gud-context-call "-exec-step" "%p" t)
679 "\C-s"
680 "Step one source line with display.")
681 (gud-def gud-stepi (gdb-gud-context-call "-exec-step-instruction" "%p" t)
682 "\C-i"
683 "Step one instruction with display.")
684 (gud-def gud-next (gdb-gud-context-call "-exec-next" "%p" t)
685 "\C-n"
686 "Step one line (skip functions).")
687 (gud-def gud-nexti (gdb-gud-context-call "-exec-next-instruction" "%p" t)
688 nil
689 "Step one instruction (skip functions).")
690 (gud-def gud-cont (gdb-gud-context-call "-exec-continue")
691 "\C-r"
692 "Continue with display.")
693 (gud-def gud-finish (gdb-gud-context-call "-exec-finish" nil t)
694 "\C-f"
695 "Finish executing current function.")
696 (gud-def gud-run "-exec-run"
697 nil
698 "Run the program.")
699
700 (gud-def gud-break (if (not (string-match "Disassembly" mode-name))
701 (gud-call "break %f:%l" arg)
702 (save-excursion
703 (beginning-of-line)
704 (forward-char 2)
705 (gud-call "break *%a" arg)))
706 "\C-b" "Set breakpoint at current line or address.")
707
708 (gud-def gud-remove (if (not (string-match "Disassembly" mode-name))
709 (gud-call "clear %f:%l" arg)
710 (save-excursion
711 (beginning-of-line)
712 (forward-char 2)
713 (gud-call "clear *%a" arg)))
714 "\C-d" "Remove breakpoint at current line or address.")
715
716 ;; -exec-until doesn't support --all yet
717 (gud-def gud-until (if (not (string-match "Disassembly" mode-name))
718 (gud-call "-exec-until %f:%l" arg)
719 (save-excursion
720 (beginning-of-line)
721 (forward-char 2)
722 (gud-call "-exec-until *%a" arg)))
723 "\C-u" "Continue to current line or address.")
724 ;; TODO Why arg here?
725 (gud-def
726 gud-go (gud-call (if gdb-active-process
727 (gdb-gud-context-command "-exec-continue")
728 "-exec-run") arg)
729 nil "Start or continue execution.")
730
731 ;; For debugging Emacs only.
732 (gud-def gud-pp
733 (gud-call
734 (concat
735 "pp1 " (if (eq (buffer-local-value
736 'major-mode (window-buffer)) 'speedbar-mode)
737 (gdb-find-watch-expression) "%e")) arg)
738 nil "Print the Emacs s-expression.")
739
740 (define-key gud-minor-mode-map [left-margin mouse-1]
741 'gdb-mouse-set-clear-breakpoint)
742 (define-key gud-minor-mode-map [left-fringe mouse-1]
743 'gdb-mouse-set-clear-breakpoint)
744 (define-key gud-minor-mode-map [left-margin C-mouse-1]
745 'gdb-mouse-toggle-breakpoint-margin)
746 (define-key gud-minor-mode-map [left-fringe C-mouse-1]
747 'gdb-mouse-toggle-breakpoint-fringe)
748
749 (define-key gud-minor-mode-map [left-margin drag-mouse-1]
750 'gdb-mouse-until)
751 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
752 'gdb-mouse-until)
753 (define-key gud-minor-mode-map [left-margin mouse-3]
754 'gdb-mouse-until)
755 (define-key gud-minor-mode-map [left-fringe mouse-3]
756 'gdb-mouse-until)
757
758 (define-key gud-minor-mode-map [left-margin C-drag-mouse-1]
759 'gdb-mouse-jump)
760 (define-key gud-minor-mode-map [left-fringe C-drag-mouse-1]
761 'gdb-mouse-jump)
762 (define-key gud-minor-mode-map [left-fringe C-mouse-3]
763 'gdb-mouse-jump)
764 (define-key gud-minor-mode-map [left-margin C-mouse-3]
765 'gdb-mouse-jump)
766
767 (local-set-key "\C-i" 'gud-gdb-complete-command)
768 (setq gdb-first-prompt t)
769 (setq gud-running nil)
770
771 (gdb-update)
772
773 (run-hooks 'gdb-mode-hook))
774
775 (defun gdb-init-1 ()
776 ;; (re-)initialise
777 (setq gdb-selected-frame nil
778 gdb-frame-number nil
779 gdb-thread-number nil
780 gdb-var-list nil
781 gdb-pending-triggers nil
782 gdb-output-sink 'user
783 gdb-location-alist nil
784 gdb-source-file-list nil
785 gdb-last-command nil
786 gdb-token-number 0
787 gdb-handler-alist '()
788 gdb-handler-number nil
789 gdb-prompt-name nil
790 gdb-first-done-or-error t
791 gdb-buffer-fringe-width (car (window-fringes))
792 gdb-debug-log nil
793 gdb-source-window nil
794 gdb-inferior-status nil
795 gdb-continuation nil
796 gdb-buf-publisher '()
797 gdb-threads-list '()
798 gdb-breakpoints-list '()
799 gdb-register-names '()
800 gdb-non-stop gdb-non-stop-setting)
801 ;;
802 (setq gdb-buffer-type 'gdbmi)
803 ;;
804 (gdb-force-mode-line-update
805 (propertize "initializing..." 'face font-lock-variable-name-face))
806
807 (gdb-get-buffer-create 'gdb-inferior-io)
808 (gdb-clear-inferior-io)
809 (set-process-filter (get-process "gdb-inferior") 'gdb-inferior-filter)
810 (gdb-input
811 ;; Needs GDB 6.4 onwards
812 (list (concat "-inferior-tty-set "
813 (or
814 ;; The process can run on a remote host.
815 (process-get (get-process "gdb-inferior") 'remote-tty)
816 (process-tty-name (get-process "gdb-inferior"))))
817 'ignore))
818 (if (eq window-system 'w32)
819 (gdb-input (list "-gdb-set new-console off" 'ignore)))
820 (gdb-input (list "-gdb-set height 0" 'ignore))
821
822 (when gdb-non-stop
823 (gdb-input (list "-gdb-set non-stop 1" 'gdb-non-stop-handler)))
824
825 ;; find source file and compilation directory here
826 (gdb-input
827 ; Needs GDB 6.2 onwards.
828 (list "-file-list-exec-source-files" 'gdb-get-source-file-list))
829 (if gdb-create-source-file-list
830 (gdb-input
831 ; Needs GDB 6.0 onwards.
832 (list "-file-list-exec-source-file" 'gdb-get-source-file)))
833 (gdb-input
834 (list "-gdb-show prompt" 'gdb-get-prompt)))
835
836 (defun gdb-non-stop-handler ()
837 (goto-char (point-min))
838 (if (re-search-forward "No symbol" nil t)
839 (progn
840 (message "This version of GDB doesn't support non-stop mode. Turning it off.")
841 (setq gdb-non-stop nil)
842 (setq gdb-version "pre-7.0"))
843 (setq gdb-version "7.0+")
844 (gdb-input (list "-gdb-set target-async 1" 'ignore))
845 (gdb-input (list "-enable-pretty-printing" 'ignore))))
846
847 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
848
849 (defun gdb-create-define-alist ()
850 "Create an alist of #define directives for GUD tooltips."
851 (let* ((file (buffer-file-name))
852 (output
853 (with-output-to-string
854 (with-current-buffer standard-output
855 (and file
856 (file-exists-p file)
857 ;; call-process doesn't work with remote file names.
858 (not (file-remote-p default-directory))
859 (call-process shell-file-name file
860 (list t nil) nil "-c"
861 (concat gdb-cpp-define-alist-program " "
862 gdb-cpp-define-alist-flags))))))
863 (define-list (split-string output "\n" t))
864 (name))
865 (setq gdb-define-alist nil)
866 (dolist (define define-list)
867 (setq name (nth 1 (split-string define "[( ]")))
868 (push (cons name define) gdb-define-alist))))
869
870 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
871 (defvar tooltip-use-echo-area)
872
873 (defun gdb-tooltip-print (expr)
874 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
875 (goto-char (point-min))
876 (if (re-search-forward ".*value=\\(\".*\"\\)" nil t)
877 (tooltip-show
878 (concat expr " = " (read (match-string 1)))
879 (or gud-tooltip-echo-area tooltip-use-echo-area
880 (not (display-graphic-p)))))))
881
882 ;; If expr is a macro for a function don't print because of possible dangerous
883 ;; side-effects. Also printing a function within a tooltip generates an
884 ;; unexpected starting annotation (phase error).
885 (defun gdb-tooltip-print-1 (expr)
886 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
887 (goto-char (point-min))
888 (if (search-forward "expands to: " nil t)
889 (unless (looking-at "\\S-+.*(.*).*")
890 (gdb-input
891 (list (concat "-data-evaluate-expression " expr)
892 `(lambda () (gdb-tooltip-print ,expr))))))))
893
894 (defun gdb-init-buffer ()
895 (set (make-local-variable 'gud-minor-mode) 'gdbmi)
896 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
897 (when gud-tooltip-mode
898 (make-local-variable 'gdb-define-alist)
899 (gdb-create-define-alist)
900 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))
901
902 (defmacro gdb-if-arrow (arrow-position &rest body)
903 `(if ,arrow-position
904 (let ((buffer (marker-buffer ,arrow-position)) (line))
905 (if (equal buffer (window-buffer (posn-window end)))
906 (with-current-buffer buffer
907 (when (or (equal start end)
908 (equal (posn-point start)
909 (marker-position ,arrow-position)))
910 ,@body))))))
911
912 (defun gdb-mouse-until (event)
913 "Continue running until a source line past the current line.
914 The destination source line can be selected either by clicking
915 with mouse-3 on the fringe/margin or dragging the arrow
916 with mouse-1 (default bindings)."
917 (interactive "e")
918 (let ((start (event-start event))
919 (end (event-end event)))
920 (gdb-if-arrow gud-overlay-arrow-position
921 (setq line (line-number-at-pos (posn-point end)))
922 (gud-call (concat "until " (number-to-string line))))
923 (gdb-if-arrow gdb-disassembly-position
924 (save-excursion
925 (goto-char (point-min))
926 (forward-line (1- (line-number-at-pos (posn-point end))))
927 (forward-char 2)
928 (gud-call (concat "until *%a"))))))
929
930 (defun gdb-mouse-jump (event)
931 "Set execution address/line.
932 The destination source line can be selected either by clicking with C-mouse-3
933 on the fringe/margin or dragging the arrow with C-mouse-1 (default bindings).
934 Unlike `gdb-mouse-until' the destination address can be before the current
935 line, and no execution takes place."
936 (interactive "e")
937 (let ((start (event-start event))
938 (end (event-end event)))
939 (gdb-if-arrow gud-overlay-arrow-position
940 (setq line (line-number-at-pos (posn-point end)))
941 (progn
942 (gud-call (concat "tbreak " (number-to-string line)))
943 (gud-call (concat "jump " (number-to-string line)))))
944 (gdb-if-arrow gdb-disassembly-position
945 (save-excursion
946 (goto-char (point-min))
947 (forward-line (1- (line-number-at-pos (posn-point end))))
948 (forward-char 2)
949 (progn
950 (gud-call (concat "tbreak *%a"))
951 (gud-call (concat "jump *%a")))))))
952
953 (defcustom gdb-show-changed-values t
954 "If non-nil change the face of out of scope variables and changed values.
955 Out of scope variables are suppressed with `shadow' face.
956 Changed values are highlighted with the face `font-lock-warning-face'."
957 :type 'boolean
958 :group 'gdb
959 :version "22.1")
960
961 (defcustom gdb-max-children 40
962 "Maximum number of children before expansion requires confirmation."
963 :type 'integer
964 :group 'gdb
965 :version "22.1")
966
967 (defcustom gdb-delete-out-of-scope t
968 "If non-nil delete watch expressions automatically when they go out of scope."
969 :type 'boolean
970 :group 'gdb
971 :version "22.2")
972
973 (defcustom gdb-speedbar-auto-raise nil
974 "If non-nil raise speedbar every time display of watch expressions is\
975 updated."
976 :type 'boolean
977 :group 'gdb
978 :version "22.1")
979
980 (defcustom gdb-use-colon-colon-notation nil
981 "If non-nil use FUN::VAR format to display variables in the speedbar."
982 :type 'boolean
983 :group 'gdb
984 :version "22.1")
985
986 (defun gdb-speedbar-auto-raise (arg)
987 "Toggle automatic raising of the speedbar for watch expressions.
988 With prefix argument ARG, automatically raise speedbar if ARG is
989 positive, otherwise don't automatically raise it."
990 (interactive "P")
991 (setq gdb-speedbar-auto-raise
992 (if (null arg)
993 (not gdb-speedbar-auto-raise)
994 (> (prefix-numeric-value arg) 0)))
995 (message (format "Auto raising %sabled"
996 (if gdb-speedbar-auto-raise "en" "dis"))))
997
998 (define-key gud-minor-mode-map "\C-c\C-w" 'gud-watch)
999 (define-key global-map (concat gud-key-prefix "\C-w") 'gud-watch)
1000
1001 (declare-function tooltip-identifier-from-point "tooltip" (point))
1002
1003 (defun gud-watch (&optional arg event)
1004 "Watch expression at point.
1005 With arg, enter name of variable to be watched in the minibuffer."
1006 (interactive (list current-prefix-arg last-input-event))
1007 (let ((minor-mode (buffer-local-value 'gud-minor-mode gud-comint-buffer)))
1008 (if (eq minor-mode 'gdbmi)
1009 (progn
1010 (if event (posn-set-point (event-end event)))
1011 (require 'tooltip)
1012 (save-selected-window
1013 (let ((expr
1014 (if arg
1015 (completing-read "Name of variable: "
1016 'gud-gdb-complete-command)
1017 (if (and transient-mark-mode mark-active)
1018 (buffer-substring (region-beginning) (region-end))
1019 (concat (if (derived-mode-p 'gdb-registers-mode) "$")
1020 (tooltip-identifier-from-point (point)))))))
1021 (set-text-properties 0 (length expr) nil expr)
1022 (gdb-input
1023 (list (concat"-var-create - * " expr "")
1024 `(lambda () (gdb-var-create-handler ,expr)))))))
1025 (message "gud-watch is a no-op in this mode."))))
1026
1027 (defun gdb-var-create-handler (expr)
1028 (let* ((result (gdb-json-partial-output)))
1029 (if (not (bindat-get-field result 'msg))
1030 (let ((var
1031 (list (bindat-get-field result 'name)
1032 (if (and (string-equal gdb-current-language "c")
1033 gdb-use-colon-colon-notation gdb-selected-frame)
1034 (setq expr (concat gdb-selected-frame "::" expr))
1035 expr)
1036 (bindat-get-field result 'numchild)
1037 (bindat-get-field result 'type)
1038 (bindat-get-field result 'value)
1039 nil
1040 (bindat-get-field result 'has_more)
1041 gdb-frame-address)))
1042 (push var gdb-var-list)
1043 (speedbar 1)
1044 (unless (string-equal
1045 speedbar-initial-expansion-list-name "GUD")
1046 (speedbar-change-initial-expansion-list "GUD")))
1047 (message-box "No symbol \"%s\" in current context." expr))))
1048
1049 (defun gdb-speedbar-update ()
1050 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame)
1051 (not (gdb-pending-p 'gdb-speedbar-timer)))
1052 ;; Dummy command to update speedbar even when idle.
1053 (gdb-input (list "-environment-pwd" 'gdb-speedbar-timer-fn))
1054 ;; Keep gdb-pending-triggers non-nil till end.
1055 (gdb-add-pending 'gdb-speedbar-timer)))
1056
1057 (defun gdb-speedbar-timer-fn ()
1058 (if gdb-speedbar-auto-raise
1059 (raise-frame speedbar-frame))
1060 (gdb-delete-pending 'gdb-speedbar-timer)
1061 (speedbar-timer-fn))
1062
1063 (defun gdb-var-evaluate-expression-handler (varnum changed)
1064 (goto-char (point-min))
1065 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
1066 (let ((var (assoc varnum gdb-var-list)))
1067 (when var
1068 (if changed (setcar (nthcdr 5 var) 'changed))
1069 (setcar (nthcdr 4 var) (read (match-string 1)))))
1070 (gdb-speedbar-update))
1071
1072 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
1073 (defun gdb-var-list-children (varnum)
1074 (gdb-input
1075 (list (concat "-var-update " varnum) 'ignore))
1076 (gdb-input
1077 (list (concat "-var-list-children --all-values "
1078 varnum)
1079 `(lambda () (gdb-var-list-children-handler ,varnum)))))
1080
1081 (defun gdb-var-list-children-handler (varnum)
1082 (let* ((var-list nil)
1083 (output (bindat-get-field (gdb-json-partial-output "child")))
1084 (children (bindat-get-field output 'children)))
1085 (catch 'child-already-watched
1086 (dolist (var gdb-var-list)
1087 (if (string-equal varnum (car var))
1088 (progn
1089 ;; With dynamic varobjs numchild may have increased.
1090 (setcar (nthcdr 2 var) (bindat-get-field output 'numchild))
1091 (push var var-list)
1092 (dolist (child children)
1093 (let ((varchild (list (bindat-get-field child 'name)
1094 (bindat-get-field child 'exp)
1095 (bindat-get-field child 'numchild)
1096 (bindat-get-field child 'type)
1097 (bindat-get-field child 'value)
1098 nil
1099 (bindat-get-field child 'has_more))))
1100 (if (assoc (car varchild) gdb-var-list)
1101 (throw 'child-already-watched nil))
1102 (push varchild var-list))))
1103 (push var var-list)))
1104 (setq gdb-var-list (nreverse var-list))))
1105 (gdb-speedbar-update))
1106
1107 (defun gdb-var-set-format (format)
1108 "Set the output format for a variable displayed in the speedbar."
1109 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1110 (varnum (car var)))
1111 (gdb-input
1112 (list (concat "-var-set-format " varnum " " format) 'ignore))
1113 (gdb-var-update)))
1114
1115 (defun gdb-var-delete-1 (var varnum)
1116 (gdb-input
1117 (list (concat "-var-delete " varnum) 'ignore))
1118 (setq gdb-var-list (delq var gdb-var-list))
1119 (dolist (varchild gdb-var-list)
1120 (if (string-match (concat (car var) "\\.") (car varchild))
1121 (setq gdb-var-list (delq varchild gdb-var-list)))))
1122
1123 (defun gdb-var-delete ()
1124 "Delete watch expression at point from the speedbar."
1125 (interactive)
1126 (let ((text (speedbar-line-text)))
1127 (string-match "\\(\\S-+\\)" text)
1128 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1129 (varnum (car var)))
1130 (if (string-match "\\." (car var))
1131 (message-box "Can only delete a root expression")
1132 (gdb-var-delete-1 var varnum)))))
1133
1134 (defun gdb-var-delete-children (varnum)
1135 "Delete children of variable object at point from the speedbar."
1136 (gdb-input
1137 (list (concat "-var-delete -c " varnum) 'ignore)))
1138
1139 (defun gdb-edit-value (text token indent)
1140 "Assign a value to a variable displayed in the speedbar."
1141 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
1142 (varnum (car var)) (value))
1143 (setq value (read-string "New value: "))
1144 (gdb-input
1145 (list (concat "-var-assign " varnum " " value)
1146 `(lambda () (gdb-edit-value-handler ,value))))))
1147
1148 (defconst gdb-error-regexp "\\^error,msg=\\(\".+\"\\)")
1149
1150 (defun gdb-edit-value-handler (value)
1151 (goto-char (point-min))
1152 (if (re-search-forward gdb-error-regexp nil t)
1153 (message-box "Invalid number or expression (%s)" value)))
1154
1155 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
1156 (defun gdb-var-update ()
1157 (if (not (gdb-pending-p 'gdb-var-update))
1158 (gdb-input
1159 (list "-var-update --all-values *" 'gdb-var-update-handler)))
1160 (gdb-add-pending 'gdb-var-update))
1161
1162 (defun gdb-var-update-handler ()
1163 (let ((changelist (bindat-get-field (gdb-json-partial-output) 'changelist)))
1164 (dolist (var gdb-var-list)
1165 (setcar (nthcdr 5 var) nil))
1166 (let ((temp-var-list gdb-var-list))
1167 (dolist (change changelist)
1168 (let* ((varnum (bindat-get-field change 'name))
1169 (var (assoc varnum gdb-var-list))
1170 (new-num (bindat-get-field change 'new_num_children)))
1171 (when var
1172 (let ((scope (bindat-get-field change 'in_scope))
1173 (has-more (bindat-get-field change 'has_more)))
1174 (cond ((string-equal scope "false")
1175 (if gdb-delete-out-of-scope
1176 (gdb-var-delete-1 var varnum)
1177 (setcar (nthcdr 5 var) 'out-of-scope)))
1178 ((string-equal scope "true")
1179 (setcar (nthcdr 6 var) has-more)
1180 (when (and (or (not has-more)
1181 (string-equal has-more "0"))
1182 (not new-num)
1183 (string-equal (nth 2 var) "0"))
1184 (setcar (nthcdr 4 var)
1185 (bindat-get-field change 'value))
1186 (setcar (nthcdr 5 var) 'changed)))
1187 ((string-equal scope "invalid")
1188 (gdb-var-delete-1 var varnum)))))
1189 (let ((var-list nil) var1
1190 (children (bindat-get-field change 'new_children)))
1191 (if new-num
1192 (progn
1193 (setq var1 (pop temp-var-list))
1194 (while var1
1195 (if (string-equal varnum (car var1))
1196 (let ((new (string-to-number new-num))
1197 (previous (string-to-number (nth 2 var1))))
1198 (setcar (nthcdr 2 var1) new-num)
1199 (push var1 var-list)
1200 (cond ((> new previous)
1201 ;; Add new children to list.
1202 (dotimes (dummy previous)
1203 (push (pop temp-var-list) var-list))
1204 (dolist (child children)
1205 (let ((varchild
1206 (list (bindat-get-field child 'name)
1207 (bindat-get-field child 'exp)
1208 (bindat-get-field child 'numchild)
1209 (bindat-get-field child 'type)
1210 (bindat-get-field child 'value)
1211 'changed
1212 (bindat-get-field child 'has_more))))
1213 (push varchild var-list))))
1214 ;; Remove deleted children from list.
1215 ((< new previous)
1216 (dotimes (dummy new)
1217 (push (pop temp-var-list) var-list))
1218 (dotimes (dummy (- previous new))
1219 (pop temp-var-list)))))
1220 (push var1 var-list))
1221 (setq var1 (pop temp-var-list)))
1222 (setq gdb-var-list (nreverse var-list)))))))))
1223 (setq gdb-pending-triggers
1224 (delq 'gdb-var-update gdb-pending-triggers))
1225 (gdb-speedbar-update))
1226
1227 (defun gdb-speedbar-expand-node (text token indent)
1228 "Expand the node the user clicked on.
1229 TEXT is the text of the button we clicked on, a + or - item.
1230 TOKEN is data related to this node.
1231 INDENT is the current indentation depth."
1232 (cond ((string-match "+" text) ;expand this node
1233 (let* ((var (assoc token gdb-var-list))
1234 (expr (nth 1 var)) (children (nth 2 var)))
1235 (if (or (<= (string-to-number children) gdb-max-children)
1236 (y-or-n-p
1237 (format "%s has %s children. Continue? " expr children)))
1238 (gdb-var-list-children token))))
1239 ((string-match "-" text) ;contract this node
1240 (dolist (var gdb-var-list)
1241 (if (string-match (concat token "\\.") (car var))
1242 (setq gdb-var-list (delq var gdb-var-list))))
1243 (gdb-var-delete-children token)
1244 (speedbar-change-expand-button-char ?+)
1245 (speedbar-delete-subblock indent))
1246 (t (error "Ooops... not sure what to do")))
1247 (speedbar-center-buffer-smartly))
1248
1249 (defun gdb-get-target-string ()
1250 (with-current-buffer gud-comint-buffer
1251 gud-target-name))
1252 \f
1253
1254 ;;
1255 ;; gdb buffers.
1256 ;;
1257 ;; Each buffer has a TYPE -- a symbol that identifies the function
1258 ;; of that particular buffer.
1259 ;;
1260 ;; The usual gdb interaction buffer is given the type `gdbmi' and
1261 ;; is constructed specially.
1262 ;;
1263 ;; Others are constructed by gdb-get-buffer-create and
1264 ;; named according to the rules set forth in the gdb-buffer-rules
1265
1266 (defvar gdb-buffer-rules '())
1267
1268 (defun gdb-rules-name-maker (rules-entry)
1269 (cadr rules-entry))
1270 (defun gdb-rules-buffer-mode (rules-entry)
1271 (nth 2 rules-entry))
1272 (defun gdb-rules-update-trigger (rules-entry)
1273 (nth 3 rules-entry))
1274
1275 (defun gdb-update-buffer-name ()
1276 "Rename current buffer according to name-maker associated with
1277 it in `gdb-buffer-rules'."
1278 (let ((f (gdb-rules-name-maker (assoc gdb-buffer-type
1279 gdb-buffer-rules))))
1280 (when f (rename-buffer (funcall f)))))
1281
1282 (defun gdb-current-buffer-rules ()
1283 "Get `gdb-buffer-rules' entry for current buffer type."
1284 (assoc gdb-buffer-type gdb-buffer-rules))
1285
1286 (defun gdb-current-buffer-thread ()
1287 "Get thread object of current buffer from `gdb-threads-list'.
1288
1289 When current buffer is not bound to any thread, return main
1290 thread."
1291 (cdr (assoc gdb-thread-number gdb-threads-list)))
1292
1293 (defun gdb-current-buffer-frame ()
1294 "Get current stack frame object for thread of current buffer."
1295 (bindat-get-field (gdb-current-buffer-thread) 'frame))
1296
1297 (defun gdb-buffer-type (buffer)
1298 "Get value of `gdb-buffer-type' for BUFFER."
1299 (with-current-buffer buffer
1300 gdb-buffer-type))
1301
1302 (defun gdb-buffer-shows-main-thread-p ()
1303 "Return t if current GDB buffer shows main selected thread and
1304 is not bound to it."
1305 (current-buffer)
1306 (not (local-variable-p 'gdb-thread-number)))
1307
1308 (defun gdb-get-buffer (buffer-type &optional thread)
1309 "Get a specific GDB buffer.
1310
1311 In that buffer, `gdb-buffer-type' must be equal to BUFFER-TYPE
1312 and `gdb-thread-number' (if provided) must be equal to THREAD."
1313 (catch 'found
1314 (dolist (buffer (buffer-list) nil)
1315 (with-current-buffer buffer
1316 (when (and (eq gdb-buffer-type buffer-type)
1317 (or (not thread)
1318 (equal gdb-thread-number thread)))
1319 (throw 'found buffer))))))
1320
1321 (defun gdb-get-buffer-create (buffer-type &optional thread)
1322 "Create a new GDB buffer of the type specified by BUFFER-TYPE.
1323 The buffer-type should be one of the cars in `gdb-buffer-rules'.
1324
1325 If THREAD is non-nil, it is assigned to `gdb-thread-number'
1326 buffer-local variable of the new buffer.
1327
1328 Buffer mode and name are selected according to buffer type.
1329
1330 If buffer has trigger associated with it in `gdb-buffer-rules',
1331 this trigger is subscribed to `gdb-buf-publisher' and called with
1332 'update argument."
1333 (or (gdb-get-buffer buffer-type thread)
1334 (let ((rules (assoc buffer-type gdb-buffer-rules))
1335 (new (generate-new-buffer "limbo")))
1336 (with-current-buffer new
1337 (let ((mode (gdb-rules-buffer-mode rules))
1338 (trigger (gdb-rules-update-trigger rules)))
1339 (when mode (funcall mode))
1340 (setq gdb-buffer-type buffer-type)
1341 (when thread
1342 (set (make-local-variable 'gdb-thread-number) thread))
1343 (set (make-local-variable 'gud-minor-mode)
1344 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1345 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1346 (rename-buffer (funcall (gdb-rules-name-maker rules)))
1347 (when trigger
1348 (gdb-add-subscriber gdb-buf-publisher
1349 (cons (current-buffer)
1350 (gdb-bind-function-to-buffer trigger (current-buffer))))
1351 (funcall trigger 'start))
1352 (current-buffer))))))
1353
1354 (defun gdb-bind-function-to-buffer (expr buffer)
1355 "Return a function which will evaluate EXPR in BUFFER."
1356 `(lambda (&rest args)
1357 (with-current-buffer ,buffer
1358 (apply ',expr args))))
1359
1360 ;; Used to define all gdb-frame-*-buffer functions except
1361 ;; `gdb-frame-io-buffer'
1362 (defmacro def-gdb-frame-for-buffer (name buffer &optional doc)
1363 "Define a function NAME which shows gdb BUFFER in a separate frame.
1364
1365 DOC is an optional documentation string."
1366 `(defun ,name (&optional thread)
1367 ,(when doc doc)
1368 (interactive)
1369 (let ((special-display-regexps (append special-display-regexps '(".*")))
1370 (special-display-frame-alist gdb-frame-parameters))
1371 (display-buffer (gdb-get-buffer-create ,buffer thread)))))
1372
1373 (defmacro def-gdb-display-buffer (name buffer &optional doc)
1374 "Define a function NAME which shows gdb BUFFER.
1375
1376 DOC is an optional documentation string."
1377 `(defun ,name (&optional thread)
1378 ,(when doc doc)
1379 (interactive)
1380 (gdb-display-buffer
1381 (gdb-get-buffer-create ,buffer thread) t)))
1382
1383 ;; Used to display windows with thread-bound buffers
1384 (defmacro def-gdb-preempt-display-buffer (name buffer &optional doc
1385 split-horizontal)
1386 `(defun ,name (&optional thread)
1387 ,(when doc doc)
1388 (message thread)
1389 (gdb-preempt-existing-or-display-buffer
1390 (gdb-get-buffer-create ,buffer thread)
1391 ,split-horizontal)))
1392
1393 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1394 ;; at least one and possible more functions. The functions have these
1395 ;; roles in defining a buffer type:
1396 ;;
1397 ;; NAME - Return a name for this buffer type.
1398 ;;
1399 ;; The remaining function(s) are optional:
1400 ;;
1401 ;; MODE - called in a new buffer with no arguments, should establish
1402 ;; the proper mode for the buffer.
1403 ;;
1404
1405 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1406 (let ((binding (assoc buffer-type gdb-buffer-rules)))
1407 (if binding
1408 (setcdr binding rules)
1409 (push (cons buffer-type rules)
1410 gdb-buffer-rules))))
1411
1412 (defun gdb-parent-mode ()
1413 "Generic mode to derive all other GDB buffer modes from."
1414 (kill-all-local-variables)
1415 (setq buffer-read-only t)
1416 (buffer-disable-undo)
1417 ;; Delete buffer from gdb-buf-publisher when it's killed
1418 ;; (if it has an associated update trigger)
1419 (add-hook
1420 'kill-buffer-hook
1421 (function
1422 (lambda ()
1423 (let ((trigger (gdb-rules-update-trigger
1424 (gdb-current-buffer-rules))))
1425 (when trigger
1426 (gdb-delete-subscriber
1427 gdb-buf-publisher
1428 ;; This should match gdb-add-subscriber done in
1429 ;; gdb-get-buffer-create
1430 (cons (current-buffer)
1431 (gdb-bind-function-to-buffer trigger (current-buffer))))))))
1432 nil t))
1433
1434 ;; Partial-output buffer : This accumulates output from a command executed on
1435 ;; behalf of emacs (rather than the user).
1436 ;;
1437 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1438 'gdb-partial-output-name)
1439
1440 (defun gdb-partial-output-name ()
1441 (concat " *partial-output-"
1442 (gdb-get-target-string)
1443 "*"))
1444
1445 \f
1446 (gdb-set-buffer-rules 'gdb-inferior-io
1447 'gdb-inferior-io-name
1448 'gdb-inferior-io-mode)
1449
1450 (defun gdb-inferior-io-name ()
1451 (concat "*input/output of "
1452 (gdb-get-target-string)
1453 "*"))
1454
1455 (defun gdb-display-io-buffer ()
1456 "Display IO of debugged program in a separate window."
1457 (interactive)
1458 (gdb-display-buffer
1459 (gdb-get-buffer-create 'gdb-inferior-io) t))
1460
1461 (defconst gdb-frame-parameters
1462 '((height . 14) (width . 80)
1463 (unsplittable . t)
1464 (tool-bar-lines . nil)
1465 (menu-bar-lines . nil)
1466 (minibuffer . nil)))
1467
1468 (defun gdb-frame-io-buffer ()
1469 "Display IO of debugged program in a new frame."
1470 (interactive)
1471 (let ((special-display-regexps (append special-display-regexps '(".*")))
1472 (special-display-frame-alist gdb-frame-parameters))
1473 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io))))
1474
1475 (defvar gdb-inferior-io-mode-map
1476 (let ((map (make-sparse-keymap)))
1477 (define-key map "\C-c\C-c" 'gdb-io-interrupt)
1478 (define-key map "\C-c\C-z" 'gdb-io-stop)
1479 (define-key map "\C-c\C-\\" 'gdb-io-quit)
1480 (define-key map "\C-c\C-d" 'gdb-io-eof)
1481 (define-key map "\C-d" 'gdb-io-eof)
1482 map))
1483
1484 ;; We want to use comint because it has various nifty and familiar features.
1485 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1486 "Major mode for gdb inferior-io."
1487 :syntax-table nil :abbrev-table nil
1488 (make-comint-in-buffer "gdb-inferior" (current-buffer) nil))
1489
1490 (defun gdb-inferior-filter (proc string)
1491 (unless (string-equal string "")
1492 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t))
1493 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1494 (comint-output-filter proc string)))
1495
1496 (defun gdb-io-interrupt ()
1497 "Interrupt the program being debugged."
1498 (interactive)
1499 (interrupt-process
1500 (get-buffer-process gud-comint-buffer) comint-ptyp))
1501
1502 (defun gdb-io-quit ()
1503 "Send quit signal to the program being debugged."
1504 (interactive)
1505 (quit-process
1506 (get-buffer-process gud-comint-buffer) comint-ptyp))
1507
1508 (defun gdb-io-stop ()
1509 "Stop the program being debugged."
1510 (interactive)
1511 (stop-process
1512 (get-buffer-process gud-comint-buffer) comint-ptyp))
1513
1514 (defun gdb-io-eof ()
1515 "Send end-of-file to the program being debugged."
1516 (interactive)
1517 (process-send-eof
1518 (get-buffer-process gud-comint-buffer)))
1519
1520 (defun gdb-clear-inferior-io ()
1521 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1522 (erase-buffer)))
1523 \f
1524
1525 (defconst breakpoint-xpm-data
1526 "/* XPM */
1527 static char *magick[] = {
1528 /* columns rows colors chars-per-pixel */
1529 \"10 10 2 1\",
1530 \" c red\",
1531 \"+ c None\",
1532 /* pixels */
1533 \"+++ +++\",
1534 \"++ ++\",
1535 \"+ +\",
1536 \" \",
1537 \" \",
1538 \" \",
1539 \" \",
1540 \"+ +\",
1541 \"++ ++\",
1542 \"+++ +++\",
1543 };"
1544 "XPM data used for breakpoint icon.")
1545
1546 (defconst breakpoint-enabled-pbm-data
1547 "P1
1548 10 10\",
1549 0 0 0 0 1 1 1 1 0 0 0 0
1550 0 0 0 1 1 1 1 1 1 0 0 0
1551 0 0 1 1 1 1 1 1 1 1 0 0
1552 0 1 1 1 1 1 1 1 1 1 1 0
1553 0 1 1 1 1 1 1 1 1 1 1 0
1554 0 1 1 1 1 1 1 1 1 1 1 0
1555 0 1 1 1 1 1 1 1 1 1 1 0
1556 0 0 1 1 1 1 1 1 1 1 0 0
1557 0 0 0 1 1 1 1 1 1 0 0 0
1558 0 0 0 0 1 1 1 1 0 0 0 0"
1559 "PBM data used for enabled breakpoint icon.")
1560
1561 (defconst breakpoint-disabled-pbm-data
1562 "P1
1563 10 10\",
1564 0 0 1 0 1 0 1 0 0 0
1565 0 1 0 1 0 1 0 1 0 0
1566 1 0 1 0 1 0 1 0 1 0
1567 0 1 0 1 0 1 0 1 0 1
1568 1 0 1 0 1 0 1 0 1 0
1569 0 1 0 1 0 1 0 1 0 1
1570 1 0 1 0 1 0 1 0 1 0
1571 0 1 0 1 0 1 0 1 0 1
1572 0 0 1 0 1 0 1 0 1 0
1573 0 0 0 1 0 1 0 1 0 0"
1574 "PBM data used for disabled breakpoint icon.")
1575
1576 (defvar breakpoint-enabled-icon nil
1577 "Icon for enabled breakpoint in display margin.")
1578
1579 (defvar breakpoint-disabled-icon nil
1580 "Icon for disabled breakpoint in display margin.")
1581
1582 (declare-function define-fringe-bitmap "fringe.c"
1583 (bitmap bits &optional height width align))
1584
1585 (and (display-images-p)
1586 ;; Bitmap for breakpoint in fringe
1587 (define-fringe-bitmap 'breakpoint
1588 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1589 ;; Bitmap for gud-overlay-arrow in fringe
1590 (define-fringe-bitmap 'hollow-right-triangle
1591 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1592
1593 (defface breakpoint-enabled
1594 '((t
1595 :foreground "red1"
1596 :weight bold))
1597 "Face for enabled breakpoint icon in fringe."
1598 :group 'gdb)
1599
1600 (defface breakpoint-disabled
1601 '((((class color) (min-colors 88)) :foreground "grey70")
1602 ;; Ensure that on low-color displays that we end up something visible.
1603 (((class color) (min-colors 8) (background light))
1604 :foreground "black")
1605 (((class color) (min-colors 8) (background dark))
1606 :foreground "white")
1607 (((type tty) (class mono))
1608 :inverse-video t)
1609 (t :background "gray"))
1610 "Face for disabled breakpoint icon in fringe."
1611 :group 'gdb)
1612
1613 \f
1614 (defun gdb-send (proc string)
1615 "A comint send filter for gdb."
1616 (with-current-buffer gud-comint-buffer
1617 (let ((inhibit-read-only t))
1618 (remove-text-properties (point-min) (point-max) '(face))))
1619 ;; mimic <RET> key to repeat previous command in GDB
1620 (if (not (string= "" string))
1621 (setq gdb-last-command string)
1622 (if gdb-last-command (setq string gdb-last-command)))
1623 (if gdb-enable-debug
1624 (push (cons 'mi-send (concat string "\n")) gdb-debug-log))
1625 (if (string-match "^-" string)
1626 ;; MI command
1627 (progn
1628 (setq gdb-first-done-or-error t)
1629 (process-send-string proc (concat string "\n")))
1630 ;; CLI command
1631 (if (string-match "\\\\$" string)
1632 (setq gdb-continuation (concat gdb-continuation string "\n"))
1633 (setq gdb-first-done-or-error t)
1634 (process-send-string proc (concat "-interpreter-exec console \""
1635 gdb-continuation string "\"\n"))
1636 (setq gdb-continuation nil))))
1637
1638 (defun gdb-input (item)
1639 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1640 (setq gdb-token-number (1+ gdb-token-number))
1641 (setcar item (concat (number-to-string gdb-token-number) (car item)))
1642 (push (cons gdb-token-number (car (cdr item))) gdb-handler-alist)
1643 (process-send-string (get-buffer-process gud-comint-buffer)
1644 (concat (car item) "\n")))
1645
1646 ;; NOFRAME is used for gud execution control commands
1647 (defun gdb-current-context-command (command)
1648 "Add --thread to gdb COMMAND when needed."
1649 (if (and gdb-thread-number
1650 (string-equal gdb-version "7.0+"))
1651 (concat command " --thread " gdb-thread-number)
1652 command))
1653
1654 (defun gdb-current-context-buffer-name (name)
1655 "Add thread information and asterisks to string NAME.
1656
1657 If `gdb-thread-number' is nil, just wrap NAME in asterisks."
1658 (concat "*" name
1659 (if (local-variable-p 'gdb-thread-number)
1660 (format " (bound to thread %s)" gdb-thread-number)
1661 "")
1662 "*"))
1663
1664 (defun gdb-current-context-mode-name (mode)
1665 "Add thread information to MODE which is to be used as
1666 `mode-name'."
1667 (concat mode
1668 (if gdb-thread-number
1669 (format " [thread %s]" gdb-thread-number)
1670 "")))
1671 \f
1672
1673 (defcustom gud-gdb-command-name "gdb -i=mi"
1674 "Default command to execute an executable under the GDB debugger."
1675 :type 'string
1676 :group 'gdb)
1677
1678 (defun gdb-resync()
1679 (setq gud-running nil)
1680 (setq gdb-output-sink 'user)
1681 (setq gdb-pending-triggers nil))
1682
1683 (defun gdb-update ()
1684 "Update buffers showing status of debug session."
1685 (when gdb-first-prompt
1686 (gdb-force-mode-line-update
1687 (propertize "initializing..." 'face font-lock-variable-name-face))
1688 (gdb-init-1)
1689 (setq gdb-first-prompt nil))
1690
1691 (gdb-get-main-selected-frame)
1692 ;; We may need to update gdb-threads-list so we can use
1693 (gdb-get-buffer-create 'gdb-threads-buffer)
1694 ;; gdb-break-list is maintained in breakpoints handler
1695 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1696
1697 (gdb-emit-signal gdb-buf-publisher 'update)
1698
1699 (gdb-get-changed-registers)
1700
1701 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1702 (dolist (var gdb-var-list)
1703 (setcar (nthcdr 5 var) nil))
1704 (gdb-var-update)))
1705
1706 ;; gdb-setq-thread-number and gdb-update-gud-running are decoupled
1707 ;; because we may need to update current gud-running value without
1708 ;; changing current thread (see gdb-running)
1709 (defun gdb-setq-thread-number (number)
1710 "Only this function must be used to change `gdb-thread-number'
1711 value to NUMBER, because `gud-running' and `gdb-frame-number'
1712 need to be updated appropriately when current thread changes."
1713 ;; GDB 6.8 and earlier always output thread-id="0" when stopping.
1714 (unless (string-equal number "0") (setq gdb-thread-number number))
1715 (setq gdb-frame-number "0")
1716 (gdb-update-gud-running))
1717
1718 (defun gdb-update-gud-running ()
1719 "Set `gud-running' according to the state of current thread.
1720
1721 `gdb-frame-number' is set to 0 if current thread is now stopped.
1722
1723 Note that when `gdb-gud-control-all-threads' is t, `gud-running'
1724 cannot be reliably used to determine whether or not execution
1725 control buttons should be shown in menu or toolbar. Use
1726 `gdb-running-threads-count' and `gdb-stopped-threads-count'
1727 instead.
1728
1729 For all-stop mode, thread information is unavailable while target
1730 is running."
1731 (let ((old-value gud-running))
1732 (setq gud-running
1733 (string= (bindat-get-field (gdb-current-buffer-thread) 'state)
1734 "running"))
1735 ;; Set frame number to "0" when _current_ threads stops
1736 (when (and (gdb-current-buffer-thread)
1737 (not (eq gud-running old-value)))
1738 (setq gdb-frame-number "0"))))
1739
1740 (defun gdb-show-run-p ()
1741 "Return t if \"Run/continue\" should be shown on the toolbar."
1742 (or (not gdb-active-process)
1743 (and (or
1744 (not gdb-gud-control-all-threads)
1745 (not gdb-non-stop))
1746 (not gud-running))
1747 (and gdb-gud-control-all-threads
1748 (> gdb-stopped-threads-count 0))))
1749
1750 (defun gdb-show-stop-p ()
1751 "Return t if \"Stop\" should be shown on the toolbar."
1752 (or (and (or
1753 (not gdb-gud-control-all-threads)
1754 (not gdb-non-stop))
1755 gud-running)
1756 (and gdb-gud-control-all-threads
1757 (> gdb-running-threads-count 0))))
1758
1759 ;; GUD displays the selected GDB frame. This might might not be the current
1760 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1761 ;; visited breakpoint is, use that window.
1762 (defun gdb-display-source-buffer (buffer)
1763 (let* ((last-window (if gud-last-last-frame
1764 (get-buffer-window
1765 (gud-find-file (car gud-last-last-frame)))))
1766 (source-window (or last-window
1767 (if (and gdb-source-window
1768 (window-live-p gdb-source-window))
1769 gdb-source-window))))
1770 (when source-window
1771 (setq gdb-source-window source-window)
1772 (set-window-buffer source-window buffer))
1773 source-window))
1774
1775 (defun gdb-car< (a b)
1776 (< (car a) (car b)))
1777
1778 (defvar gdbmi-record-list
1779 '((gdb-gdb . "(gdb) \n")
1780 (gdb-done . "\\([0-9]*\\)\\^done,?\\(.*?\\)\n")
1781 (gdb-starting . "\\([0-9]*\\)\\^running\n")
1782 (gdb-error . "\\([0-9]*\\)\\^error,\\(.*?\\)\n")
1783 (gdb-console . "~\\(\".*?\"\\)\n")
1784 (gdb-internals . "&\\(\".*?\"\\)\n")
1785 (gdb-stopped . "\\*stopped,?\\(.*?\\)\n")
1786 (gdb-running . "\\*running,\\(.*?\n\\)")
1787 (gdb-thread-created . "=thread-created,\\(.*?\n\\)")
1788 (gdb-thread-selected . "=thread-selected,\\(.*?\\)\n")
1789 (gdb-thread-exited . "=thread-exited,\\(.*?\n\\)")
1790 (gdb-ignored-notification . "=[-[:alpha:]]+,?\\(.*?\\)\n")
1791 (gdb-shell . "\\(\\(?:^.+\n\\)+\\)")))
1792
1793 (defun gud-gdbmi-marker-filter (string)
1794 "Filter GDB/MI output."
1795
1796 ;; Record transactions if logging is enabled.
1797 (when gdb-enable-debug
1798 (push (cons 'recv string) gdb-debug-log)
1799 (if (and gdb-debug-log-max
1800 (> (length gdb-debug-log) gdb-debug-log-max))
1801 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1802
1803 ;; Recall the left over gud-marker-acc from last time
1804 (setq gud-marker-acc (concat gud-marker-acc string))
1805
1806 ;; Start accumulating output for the GUD buffer
1807 (setq gdb-filter-output "")
1808 (let ((output-record) (output-record-list))
1809
1810 ;; Process all the complete markers in this chunk.
1811 (dolist (gdbmi-record gdbmi-record-list)
1812 (while (string-match (cdr gdbmi-record) gud-marker-acc)
1813 (push (list (match-beginning 0)
1814 (car gdbmi-record)
1815 (match-string 1 gud-marker-acc)
1816 (match-string 2 gud-marker-acc)
1817 (match-end 0))
1818 output-record-list)
1819 (setq gud-marker-acc
1820 (concat (substring gud-marker-acc 0 (match-beginning 0))
1821 ;; Pad with spaces to preserve position.
1822 (make-string (length (match-string 0 gud-marker-acc)) 32)
1823 (substring gud-marker-acc (match-end 0))))))
1824
1825 (setq output-record-list (sort output-record-list 'gdb-car<))
1826
1827 (dolist (output-record output-record-list)
1828 (let ((record-type (cadr output-record))
1829 (arg1 (nth 2 output-record))
1830 (arg2 (nth 3 output-record)))
1831 (if (eq record-type 'gdb-error)
1832 (gdb-done-or-error arg2 arg1 'error)
1833 (if (eq record-type 'gdb-done)
1834 (gdb-done-or-error arg2 arg1 'done)
1835 ;; Suppress "No registers." since GDB 6.8 and earlier duplicates MI
1836 ;; error message on internal stream. Don't print to GUD buffer.
1837 (unless (and (eq record-type 'gdb-internals)
1838 (string-equal (read arg1) "No registers.\n"))
1839 (funcall record-type arg1))))))
1840
1841 (setq gdb-output-sink 'user)
1842 ;; Remove padding.
1843 (string-match "^ *" gud-marker-acc)
1844 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1845
1846 gdb-filter-output))
1847
1848 (defun gdb-gdb (output-field))
1849
1850 (defun gdb-shell (output-field)
1851 (let ((gdb-output-sink gdb-output-sink))
1852 (setq gdb-filter-output
1853 (concat output-field gdb-filter-output))))
1854
1855 (defun gdb-ignored-notification (output-field))
1856
1857 ;; gdb-invalidate-threads is defined to accept 'update-threads signal
1858 (defun gdb-thread-created (output-field))
1859 (defun gdb-thread-exited (output-field)
1860 "Handle =thread-exited async record: unset `gdb-thread-number'
1861 if current thread exited and update threads list."
1862 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'id)))
1863 (if (string= gdb-thread-number thread-id)
1864 (gdb-setq-thread-number nil))
1865 ;; When we continue current thread and it quickly exits,
1866 ;; gdb-pending-triggers left after gdb-running disallow us to
1867 ;; properly call -thread-info without --thread option. Thus we
1868 ;; need to use gdb-wait-for-pending.
1869 (gdb-wait-for-pending
1870 (gdb-emit-signal gdb-buf-publisher 'update-threads))))
1871
1872 (defun gdb-thread-selected (output-field)
1873 "Handler for =thread-selected MI output record.
1874
1875 Sets `gdb-thread-number' to new id."
1876 (let* ((result (gdb-json-string output-field))
1877 (thread-id (bindat-get-field result 'id)))
1878 (gdb-setq-thread-number thread-id)
1879 ;; Typing `thread N` in GUD buffer makes GDB emit `^done` followed
1880 ;; by `=thread-selected` notification. `^done` causes `gdb-update`
1881 ;; as usually. Things happen to fast and second call (from
1882 ;; gdb-thread-selected handler) gets cut off by our beloved
1883 ;; gdb-pending-triggers.
1884 ;; Solution is `gdb-wait-for-pending` macro: it guarantees that its
1885 ;; body will get executed when `gdb-pending-triggers` is empty.
1886 (gdb-wait-for-pending
1887 (gdb-update))))
1888
1889 (defun gdb-running (output-field)
1890 (let* ((thread-id (bindat-get-field (gdb-json-string output-field) 'thread-id)))
1891 ;; We reset gdb-frame-number to nil if current thread has gone
1892 ;; running. This can't be done in gdb-thread-list-handler-custom
1893 ;; because we need correct gdb-frame-number by the time
1894 ;; -thread-info command is sent.
1895 (when (or (string-equal thread-id "all")
1896 (string-equal thread-id gdb-thread-number))
1897 (setq gdb-frame-number nil)))
1898 (setq gdb-inferior-status "running")
1899 (gdb-force-mode-line-update
1900 (propertize gdb-inferior-status 'face font-lock-type-face))
1901 (when (not gdb-non-stop)
1902 (setq gud-running t))
1903 (setq gdb-active-process t)
1904 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1905
1906 (defun gdb-starting (output-field)
1907 ;; CLI commands don't emit ^running at the moment so use gdb-running too.
1908 (setq gdb-inferior-status "running")
1909 (gdb-force-mode-line-update
1910 (propertize gdb-inferior-status 'face font-lock-type-face))
1911 (setq gdb-active-process t)
1912 (setq gud-running t)
1913 ;; GDB doesn't seem to respond to -thread-info before first stop or
1914 ;; thread exit (even in non-stop mode), so this is useless.
1915 ;; Behaviour may change in the future.
1916 (gdb-emit-signal gdb-buf-publisher 'update-threads))
1917
1918 ;; -break-insert -t didn't give a reason before gdb 6.9
1919
1920 (defun gdb-stopped (output-field)
1921 "Given the contents of *stopped MI async record, select new
1922 current thread and update GDB buffers."
1923 ;; Reason is available with target-async only
1924 (let* ((result (gdb-json-string output-field))
1925 (reason (bindat-get-field result 'reason))
1926 (thread-id (bindat-get-field result 'thread-id)))
1927
1928 ;; -data-list-register-names needs to be issued for any stopped
1929 ;; thread
1930 (when (not gdb-register-names)
1931 (gdb-input
1932 (list (concat "-data-list-register-names"
1933 (if (string-equal gdb-version "7.0+")
1934 (concat" --thread " thread-id)))
1935 'gdb-register-names-handler)))
1936
1937 ;;; Don't set gud-last-frame here as it's currently done in gdb-frame-handler
1938 ;;; because synchronous GDB doesn't give these fields with CLI.
1939 ;;; (when file
1940 ;;; (setq
1941 ;;; ;; Extract the frame position from the marker.
1942 ;;; gud-last-frame (cons file
1943 ;;; (string-to-number
1944 ;;; (match-string 6 gud-marker-acc)))))
1945
1946 (setq gdb-inferior-status (or reason "unknown"))
1947 (gdb-force-mode-line-update
1948 (propertize gdb-inferior-status 'face font-lock-warning-face))
1949 (if (string-equal reason "exited-normally")
1950 (setq gdb-active-process nil))
1951
1952 ;; Select new current thread.
1953
1954 ;; Don't switch if we have no reasons selected
1955 (when gdb-switch-reasons
1956 ;; Switch from another stopped thread only if we have
1957 ;; gdb-switch-when-another-stopped:
1958 (when (or gdb-switch-when-another-stopped
1959 (not (string= "stopped"
1960 (bindat-get-field (gdb-current-buffer-thread) 'state))))
1961 ;; Switch if current reason has been selected or we have no
1962 ;; reasons
1963 (if (or (eq gdb-switch-reasons t)
1964 (member reason gdb-switch-reasons))
1965 (when (not (string-equal gdb-thread-number thread-id))
1966 (message (concat "Switched to thread " thread-id))
1967 (gdb-setq-thread-number thread-id))
1968 (message (format "Thread %s stopped" thread-id)))))
1969
1970 ;; Print "(gdb)" to GUD console
1971 (when gdb-first-done-or-error
1972 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
1973
1974 ;; In non-stop, we update information as soon as another thread gets
1975 ;; stopped
1976 (when (or gdb-first-done-or-error
1977 gdb-non-stop)
1978 ;; In all-stop this updates gud-running properly as well.
1979 (gdb-update)
1980 (setq gdb-first-done-or-error nil))
1981 (run-hook-with-args 'gdb-stopped-hooks result)))
1982
1983 ;; Remove the trimmings from log stream containing debugging messages
1984 ;; being produced by GDB's internals, use warning face and send to GUD
1985 ;; buffer.
1986 (defun gdb-internals (output-field)
1987 (setq gdb-filter-output
1988 (gdb-concat-output
1989 gdb-filter-output
1990 (let ((error-message
1991 (read output-field)))
1992 (put-text-property
1993 0 (length error-message)
1994 'face font-lock-warning-face
1995 error-message)
1996 error-message))))
1997
1998 ;; Remove the trimmings from the console stream and send to GUD buffer
1999 ;; (frontend MI commands should not print to this stream)
2000 (defun gdb-console (output-field)
2001 (setq gdb-filter-output
2002 (gdb-concat-output
2003 gdb-filter-output
2004 (read output-field))))
2005
2006 (defun gdb-done-or-error (output-field token-number type)
2007 (if (string-equal token-number "")
2008 ;; Output from command entered by user
2009 (progn
2010 (setq gdb-output-sink 'user)
2011 (setq token-number nil)
2012 ;; MI error - send to minibuffer
2013 (when (eq type 'error)
2014 ;; Skip "msg=" from `output-field'
2015 (message (read (substring output-field 4)))
2016 ;; Don't send to the console twice. (If it is a console error
2017 ;; it is also in the console stream.)
2018 (setq output-field nil)))
2019 ;; Output from command from frontend.
2020 (setq gdb-output-sink 'emacs))
2021
2022 (gdb-clear-partial-output)
2023 (when gdb-first-done-or-error
2024 (unless (or token-number gud-running)
2025 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
2026 (gdb-update)
2027 (setq gdb-first-done-or-error nil))
2028
2029 (setq gdb-filter-output
2030 (gdb-concat-output gdb-filter-output output-field))
2031
2032 (if token-number
2033 (progn
2034 (with-current-buffer
2035 (gdb-get-buffer-create 'gdb-partial-output-buffer)
2036 (funcall
2037 (cdr (assoc (string-to-number token-number) gdb-handler-alist))))
2038 (setq gdb-handler-alist
2039 (assq-delete-all token-number gdb-handler-alist)))))
2040
2041 (defun gdb-concat-output (so-far new)
2042 (let ((sink gdb-output-sink))
2043 (cond
2044 ((eq sink 'user) (concat so-far new))
2045 ((eq sink 'emacs)
2046 (gdb-append-to-partial-output new)
2047 so-far))))
2048
2049 (defun gdb-append-to-partial-output (string)
2050 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2051 (goto-char (point-max))
2052 (insert string)))
2053
2054 (defun gdb-clear-partial-output ()
2055 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2056 (erase-buffer)))
2057
2058 (defun gdb-jsonify-buffer (&optional fix-key fix-list)
2059 "Prepare GDB/MI output in current buffer for parsing with `json-read'.
2060
2061 Field names are wrapped in double quotes and equal signs are
2062 replaced with semicolons.
2063
2064 If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
2065 partial output. This is used to get rid of useless keys in lists
2066 in MI messages, e.g.: [key=.., key=..]. -stack-list-frames and
2067 -break-info are examples of MI commands which issue such
2068 responses.
2069
2070 If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
2071 \"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
2072 -break-info output when it contains breakpoint script field
2073 incompatible with GDB/MI output syntax."
2074 (save-excursion
2075 (goto-char (point-min))
2076 (when fix-key
2077 (save-excursion
2078 (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
2079 (replace-match "" nil nil nil 1))))
2080 (when fix-list
2081 (save-excursion
2082 ;; Find positions of braces which enclose broken list
2083 (while (re-search-forward (concat fix-list "={\"") nil t)
2084 (let ((p1 (goto-char (- (point) 2)))
2085 (p2 (progn (forward-sexp)
2086 (1- (point)))))
2087 ;; Replace braces with brackets
2088 (save-excursion
2089 (goto-char p1)
2090 (delete-char 1)
2091 (insert "[")
2092 (goto-char p2)
2093 (delete-char 1)
2094 (insert "]"))))))
2095 (goto-char (point-min))
2096 (insert "{")
2097 (while (re-search-forward
2098 "\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\".*?[^\\]\"\\)" nil t)
2099 (replace-match "\"\\1\":\\2" nil nil))
2100 (goto-char (point-max))
2101 (insert "}")))
2102
2103 (defun gdb-json-read-buffer (&optional fix-key fix-list)
2104 "Prepare and parse GDB/MI output in current buffer with `json-read'.
2105
2106 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2107 (gdb-jsonify-buffer fix-key fix-list)
2108 (save-excursion
2109 (goto-char (point-min))
2110 (let ((json-array-type 'list))
2111 (json-read))))
2112
2113 (defun gdb-json-string (string &optional fix-key fix-list)
2114 "Prepare and parse STRING containing GDB/MI output with `json-read'.
2115
2116 FIX-KEY and FIX-LIST work as in `gdb-jsonify-buffer'."
2117 (with-temp-buffer
2118 (insert string)
2119 (gdb-json-read-buffer fix-key fix-list)))
2120
2121 (defun gdb-json-partial-output (&optional fix-key fix-list)
2122 "Prepare and parse gdb-partial-output-buffer with `json-read'.
2123
2124 FIX-KEY and FIX-KEY work as in `gdb-jsonify-buffer'."
2125 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
2126 (gdb-json-read-buffer fix-key fix-list)))
2127
2128 (defun gdb-line-posns (line)
2129 "Return a pair of LINE beginning and end positions."
2130 (let ((offset (1+ (- line (line-number-at-pos)))))
2131 (cons
2132 (line-beginning-position offset)
2133 (line-end-position offset))))
2134
2135 (defmacro gdb-mark-line (line variable)
2136 "Set VARIABLE marker to point at beginning of LINE.
2137
2138 If current window has no fringes, inverse colors on LINE.
2139
2140 Return position where LINE begins."
2141 `(save-excursion
2142 (let* ((posns (gdb-line-posns ,line))
2143 (start-posn (car posns))
2144 (end-posn (cdr posns)))
2145 (set-marker ,variable (copy-marker start-posn))
2146 (when (not (> (car (window-fringes)) 0))
2147 (put-text-property start-posn end-posn
2148 'font-lock-face '(:inverse-video t)))
2149 start-posn)))
2150
2151 (defun gdb-pad-string (string padding)
2152 (format (concat "%" (number-to-string padding) "s") string))
2153
2154 ;; gdb-table struct is a way to programmatically construct simple
2155 ;; tables. It help to reliably align columns of data in GDB buffers
2156 ;; and provides
2157 (defstruct
2158 gdb-table
2159 (column-sizes nil)
2160 (rows nil)
2161 (row-properties nil)
2162 (right-align nil))
2163
2164 (defun gdb-mapcar* (function &rest seqs)
2165 "Apply FUNCTION to each element of SEQS, and make a list of the results.
2166 If there are several SEQS, FUNCTION is called with that many
2167 arugments, and mapping stops as sson as the shortest list runs
2168 out."
2169 (let ((shortest (apply #'min (mapcar #'length seqs))))
2170 (mapcar (lambda (i)
2171 (apply function
2172 (mapcar
2173 (lambda (seq)
2174 (nth i seq))
2175 seqs)))
2176 (number-sequence 0 (1- shortest)))))
2177
2178 (defun gdb-table-add-row (table row &optional properties)
2179 "Add ROW of string to TABLE and recalculate column sizes.
2180
2181 When non-nil, PROPERTIES will be added to the whole row when
2182 calling `gdb-table-string'."
2183 (let ((rows (gdb-table-rows table))
2184 (row-properties (gdb-table-row-properties table))
2185 (column-sizes (gdb-table-column-sizes table))
2186 (right-align (gdb-table-right-align table)))
2187 (when (not column-sizes)
2188 (setf (gdb-table-column-sizes table)
2189 (make-list (length row) 0)))
2190 (setf (gdb-table-rows table)
2191 (append rows (list row)))
2192 (setf (gdb-table-row-properties table)
2193 (append row-properties (list properties)))
2194 (setf (gdb-table-column-sizes table)
2195 (gdb-mapcar* (lambda (x s)
2196 (let ((new-x
2197 (max (abs x) (string-width (or s "")))))
2198 (if right-align new-x (- new-x))))
2199 (gdb-table-column-sizes table)
2200 row))
2201 ;; Avoid trailing whitespace at eol
2202 (if (not (gdb-table-right-align table))
2203 (setcar (last (gdb-table-column-sizes table)) 0))))
2204
2205 (defun gdb-table-string (table &optional sep)
2206 "Return TABLE as a string with columns separated with SEP."
2207 (let ((column-sizes (gdb-table-column-sizes table))
2208 (res ""))
2209 (mapconcat
2210 'identity
2211 (gdb-mapcar*
2212 (lambda (row properties)
2213 (apply 'propertize
2214 (mapconcat 'identity
2215 (gdb-mapcar* (lambda (s x) (gdb-pad-string s x))
2216 row column-sizes)
2217 sep)
2218 properties))
2219 (gdb-table-rows table)
2220 (gdb-table-row-properties table))
2221 "\n")))
2222
2223 ;; bindat-get-field goes deep, gdb-get-many-fields goes wide
2224 (defun gdb-get-many-fields (struct &rest fields)
2225 "Return a list of FIELDS values from STRUCT."
2226 (let ((values))
2227 (dolist (field fields values)
2228 (setq values (append values (list (bindat-get-field struct field)))))))
2229
2230 (defmacro def-gdb-auto-update-trigger (trigger-name gdb-command
2231 handler-name
2232 &optional signal-list)
2233 "Define a trigger TRIGGER-NAME which sends GDB-COMMAND and sets
2234 HANDLER-NAME as its handler. HANDLER-NAME is bound to current
2235 buffer with `gdb-bind-function-to-buffer'.
2236
2237 If SIGNAL-LIST is non-nil, GDB-COMMAND is sent only when the
2238 defined trigger is called with an argument from SIGNAL-LIST. It's
2239 not recommended to define triggers with empty SIGNAL-LIST.
2240 Normally triggers should respond at least to 'update signal.
2241
2242 Normally the trigger defined by this command must be called from
2243 the buffer where HANDLER-NAME must work. This should be done so
2244 that buffer-local thread number may be used in GDB-COMMAND (by
2245 calling `gdb-current-context-command').
2246 `gdb-bind-function-to-buffer' is used to achieve this, see
2247 `gdb-get-buffer-create'.
2248
2249 Triggers defined by this command are meant to be used as a
2250 trigger argument when describing buffer types with
2251 `gdb-set-buffer-rules'."
2252 `(defun ,trigger-name (&optional signal)
2253 (when
2254 (or (not ,signal-list)
2255 (memq signal ,signal-list))
2256 (when (not (gdb-pending-p
2257 (cons (current-buffer) ',trigger-name)))
2258 (gdb-input
2259 (list ,gdb-command
2260 (gdb-bind-function-to-buffer ',handler-name (current-buffer))))
2261 (gdb-add-pending (cons (current-buffer) ',trigger-name))))))
2262
2263 ;; Used by disassembly buffer only, the rest use
2264 ;; def-gdb-trigger-and-handler
2265 (defmacro def-gdb-auto-update-handler (handler-name trigger-name custom-defun
2266 &optional nopreserve)
2267 "Define a handler HANDLER-NAME for TRIGGER-NAME with CUSTOM-DEFUN.
2268
2269 Handlers are normally called from the buffers they put output in.
2270
2271 Delete ((current-buffer) . TRIGGER-NAME) from
2272 `gdb-pending-triggers', erase current buffer and evaluate
2273 CUSTOM-DEFUN. Then `gdb-update-buffer-name' is called.
2274
2275 If NOPRESERVE is non-nil, window point is not restored after CUSTOM-DEFUN."
2276 `(defun ,handler-name ()
2277 (gdb-delete-pending (cons (current-buffer) ',trigger-name))
2278 (let* ((buffer-read-only nil)
2279 (window (get-buffer-window (current-buffer) 0))
2280 (start (window-start window))
2281 (p (window-point window)))
2282 (erase-buffer)
2283 (,custom-defun)
2284 (gdb-update-buffer-name)
2285 ,(when (not nopreserve)
2286 '(set-window-start window start)
2287 '(set-window-point window p)))))
2288
2289 (defmacro def-gdb-trigger-and-handler (trigger-name gdb-command
2290 handler-name custom-defun
2291 &optional signal-list)
2292 "Define trigger and handler.
2293
2294 TRIGGER-NAME trigger is defined to send GDB-COMMAND. See
2295 `def-gdb-auto-update-trigger'.
2296
2297 HANDLER-NAME handler uses customization of CUSTOM-DEFUN. See
2298 `def-gdb-auto-update-handler'."
2299 `(progn
2300 (def-gdb-auto-update-trigger ,trigger-name
2301 ,gdb-command
2302 ,handler-name ,signal-list)
2303 (def-gdb-auto-update-handler ,handler-name
2304 ,trigger-name ,custom-defun)))
2305
2306 \f
2307
2308 ;; Breakpoint buffer : This displays the output of `-break-list'.
2309 (def-gdb-trigger-and-handler
2310 gdb-invalidate-breakpoints "-break-list"
2311 gdb-breakpoints-list-handler gdb-breakpoints-list-handler-custom
2312 '(start update))
2313
2314 (gdb-set-buffer-rules
2315 'gdb-breakpoints-buffer
2316 'gdb-breakpoints-buffer-name
2317 'gdb-breakpoints-mode
2318 'gdb-invalidate-breakpoints)
2319
2320 (defun gdb-breakpoints-list-handler-custom ()
2321 (let ((breakpoints-list (bindat-get-field
2322 (gdb-json-partial-output "bkpt" "script")
2323 'BreakpointTable 'body))
2324 (table (make-gdb-table)))
2325 (setq gdb-breakpoints-list nil)
2326 (gdb-table-add-row table '("Num" "Type" "Disp" "Enb" "Addr" "Hits" "What"))
2327 (dolist (breakpoint breakpoints-list)
2328 (add-to-list 'gdb-breakpoints-list
2329 (cons (bindat-get-field breakpoint 'number)
2330 breakpoint))
2331 (let ((at (bindat-get-field breakpoint 'at))
2332 (pending (bindat-get-field breakpoint 'pending))
2333 (func (bindat-get-field breakpoint 'func))
2334 (type (bindat-get-field breakpoint 'type)))
2335 (gdb-table-add-row table
2336 (list
2337 (bindat-get-field breakpoint 'number)
2338 type
2339 (bindat-get-field breakpoint 'disp)
2340 (let ((flag (bindat-get-field breakpoint 'enabled)))
2341 (if (string-equal flag "y")
2342 (propertize "y" 'font-lock-face font-lock-warning-face)
2343 (propertize "n" 'font-lock-face font-lock-comment-face)))
2344 (bindat-get-field breakpoint 'addr)
2345 (bindat-get-field breakpoint 'times)
2346 (if (string-match ".*watchpoint" type)
2347 (bindat-get-field breakpoint 'what)
2348 (or pending at
2349 (concat "in "
2350 (propertize func 'font-lock-face font-lock-function-name-face)
2351 (gdb-frame-location breakpoint)))))
2352 ;; Add clickable properties only for breakpoints with file:line
2353 ;; information
2354 (append (list 'gdb-breakpoint breakpoint)
2355 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2356 mouse-face highlight))))))
2357 (insert (gdb-table-string table " "))
2358 (gdb-place-breakpoints)))
2359
2360 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2361 (defun gdb-place-breakpoints ()
2362 (let ((flag) (bptno))
2363 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2364 (dolist (buffer (buffer-list))
2365 (with-current-buffer buffer
2366 (if (and (eq gud-minor-mode 'gdbmi)
2367 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2368 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2369 (dolist (breakpoint gdb-breakpoints-list)
2370 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2371 ; an associative list
2372 (line (bindat-get-field breakpoint 'line)))
2373 (when line
2374 (let ((file (bindat-get-field breakpoint 'fullname))
2375 (flag (bindat-get-field breakpoint 'enabled))
2376 (bptno (bindat-get-field breakpoint 'number)))
2377 (unless (file-exists-p file)
2378 (setq file (cdr (assoc bptno gdb-location-alist))))
2379 (if (and file
2380 (not (string-equal file "File not found")))
2381 (with-current-buffer
2382 (find-file-noselect file 'nowarn)
2383 (gdb-init-buffer)
2384 ;; Only want one breakpoint icon at each location.
2385 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2386 (string-to-number line)))
2387 (gdb-input
2388 (list (concat "list " file ":1")
2389 'ignore))
2390 (gdb-input
2391 (list "-file-list-exec-source-file"
2392 `(lambda () (gdb-get-location
2393 ,bptno ,line ,flag)))))))))))
2394
2395 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2396
2397 (defun gdb-get-location (bptno line flag)
2398 "Find the directory containing the relevant source file.
2399 Put in buffer and place breakpoint icon."
2400 (goto-char (point-min))
2401 (catch 'file-not-found
2402 (if (re-search-forward gdb-source-file-regexp nil t)
2403 (delete (cons bptno "File not found") gdb-location-alist)
2404 (push (cons bptno (match-string 1)) gdb-location-alist)
2405 (gdb-resync)
2406 (unless (assoc bptno gdb-location-alist)
2407 (push (cons bptno "File not found") gdb-location-alist)
2408 (message-box "Cannot find source file for breakpoint location.
2409 Add directory to search path for source files using the GDB command, dir."))
2410 (throw 'file-not-found nil))
2411 (with-current-buffer (find-file-noselect (match-string 1))
2412 (gdb-init-buffer)
2413 ;; only want one breakpoint icon at each location
2414 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2415
2416 (add-hook 'find-file-hook 'gdb-find-file-hook)
2417
2418 (defun gdb-find-file-hook ()
2419 "Set up buffer for debugging if file is part of the source code
2420 of the current session."
2421 (if (and (buffer-name gud-comint-buffer)
2422 ;; in case gud or gdb-ui is just loaded
2423 gud-comint-buffer
2424 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2425 'gdbmi))
2426 (if (member buffer-file-name gdb-source-file-list)
2427 (with-current-buffer (find-buffer-visiting buffer-file-name)
2428 (gdb-init-buffer)))))
2429
2430 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2431 (declare-function gud-break "gdb-mi" t t) ; gud-def
2432 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2433
2434 (defun gdb-mouse-set-clear-breakpoint (event)
2435 "Set/clear breakpoint in left fringe/margin at mouse click.
2436 If not in a source or disassembly buffer just set point."
2437 (interactive "e")
2438 (mouse-minibuffer-check event)
2439 (let ((posn (event-end event)))
2440 (with-selected-window (posn-window posn)
2441 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2442 (if (numberp (posn-point posn))
2443 (save-excursion
2444 (goto-char (posn-point posn))
2445 (if (or (posn-object posn)
2446 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2447 'breakpoint))
2448 (gud-remove nil)
2449 (gud-break nil)))))
2450 (posn-set-point posn))))
2451
2452 (defun gdb-mouse-toggle-breakpoint-margin (event)
2453 "Enable/disable breakpoint in left margin with mouse click."
2454 (interactive "e")
2455 (mouse-minibuffer-check event)
2456 (let ((posn (event-end event)))
2457 (if (numberp (posn-point posn))
2458 (with-selected-window (posn-window posn)
2459 (save-excursion
2460 (goto-char (posn-point posn))
2461 (if (posn-object posn)
2462 (gud-basic-call
2463 (let ((bptno (get-text-property
2464 0 'gdb-bptno (car (posn-string posn)))))
2465 (concat
2466 (if (get-text-property
2467 0 'gdb-enabled (car (posn-string posn)))
2468 "-break-disable "
2469 "-break-enable ")
2470 bptno)))))))))
2471
2472 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2473 "Enable/disable breakpoint in left fringe with mouse click."
2474 (interactive "e")
2475 (mouse-minibuffer-check event)
2476 (let* ((posn (event-end event))
2477 (pos (posn-point posn))
2478 obj)
2479 (when (numberp pos)
2480 (with-selected-window (posn-window posn)
2481 (with-current-buffer (window-buffer (selected-window))
2482 (goto-char pos)
2483 (dolist (overlay (overlays-in pos pos))
2484 (when (overlay-get overlay 'put-break)
2485 (setq obj (overlay-get overlay 'before-string))))
2486 (when (stringp obj)
2487 (gud-basic-call
2488 (concat
2489 (if (get-text-property 0 'gdb-enabled obj)
2490 "-break-disable "
2491 "-break-enable ")
2492 (get-text-property 0 'gdb-bptno obj)))))))))
2493
2494 (defun gdb-breakpoints-buffer-name ()
2495 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2496
2497 (def-gdb-display-buffer
2498 gdb-display-breakpoints-buffer
2499 'gdb-breakpoints-buffer
2500 "Display status of user-settable breakpoints.")
2501
2502 (def-gdb-frame-for-buffer
2503 gdb-frame-breakpoints-buffer
2504 'gdb-breakpoints-buffer
2505 "Display status of user-settable breakpoints in a new frame.")
2506
2507 (defvar gdb-breakpoints-mode-map
2508 (let ((map (make-sparse-keymap))
2509 (menu (make-sparse-keymap "Breakpoints")))
2510 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2511 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2512 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2513 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2514 (suppress-keymap map)
2515 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2516 (define-key map " " 'gdb-toggle-breakpoint)
2517 (define-key map "D" 'gdb-delete-breakpoint)
2518 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2519 (define-key map "q" 'gdb-delete-frame-or-window)
2520 (define-key map "\r" 'gdb-goto-breakpoint)
2521 (define-key map "\t" '(lambda ()
2522 (interactive)
2523 (gdb-set-window-buffer
2524 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2525 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2526 (define-key map [follow-link] 'mouse-face)
2527 map))
2528
2529 (defun gdb-delete-frame-or-window ()
2530 "Delete frame if there is only one window. Otherwise delete the window."
2531 (interactive)
2532 (if (one-window-p) (delete-frame)
2533 (delete-window)))
2534
2535 ;;from make-mode-line-mouse-map
2536 (defun gdb-make-header-line-mouse-map (mouse function) "\
2537 Return a keymap with single entry for mouse key MOUSE on the header line.
2538 MOUSE is defined to run function FUNCTION with no args in the buffer
2539 corresponding to the mode line clicked."
2540 (let ((map (make-sparse-keymap)))
2541 (define-key map (vector 'header-line mouse) function)
2542 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2543 map))
2544
2545 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2546 `(propertize ,name
2547 'help-echo ,help-echo
2548 'mouse-face ',mouse-face
2549 'face ',face
2550 'local-map
2551 (gdb-make-header-line-mouse-map
2552 'mouse-1
2553 (lambda (event) (interactive "e")
2554 (save-selected-window
2555 (select-window (posn-window (event-start event)))
2556 (gdb-set-window-buffer
2557 (gdb-get-buffer-create ',buffer) t) )))))
2558
2559 \f
2560 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2561 ;;; Threads view
2562
2563 (defun gdb-threads-buffer-name ()
2564 (concat "*threads of " (gdb-get-target-string) "*"))
2565
2566 (def-gdb-display-buffer
2567 gdb-display-threads-buffer
2568 'gdb-threads-buffer
2569 "Display GDB threads.")
2570
2571 (def-gdb-frame-for-buffer
2572 gdb-frame-threads-buffer
2573 'gdb-threads-buffer
2574 "Display GDB threads in a new frame.")
2575
2576 (def-gdb-trigger-and-handler
2577 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2578 gdb-thread-list-handler gdb-thread-list-handler-custom
2579 '(start update update-threads))
2580
2581 (gdb-set-buffer-rules
2582 'gdb-threads-buffer
2583 'gdb-threads-buffer-name
2584 'gdb-threads-mode
2585 'gdb-invalidate-threads)
2586
2587 (defvar gdb-threads-font-lock-keywords
2588 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2589 (" \\(stopped\\)" (1 font-lock-warning-face))
2590 (" \\(running\\)" (1 font-lock-string-face))
2591 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2592 "Font lock keywords used in `gdb-threads-mode'.")
2593
2594 (defvar gdb-threads-mode-map
2595 (let ((map (make-sparse-keymap)))
2596 (define-key map "\r" 'gdb-select-thread)
2597 (define-key map "f" 'gdb-display-stack-for-thread)
2598 (define-key map "F" 'gdb-frame-stack-for-thread)
2599 (define-key map "l" 'gdb-display-locals-for-thread)
2600 (define-key map "L" 'gdb-frame-locals-for-thread)
2601 (define-key map "r" 'gdb-display-registers-for-thread)
2602 (define-key map "R" 'gdb-frame-registers-for-thread)
2603 (define-key map "d" 'gdb-display-disassembly-for-thread)
2604 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2605 (define-key map "i" 'gdb-interrupt-thread)
2606 (define-key map "c" 'gdb-continue-thread)
2607 (define-key map "s" 'gdb-step-thread)
2608 (define-key map "\t" '(lambda ()
2609 (interactive)
2610 (gdb-set-window-buffer
2611 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2612 (define-key map [mouse-2] 'gdb-select-thread)
2613 (define-key map [follow-link] 'mouse-face)
2614 map))
2615
2616 (defvar gdb-threads-header
2617 (list
2618 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
2619 "mouse-1: select" mode-line-highlight mode-line-inactive)
2620 " "
2621 (gdb-propertize-header "Threads" gdb-threads-buffer
2622 nil nil mode-line)))
2623
2624 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2625 "Major mode for GDB threads."
2626 (setq gdb-thread-position (make-marker))
2627 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2628 (setq header-line-format gdb-threads-header)
2629 (set (make-local-variable 'font-lock-defaults)
2630 '(gdb-threads-font-lock-keywords))
2631 'gdb-invalidate-threads)
2632
2633 (defun gdb-thread-list-handler-custom ()
2634 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2635 (table (make-gdb-table))
2636 (marked-line nil))
2637 (setq gdb-threads-list nil)
2638 (setq gdb-running-threads-count 0)
2639 (setq gdb-stopped-threads-count 0)
2640 (set-marker gdb-thread-position nil)
2641
2642 (dolist (thread (reverse threads-list))
2643 (let ((running (string-equal (bindat-get-field thread 'state) "running")))
2644 (add-to-list 'gdb-threads-list
2645 (cons (bindat-get-field thread 'id)
2646 thread))
2647 (if running
2648 (incf gdb-running-threads-count)
2649 (incf gdb-stopped-threads-count))
2650
2651 (gdb-table-add-row table
2652 (list
2653 (bindat-get-field thread 'id)
2654 (concat
2655 (if gdb-thread-buffer-verbose-names
2656 (concat (bindat-get-field thread 'target-id) " ") "")
2657 (bindat-get-field thread 'state)
2658 ;; Include frame information for stopped threads
2659 (if (not running)
2660 (concat
2661 " in " (bindat-get-field thread 'frame 'func)
2662 (if gdb-thread-buffer-arguments
2663 (concat
2664 " ("
2665 (let ((args (bindat-get-field thread 'frame 'args)))
2666 (mapconcat
2667 (lambda (arg)
2668 (apply 'format `("%s=%s" ,@(gdb-get-many-fields arg 'name 'value))))
2669 args ","))
2670 ")")
2671 "")
2672 (if gdb-thread-buffer-locations
2673 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2674 (if gdb-thread-buffer-addresses
2675 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2676 "")))
2677 (list
2678 'gdb-thread thread
2679 'mouse-face 'highlight
2680 'help-echo "mouse-2, RET: select thread")))
2681 (when (string-equal gdb-thread-number
2682 (bindat-get-field thread 'id))
2683 (setq marked-line (length gdb-threads-list))))
2684 (insert (gdb-table-string table " "))
2685 (when marked-line
2686 (gdb-mark-line marked-line gdb-thread-position)))
2687 ;; We update gud-running here because we need to make sure that
2688 ;; gdb-threads-list is up-to-date
2689 (gdb-update-gud-running)
2690 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2691
2692 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2693 "Define a NAME command which will act upon thread on the current line.
2694
2695 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2696 be the value of 'gdb-thread property of the current line. If
2697 'gdb-thread is nil, error is signaled."
2698 `(defun ,name (&optional event)
2699 ,(when doc doc)
2700 (interactive (list last-input-event))
2701 (if event (posn-set-point (event-end event)))
2702 (save-excursion
2703 (beginning-of-line)
2704 (let ((thread (get-text-property (point) 'gdb-thread)))
2705 (if thread
2706 ,custom-defun
2707 (error "Not recognized as thread line"))))))
2708
2709 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command &optional doc)
2710 "Define a NAME which will call BUFFER-COMMAND with id of thread
2711 on the current line."
2712 `(def-gdb-thread-buffer-command ,name
2713 (,buffer-command (bindat-get-field thread 'id))
2714 ,doc))
2715
2716 (def-gdb-thread-buffer-command gdb-select-thread
2717 (let ((new-id (bindat-get-field thread 'id)))
2718 (gdb-setq-thread-number new-id)
2719 (gdb-input (list (concat "-thread-select " new-id) 'ignore))
2720 (gdb-update))
2721 "Select the thread at current line of threads buffer.")
2722
2723 (def-gdb-thread-buffer-simple-command
2724 gdb-display-stack-for-thread
2725 gdb-preemptively-display-stack-buffer
2726 "Display stack buffer for the thread at current line.")
2727
2728 (def-gdb-thread-buffer-simple-command
2729 gdb-display-locals-for-thread
2730 gdb-preemptively-display-locals-buffer
2731 "Display locals buffer for the thread at current line.")
2732
2733 (def-gdb-thread-buffer-simple-command
2734 gdb-display-registers-for-thread
2735 gdb-preemptively-display-registers-buffer
2736 "Display registers buffer for the thread at current line.")
2737
2738 (def-gdb-thread-buffer-simple-command
2739 gdb-display-disassembly-for-thread
2740 gdb-preemptively-display-disassembly-buffer
2741 "Display disassembly buffer for the thread at current line.")
2742
2743 (def-gdb-thread-buffer-simple-command
2744 gdb-frame-stack-for-thread
2745 gdb-frame-stack-buffer
2746 "Display a new frame with stack buffer for the thread at
2747 current line.")
2748
2749 (def-gdb-thread-buffer-simple-command
2750 gdb-frame-locals-for-thread
2751 gdb-frame-locals-buffer
2752 "Display a new frame with locals buffer for the thread at
2753 current line.")
2754
2755 (def-gdb-thread-buffer-simple-command
2756 gdb-frame-registers-for-thread
2757 gdb-frame-registers-buffer
2758 "Display a new frame with registers buffer for the thread at
2759 current line.")
2760
2761 (def-gdb-thread-buffer-simple-command
2762 gdb-frame-disassembly-for-thread
2763 gdb-frame-disassembly-buffer
2764 "Display a new frame with disassembly buffer for the thread at
2765 current line.")
2766
2767 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2768 "Define a NAME which will execute GUD-COMMAND with
2769 `gdb-thread-number' locally bound to id of thread on the current
2770 line."
2771 `(def-gdb-thread-buffer-command ,name
2772 (if gdb-non-stop
2773 (let ((gdb-thread-number (bindat-get-field thread 'id))
2774 (gdb-gud-control-all-threads nil))
2775 (call-interactively #',gud-command))
2776 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2777 ,doc))
2778
2779 (def-gdb-thread-buffer-gud-command
2780 gdb-interrupt-thread
2781 gud-stop-subjob
2782 "Interrupt thread at current line.")
2783
2784 (def-gdb-thread-buffer-gud-command
2785 gdb-continue-thread
2786 gud-cont
2787 "Continue thread at current line.")
2788
2789 (def-gdb-thread-buffer-gud-command
2790 gdb-step-thread
2791 gud-step
2792 "Step thread at current line.")
2793
2794 \f
2795 ;;; Memory view
2796
2797 (defcustom gdb-memory-rows 8
2798 "Number of data rows in memory window."
2799 :type 'integer
2800 :group 'gud
2801 :version "23.2")
2802
2803 (defcustom gdb-memory-columns 4
2804 "Number of data columns in memory window."
2805 :type 'integer
2806 :group 'gud
2807 :version "23.2")
2808
2809 (defcustom gdb-memory-format "x"
2810 "Display format of data items in memory window."
2811 :type '(choice (const :tag "Hexadecimal" "x")
2812 (const :tag "Signed decimal" "d")
2813 (const :tag "Unsigned decimal" "u")
2814 (const :tag "Octal" "o")
2815 (const :tag "Binary" "t"))
2816 :group 'gud
2817 :version "22.1")
2818
2819 (defcustom gdb-memory-unit 4
2820 "Unit size of data items in memory window."
2821 :type '(choice (const :tag "Byte" 1)
2822 (const :tag "Halfword" 2)
2823 (const :tag "Word" 4)
2824 (const :tag "Giant word" 8))
2825 :group 'gud
2826 :version "23.2")
2827
2828 (def-gdb-trigger-and-handler
2829 gdb-invalidate-memory
2830 (format "-data-read-memory %s %s %d %d %d"
2831 gdb-memory-address
2832 gdb-memory-format
2833 gdb-memory-unit
2834 gdb-memory-rows
2835 gdb-memory-columns)
2836 gdb-read-memory-handler
2837 gdb-read-memory-custom
2838 '(start update))
2839
2840 (gdb-set-buffer-rules
2841 'gdb-memory-buffer
2842 'gdb-memory-buffer-name
2843 'gdb-memory-mode
2844 'gdb-invalidate-memory)
2845
2846 (defun gdb-memory-column-width (size format)
2847 "Return length of string with memory unit of SIZE in FORMAT.
2848
2849 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2850 in `gdb-memory-format'."
2851 (let ((format-base (cdr (assoc format
2852 '(("x" . 16)
2853 ("d" . 10) ("u" . 10)
2854 ("o" . 8)
2855 ("t" . 2))))))
2856 (if format-base
2857 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2858 (cond ((string-equal format "x")
2859 (+ 2 res)) ; hexadecimal numbers have 0x in front
2860 ((or (string-equal format "d")
2861 (string-equal format "o"))
2862 (1+ res))
2863 (t res)))
2864 (error "Unknown format"))))
2865
2866 (defun gdb-read-memory-custom ()
2867 (let* ((res (gdb-json-partial-output))
2868 (err-msg (bindat-get-field res 'msg)))
2869 (if (not err-msg)
2870 (let ((memory (bindat-get-field res 'memory)))
2871 (setq gdb-memory-address (bindat-get-field res 'addr))
2872 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2873 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2874 (setq gdb-memory-last-address gdb-memory-address)
2875 (dolist (row memory)
2876 (insert (concat (bindat-get-field row 'addr) ":"))
2877 (dolist (column (bindat-get-field row 'data))
2878 (insert (gdb-pad-string column
2879 (+ 2 (gdb-memory-column-width
2880 gdb-memory-unit
2881 gdb-memory-format)))))
2882 (newline)))
2883 ;; Show last page instead of empty buffer when out of bounds
2884 (progn
2885 (let ((gdb-memory-address gdb-memory-last-address))
2886 (gdb-invalidate-memory 'update)
2887 (error err-msg))))))
2888
2889 (defvar gdb-memory-mode-map
2890 (let ((map (make-sparse-keymap)))
2891 (suppress-keymap map t)
2892 (define-key map "q" 'kill-this-buffer)
2893 (define-key map "n" 'gdb-memory-show-next-page)
2894 (define-key map "p" 'gdb-memory-show-previous-page)
2895 (define-key map "a" 'gdb-memory-set-address)
2896 (define-key map "t" 'gdb-memory-format-binary)
2897 (define-key map "o" 'gdb-memory-format-octal)
2898 (define-key map "u" 'gdb-memory-format-unsigned)
2899 (define-key map "d" 'gdb-memory-format-signed)
2900 (define-key map "x" 'gdb-memory-format-hexadecimal)
2901 (define-key map "b" 'gdb-memory-unit-byte)
2902 (define-key map "h" 'gdb-memory-unit-halfword)
2903 (define-key map "w" 'gdb-memory-unit-word)
2904 (define-key map "g" 'gdb-memory-unit-giant)
2905 (define-key map "R" 'gdb-memory-set-rows)
2906 (define-key map "C" 'gdb-memory-set-columns)
2907 map))
2908
2909 (defun gdb-memory-set-address-event (event)
2910 "Handle a click on address field in memory buffer header."
2911 (interactive "e")
2912 (save-selected-window
2913 (select-window (posn-window (event-start event)))
2914 (gdb-memory-set-address)))
2915
2916 ;; Non-event version for use within keymap
2917 (defun gdb-memory-set-address ()
2918 "Set the start memory address."
2919 (interactive)
2920 (let ((arg (read-from-minibuffer "Memory address: ")))
2921 (setq gdb-memory-address arg))
2922 (gdb-invalidate-memory 'update))
2923
2924 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
2925 "Define a function NAME which reads new VAR value from minibuffer."
2926 `(defun ,name (event)
2927 ,(when doc doc)
2928 (interactive "e")
2929 (save-selected-window
2930 (select-window (posn-window (event-start event)))
2931 (let* ((arg (read-from-minibuffer ,echo-string))
2932 (count (string-to-number arg)))
2933 (if (<= count 0)
2934 (error "Positive number only")
2935 (customize-set-variable ',variable count)
2936 (gdb-invalidate-memory 'update))))))
2937
2938 (def-gdb-set-positive-number
2939 gdb-memory-set-rows
2940 gdb-memory-rows
2941 "Rows: "
2942 "Set the number of data rows in memory window.")
2943
2944 (def-gdb-set-positive-number
2945 gdb-memory-set-columns
2946 gdb-memory-columns
2947 "Columns: "
2948 "Set the number of data columns in memory window.")
2949
2950 (defmacro def-gdb-memory-format (name format doc)
2951 "Define a function NAME to switch memory buffer to use FORMAT.
2952
2953 DOC is an optional documentation string."
2954 `(defun ,name () ,(when doc doc)
2955 (interactive)
2956 (customize-set-variable 'gdb-memory-format ,format)
2957 (gdb-invalidate-memory 'update)))
2958
2959 (def-gdb-memory-format
2960 gdb-memory-format-binary "t"
2961 "Set the display format to binary.")
2962
2963 (def-gdb-memory-format
2964 gdb-memory-format-octal "o"
2965 "Set the display format to octal.")
2966
2967 (def-gdb-memory-format
2968 gdb-memory-format-unsigned "u"
2969 "Set the display format to unsigned decimal.")
2970
2971 (def-gdb-memory-format
2972 gdb-memory-format-signed "d"
2973 "Set the display format to decimal.")
2974
2975 (def-gdb-memory-format
2976 gdb-memory-format-hexadecimal "x"
2977 "Set the display format to hexadecimal.")
2978
2979 (defvar gdb-memory-format-map
2980 (let ((map (make-sparse-keymap)))
2981 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2982 map)
2983 "Keymap to select format in the header line.")
2984
2985 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2986 "Menu of display formats in the header line.")
2987
2988 (define-key gdb-memory-format-menu [binary]
2989 '(menu-item "Binary" gdb-memory-format-binary
2990 :button (:radio . (equal gdb-memory-format "t"))))
2991 (define-key gdb-memory-format-menu [octal]
2992 '(menu-item "Octal" gdb-memory-format-octal
2993 :button (:radio . (equal gdb-memory-format "o"))))
2994 (define-key gdb-memory-format-menu [unsigned]
2995 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2996 :button (:radio . (equal gdb-memory-format "u"))))
2997 (define-key gdb-memory-format-menu [signed]
2998 '(menu-item "Signed Decimal" gdb-memory-format-signed
2999 :button (:radio . (equal gdb-memory-format "d"))))
3000 (define-key gdb-memory-format-menu [hexadecimal]
3001 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3002 :button (:radio . (equal gdb-memory-format "x"))))
3003
3004 (defun gdb-memory-format-menu (event)
3005 (interactive "@e")
3006 (x-popup-menu event gdb-memory-format-menu))
3007
3008 (defun gdb-memory-format-menu-1 (event)
3009 (interactive "e")
3010 (save-selected-window
3011 (select-window (posn-window (event-start event)))
3012 (let* ((selection (gdb-memory-format-menu event))
3013 (binding (and selection (lookup-key gdb-memory-format-menu
3014 (vector (car selection))))))
3015 (if binding (call-interactively binding)))))
3016
3017 (defmacro def-gdb-memory-unit (name unit-size doc)
3018 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3019
3020 DOC is an optional documentation string."
3021 `(defun ,name () ,(when doc doc)
3022 (interactive)
3023 (customize-set-variable 'gdb-memory-unit ,unit-size)
3024 (gdb-invalidate-memory 'update)))
3025
3026 (def-gdb-memory-unit gdb-memory-unit-giant 8
3027 "Set the unit size to giant words (eight bytes).")
3028
3029 (def-gdb-memory-unit gdb-memory-unit-word 4
3030 "Set the unit size to words (four bytes).")
3031
3032 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3033 "Set the unit size to halfwords (two bytes).")
3034
3035 (def-gdb-memory-unit gdb-memory-unit-byte 1
3036 "Set the unit size to bytes.")
3037
3038 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3039 "Define a function NAME which show new address in memory buffer.
3040
3041 The defined function switches Memory buffer to show address
3042 stored in ADDRESS-VAR variable.
3043
3044 DOC is an optional documentation string."
3045 `(defun ,name
3046 ,(when doc doc)
3047 (interactive)
3048 (let ((gdb-memory-address ,address-var))
3049 (gdb-invalidate-memory))))
3050
3051 (def-gdb-memory-show-page gdb-memory-show-previous-page
3052 gdb-memory-prev-page)
3053
3054 (def-gdb-memory-show-page gdb-memory-show-next-page
3055 gdb-memory-next-page)
3056
3057 (defvar gdb-memory-unit-map
3058 (let ((map (make-sparse-keymap)))
3059 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3060 map)
3061 "Keymap to select units in the header line.")
3062
3063 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
3064 "Menu of units in the header line.")
3065
3066 (define-key gdb-memory-unit-menu [giantwords]
3067 '(menu-item "Giant words" gdb-memory-unit-giant
3068 :button (:radio . (equal gdb-memory-unit 8))))
3069 (define-key gdb-memory-unit-menu [words]
3070 '(menu-item "Words" gdb-memory-unit-word
3071 :button (:radio . (equal gdb-memory-unit 4))))
3072 (define-key gdb-memory-unit-menu [halfwords]
3073 '(menu-item "Halfwords" gdb-memory-unit-halfword
3074 :button (:radio . (equal gdb-memory-unit 2))))
3075 (define-key gdb-memory-unit-menu [bytes]
3076 '(menu-item "Bytes" gdb-memory-unit-byte
3077 :button (:radio . (equal gdb-memory-unit 1))))
3078
3079 (defun gdb-memory-unit-menu (event)
3080 (interactive "@e")
3081 (x-popup-menu event gdb-memory-unit-menu))
3082
3083 (defun gdb-memory-unit-menu-1 (event)
3084 (interactive "e")
3085 (save-selected-window
3086 (select-window (posn-window (event-start event)))
3087 (let* ((selection (gdb-memory-unit-menu event))
3088 (binding (and selection (lookup-key gdb-memory-unit-menu
3089 (vector (car selection))))))
3090 (if binding (call-interactively binding)))))
3091
3092 (defvar gdb-memory-font-lock-keywords
3093 '(;; <__function.name+n>
3094 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
3095 )
3096 "Font lock keywords used in `gdb-memory-mode'.")
3097
3098 (defvar gdb-memory-header
3099 '(:eval
3100 (concat
3101 "Start address["
3102 (propertize "-"
3103 'face font-lock-warning-face
3104 'help-echo "mouse-1: decrement address"
3105 'mouse-face 'mode-line-highlight
3106 'local-map (gdb-make-header-line-mouse-map
3107 'mouse-1
3108 #'gdb-memory-show-previous-page))
3109 "|"
3110 (propertize "+"
3111 'face font-lock-warning-face
3112 'help-echo "mouse-1: increment address"
3113 'mouse-face 'mode-line-highlight
3114 'local-map (gdb-make-header-line-mouse-map
3115 'mouse-1
3116 #'gdb-memory-show-next-page))
3117 "]: "
3118 (propertize gdb-memory-address
3119 'face font-lock-warning-face
3120 'help-echo "mouse-1: set start address"
3121 'mouse-face 'mode-line-highlight
3122 'local-map (gdb-make-header-line-mouse-map
3123 'mouse-1
3124 #'gdb-memory-set-address-event))
3125 " Rows: "
3126 (propertize (number-to-string gdb-memory-rows)
3127 'face font-lock-warning-face
3128 'help-echo "mouse-1: set number of columns"
3129 'mouse-face 'mode-line-highlight
3130 'local-map (gdb-make-header-line-mouse-map
3131 'mouse-1
3132 #'gdb-memory-set-rows))
3133 " Columns: "
3134 (propertize (number-to-string gdb-memory-columns)
3135 'face font-lock-warning-face
3136 'help-echo "mouse-1: set number of columns"
3137 'mouse-face 'mode-line-highlight
3138 'local-map (gdb-make-header-line-mouse-map
3139 'mouse-1
3140 #'gdb-memory-set-columns))
3141 " Display Format: "
3142 (propertize gdb-memory-format
3143 'face font-lock-warning-face
3144 'help-echo "mouse-3: select display format"
3145 'mouse-face 'mode-line-highlight
3146 'local-map gdb-memory-format-map)
3147 " Unit Size: "
3148 (propertize (number-to-string gdb-memory-unit)
3149 'face font-lock-warning-face
3150 'help-echo "mouse-3: select unit size"
3151 'mouse-face 'mode-line-highlight
3152 'local-map gdb-memory-unit-map)))
3153 "Header line used in `gdb-memory-mode'.")
3154
3155 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3156 "Major mode for examining memory."
3157 (setq header-line-format gdb-memory-header)
3158 (set (make-local-variable 'font-lock-defaults)
3159 '(gdb-memory-font-lock-keywords))
3160 'gdb-invalidate-memory)
3161
3162 (defun gdb-memory-buffer-name ()
3163 (concat "*memory of " (gdb-get-target-string) "*"))
3164
3165 (def-gdb-display-buffer
3166 gdb-display-memory-buffer
3167 'gdb-memory-buffer
3168 "Display memory contents.")
3169
3170 (defun gdb-frame-memory-buffer ()
3171 "Display memory contents in a new frame."
3172 (interactive)
3173 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3174 (special-display-frame-alist
3175 `((left-fringe . 0)
3176 (right-fringe . 0)
3177 (width . 83)
3178 ,@gdb-frame-parameters)))
3179 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3180
3181 \f
3182 ;;; Disassembly view
3183
3184 (defun gdb-disassembly-buffer-name ()
3185 (gdb-current-context-buffer-name
3186 (concat "disassembly of " (gdb-get-target-string))))
3187
3188 (def-gdb-display-buffer
3189 gdb-display-disassembly-buffer
3190 'gdb-disassembly-buffer
3191 "Display disassembly for current stack frame.")
3192
3193 (def-gdb-preempt-display-buffer
3194 gdb-preemptively-display-disassembly-buffer
3195 'gdb-disassembly-buffer)
3196
3197 (def-gdb-frame-for-buffer
3198 gdb-frame-disassembly-buffer
3199 'gdb-disassembly-buffer
3200 "Display disassembly in a new frame.")
3201
3202 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3203 (let* ((frame (gdb-current-buffer-frame))
3204 (file (bindat-get-field frame 'fullname))
3205 (line (bindat-get-field frame 'line)))
3206 (when file
3207 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)))
3208 gdb-disassembly-handler
3209 ;; We update disassembly only after we have actual frame information
3210 ;; about all threads, so no there's `update' signal in this list
3211 '(start update-disassembly))
3212
3213 (def-gdb-auto-update-handler
3214 gdb-disassembly-handler
3215 gdb-invalidate-disassembly
3216 gdb-disassembly-handler-custom
3217 t)
3218
3219 (gdb-set-buffer-rules
3220 'gdb-disassembly-buffer
3221 'gdb-disassembly-buffer-name
3222 'gdb-disassembly-mode
3223 'gdb-invalidate-disassembly)
3224
3225 (defvar gdb-disassembly-font-lock-keywords
3226 '(;; <__function.name+n>
3227 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3228 (1 font-lock-function-name-face))
3229 ;; 0xNNNNNNNN <__function.name+n>: opcode
3230 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3231 (4 font-lock-keyword-face))
3232 ;; %register(at least i386)
3233 ("%\\sw+" . font-lock-variable-name-face)
3234 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3235 (1 font-lock-comment-face)
3236 (2 font-lock-function-name-face))
3237 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3238 "Font lock keywords used in `gdb-disassembly-mode'.")
3239
3240 (defvar gdb-disassembly-mode-map
3241 ;; TODO
3242 (let ((map (make-sparse-keymap)))
3243 (suppress-keymap map)
3244 (define-key map "q" 'kill-this-buffer)
3245 map))
3246
3247 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3248 "Major mode for GDB disassembly information."
3249 ;; TODO Rename overlay variable for disassembly mode
3250 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3251 (setq fringes-outside-margins t)
3252 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3253 (set (make-local-variable 'font-lock-defaults)
3254 '(gdb-disassembly-font-lock-keywords))
3255 'gdb-invalidate-disassembly)
3256
3257 (defun gdb-disassembly-handler-custom ()
3258 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3259 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3260 (pos 1)
3261 (table (make-gdb-table))
3262 (marked-line nil))
3263 (dolist (instr instructions)
3264 (gdb-table-add-row table
3265 (list
3266 (bindat-get-field instr 'address)
3267 (apply 'format `("<%s+%s>:" ,@(gdb-get-many-fields instr 'func-name 'offset)))
3268 (bindat-get-field instr 'inst)))
3269 (when (string-equal (bindat-get-field instr 'address)
3270 address)
3271 (progn
3272 (setq marked-line (length (gdb-table-rows table)))
3273 (setq fringe-indicator-alist
3274 (if (string-equal gdb-frame-number "0")
3275 nil
3276 '((overlay-arrow . hollow-right-triangle)))))))
3277 (insert (gdb-table-string table " "))
3278 (gdb-disassembly-place-breakpoints)
3279 ;; Mark current position with overlay arrow and scroll window to
3280 ;; that point
3281 (when marked-line
3282 (let ((window (get-buffer-window (current-buffer) 0)))
3283 (set-window-point window (gdb-mark-line marked-line gdb-disassembly-position))))
3284 (setq mode-name
3285 (gdb-current-context-mode-name
3286 (concat "Disassembly: "
3287 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3288
3289 (defun gdb-disassembly-place-breakpoints ()
3290 (gdb-remove-breakpoint-icons (point-min) (point-max))
3291 (dolist (breakpoint gdb-breakpoints-list)
3292 (let* ((breakpoint (cdr breakpoint))
3293 (bptno (bindat-get-field breakpoint 'number))
3294 (flag (bindat-get-field breakpoint 'enabled))
3295 (address (bindat-get-field breakpoint 'addr)))
3296 (save-excursion
3297 (goto-char (point-min))
3298 (if (re-search-forward (concat "^" address) nil t)
3299 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3300
3301 \f
3302 (defvar gdb-breakpoints-header
3303 (list
3304 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3305 nil nil mode-line)
3306 " "
3307 (gdb-propertize-header "Threads" gdb-threads-buffer
3308 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3309
3310 ;;; Breakpoints view
3311 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3312 "Major mode for gdb breakpoints."
3313 (setq header-line-format gdb-breakpoints-header)
3314 'gdb-invalidate-breakpoints)
3315
3316 (defun gdb-toggle-breakpoint ()
3317 "Enable/disable breakpoint at current line of breakpoints buffer."
3318 (interactive)
3319 (save-excursion
3320 (beginning-of-line)
3321 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3322 (if breakpoint
3323 (gud-basic-call
3324 (concat (if (string-equal "y" (bindat-get-field breakpoint 'enabled))
3325 "-break-disable "
3326 "-break-enable ")
3327 (bindat-get-field breakpoint 'number)))
3328 (error "Not recognized as break/watchpoint line")))))
3329
3330 (defun gdb-delete-breakpoint ()
3331 "Delete the breakpoint at current line of breakpoints buffer."
3332 (interactive)
3333 (save-excursion
3334 (beginning-of-line)
3335 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3336 (if breakpoint
3337 (gud-basic-call (concat "-break-delete " (bindat-get-field breakpoint 'number)))
3338 (error "Not recognized as break/watchpoint line")))))
3339
3340 (defun gdb-goto-breakpoint (&optional event)
3341 "Go to the location of breakpoint at current line of
3342 breakpoints buffer."
3343 (interactive (list last-input-event))
3344 (if event (posn-set-point (event-end event)))
3345 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3346 (let ((window (get-buffer-window gud-comint-buffer)))
3347 (if window (save-selected-window (select-window window))))
3348 (save-excursion
3349 (beginning-of-line)
3350 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3351 (if breakpoint
3352 (let ((bptno (bindat-get-field breakpoint 'number))
3353 (file (bindat-get-field breakpoint 'fullname))
3354 (line (bindat-get-field breakpoint 'line)))
3355 (save-selected-window
3356 (let* ((buffer (find-file-noselect
3357 (if (file-exists-p file) file
3358 (cdr (assoc bptno gdb-location-alist)))))
3359 (window (or (gdb-display-source-buffer buffer)
3360 (display-buffer buffer))))
3361 (setq gdb-source-window window)
3362 (with-current-buffer buffer
3363 (goto-char (point-min))
3364 (forward-line (1- (string-to-number line)))
3365 (set-window-point window (point))))))
3366 (error "Not recognized as break/watchpoint line")))))
3367
3368 \f
3369 ;; Frames buffer. This displays a perpetually correct bactrack trace.
3370 ;;
3371 (def-gdb-trigger-and-handler
3372 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3373 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3374 '(start update))
3375
3376 (gdb-set-buffer-rules
3377 'gdb-stack-buffer
3378 'gdb-stack-buffer-name
3379 'gdb-frames-mode
3380 'gdb-invalidate-frames)
3381
3382 (defun gdb-frame-location (frame)
3383 "Return \" of file:line\" or \" of library\" for structure FRAME.
3384
3385 FRAME must have either \"file\" and \"line\" members or \"from\"
3386 member."
3387 (let ((file (bindat-get-field frame 'file))
3388 (line (bindat-get-field frame 'line))
3389 (from (bindat-get-field frame 'from)))
3390 (let ((res (or (and file line (concat file ":" line))
3391 from)))
3392 (if res (concat " of " res) ""))))
3393
3394 (defun gdb-stack-list-frames-custom ()
3395 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3396 (table (make-gdb-table)))
3397 (set-marker gdb-stack-position nil)
3398 (dolist (frame stack)
3399 (gdb-table-add-row table
3400 (list
3401 (bindat-get-field frame 'level)
3402 "in"
3403 (concat
3404 (bindat-get-field frame 'func)
3405 (if gdb-stack-buffer-locations
3406 (gdb-frame-location frame) "")
3407 (if gdb-stack-buffer-addresses
3408 (concat " at " (bindat-get-field frame 'addr)) "")))
3409 `(mouse-face highlight
3410 help-echo "mouse-2, RET: Select frame"
3411 gdb-frame ,frame)))
3412 (insert (gdb-table-string table " ")))
3413 (when (and gdb-frame-number
3414 (gdb-buffer-shows-main-thread-p))
3415 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3416 gdb-stack-position))
3417 (setq mode-name
3418 (gdb-current-context-mode-name "Frames")))
3419
3420 (defun gdb-stack-buffer-name ()
3421 (gdb-current-context-buffer-name
3422 (concat "stack frames of " (gdb-get-target-string))))
3423
3424 (def-gdb-display-buffer
3425 gdb-display-stack-buffer
3426 'gdb-stack-buffer
3427 "Display backtrace of current stack.")
3428
3429 (def-gdb-preempt-display-buffer
3430 gdb-preemptively-display-stack-buffer
3431 'gdb-stack-buffer nil t)
3432
3433 (def-gdb-frame-for-buffer
3434 gdb-frame-stack-buffer
3435 'gdb-stack-buffer
3436 "Display backtrace of current stack in a new frame.")
3437
3438 (defvar gdb-frames-mode-map
3439 (let ((map (make-sparse-keymap)))
3440 (suppress-keymap map)
3441 (define-key map "q" 'kill-this-buffer)
3442 (define-key map "\r" 'gdb-select-frame)
3443 (define-key map [mouse-2] 'gdb-select-frame)
3444 (define-key map [follow-link] 'mouse-face)
3445 map))
3446
3447 (defvar gdb-frames-font-lock-keywords
3448 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3449 "Font lock keywords used in `gdb-frames-mode'.")
3450
3451 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3452 "Major mode for gdb call stack."
3453 (setq gdb-stack-position (make-marker))
3454 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3455 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3456 (set (make-local-variable 'font-lock-defaults)
3457 '(gdb-frames-font-lock-keywords))
3458 'gdb-invalidate-frames)
3459
3460 (defun gdb-select-frame (&optional event)
3461 "Select the frame and display the relevant source."
3462 (interactive (list last-input-event))
3463 (if event (posn-set-point (event-end event)))
3464 (let ((frame (get-text-property (point) 'gdb-frame)))
3465 (if frame
3466 (if (gdb-buffer-shows-main-thread-p)
3467 (let ((new-level (bindat-get-field frame 'level)))
3468 (setq gdb-frame-number new-level)
3469 (gdb-input (list (concat "-stack-select-frame " new-level) 'ignore))
3470 (gdb-update))
3471 (error "Could not select frame for non-current thread"))
3472 (error "Not recognized as frame line"))))
3473
3474 \f
3475 ;; Locals buffer.
3476 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3477 (def-gdb-trigger-and-handler
3478 gdb-invalidate-locals
3479 (concat (gdb-current-context-command "-stack-list-locals") " --simple-values")
3480 gdb-locals-handler gdb-locals-handler-custom
3481 '(start update))
3482
3483 (gdb-set-buffer-rules
3484 'gdb-locals-buffer
3485 'gdb-locals-buffer-name
3486 'gdb-locals-mode
3487 'gdb-invalidate-locals)
3488
3489 (defvar gdb-locals-watch-map
3490 (let ((map (make-sparse-keymap)))
3491 (suppress-keymap map)
3492 (define-key map "\r" 'gud-watch)
3493 (define-key map [mouse-2] 'gud-watch)
3494 map)
3495 "Keymap to create watch expression of a complex data type local variable.")
3496
3497 (defvar gdb-edit-locals-map-1
3498 (let ((map (make-sparse-keymap)))
3499 (suppress-keymap map)
3500 (define-key map "\r" 'gdb-edit-locals-value)
3501 (define-key map [mouse-2] 'gdb-edit-locals-value)
3502 map)
3503 "Keymap to edit value of a simple data type local variable.")
3504
3505 (defun gdb-edit-locals-value (&optional event)
3506 "Assign a value to a variable displayed in the locals buffer."
3507 (interactive (list last-input-event))
3508 (save-excursion
3509 (if event (posn-set-point (event-end event)))
3510 (beginning-of-line)
3511 (let* ((var (bindat-get-field
3512 (get-text-property (point) 'gdb-local-variable) 'name))
3513 (value (read-string (format "New value (%s): " var))))
3514 (gud-basic-call
3515 (concat "-gdb-set variable " var " = " value)))))
3516
3517 ;; Dont display values of arrays or structures.
3518 ;; These can be expanded using gud-watch.
3519 (defun gdb-locals-handler-custom ()
3520 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3521 (table (make-gdb-table)))
3522 (dolist (local locals-list)
3523 (let ((name (bindat-get-field local 'name))
3524 (value (bindat-get-field local 'value))
3525 (type (bindat-get-field local 'type)))
3526 (if (or (not value)
3527 (string-match "\\0x" value))
3528 (add-text-properties 0 (length name)
3529 `(mouse-face highlight
3530 help-echo "mouse-2: create watch expression"
3531 local-map ,gdb-locals-watch-map)
3532 name)
3533 (add-text-properties 0 (length value)
3534 `(mouse-face highlight
3535 help-echo "mouse-2: edit value"
3536 local-map ,gdb-edit-locals-map-1)
3537 value))
3538 (gdb-table-add-row
3539 table
3540 (list
3541 (propertize type 'font-lock-face font-lock-type-face)
3542 (propertize name 'font-lock-face font-lock-variable-name-face)
3543 value)
3544 `(gdb-local-variable ,local))))
3545 (insert (gdb-table-string table " "))
3546 (setq mode-name
3547 (gdb-current-context-mode-name
3548 (concat "Locals: " (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3549
3550 (defvar gdb-locals-header
3551 (list
3552 (gdb-propertize-header "Locals" gdb-locals-buffer
3553 nil nil mode-line)
3554 " "
3555 (gdb-propertize-header "Registers" gdb-registers-buffer
3556 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3557
3558 (defvar gdb-locals-mode-map
3559 (let ((map (make-sparse-keymap)))
3560 (suppress-keymap map)
3561 (define-key map "q" 'kill-this-buffer)
3562 (define-key map "\t" '(lambda ()
3563 (interactive)
3564 (gdb-set-window-buffer
3565 (gdb-get-buffer-create
3566 'gdb-registers-buffer
3567 gdb-thread-number) t)))
3568 map))
3569
3570 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3571 "Major mode for gdb locals."
3572 (setq header-line-format gdb-locals-header)
3573 'gdb-invalidate-locals)
3574
3575 (defun gdb-locals-buffer-name ()
3576 (gdb-current-context-buffer-name
3577 (concat "locals of " (gdb-get-target-string))))
3578
3579 (def-gdb-display-buffer
3580 gdb-display-locals-buffer
3581 'gdb-locals-buffer
3582 "Display local variables of current stack and their values.")
3583
3584 (def-gdb-preempt-display-buffer
3585 gdb-preemptively-display-locals-buffer
3586 'gdb-locals-buffer nil t)
3587
3588 (def-gdb-frame-for-buffer
3589 gdb-frame-locals-buffer
3590 'gdb-locals-buffer
3591 "Display local variables of current stack and their values in a new frame.")
3592
3593 \f
3594 ;; Registers buffer.
3595
3596 (def-gdb-trigger-and-handler
3597 gdb-invalidate-registers
3598 (concat (gdb-current-context-command "-data-list-register-values") " x")
3599 gdb-registers-handler
3600 gdb-registers-handler-custom
3601 '(start update))
3602
3603 (gdb-set-buffer-rules
3604 'gdb-registers-buffer
3605 'gdb-registers-buffer-name
3606 'gdb-registers-mode
3607 'gdb-invalidate-registers)
3608
3609 (defun gdb-registers-handler-custom ()
3610 (when gdb-register-names
3611 (let ((register-values (bindat-get-field (gdb-json-partial-output) 'register-values))
3612 (table (make-gdb-table)))
3613 (dolist (register register-values)
3614 (let* ((register-number (bindat-get-field register 'number))
3615 (value (bindat-get-field register 'value))
3616 (register-name (nth (string-to-number register-number)
3617 gdb-register-names)))
3618 (gdb-table-add-row
3619 table
3620 (list
3621 (propertize register-name 'font-lock-face font-lock-variable-name-face)
3622 (if (member register-number gdb-changed-registers)
3623 (propertize value 'font-lock-face font-lock-warning-face)
3624 value))
3625 `(mouse-face highlight
3626 help-echo "mouse-2: edit value"
3627 gdb-register-name ,register-name))))
3628 (insert (gdb-table-string table " ")))
3629 (setq mode-name
3630 (gdb-current-context-mode-name "Registers"))))
3631
3632 (defun gdb-edit-register-value (&optional event)
3633 "Assign a value to a register displayed in the registers buffer."
3634 (interactive (list last-input-event))
3635 (save-excursion
3636 (if event (posn-set-point (event-end event)))
3637 (beginning-of-line)
3638 (let* ((var (bindat-get-field
3639 (get-text-property (point) 'gdb-register-name)))
3640 (value (read-string (format "New value (%s): " var))))
3641 (gud-basic-call
3642 (concat "-gdb-set variable $" var " = " value)))))
3643
3644 (defvar gdb-registers-mode-map
3645 (let ((map (make-sparse-keymap)))
3646 (suppress-keymap map)
3647 (define-key map "\r" 'gdb-edit-register-value)
3648 (define-key map [mouse-2] 'gdb-edit-register-value)
3649 (define-key map "q" 'kill-this-buffer)
3650 (define-key map "\t" '(lambda ()
3651 (interactive)
3652 (gdb-set-window-buffer
3653 (gdb-get-buffer-create
3654 'gdb-locals-buffer
3655 gdb-thread-number) t)))
3656 map))
3657
3658 (defvar gdb-registers-header
3659 (list
3660 (gdb-propertize-header "Locals" gdb-locals-buffer
3661 "mouse-1: select" mode-line-highlight mode-line-inactive)
3662 " "
3663 (gdb-propertize-header "Registers" gdb-registers-buffer
3664 nil nil mode-line)))
3665
3666 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3667 "Major mode for gdb registers."
3668 (setq header-line-format gdb-registers-header)
3669 'gdb-invalidate-registers)
3670
3671 (defun gdb-registers-buffer-name ()
3672 (gdb-current-context-buffer-name
3673 (concat "registers of " (gdb-get-target-string))))
3674
3675 (def-gdb-display-buffer
3676 gdb-display-registers-buffer
3677 'gdb-registers-buffer
3678 "Display integer register contents.")
3679
3680 (def-gdb-preempt-display-buffer
3681 gdb-preemptively-display-registers-buffer
3682 'gdb-registers-buffer nil t)
3683
3684 (def-gdb-frame-for-buffer
3685 gdb-frame-registers-buffer
3686 'gdb-registers-buffer
3687 "Display integer register contents in a new frame.")
3688
3689 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3690 (defun gdb-get-changed-registers ()
3691 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3692 (not (gdb-pending-p 'gdb-get-changed-registers)))
3693 (progn
3694 (gdb-input
3695 (list
3696 "-data-list-changed-registers"
3697 'gdb-changed-registers-handler))
3698 (gdb-add-pending 'gdb-get-changed-registers))))
3699
3700 (defun gdb-changed-registers-handler ()
3701 (gdb-delete-pending 'gdb-get-changed-registers)
3702 (setq gdb-changed-registers nil)
3703 (dolist (register-number (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3704 (push register-number gdb-changed-registers)))
3705
3706 (defun gdb-register-names-handler ()
3707 ;; Don't use gdb-pending-triggers because this handler is called
3708 ;; only once (in gdb-init-1)
3709 (setq gdb-register-names nil)
3710 (dolist (register-name (bindat-get-field (gdb-json-partial-output) 'register-names))
3711 (push register-name gdb-register-names))
3712 (setq gdb-register-names (reverse gdb-register-names)))
3713 \f
3714
3715 (defun gdb-get-source-file-list ()
3716 "Create list of source files for current GDB session.
3717 If buffers already exist for any of these files, gud-minor-mode
3718 is set in them."
3719 (goto-char (point-min))
3720 (while (re-search-forward gdb-source-file-regexp nil t)
3721 (push (match-string 1) gdb-source-file-list))
3722 (dolist (buffer (buffer-list))
3723 (with-current-buffer buffer
3724 (when (member buffer-file-name gdb-source-file-list)
3725 (gdb-init-buffer))))
3726 (gdb-force-mode-line-update
3727 (propertize "ready" 'face font-lock-variable-name-face)))
3728
3729 (defun gdb-get-main-selected-frame ()
3730 "Trigger for `gdb-frame-handler' which uses main current
3731 thread. Called from `gdb-update'."
3732 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3733 (progn
3734 (gdb-input
3735 (list (gdb-current-context-command "-stack-info-frame") 'gdb-frame-handler))
3736 (gdb-add-pending 'gdb-get-main-selected-frame))))
3737
3738 (defun gdb-frame-handler ()
3739 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3740 overlay arrow in source buffer."
3741 (gdb-delete-pending 'gdb-get-main-selected-frame)
3742 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3743 (when frame
3744 (setq gdb-selected-frame (bindat-get-field frame 'func))
3745 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3746 (setq gdb-frame-number (bindat-get-field frame 'level))
3747 (setq gdb-frame-address (bindat-get-field frame 'addr))
3748 (let ((line (bindat-get-field frame 'line)))
3749 (setq gdb-selected-line (and line (string-to-number line)))
3750 (when (and gdb-selected-file gdb-selected-line)
3751 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3752 (gud-display-frame)))
3753 (if gud-overlay-arrow-position
3754 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3755 (position (marker-position gud-overlay-arrow-position)))
3756 (when buffer
3757 (with-current-buffer buffer
3758 (setq fringe-indicator-alist
3759 (if (string-equal gdb-frame-number "0")
3760 nil
3761 '((overlay-arrow . hollow-right-triangle))))
3762 (setq gud-overlay-arrow-position (make-marker))
3763 (set-marker gud-overlay-arrow-position position))))))))
3764
3765 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3766
3767 (defun gdb-get-prompt ()
3768 "Find prompt for GDB session."
3769 (goto-char (point-min))
3770 (setq gdb-prompt-name nil)
3771 (re-search-forward gdb-prompt-name-regexp nil t)
3772 (setq gdb-prompt-name (match-string 1))
3773 ;; Insert first prompt.
3774 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3775
3776 ;;;; Window management
3777 (defun gdb-display-buffer (buf dedicated &optional frame)
3778 "Show buffer BUF.
3779
3780 If BUF is already displayed in some window, show it, deiconifying
3781 the frame if necessary. Otherwise, find least recently used
3782 window and show BUF there, if the window is not used for GDB
3783 already, in which case that window is splitted first."
3784 (let ((answer (get-buffer-window buf (or frame 0))))
3785 (if answer
3786 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
3787 (let ((window (get-lru-window)))
3788 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3789 'gdbmi)
3790 (let* ((largest (get-largest-window))
3791 (cur-size (window-height largest)))
3792 (setq answer (split-window largest))
3793 (set-window-buffer answer buf)
3794 (set-window-dedicated-p answer dedicated)
3795 answer)
3796 (set-window-buffer window buf)
3797 window)))))
3798
3799 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3800 "Find window displaying a buffer with the same
3801 `gdb-buffer-type' as BUF and show BUF there. If no such window
3802 exists, just call `gdb-display-buffer' for BUF. If the window
3803 found is already dedicated, split window according to
3804 SPLIT-HORIZONTAL and show BUF in the new window."
3805 (if buf
3806 (when (not (get-buffer-window buf))
3807 (let* ((buf-type (gdb-buffer-type buf))
3808 (existing-window
3809 (get-window-with-predicate
3810 #'(lambda (w)
3811 (and (eq buf-type
3812 (gdb-buffer-type (window-buffer w)))
3813 (not (window-dedicated-p w)))))))
3814 (if existing-window
3815 (set-window-buffer existing-window buf)
3816 (let ((dedicated-window
3817 (get-window-with-predicate
3818 #'(lambda (w)
3819 (eq buf-type
3820 (gdb-buffer-type (window-buffer w)))))))
3821 (if dedicated-window
3822 (set-window-buffer
3823 (split-window dedicated-window nil split-horizontal) buf)
3824 (gdb-display-buffer buf t))))))
3825 (error "Null buffer")))
3826 \f
3827 ;;; Shared keymap initialization:
3828
3829 (let ((menu (make-sparse-keymap "GDB-Windows")))
3830 (define-key gud-menu-map [displays]
3831 `(menu-item "GDB-Windows" ,menu
3832 :visible (eq gud-minor-mode 'gdbmi)))
3833 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3834 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3835 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3836 (define-key menu [disassembly]
3837 '("Disassembly" . gdb-display-disassembly-buffer))
3838 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3839 (define-key menu [inferior]
3840 '("IO" . gdb-display-io-buffer))
3841 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3842 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3843 (define-key menu [breakpoints]
3844 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3845
3846 (let ((menu (make-sparse-keymap "GDB-Frames")))
3847 (define-key gud-menu-map [frames]
3848 `(menu-item "GDB-Frames" ,menu
3849 :visible (eq gud-minor-mode 'gdbmi)))
3850 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3851 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3852 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3853 (define-key menu [disassembly] '("Disassembly" . gdb-frame-disassembly-buffer))
3854 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3855 (define-key menu [inferior]
3856 '("IO" . gdb-frame-io-buffer))
3857 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3858 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3859 (define-key menu [breakpoints]
3860 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3861
3862 (let ((menu (make-sparse-keymap "GDB-MI")))
3863 (define-key menu [gdb-customize]
3864 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3865 :help "Customize Gdb Graphical Mode options."))
3866 (define-key menu [gdb-many-windows]
3867 '(menu-item "Display Other Windows" gdb-many-windows
3868 :help "Toggle display of locals, stack and breakpoint information"
3869 :button (:toggle . gdb-many-windows)))
3870 (define-key menu [gdb-restore-windows]
3871 '(menu-item "Restore Window Layout" gdb-restore-windows
3872 :help "Restore standard layout for debug session."))
3873 (define-key menu [sep1]
3874 '(menu-item "--"))
3875 (define-key menu [all-threads]
3876 '(menu-item "GUD controls all threads"
3877 (lambda ()
3878 (interactive)
3879 (setq gdb-gud-control-all-threads t))
3880 :help "GUD start/stop commands apply to all threads"
3881 :button (:radio . gdb-gud-control-all-threads)))
3882 (define-key menu [current-thread]
3883 '(menu-item "GUD controls current thread"
3884 (lambda ()
3885 (interactive)
3886 (setq gdb-gud-control-all-threads nil))
3887 :help "GUD start/stop commands apply to current thread only"
3888 :button (:radio . (not gdb-gud-control-all-threads))))
3889 (define-key menu [sep2]
3890 '(menu-item "--"))
3891 (define-key menu [gdb-customize-reasons]
3892 '(menu-item "Customize switching..."
3893 (lambda ()
3894 (interactive)
3895 (customize-option 'gdb-switch-reasons))))
3896 (define-key menu [gdb-switch-when-another-stopped]
3897 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped gdb-switch-when-another-stopped
3898 "Automatically switch to stopped thread"
3899 "GDB thread switching %s"
3900 "Switch to stopped thread"))
3901 (define-key gud-menu-map [mi]
3902 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
3903
3904 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
3905 ;; GDB-MI menu will need to be moved to gud.el. We can't use
3906 ;; tool-bar-local-item-from-menu here because it appends new buttons
3907 ;; to toolbar from right to left while we want our A/T throttle to
3908 ;; show up right before Run button.
3909 (define-key-after gud-tool-bar-map [all-threads]
3910 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
3911 :image (find-image '((:type xpm :file "gud/thread.xpm")))
3912 :visible (and (eq gud-minor-mode 'gdbmi)
3913 gdb-non-stop
3914 (not gdb-gud-control-all-threads)))
3915 'run)
3916
3917 (define-key-after gud-tool-bar-map [current-thread]
3918 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
3919 :image (find-image '((:type xpm :file "gud/all.xpm")))
3920 :visible (and (eq gud-minor-mode 'gdbmi)
3921 gdb-non-stop
3922 gdb-gud-control-all-threads))
3923 'all-threads)
3924
3925 (defun gdb-frame-gdb-buffer ()
3926 "Display GUD buffer in a new frame."
3927 (interactive)
3928 (let ((special-display-regexps (append special-display-regexps '(".*")))
3929 (special-display-frame-alist
3930 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3931 gdb-frame-parameters)))
3932 (same-window-regexps nil))
3933 (display-buffer gud-comint-buffer)))
3934
3935 (defun gdb-display-gdb-buffer ()
3936 "Display GUD buffer."
3937 (interactive)
3938 (let ((same-window-regexps nil))
3939 (select-window (display-buffer gud-comint-buffer nil 0))))
3940
3941 (defun gdb-set-window-buffer (name &optional ignore-dedicated)
3942 "Set buffer of selected window to NAME and dedicate window.
3943
3944 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
3945 window is dedicated."
3946 (when ignore-dedicated
3947 (set-window-dedicated-p (selected-window) nil))
3948 (set-window-buffer (selected-window) (get-buffer name))
3949 (set-window-dedicated-p (selected-window) t))
3950
3951 (defun gdb-setup-windows ()
3952 "Layout the window pattern for `gdb-many-windows'."
3953 (gdb-display-locals-buffer)
3954 (gdb-display-stack-buffer)
3955 (delete-other-windows)
3956 (gdb-display-breakpoints-buffer)
3957 (delete-other-windows)
3958 ; Don't dedicate.
3959 (pop-to-buffer gud-comint-buffer)
3960 (split-window nil ( / ( * (window-height) 3) 4))
3961 (split-window nil ( / (window-height) 3))
3962 (split-window-horizontally)
3963 (other-window 1)
3964 (gdb-set-window-buffer (gdb-locals-buffer-name))
3965 (other-window 1)
3966 (switch-to-buffer
3967 (if gud-last-last-frame
3968 (gud-find-file (car gud-last-last-frame))
3969 (if gdb-main-file
3970 (gud-find-file gdb-main-file)
3971 ;; Put buffer list in window if we
3972 ;; can't find a source file.
3973 (list-buffers-noselect))))
3974 (setq gdb-source-window (selected-window))
3975 (split-window-horizontally)
3976 (other-window 1)
3977 (gdb-set-window-buffer
3978 (gdb-get-buffer-create 'gdb-inferior-io))
3979 (other-window 1)
3980 (gdb-set-window-buffer (gdb-stack-buffer-name))
3981 (split-window-horizontally)
3982 (other-window 1)
3983 (gdb-set-window-buffer (if gdb-show-threads-by-default
3984 (gdb-threads-buffer-name)
3985 (gdb-breakpoints-buffer-name)))
3986 (other-window 1))
3987
3988 (defcustom gdb-many-windows nil
3989 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
3990 In this case it starts with two windows: one displaying the GUD
3991 buffer and the other with the source file with the main routine
3992 of the debugged program. Non-nil means display the layout shown for
3993 `gdb'."
3994 :type 'boolean
3995 :group 'gdb
3996 :version "22.1")
3997
3998 (defun gdb-many-windows (arg)
3999 "Toggle the number of windows in the basic arrangement.
4000 With arg, display additional buffers iff arg is positive."
4001 (interactive "P")
4002 (setq gdb-many-windows
4003 (if (null arg)
4004 (not gdb-many-windows)
4005 (> (prefix-numeric-value arg) 0)))
4006 (message (format "Display of other windows %sabled"
4007 (if gdb-many-windows "en" "dis")))
4008 (if (and gud-comint-buffer
4009 (buffer-name gud-comint-buffer))
4010 (condition-case nil
4011 (gdb-restore-windows)
4012 (error nil))))
4013
4014 (defun gdb-restore-windows ()
4015 "Restore the basic arrangement of windows used by gdb.
4016 This arrangement depends on the value of `gdb-many-windows'."
4017 (interactive)
4018 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
4019 (delete-other-windows)
4020 (if gdb-many-windows
4021 (gdb-setup-windows)
4022 (when (or gud-last-last-frame gdb-show-main)
4023 (split-window)
4024 (other-window 1)
4025 (switch-to-buffer
4026 (if gud-last-last-frame
4027 (gud-find-file (car gud-last-last-frame))
4028 (gud-find-file gdb-main-file)))
4029 (setq gdb-source-window (selected-window))
4030 (other-window 1))))
4031
4032 (defun gdb-reset ()
4033 "Exit a debugging session cleanly.
4034 Kills the gdb buffers, and resets variables and the source buffers."
4035 (dolist (buffer (buffer-list))
4036 (unless (eq buffer gud-comint-buffer)
4037 (with-current-buffer buffer
4038 (if (eq gud-minor-mode 'gdbmi)
4039 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4040 (kill-buffer nil)
4041 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4042 (setq gud-minor-mode nil)
4043 (kill-local-variable 'tool-bar-map)
4044 (kill-local-variable 'gdb-define-alist))))))
4045 (setq gdb-disassembly-position nil)
4046 (setq overlay-arrow-variable-list
4047 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4048 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4049 (setq gdb-stack-position nil)
4050 (setq overlay-arrow-variable-list
4051 (delq 'gdb-stack-position overlay-arrow-variable-list))
4052 (setq gdb-thread-position nil)
4053 (setq overlay-arrow-variable-list
4054 (delq 'gdb-thread-position overlay-arrow-variable-list))
4055 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4056 (setq gud-running nil)
4057 (setq gdb-active-process nil)
4058 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4059
4060 (defun gdb-get-source-file ()
4061 "Find the source file where the program starts and display it with related
4062 buffers, if required."
4063 (goto-char (point-min))
4064 (if (re-search-forward gdb-source-file-regexp nil t)
4065 (setq gdb-main-file (match-string 1)))
4066 (if gdb-many-windows
4067 (gdb-setup-windows)
4068 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4069 (if gdb-show-main
4070 (let ((pop-up-windows t))
4071 (display-buffer (gud-find-file gdb-main-file))))))
4072
4073 ;;from put-image
4074 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4075 "Put string PUTSTRING in front of POS in the current buffer.
4076 PUTSTRING is displayed by putting an overlay into the current buffer with a
4077 `before-string' string that has a `display' property whose value is
4078 PUTSTRING."
4079 (let ((string (make-string 1 ?x))
4080 (buffer (current-buffer)))
4081 (setq putstring (copy-sequence putstring))
4082 (let ((overlay (make-overlay pos pos buffer))
4083 (prop (or dprop
4084 (list (list 'margin 'left-margin) putstring))))
4085 (put-text-property 0 1 'display prop string)
4086 (if sprops
4087 (add-text-properties 0 1 sprops string))
4088 (overlay-put overlay 'put-break t)
4089 (overlay-put overlay 'before-string string))))
4090
4091 ;;from remove-images
4092 (defun gdb-remove-strings (start end &optional buffer)
4093 "Remove strings between START and END in BUFFER.
4094 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4095 BUFFER nil or omitted means use the current buffer."
4096 (unless buffer
4097 (setq buffer (current-buffer)))
4098 (dolist (overlay (overlays-in start end))
4099 (when (overlay-get overlay 'put-break)
4100 (delete-overlay overlay))))
4101
4102 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4103 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4104 (start (- (car posns) 1))
4105 (end (+ (cdr posns) 1))
4106 (putstring (if enabled "B" "b"))
4107 (source-window (get-buffer-window (current-buffer) 0)))
4108 (add-text-properties
4109 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4110 putstring)
4111 (if enabled
4112 (add-text-properties
4113 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4114 (add-text-properties
4115 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4116 (gdb-remove-breakpoint-icons start end)
4117 (if (display-images-p)
4118 (if (>= (or left-fringe-width
4119 (if source-window (car (window-fringes source-window)))
4120 gdb-buffer-fringe-width) 8)
4121 (gdb-put-string
4122 nil (1+ start)
4123 `(left-fringe breakpoint
4124 ,(if enabled
4125 'breakpoint-enabled
4126 'breakpoint-disabled))
4127 'gdb-bptno bptno
4128 'gdb-enabled enabled)
4129 (when (< left-margin-width 2)
4130 (save-current-buffer
4131 (setq left-margin-width 2)
4132 (if source-window
4133 (set-window-margins
4134 source-window
4135 left-margin-width right-margin-width))))
4136 (put-image
4137 (if enabled
4138 (or breakpoint-enabled-icon
4139 (setq breakpoint-enabled-icon
4140 (find-image `((:type xpm :data
4141 ,breakpoint-xpm-data
4142 :ascent 100 :pointer hand)
4143 (:type pbm :data
4144 ,breakpoint-enabled-pbm-data
4145 :ascent 100 :pointer hand)))))
4146 (or breakpoint-disabled-icon
4147 (setq breakpoint-disabled-icon
4148 (find-image `((:type xpm :data
4149 ,breakpoint-xpm-data
4150 :conversion disabled
4151 :ascent 100 :pointer hand)
4152 (:type pbm :data
4153 ,breakpoint-disabled-pbm-data
4154 :ascent 100 :pointer hand))))))
4155 (+ start 1)
4156 putstring
4157 'left-margin))
4158 (when (< left-margin-width 2)
4159 (save-current-buffer
4160 (setq left-margin-width 2)
4161 (let ((window (get-buffer-window (current-buffer) 0)))
4162 (if window
4163 (set-window-margins
4164 window left-margin-width right-margin-width)))))
4165 (gdb-put-string
4166 (propertize putstring
4167 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
4168 (1+ start)))))
4169
4170 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4171 (gdb-remove-strings start end)
4172 (if (display-images-p)
4173 (remove-images start end))
4174 (when remove-margin
4175 (setq left-margin-width 0)
4176 (let ((window (get-buffer-window (current-buffer) 0)))
4177 (if window
4178 (set-window-margins
4179 window left-margin-width right-margin-width)))))
4180
4181 (provide 'gdb-mi)
4182
4183 ;;; gdb-mi.el ends here