]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-mi.el
* lib/ignore-value.h: New file.
[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 (or func "unknown")
2351 'font-lock-face font-lock-function-name-face)
2352 (gdb-frame-location breakpoint)))))
2353 ;; Add clickable properties only for breakpoints with file:line
2354 ;; information
2355 (append (list 'gdb-breakpoint breakpoint)
2356 (when func '(help-echo "mouse-2, RET: visit breakpoint"
2357 mouse-face highlight))))))
2358 (insert (gdb-table-string table " "))
2359 (gdb-place-breakpoints)))
2360
2361 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
2362 (defun gdb-place-breakpoints ()
2363 (let ((flag) (bptno))
2364 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
2365 (dolist (buffer (buffer-list))
2366 (with-current-buffer buffer
2367 (if (and (eq gud-minor-mode 'gdbmi)
2368 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
2369 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
2370 (dolist (breakpoint gdb-breakpoints-list)
2371 (let* ((breakpoint (cdr breakpoint)) ; gdb-breakpoints-list is
2372 ; an associative list
2373 (line (bindat-get-field breakpoint 'line)))
2374 (when line
2375 (let ((file (bindat-get-field breakpoint 'fullname))
2376 (flag (bindat-get-field breakpoint 'enabled))
2377 (bptno (bindat-get-field breakpoint 'number)))
2378 (unless (file-exists-p file)
2379 (setq file (cdr (assoc bptno gdb-location-alist))))
2380 (if (and file
2381 (not (string-equal file "File not found")))
2382 (with-current-buffer
2383 (find-file-noselect file 'nowarn)
2384 (gdb-init-buffer)
2385 ;; Only want one breakpoint icon at each location.
2386 (gdb-put-breakpoint-icon (string-equal flag "y") bptno
2387 (string-to-number line)))
2388 (gdb-input
2389 (list (concat "list " file ":1")
2390 'ignore))
2391 (gdb-input
2392 (list "-file-list-exec-source-file"
2393 `(lambda () (gdb-get-location
2394 ,bptno ,line ,flag)))))))))))
2395
2396 (defvar gdb-source-file-regexp "fullname=\"\\(.*?\\)\"")
2397
2398 (defun gdb-get-location (bptno line flag)
2399 "Find the directory containing the relevant source file.
2400 Put in buffer and place breakpoint icon."
2401 (goto-char (point-min))
2402 (catch 'file-not-found
2403 (if (re-search-forward gdb-source-file-regexp nil t)
2404 (delete (cons bptno "File not found") gdb-location-alist)
2405 (push (cons bptno (match-string 1)) gdb-location-alist)
2406 (gdb-resync)
2407 (unless (assoc bptno gdb-location-alist)
2408 (push (cons bptno "File not found") gdb-location-alist)
2409 (message-box "Cannot find source file for breakpoint location.
2410 Add directory to search path for source files using the GDB command, dir."))
2411 (throw 'file-not-found nil))
2412 (with-current-buffer (find-file-noselect (match-string 1))
2413 (gdb-init-buffer)
2414 ;; only want one breakpoint icon at each location
2415 (gdb-put-breakpoint-icon (eq flag ?y) bptno (string-to-number line)))))
2416
2417 (add-hook 'find-file-hook 'gdb-find-file-hook)
2418
2419 (defun gdb-find-file-hook ()
2420 "Set up buffer for debugging if file is part of the source code
2421 of the current session."
2422 (if (and (buffer-name gud-comint-buffer)
2423 ;; in case gud or gdb-ui is just loaded
2424 gud-comint-buffer
2425 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2426 'gdbmi))
2427 (if (member buffer-file-name gdb-source-file-list)
2428 (with-current-buffer (find-buffer-visiting buffer-file-name)
2429 (gdb-init-buffer)))))
2430
2431 (declare-function gud-remove "gdb-mi" t t) ; gud-def
2432 (declare-function gud-break "gdb-mi" t t) ; gud-def
2433 (declare-function fringe-bitmaps-at-pos "fringe.c" (&optional pos window))
2434
2435 (defun gdb-mouse-set-clear-breakpoint (event)
2436 "Set/clear breakpoint in left fringe/margin at mouse click.
2437 If not in a source or disassembly buffer just set point."
2438 (interactive "e")
2439 (mouse-minibuffer-check event)
2440 (let ((posn (event-end event)))
2441 (with-selected-window (posn-window posn)
2442 (if (or (buffer-file-name) (derived-mode-p 'gdb-disassembly-mode))
2443 (if (numberp (posn-point posn))
2444 (save-excursion
2445 (goto-char (posn-point posn))
2446 (if (or (posn-object posn)
2447 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
2448 'breakpoint))
2449 (gud-remove nil)
2450 (gud-break nil)))))
2451 (posn-set-point posn))))
2452
2453 (defun gdb-mouse-toggle-breakpoint-margin (event)
2454 "Enable/disable breakpoint in left margin with mouse click."
2455 (interactive "e")
2456 (mouse-minibuffer-check event)
2457 (let ((posn (event-end event)))
2458 (if (numberp (posn-point posn))
2459 (with-selected-window (posn-window posn)
2460 (save-excursion
2461 (goto-char (posn-point posn))
2462 (if (posn-object posn)
2463 (gud-basic-call
2464 (let ((bptno (get-text-property
2465 0 'gdb-bptno (car (posn-string posn)))))
2466 (concat
2467 (if (get-text-property
2468 0 'gdb-enabled (car (posn-string posn)))
2469 "-break-disable "
2470 "-break-enable ")
2471 bptno)))))))))
2472
2473 (defun gdb-mouse-toggle-breakpoint-fringe (event)
2474 "Enable/disable breakpoint in left fringe with mouse click."
2475 (interactive "e")
2476 (mouse-minibuffer-check event)
2477 (let* ((posn (event-end event))
2478 (pos (posn-point posn))
2479 obj)
2480 (when (numberp pos)
2481 (with-selected-window (posn-window posn)
2482 (with-current-buffer (window-buffer (selected-window))
2483 (goto-char pos)
2484 (dolist (overlay (overlays-in pos pos))
2485 (when (overlay-get overlay 'put-break)
2486 (setq obj (overlay-get overlay 'before-string))))
2487 (when (stringp obj)
2488 (gud-basic-call
2489 (concat
2490 (if (get-text-property 0 'gdb-enabled obj)
2491 "-break-disable "
2492 "-break-enable ")
2493 (get-text-property 0 'gdb-bptno obj)))))))))
2494
2495 (defun gdb-breakpoints-buffer-name ()
2496 (concat "*breakpoints of " (gdb-get-target-string) "*"))
2497
2498 (def-gdb-display-buffer
2499 gdb-display-breakpoints-buffer
2500 'gdb-breakpoints-buffer
2501 "Display status of user-settable breakpoints.")
2502
2503 (def-gdb-frame-for-buffer
2504 gdb-frame-breakpoints-buffer
2505 'gdb-breakpoints-buffer
2506 "Display status of user-settable breakpoints in a new frame.")
2507
2508 (defvar gdb-breakpoints-mode-map
2509 (let ((map (make-sparse-keymap))
2510 (menu (make-sparse-keymap "Breakpoints")))
2511 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2512 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2513 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2514 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2515 (suppress-keymap map)
2516 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2517 (define-key map " " 'gdb-toggle-breakpoint)
2518 (define-key map "D" 'gdb-delete-breakpoint)
2519 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2520 (define-key map "q" 'gdb-delete-frame-or-window)
2521 (define-key map "\r" 'gdb-goto-breakpoint)
2522 (define-key map "\t" '(lambda ()
2523 (interactive)
2524 (gdb-set-window-buffer
2525 (gdb-get-buffer-create 'gdb-threads-buffer) t)))
2526 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2527 (define-key map [follow-link] 'mouse-face)
2528 map))
2529
2530 (defun gdb-delete-frame-or-window ()
2531 "Delete frame if there is only one window. Otherwise delete the window."
2532 (interactive)
2533 (if (one-window-p) (delete-frame)
2534 (delete-window)))
2535
2536 ;;from make-mode-line-mouse-map
2537 (defun gdb-make-header-line-mouse-map (mouse function) "\
2538 Return a keymap with single entry for mouse key MOUSE on the header line.
2539 MOUSE is defined to run function FUNCTION with no args in the buffer
2540 corresponding to the mode line clicked."
2541 (let ((map (make-sparse-keymap)))
2542 (define-key map (vector 'header-line mouse) function)
2543 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2544 map))
2545
2546 (defmacro gdb-propertize-header (name buffer help-echo mouse-face face)
2547 `(propertize ,name
2548 'help-echo ,help-echo
2549 'mouse-face ',mouse-face
2550 'face ',face
2551 'local-map
2552 (gdb-make-header-line-mouse-map
2553 'mouse-1
2554 (lambda (event) (interactive "e")
2555 (save-selected-window
2556 (select-window (posn-window (event-start event)))
2557 (gdb-set-window-buffer
2558 (gdb-get-buffer-create ',buffer) t) )))))
2559
2560 \f
2561 ;; uses "-thread-info". Needs GDB 7.0 onwards.
2562 ;;; Threads view
2563
2564 (defun gdb-threads-buffer-name ()
2565 (concat "*threads of " (gdb-get-target-string) "*"))
2566
2567 (def-gdb-display-buffer
2568 gdb-display-threads-buffer
2569 'gdb-threads-buffer
2570 "Display GDB threads.")
2571
2572 (def-gdb-frame-for-buffer
2573 gdb-frame-threads-buffer
2574 'gdb-threads-buffer
2575 "Display GDB threads in a new frame.")
2576
2577 (def-gdb-trigger-and-handler
2578 gdb-invalidate-threads (gdb-current-context-command "-thread-info")
2579 gdb-thread-list-handler gdb-thread-list-handler-custom
2580 '(start update update-threads))
2581
2582 (gdb-set-buffer-rules
2583 'gdb-threads-buffer
2584 'gdb-threads-buffer-name
2585 'gdb-threads-mode
2586 'gdb-invalidate-threads)
2587
2588 (defvar gdb-threads-font-lock-keywords
2589 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face))
2590 (" \\(stopped\\)" (1 font-lock-warning-face))
2591 (" \\(running\\)" (1 font-lock-string-face))
2592 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2593 "Font lock keywords used in `gdb-threads-mode'.")
2594
2595 (defvar gdb-threads-mode-map
2596 (let ((map (make-sparse-keymap)))
2597 (define-key map "\r" 'gdb-select-thread)
2598 (define-key map "f" 'gdb-display-stack-for-thread)
2599 (define-key map "F" 'gdb-frame-stack-for-thread)
2600 (define-key map "l" 'gdb-display-locals-for-thread)
2601 (define-key map "L" 'gdb-frame-locals-for-thread)
2602 (define-key map "r" 'gdb-display-registers-for-thread)
2603 (define-key map "R" 'gdb-frame-registers-for-thread)
2604 (define-key map "d" 'gdb-display-disassembly-for-thread)
2605 (define-key map "D" 'gdb-frame-disassembly-for-thread)
2606 (define-key map "i" 'gdb-interrupt-thread)
2607 (define-key map "c" 'gdb-continue-thread)
2608 (define-key map "s" 'gdb-step-thread)
2609 (define-key map "\t" '(lambda ()
2610 (interactive)
2611 (gdb-set-window-buffer
2612 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t)))
2613 (define-key map [mouse-2] 'gdb-select-thread)
2614 (define-key map [follow-link] 'mouse-face)
2615 map))
2616
2617 (defvar gdb-threads-header
2618 (list
2619 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
2620 "mouse-1: select" mode-line-highlight mode-line-inactive)
2621 " "
2622 (gdb-propertize-header "Threads" gdb-threads-buffer
2623 nil nil mode-line)))
2624
2625 (define-derived-mode gdb-threads-mode gdb-parent-mode "Threads"
2626 "Major mode for GDB threads."
2627 (setq gdb-thread-position (make-marker))
2628 (add-to-list 'overlay-arrow-variable-list 'gdb-thread-position)
2629 (setq header-line-format gdb-threads-header)
2630 (set (make-local-variable 'font-lock-defaults)
2631 '(gdb-threads-font-lock-keywords))
2632 'gdb-invalidate-threads)
2633
2634 (defun gdb-thread-list-handler-custom ()
2635 (let ((threads-list (bindat-get-field (gdb-json-partial-output) 'threads))
2636 (table (make-gdb-table))
2637 (marked-line nil))
2638 (setq gdb-threads-list nil)
2639 (setq gdb-running-threads-count 0)
2640 (setq gdb-stopped-threads-count 0)
2641 (set-marker gdb-thread-position nil)
2642
2643 (dolist (thread (reverse threads-list))
2644 (let ((running (string-equal (bindat-get-field thread 'state) "running")))
2645 (add-to-list 'gdb-threads-list
2646 (cons (bindat-get-field thread 'id)
2647 thread))
2648 (if running
2649 (incf gdb-running-threads-count)
2650 (incf gdb-stopped-threads-count))
2651
2652 (gdb-table-add-row table
2653 (list
2654 (bindat-get-field thread 'id)
2655 (concat
2656 (if gdb-thread-buffer-verbose-names
2657 (concat (bindat-get-field thread 'target-id) " ") "")
2658 (bindat-get-field thread 'state)
2659 ;; Include frame information for stopped threads
2660 (if (not running)
2661 (concat
2662 " in " (bindat-get-field thread 'frame 'func)
2663 (if gdb-thread-buffer-arguments
2664 (concat
2665 " ("
2666 (let ((args (bindat-get-field thread 'frame 'args)))
2667 (mapconcat
2668 (lambda (arg)
2669 (apply 'format `("%s=%s" ,@(gdb-get-many-fields arg 'name 'value))))
2670 args ","))
2671 ")")
2672 "")
2673 (if gdb-thread-buffer-locations
2674 (gdb-frame-location (bindat-get-field thread 'frame)) "")
2675 (if gdb-thread-buffer-addresses
2676 (concat " at " (bindat-get-field thread 'frame 'addr)) ""))
2677 "")))
2678 (list
2679 'gdb-thread thread
2680 'mouse-face 'highlight
2681 'help-echo "mouse-2, RET: select thread")))
2682 (when (string-equal gdb-thread-number
2683 (bindat-get-field thread 'id))
2684 (setq marked-line (length gdb-threads-list))))
2685 (insert (gdb-table-string table " "))
2686 (when marked-line
2687 (gdb-mark-line marked-line gdb-thread-position)))
2688 ;; We update gud-running here because we need to make sure that
2689 ;; gdb-threads-list is up-to-date
2690 (gdb-update-gud-running)
2691 (gdb-emit-signal gdb-buf-publisher 'update-disassembly))
2692
2693 (defmacro def-gdb-thread-buffer-command (name custom-defun &optional doc)
2694 "Define a NAME command which will act upon thread on the current line.
2695
2696 CUSTOM-DEFUN may use locally bound `thread' variable, which will
2697 be the value of 'gdb-thread property of the current line. If
2698 'gdb-thread is nil, error is signaled."
2699 `(defun ,name (&optional event)
2700 ,(when doc doc)
2701 (interactive (list last-input-event))
2702 (if event (posn-set-point (event-end event)))
2703 (save-excursion
2704 (beginning-of-line)
2705 (let ((thread (get-text-property (point) 'gdb-thread)))
2706 (if thread
2707 ,custom-defun
2708 (error "Not recognized as thread line"))))))
2709
2710 (defmacro def-gdb-thread-buffer-simple-command (name buffer-command &optional doc)
2711 "Define a NAME which will call BUFFER-COMMAND with id of thread
2712 on the current line."
2713 `(def-gdb-thread-buffer-command ,name
2714 (,buffer-command (bindat-get-field thread 'id))
2715 ,doc))
2716
2717 (def-gdb-thread-buffer-command gdb-select-thread
2718 (let ((new-id (bindat-get-field thread 'id)))
2719 (gdb-setq-thread-number new-id)
2720 (gdb-input (list (concat "-thread-select " new-id) 'ignore))
2721 (gdb-update))
2722 "Select the thread at current line of threads buffer.")
2723
2724 (def-gdb-thread-buffer-simple-command
2725 gdb-display-stack-for-thread
2726 gdb-preemptively-display-stack-buffer
2727 "Display stack buffer for the thread at current line.")
2728
2729 (def-gdb-thread-buffer-simple-command
2730 gdb-display-locals-for-thread
2731 gdb-preemptively-display-locals-buffer
2732 "Display locals buffer for the thread at current line.")
2733
2734 (def-gdb-thread-buffer-simple-command
2735 gdb-display-registers-for-thread
2736 gdb-preemptively-display-registers-buffer
2737 "Display registers buffer for the thread at current line.")
2738
2739 (def-gdb-thread-buffer-simple-command
2740 gdb-display-disassembly-for-thread
2741 gdb-preemptively-display-disassembly-buffer
2742 "Display disassembly buffer for the thread at current line.")
2743
2744 (def-gdb-thread-buffer-simple-command
2745 gdb-frame-stack-for-thread
2746 gdb-frame-stack-buffer
2747 "Display a new frame with stack buffer for the thread at
2748 current line.")
2749
2750 (def-gdb-thread-buffer-simple-command
2751 gdb-frame-locals-for-thread
2752 gdb-frame-locals-buffer
2753 "Display a new frame with locals buffer for the thread at
2754 current line.")
2755
2756 (def-gdb-thread-buffer-simple-command
2757 gdb-frame-registers-for-thread
2758 gdb-frame-registers-buffer
2759 "Display a new frame with registers buffer for the thread at
2760 current line.")
2761
2762 (def-gdb-thread-buffer-simple-command
2763 gdb-frame-disassembly-for-thread
2764 gdb-frame-disassembly-buffer
2765 "Display a new frame with disassembly buffer for the thread at
2766 current line.")
2767
2768 (defmacro def-gdb-thread-buffer-gud-command (name gud-command &optional doc)
2769 "Define a NAME which will execute GUD-COMMAND with
2770 `gdb-thread-number' locally bound to id of thread on the current
2771 line."
2772 `(def-gdb-thread-buffer-command ,name
2773 (if gdb-non-stop
2774 (let ((gdb-thread-number (bindat-get-field thread 'id))
2775 (gdb-gud-control-all-threads nil))
2776 (call-interactively #',gud-command))
2777 (error "Available in non-stop mode only, customize `gdb-non-stop-setting'"))
2778 ,doc))
2779
2780 (def-gdb-thread-buffer-gud-command
2781 gdb-interrupt-thread
2782 gud-stop-subjob
2783 "Interrupt thread at current line.")
2784
2785 (def-gdb-thread-buffer-gud-command
2786 gdb-continue-thread
2787 gud-cont
2788 "Continue thread at current line.")
2789
2790 (def-gdb-thread-buffer-gud-command
2791 gdb-step-thread
2792 gud-step
2793 "Step thread at current line.")
2794
2795 \f
2796 ;;; Memory view
2797
2798 (defcustom gdb-memory-rows 8
2799 "Number of data rows in memory window."
2800 :type 'integer
2801 :group 'gud
2802 :version "23.2")
2803
2804 (defcustom gdb-memory-columns 4
2805 "Number of data columns in memory window."
2806 :type 'integer
2807 :group 'gud
2808 :version "23.2")
2809
2810 (defcustom gdb-memory-format "x"
2811 "Display format of data items in memory window."
2812 :type '(choice (const :tag "Hexadecimal" "x")
2813 (const :tag "Signed decimal" "d")
2814 (const :tag "Unsigned decimal" "u")
2815 (const :tag "Octal" "o")
2816 (const :tag "Binary" "t"))
2817 :group 'gud
2818 :version "22.1")
2819
2820 (defcustom gdb-memory-unit 4
2821 "Unit size of data items in memory window."
2822 :type '(choice (const :tag "Byte" 1)
2823 (const :tag "Halfword" 2)
2824 (const :tag "Word" 4)
2825 (const :tag "Giant word" 8))
2826 :group 'gud
2827 :version "23.2")
2828
2829 (def-gdb-trigger-and-handler
2830 gdb-invalidate-memory
2831 (format "-data-read-memory %s %s %d %d %d"
2832 gdb-memory-address
2833 gdb-memory-format
2834 gdb-memory-unit
2835 gdb-memory-rows
2836 gdb-memory-columns)
2837 gdb-read-memory-handler
2838 gdb-read-memory-custom
2839 '(start update))
2840
2841 (gdb-set-buffer-rules
2842 'gdb-memory-buffer
2843 'gdb-memory-buffer-name
2844 'gdb-memory-mode
2845 'gdb-invalidate-memory)
2846
2847 (defun gdb-memory-column-width (size format)
2848 "Return length of string with memory unit of SIZE in FORMAT.
2849
2850 SIZE is in bytes, as in `gdb-memory-unit'. FORMAT is a string as
2851 in `gdb-memory-format'."
2852 (let ((format-base (cdr (assoc format
2853 '(("x" . 16)
2854 ("d" . 10) ("u" . 10)
2855 ("o" . 8)
2856 ("t" . 2))))))
2857 (if format-base
2858 (let ((res (ceiling (log (expt 2.0 (* size 8)) format-base))))
2859 (cond ((string-equal format "x")
2860 (+ 2 res)) ; hexadecimal numbers have 0x in front
2861 ((or (string-equal format "d")
2862 (string-equal format "o"))
2863 (1+ res))
2864 (t res)))
2865 (error "Unknown format"))))
2866
2867 (defun gdb-read-memory-custom ()
2868 (let* ((res (gdb-json-partial-output))
2869 (err-msg (bindat-get-field res 'msg)))
2870 (if (not err-msg)
2871 (let ((memory (bindat-get-field res 'memory)))
2872 (setq gdb-memory-address (bindat-get-field res 'addr))
2873 (setq gdb-memory-next-page (bindat-get-field res 'next-page))
2874 (setq gdb-memory-prev-page (bindat-get-field res 'prev-page))
2875 (setq gdb-memory-last-address gdb-memory-address)
2876 (dolist (row memory)
2877 (insert (concat (bindat-get-field row 'addr) ":"))
2878 (dolist (column (bindat-get-field row 'data))
2879 (insert (gdb-pad-string column
2880 (+ 2 (gdb-memory-column-width
2881 gdb-memory-unit
2882 gdb-memory-format)))))
2883 (newline)))
2884 ;; Show last page instead of empty buffer when out of bounds
2885 (progn
2886 (let ((gdb-memory-address gdb-memory-last-address))
2887 (gdb-invalidate-memory 'update)
2888 (error err-msg))))))
2889
2890 (defvar gdb-memory-mode-map
2891 (let ((map (make-sparse-keymap)))
2892 (suppress-keymap map t)
2893 (define-key map "q" 'kill-this-buffer)
2894 (define-key map "n" 'gdb-memory-show-next-page)
2895 (define-key map "p" 'gdb-memory-show-previous-page)
2896 (define-key map "a" 'gdb-memory-set-address)
2897 (define-key map "t" 'gdb-memory-format-binary)
2898 (define-key map "o" 'gdb-memory-format-octal)
2899 (define-key map "u" 'gdb-memory-format-unsigned)
2900 (define-key map "d" 'gdb-memory-format-signed)
2901 (define-key map "x" 'gdb-memory-format-hexadecimal)
2902 (define-key map "b" 'gdb-memory-unit-byte)
2903 (define-key map "h" 'gdb-memory-unit-halfword)
2904 (define-key map "w" 'gdb-memory-unit-word)
2905 (define-key map "g" 'gdb-memory-unit-giant)
2906 (define-key map "R" 'gdb-memory-set-rows)
2907 (define-key map "C" 'gdb-memory-set-columns)
2908 map))
2909
2910 (defun gdb-memory-set-address-event (event)
2911 "Handle a click on address field in memory buffer header."
2912 (interactive "e")
2913 (save-selected-window
2914 (select-window (posn-window (event-start event)))
2915 (gdb-memory-set-address)))
2916
2917 ;; Non-event version for use within keymap
2918 (defun gdb-memory-set-address ()
2919 "Set the start memory address."
2920 (interactive)
2921 (let ((arg (read-from-minibuffer "Memory address: ")))
2922 (setq gdb-memory-address arg))
2923 (gdb-invalidate-memory 'update))
2924
2925 (defmacro def-gdb-set-positive-number (name variable echo-string &optional doc)
2926 "Define a function NAME which reads new VAR value from minibuffer."
2927 `(defun ,name (event)
2928 ,(when doc doc)
2929 (interactive "e")
2930 (save-selected-window
2931 (select-window (posn-window (event-start event)))
2932 (let* ((arg (read-from-minibuffer ,echo-string))
2933 (count (string-to-number arg)))
2934 (if (<= count 0)
2935 (error "Positive number only")
2936 (customize-set-variable ',variable count)
2937 (gdb-invalidate-memory 'update))))))
2938
2939 (def-gdb-set-positive-number
2940 gdb-memory-set-rows
2941 gdb-memory-rows
2942 "Rows: "
2943 "Set the number of data rows in memory window.")
2944
2945 (def-gdb-set-positive-number
2946 gdb-memory-set-columns
2947 gdb-memory-columns
2948 "Columns: "
2949 "Set the number of data columns in memory window.")
2950
2951 (defmacro def-gdb-memory-format (name format doc)
2952 "Define a function NAME to switch memory buffer to use FORMAT.
2953
2954 DOC is an optional documentation string."
2955 `(defun ,name () ,(when doc doc)
2956 (interactive)
2957 (customize-set-variable 'gdb-memory-format ,format)
2958 (gdb-invalidate-memory 'update)))
2959
2960 (def-gdb-memory-format
2961 gdb-memory-format-binary "t"
2962 "Set the display format to binary.")
2963
2964 (def-gdb-memory-format
2965 gdb-memory-format-octal "o"
2966 "Set the display format to octal.")
2967
2968 (def-gdb-memory-format
2969 gdb-memory-format-unsigned "u"
2970 "Set the display format to unsigned decimal.")
2971
2972 (def-gdb-memory-format
2973 gdb-memory-format-signed "d"
2974 "Set the display format to decimal.")
2975
2976 (def-gdb-memory-format
2977 gdb-memory-format-hexadecimal "x"
2978 "Set the display format to hexadecimal.")
2979
2980 (defvar gdb-memory-format-map
2981 (let ((map (make-sparse-keymap)))
2982 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2983 map)
2984 "Keymap to select format in the header line.")
2985
2986 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2987 "Menu of display formats in the header line.")
2988
2989 (define-key gdb-memory-format-menu [binary]
2990 '(menu-item "Binary" gdb-memory-format-binary
2991 :button (:radio . (equal gdb-memory-format "t"))))
2992 (define-key gdb-memory-format-menu [octal]
2993 '(menu-item "Octal" gdb-memory-format-octal
2994 :button (:radio . (equal gdb-memory-format "o"))))
2995 (define-key gdb-memory-format-menu [unsigned]
2996 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2997 :button (:radio . (equal gdb-memory-format "u"))))
2998 (define-key gdb-memory-format-menu [signed]
2999 '(menu-item "Signed Decimal" gdb-memory-format-signed
3000 :button (:radio . (equal gdb-memory-format "d"))))
3001 (define-key gdb-memory-format-menu [hexadecimal]
3002 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
3003 :button (:radio . (equal gdb-memory-format "x"))))
3004
3005 (defun gdb-memory-format-menu (event)
3006 (interactive "@e")
3007 (x-popup-menu event gdb-memory-format-menu))
3008
3009 (defun gdb-memory-format-menu-1 (event)
3010 (interactive "e")
3011 (save-selected-window
3012 (select-window (posn-window (event-start event)))
3013 (let* ((selection (gdb-memory-format-menu event))
3014 (binding (and selection (lookup-key gdb-memory-format-menu
3015 (vector (car selection))))))
3016 (if binding (call-interactively binding)))))
3017
3018 (defmacro def-gdb-memory-unit (name unit-size doc)
3019 "Define a function NAME to switch memory unit size to UNIT-SIZE.
3020
3021 DOC is an optional documentation string."
3022 `(defun ,name () ,(when doc doc)
3023 (interactive)
3024 (customize-set-variable 'gdb-memory-unit ,unit-size)
3025 (gdb-invalidate-memory 'update)))
3026
3027 (def-gdb-memory-unit gdb-memory-unit-giant 8
3028 "Set the unit size to giant words (eight bytes).")
3029
3030 (def-gdb-memory-unit gdb-memory-unit-word 4
3031 "Set the unit size to words (four bytes).")
3032
3033 (def-gdb-memory-unit gdb-memory-unit-halfword 2
3034 "Set the unit size to halfwords (two bytes).")
3035
3036 (def-gdb-memory-unit gdb-memory-unit-byte 1
3037 "Set the unit size to bytes.")
3038
3039 (defmacro def-gdb-memory-show-page (name address-var &optional doc)
3040 "Define a function NAME which show new address in memory buffer.
3041
3042 The defined function switches Memory buffer to show address
3043 stored in ADDRESS-VAR variable.
3044
3045 DOC is an optional documentation string."
3046 `(defun ,name
3047 ,(when doc doc)
3048 (interactive)
3049 (let ((gdb-memory-address ,address-var))
3050 (gdb-invalidate-memory))))
3051
3052 (def-gdb-memory-show-page gdb-memory-show-previous-page
3053 gdb-memory-prev-page)
3054
3055 (def-gdb-memory-show-page gdb-memory-show-next-page
3056 gdb-memory-next-page)
3057
3058 (defvar gdb-memory-unit-map
3059 (let ((map (make-sparse-keymap)))
3060 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
3061 map)
3062 "Keymap to select units in the header line.")
3063
3064 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
3065 "Menu of units in the header line.")
3066
3067 (define-key gdb-memory-unit-menu [giantwords]
3068 '(menu-item "Giant words" gdb-memory-unit-giant
3069 :button (:radio . (equal gdb-memory-unit 8))))
3070 (define-key gdb-memory-unit-menu [words]
3071 '(menu-item "Words" gdb-memory-unit-word
3072 :button (:radio . (equal gdb-memory-unit 4))))
3073 (define-key gdb-memory-unit-menu [halfwords]
3074 '(menu-item "Halfwords" gdb-memory-unit-halfword
3075 :button (:radio . (equal gdb-memory-unit 2))))
3076 (define-key gdb-memory-unit-menu [bytes]
3077 '(menu-item "Bytes" gdb-memory-unit-byte
3078 :button (:radio . (equal gdb-memory-unit 1))))
3079
3080 (defun gdb-memory-unit-menu (event)
3081 (interactive "@e")
3082 (x-popup-menu event gdb-memory-unit-menu))
3083
3084 (defun gdb-memory-unit-menu-1 (event)
3085 (interactive "e")
3086 (save-selected-window
3087 (select-window (posn-window (event-start event)))
3088 (let* ((selection (gdb-memory-unit-menu event))
3089 (binding (and selection (lookup-key gdb-memory-unit-menu
3090 (vector (car selection))))))
3091 (if binding (call-interactively binding)))))
3092
3093 (defvar gdb-memory-font-lock-keywords
3094 '(;; <__function.name+n>
3095 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
3096 )
3097 "Font lock keywords used in `gdb-memory-mode'.")
3098
3099 (defvar gdb-memory-header
3100 '(:eval
3101 (concat
3102 "Start address["
3103 (propertize "-"
3104 'face font-lock-warning-face
3105 'help-echo "mouse-1: decrement address"
3106 'mouse-face 'mode-line-highlight
3107 'local-map (gdb-make-header-line-mouse-map
3108 'mouse-1
3109 #'gdb-memory-show-previous-page))
3110 "|"
3111 (propertize "+"
3112 'face font-lock-warning-face
3113 'help-echo "mouse-1: increment address"
3114 'mouse-face 'mode-line-highlight
3115 'local-map (gdb-make-header-line-mouse-map
3116 'mouse-1
3117 #'gdb-memory-show-next-page))
3118 "]: "
3119 (propertize gdb-memory-address
3120 'face font-lock-warning-face
3121 'help-echo "mouse-1: set start address"
3122 'mouse-face 'mode-line-highlight
3123 'local-map (gdb-make-header-line-mouse-map
3124 'mouse-1
3125 #'gdb-memory-set-address-event))
3126 " Rows: "
3127 (propertize (number-to-string gdb-memory-rows)
3128 'face font-lock-warning-face
3129 'help-echo "mouse-1: set number of columns"
3130 'mouse-face 'mode-line-highlight
3131 'local-map (gdb-make-header-line-mouse-map
3132 'mouse-1
3133 #'gdb-memory-set-rows))
3134 " Columns: "
3135 (propertize (number-to-string gdb-memory-columns)
3136 'face font-lock-warning-face
3137 'help-echo "mouse-1: set number of columns"
3138 'mouse-face 'mode-line-highlight
3139 'local-map (gdb-make-header-line-mouse-map
3140 'mouse-1
3141 #'gdb-memory-set-columns))
3142 " Display Format: "
3143 (propertize gdb-memory-format
3144 'face font-lock-warning-face
3145 'help-echo "mouse-3: select display format"
3146 'mouse-face 'mode-line-highlight
3147 'local-map gdb-memory-format-map)
3148 " Unit Size: "
3149 (propertize (number-to-string gdb-memory-unit)
3150 'face font-lock-warning-face
3151 'help-echo "mouse-3: select unit size"
3152 'mouse-face 'mode-line-highlight
3153 'local-map gdb-memory-unit-map)))
3154 "Header line used in `gdb-memory-mode'.")
3155
3156 (define-derived-mode gdb-memory-mode gdb-parent-mode "Memory"
3157 "Major mode for examining memory."
3158 (setq header-line-format gdb-memory-header)
3159 (set (make-local-variable 'font-lock-defaults)
3160 '(gdb-memory-font-lock-keywords))
3161 'gdb-invalidate-memory)
3162
3163 (defun gdb-memory-buffer-name ()
3164 (concat "*memory of " (gdb-get-target-string) "*"))
3165
3166 (def-gdb-display-buffer
3167 gdb-display-memory-buffer
3168 'gdb-memory-buffer
3169 "Display memory contents.")
3170
3171 (defun gdb-frame-memory-buffer ()
3172 "Display memory contents in a new frame."
3173 (interactive)
3174 (let* ((special-display-regexps (append special-display-regexps '(".*")))
3175 (special-display-frame-alist
3176 `((left-fringe . 0)
3177 (right-fringe . 0)
3178 (width . 83)
3179 ,@gdb-frame-parameters)))
3180 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
3181
3182 \f
3183 ;;; Disassembly view
3184
3185 (defun gdb-disassembly-buffer-name ()
3186 (gdb-current-context-buffer-name
3187 (concat "disassembly of " (gdb-get-target-string))))
3188
3189 (def-gdb-display-buffer
3190 gdb-display-disassembly-buffer
3191 'gdb-disassembly-buffer
3192 "Display disassembly for current stack frame.")
3193
3194 (def-gdb-preempt-display-buffer
3195 gdb-preemptively-display-disassembly-buffer
3196 'gdb-disassembly-buffer)
3197
3198 (def-gdb-frame-for-buffer
3199 gdb-frame-disassembly-buffer
3200 'gdb-disassembly-buffer
3201 "Display disassembly in a new frame.")
3202
3203 (def-gdb-auto-update-trigger gdb-invalidate-disassembly
3204 (let* ((frame (gdb-current-buffer-frame))
3205 (file (bindat-get-field frame 'fullname))
3206 (line (bindat-get-field frame 'line)))
3207 (when file
3208 (format "-data-disassemble -f %s -l %s -n -1 -- 0" file line)))
3209 gdb-disassembly-handler
3210 ;; We update disassembly only after we have actual frame information
3211 ;; about all threads, so no there's `update' signal in this list
3212 '(start update-disassembly))
3213
3214 (def-gdb-auto-update-handler
3215 gdb-disassembly-handler
3216 gdb-invalidate-disassembly
3217 gdb-disassembly-handler-custom
3218 t)
3219
3220 (gdb-set-buffer-rules
3221 'gdb-disassembly-buffer
3222 'gdb-disassembly-buffer-name
3223 'gdb-disassembly-mode
3224 'gdb-invalidate-disassembly)
3225
3226 (defvar gdb-disassembly-font-lock-keywords
3227 '(;; <__function.name+n>
3228 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3229 (1 font-lock-function-name-face))
3230 ;; 0xNNNNNNNN <__function.name+n>: opcode
3231 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3232 (4 font-lock-keyword-face))
3233 ;; %register(at least i386)
3234 ("%\\sw+" . font-lock-variable-name-face)
3235 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3236 (1 font-lock-comment-face)
3237 (2 font-lock-function-name-face))
3238 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3239 "Font lock keywords used in `gdb-disassembly-mode'.")
3240
3241 (defvar gdb-disassembly-mode-map
3242 ;; TODO
3243 (let ((map (make-sparse-keymap)))
3244 (suppress-keymap map)
3245 (define-key map "q" 'kill-this-buffer)
3246 map))
3247
3248 (define-derived-mode gdb-disassembly-mode gdb-parent-mode "Disassembly"
3249 "Major mode for GDB disassembly information."
3250 ;; TODO Rename overlay variable for disassembly mode
3251 (add-to-list 'overlay-arrow-variable-list 'gdb-disassembly-position)
3252 (setq fringes-outside-margins t)
3253 (set (make-local-variable 'gdb-disassembly-position) (make-marker))
3254 (set (make-local-variable 'font-lock-defaults)
3255 '(gdb-disassembly-font-lock-keywords))
3256 'gdb-invalidate-disassembly)
3257
3258 (defun gdb-disassembly-handler-custom ()
3259 (let* ((instructions (bindat-get-field (gdb-json-partial-output) 'asm_insns))
3260 (address (bindat-get-field (gdb-current-buffer-frame) 'addr))
3261 (pos 1)
3262 (table (make-gdb-table))
3263 (marked-line nil))
3264 (dolist (instr instructions)
3265 (gdb-table-add-row table
3266 (list
3267 (bindat-get-field instr 'address)
3268 (apply 'format `("<%s+%s>:" ,@(gdb-get-many-fields instr 'func-name 'offset)))
3269 (bindat-get-field instr 'inst)))
3270 (when (string-equal (bindat-get-field instr 'address)
3271 address)
3272 (progn
3273 (setq marked-line (length (gdb-table-rows table)))
3274 (setq fringe-indicator-alist
3275 (if (string-equal gdb-frame-number "0")
3276 nil
3277 '((overlay-arrow . hollow-right-triangle)))))))
3278 (insert (gdb-table-string table " "))
3279 (gdb-disassembly-place-breakpoints)
3280 ;; Mark current position with overlay arrow and scroll window to
3281 ;; that point
3282 (when marked-line
3283 (let ((window (get-buffer-window (current-buffer) 0)))
3284 (set-window-point window (gdb-mark-line marked-line gdb-disassembly-position))))
3285 (setq mode-name
3286 (gdb-current-context-mode-name
3287 (concat "Disassembly: "
3288 (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3289
3290 (defun gdb-disassembly-place-breakpoints ()
3291 (gdb-remove-breakpoint-icons (point-min) (point-max))
3292 (dolist (breakpoint gdb-breakpoints-list)
3293 (let* ((breakpoint (cdr breakpoint))
3294 (bptno (bindat-get-field breakpoint 'number))
3295 (flag (bindat-get-field breakpoint 'enabled))
3296 (address (bindat-get-field breakpoint 'addr)))
3297 (save-excursion
3298 (goto-char (point-min))
3299 (if (re-search-forward (concat "^" address) nil t)
3300 (gdb-put-breakpoint-icon (string-equal flag "y") bptno))))))
3301
3302 \f
3303 (defvar gdb-breakpoints-header
3304 (list
3305 (gdb-propertize-header "Breakpoints" gdb-breakpoints-buffer
3306 nil nil mode-line)
3307 " "
3308 (gdb-propertize-header "Threads" gdb-threads-buffer
3309 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3310
3311 ;;; Breakpoints view
3312 (define-derived-mode gdb-breakpoints-mode gdb-parent-mode "Breakpoints"
3313 "Major mode for gdb breakpoints."
3314 (setq header-line-format gdb-breakpoints-header)
3315 'gdb-invalidate-breakpoints)
3316
3317 (defun gdb-toggle-breakpoint ()
3318 "Enable/disable breakpoint at current line of breakpoints buffer."
3319 (interactive)
3320 (save-excursion
3321 (beginning-of-line)
3322 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3323 (if breakpoint
3324 (gud-basic-call
3325 (concat (if (string-equal "y" (bindat-get-field breakpoint 'enabled))
3326 "-break-disable "
3327 "-break-enable ")
3328 (bindat-get-field breakpoint 'number)))
3329 (error "Not recognized as break/watchpoint line")))))
3330
3331 (defun gdb-delete-breakpoint ()
3332 "Delete the breakpoint at current line of breakpoints buffer."
3333 (interactive)
3334 (save-excursion
3335 (beginning-of-line)
3336 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3337 (if breakpoint
3338 (gud-basic-call (concat "-break-delete " (bindat-get-field breakpoint 'number)))
3339 (error "Not recognized as break/watchpoint line")))))
3340
3341 (defun gdb-goto-breakpoint (&optional event)
3342 "Go to the location of breakpoint at current line of
3343 breakpoints buffer."
3344 (interactive (list last-input-event))
3345 (if event (posn-set-point (event-end event)))
3346 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
3347 (let ((window (get-buffer-window gud-comint-buffer)))
3348 (if window (save-selected-window (select-window window))))
3349 (save-excursion
3350 (beginning-of-line)
3351 (let ((breakpoint (get-text-property (point) 'gdb-breakpoint)))
3352 (if breakpoint
3353 (let ((bptno (bindat-get-field breakpoint 'number))
3354 (file (bindat-get-field breakpoint 'fullname))
3355 (line (bindat-get-field breakpoint 'line)))
3356 (save-selected-window
3357 (let* ((buffer (find-file-noselect
3358 (if (file-exists-p file) file
3359 (cdr (assoc bptno gdb-location-alist)))))
3360 (window (or (gdb-display-source-buffer buffer)
3361 (display-buffer buffer))))
3362 (setq gdb-source-window window)
3363 (with-current-buffer buffer
3364 (goto-char (point-min))
3365 (forward-line (1- (string-to-number line)))
3366 (set-window-point window (point))))))
3367 (error "Not recognized as break/watchpoint line")))))
3368
3369 \f
3370 ;; Frames buffer. This displays a perpetually correct bactrack trace.
3371 ;;
3372 (def-gdb-trigger-and-handler
3373 gdb-invalidate-frames (gdb-current-context-command "-stack-list-frames")
3374 gdb-stack-list-frames-handler gdb-stack-list-frames-custom
3375 '(start update))
3376
3377 (gdb-set-buffer-rules
3378 'gdb-stack-buffer
3379 'gdb-stack-buffer-name
3380 'gdb-frames-mode
3381 'gdb-invalidate-frames)
3382
3383 (defun gdb-frame-location (frame)
3384 "Return \" of file:line\" or \" of library\" for structure FRAME.
3385
3386 FRAME must have either \"file\" and \"line\" members or \"from\"
3387 member."
3388 (let ((file (bindat-get-field frame 'file))
3389 (line (bindat-get-field frame 'line))
3390 (from (bindat-get-field frame 'from)))
3391 (let ((res (or (and file line (concat file ":" line))
3392 from)))
3393 (if res (concat " of " res) ""))))
3394
3395 (defun gdb-stack-list-frames-custom ()
3396 (let ((stack (bindat-get-field (gdb-json-partial-output "frame") 'stack))
3397 (table (make-gdb-table)))
3398 (set-marker gdb-stack-position nil)
3399 (dolist (frame stack)
3400 (gdb-table-add-row table
3401 (list
3402 (bindat-get-field frame 'level)
3403 "in"
3404 (concat
3405 (bindat-get-field frame 'func)
3406 (if gdb-stack-buffer-locations
3407 (gdb-frame-location frame) "")
3408 (if gdb-stack-buffer-addresses
3409 (concat " at " (bindat-get-field frame 'addr)) "")))
3410 `(mouse-face highlight
3411 help-echo "mouse-2, RET: Select frame"
3412 gdb-frame ,frame)))
3413 (insert (gdb-table-string table " ")))
3414 (when (and gdb-frame-number
3415 (gdb-buffer-shows-main-thread-p))
3416 (gdb-mark-line (1+ (string-to-number gdb-frame-number))
3417 gdb-stack-position))
3418 (setq mode-name
3419 (gdb-current-context-mode-name "Frames")))
3420
3421 (defun gdb-stack-buffer-name ()
3422 (gdb-current-context-buffer-name
3423 (concat "stack frames of " (gdb-get-target-string))))
3424
3425 (def-gdb-display-buffer
3426 gdb-display-stack-buffer
3427 'gdb-stack-buffer
3428 "Display backtrace of current stack.")
3429
3430 (def-gdb-preempt-display-buffer
3431 gdb-preemptively-display-stack-buffer
3432 'gdb-stack-buffer nil t)
3433
3434 (def-gdb-frame-for-buffer
3435 gdb-frame-stack-buffer
3436 'gdb-stack-buffer
3437 "Display backtrace of current stack in a new frame.")
3438
3439 (defvar gdb-frames-mode-map
3440 (let ((map (make-sparse-keymap)))
3441 (suppress-keymap map)
3442 (define-key map "q" 'kill-this-buffer)
3443 (define-key map "\r" 'gdb-select-frame)
3444 (define-key map [mouse-2] 'gdb-select-frame)
3445 (define-key map [follow-link] 'mouse-face)
3446 map))
3447
3448 (defvar gdb-frames-font-lock-keywords
3449 '(("in \\([^ ]+\\)" (1 font-lock-function-name-face)))
3450 "Font lock keywords used in `gdb-frames-mode'.")
3451
3452 (define-derived-mode gdb-frames-mode gdb-parent-mode "Frames"
3453 "Major mode for gdb call stack."
3454 (setq gdb-stack-position (make-marker))
3455 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
3456 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
3457 (set (make-local-variable 'font-lock-defaults)
3458 '(gdb-frames-font-lock-keywords))
3459 'gdb-invalidate-frames)
3460
3461 (defun gdb-select-frame (&optional event)
3462 "Select the frame and display the relevant source."
3463 (interactive (list last-input-event))
3464 (if event (posn-set-point (event-end event)))
3465 (let ((frame (get-text-property (point) 'gdb-frame)))
3466 (if frame
3467 (if (gdb-buffer-shows-main-thread-p)
3468 (let ((new-level (bindat-get-field frame 'level)))
3469 (setq gdb-frame-number new-level)
3470 (gdb-input (list (concat "-stack-select-frame " new-level) 'ignore))
3471 (gdb-update))
3472 (error "Could not select frame for non-current thread"))
3473 (error "Not recognized as frame line"))))
3474
3475 \f
3476 ;; Locals buffer.
3477 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3478 (def-gdb-trigger-and-handler
3479 gdb-invalidate-locals
3480 (concat (gdb-current-context-command "-stack-list-locals") " --simple-values")
3481 gdb-locals-handler gdb-locals-handler-custom
3482 '(start update))
3483
3484 (gdb-set-buffer-rules
3485 'gdb-locals-buffer
3486 'gdb-locals-buffer-name
3487 'gdb-locals-mode
3488 'gdb-invalidate-locals)
3489
3490 (defvar gdb-locals-watch-map
3491 (let ((map (make-sparse-keymap)))
3492 (suppress-keymap map)
3493 (define-key map "\r" 'gud-watch)
3494 (define-key map [mouse-2] 'gud-watch)
3495 map)
3496 "Keymap to create watch expression of a complex data type local variable.")
3497
3498 (defvar gdb-edit-locals-map-1
3499 (let ((map (make-sparse-keymap)))
3500 (suppress-keymap map)
3501 (define-key map "\r" 'gdb-edit-locals-value)
3502 (define-key map [mouse-2] 'gdb-edit-locals-value)
3503 map)
3504 "Keymap to edit value of a simple data type local variable.")
3505
3506 (defun gdb-edit-locals-value (&optional event)
3507 "Assign a value to a variable displayed in the locals buffer."
3508 (interactive (list last-input-event))
3509 (save-excursion
3510 (if event (posn-set-point (event-end event)))
3511 (beginning-of-line)
3512 (let* ((var (bindat-get-field
3513 (get-text-property (point) 'gdb-local-variable) 'name))
3514 (value (read-string (format "New value (%s): " var))))
3515 (gud-basic-call
3516 (concat "-gdb-set variable " var " = " value)))))
3517
3518 ;; Dont display values of arrays or structures.
3519 ;; These can be expanded using gud-watch.
3520 (defun gdb-locals-handler-custom ()
3521 (let ((locals-list (bindat-get-field (gdb-json-partial-output) 'locals))
3522 (table (make-gdb-table)))
3523 (dolist (local locals-list)
3524 (let ((name (bindat-get-field local 'name))
3525 (value (bindat-get-field local 'value))
3526 (type (bindat-get-field local 'type)))
3527 (if (or (not value)
3528 (string-match "\\0x" value))
3529 (add-text-properties 0 (length name)
3530 `(mouse-face highlight
3531 help-echo "mouse-2: create watch expression"
3532 local-map ,gdb-locals-watch-map)
3533 name)
3534 (add-text-properties 0 (length value)
3535 `(mouse-face highlight
3536 help-echo "mouse-2: edit value"
3537 local-map ,gdb-edit-locals-map-1)
3538 value))
3539 (gdb-table-add-row
3540 table
3541 (list
3542 (propertize type 'font-lock-face font-lock-type-face)
3543 (propertize name 'font-lock-face font-lock-variable-name-face)
3544 value)
3545 `(gdb-local-variable ,local))))
3546 (insert (gdb-table-string table " "))
3547 (setq mode-name
3548 (gdb-current-context-mode-name
3549 (concat "Locals: " (bindat-get-field (gdb-current-buffer-frame) 'func))))))
3550
3551 (defvar gdb-locals-header
3552 (list
3553 (gdb-propertize-header "Locals" gdb-locals-buffer
3554 nil nil mode-line)
3555 " "
3556 (gdb-propertize-header "Registers" gdb-registers-buffer
3557 "mouse-1: select" mode-line-highlight mode-line-inactive)))
3558
3559 (defvar gdb-locals-mode-map
3560 (let ((map (make-sparse-keymap)))
3561 (suppress-keymap map)
3562 (define-key map "q" 'kill-this-buffer)
3563 (define-key map "\t" '(lambda ()
3564 (interactive)
3565 (gdb-set-window-buffer
3566 (gdb-get-buffer-create
3567 'gdb-registers-buffer
3568 gdb-thread-number) t)))
3569 map))
3570
3571 (define-derived-mode gdb-locals-mode gdb-parent-mode "Locals"
3572 "Major mode for gdb locals."
3573 (setq header-line-format gdb-locals-header)
3574 'gdb-invalidate-locals)
3575
3576 (defun gdb-locals-buffer-name ()
3577 (gdb-current-context-buffer-name
3578 (concat "locals of " (gdb-get-target-string))))
3579
3580 (def-gdb-display-buffer
3581 gdb-display-locals-buffer
3582 'gdb-locals-buffer
3583 "Display local variables of current stack and their values.")
3584
3585 (def-gdb-preempt-display-buffer
3586 gdb-preemptively-display-locals-buffer
3587 'gdb-locals-buffer nil t)
3588
3589 (def-gdb-frame-for-buffer
3590 gdb-frame-locals-buffer
3591 'gdb-locals-buffer
3592 "Display local variables of current stack and their values in a new frame.")
3593
3594 \f
3595 ;; Registers buffer.
3596
3597 (def-gdb-trigger-and-handler
3598 gdb-invalidate-registers
3599 (concat (gdb-current-context-command "-data-list-register-values") " x")
3600 gdb-registers-handler
3601 gdb-registers-handler-custom
3602 '(start update))
3603
3604 (gdb-set-buffer-rules
3605 'gdb-registers-buffer
3606 'gdb-registers-buffer-name
3607 'gdb-registers-mode
3608 'gdb-invalidate-registers)
3609
3610 (defun gdb-registers-handler-custom ()
3611 (when gdb-register-names
3612 (let ((register-values (bindat-get-field (gdb-json-partial-output) 'register-values))
3613 (table (make-gdb-table)))
3614 (dolist (register register-values)
3615 (let* ((register-number (bindat-get-field register 'number))
3616 (value (bindat-get-field register 'value))
3617 (register-name (nth (string-to-number register-number)
3618 gdb-register-names)))
3619 (gdb-table-add-row
3620 table
3621 (list
3622 (propertize register-name 'font-lock-face font-lock-variable-name-face)
3623 (if (member register-number gdb-changed-registers)
3624 (propertize value 'font-lock-face font-lock-warning-face)
3625 value))
3626 `(mouse-face highlight
3627 help-echo "mouse-2: edit value"
3628 gdb-register-name ,register-name))))
3629 (insert (gdb-table-string table " ")))
3630 (setq mode-name
3631 (gdb-current-context-mode-name "Registers"))))
3632
3633 (defun gdb-edit-register-value (&optional event)
3634 "Assign a value to a register displayed in the registers buffer."
3635 (interactive (list last-input-event))
3636 (save-excursion
3637 (if event (posn-set-point (event-end event)))
3638 (beginning-of-line)
3639 (let* ((var (bindat-get-field
3640 (get-text-property (point) 'gdb-register-name)))
3641 (value (read-string (format "New value (%s): " var))))
3642 (gud-basic-call
3643 (concat "-gdb-set variable $" var " = " value)))))
3644
3645 (defvar gdb-registers-mode-map
3646 (let ((map (make-sparse-keymap)))
3647 (suppress-keymap map)
3648 (define-key map "\r" 'gdb-edit-register-value)
3649 (define-key map [mouse-2] 'gdb-edit-register-value)
3650 (define-key map "q" 'kill-this-buffer)
3651 (define-key map "\t" '(lambda ()
3652 (interactive)
3653 (gdb-set-window-buffer
3654 (gdb-get-buffer-create
3655 'gdb-locals-buffer
3656 gdb-thread-number) t)))
3657 map))
3658
3659 (defvar gdb-registers-header
3660 (list
3661 (gdb-propertize-header "Locals" gdb-locals-buffer
3662 "mouse-1: select" mode-line-highlight mode-line-inactive)
3663 " "
3664 (gdb-propertize-header "Registers" gdb-registers-buffer
3665 nil nil mode-line)))
3666
3667 (define-derived-mode gdb-registers-mode gdb-parent-mode "Registers"
3668 "Major mode for gdb registers."
3669 (setq header-line-format gdb-registers-header)
3670 'gdb-invalidate-registers)
3671
3672 (defun gdb-registers-buffer-name ()
3673 (gdb-current-context-buffer-name
3674 (concat "registers of " (gdb-get-target-string))))
3675
3676 (def-gdb-display-buffer
3677 gdb-display-registers-buffer
3678 'gdb-registers-buffer
3679 "Display integer register contents.")
3680
3681 (def-gdb-preempt-display-buffer
3682 gdb-preemptively-display-registers-buffer
3683 'gdb-registers-buffer nil t)
3684
3685 (def-gdb-frame-for-buffer
3686 gdb-frame-registers-buffer
3687 'gdb-registers-buffer
3688 "Display integer register contents in a new frame.")
3689
3690 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3691 (defun gdb-get-changed-registers ()
3692 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3693 (not (gdb-pending-p 'gdb-get-changed-registers)))
3694 (progn
3695 (gdb-input
3696 (list
3697 "-data-list-changed-registers"
3698 'gdb-changed-registers-handler))
3699 (gdb-add-pending 'gdb-get-changed-registers))))
3700
3701 (defun gdb-changed-registers-handler ()
3702 (gdb-delete-pending 'gdb-get-changed-registers)
3703 (setq gdb-changed-registers nil)
3704 (dolist (register-number (bindat-get-field (gdb-json-partial-output) 'changed-registers))
3705 (push register-number gdb-changed-registers)))
3706
3707 (defun gdb-register-names-handler ()
3708 ;; Don't use gdb-pending-triggers because this handler is called
3709 ;; only once (in gdb-init-1)
3710 (setq gdb-register-names nil)
3711 (dolist (register-name (bindat-get-field (gdb-json-partial-output) 'register-names))
3712 (push register-name gdb-register-names))
3713 (setq gdb-register-names (reverse gdb-register-names)))
3714 \f
3715
3716 (defun gdb-get-source-file-list ()
3717 "Create list of source files for current GDB session.
3718 If buffers already exist for any of these files, gud-minor-mode
3719 is set in them."
3720 (goto-char (point-min))
3721 (while (re-search-forward gdb-source-file-regexp nil t)
3722 (push (match-string 1) gdb-source-file-list))
3723 (dolist (buffer (buffer-list))
3724 (with-current-buffer buffer
3725 (when (member buffer-file-name gdb-source-file-list)
3726 (gdb-init-buffer))))
3727 (gdb-force-mode-line-update
3728 (propertize "ready" 'face font-lock-variable-name-face)))
3729
3730 (defun gdb-get-main-selected-frame ()
3731 "Trigger for `gdb-frame-handler' which uses main current
3732 thread. Called from `gdb-update'."
3733 (if (not (gdb-pending-p 'gdb-get-main-selected-frame))
3734 (progn
3735 (gdb-input
3736 (list (gdb-current-context-command "-stack-info-frame") 'gdb-frame-handler))
3737 (gdb-add-pending 'gdb-get-main-selected-frame))))
3738
3739 (defun gdb-frame-handler ()
3740 "Sets `gdb-selected-frame' and `gdb-selected-file' to show
3741 overlay arrow in source buffer."
3742 (gdb-delete-pending 'gdb-get-main-selected-frame)
3743 (let ((frame (bindat-get-field (gdb-json-partial-output) 'frame)))
3744 (when frame
3745 (setq gdb-selected-frame (bindat-get-field frame 'func))
3746 (setq gdb-selected-file (bindat-get-field frame 'fullname))
3747 (setq gdb-frame-number (bindat-get-field frame 'level))
3748 (setq gdb-frame-address (bindat-get-field frame 'addr))
3749 (let ((line (bindat-get-field frame 'line)))
3750 (setq gdb-selected-line (and line (string-to-number line)))
3751 (when (and gdb-selected-file gdb-selected-line)
3752 (setq gud-last-frame (cons gdb-selected-file gdb-selected-line))
3753 (gud-display-frame)))
3754 (if gud-overlay-arrow-position
3755 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3756 (position (marker-position gud-overlay-arrow-position)))
3757 (when buffer
3758 (with-current-buffer buffer
3759 (setq fringe-indicator-alist
3760 (if (string-equal gdb-frame-number "0")
3761 nil
3762 '((overlay-arrow . hollow-right-triangle))))
3763 (setq gud-overlay-arrow-position (make-marker))
3764 (set-marker gud-overlay-arrow-position position))))))))
3765
3766 (defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
3767
3768 (defun gdb-get-prompt ()
3769 "Find prompt for GDB session."
3770 (goto-char (point-min))
3771 (setq gdb-prompt-name nil)
3772 (re-search-forward gdb-prompt-name-regexp nil t)
3773 (setq gdb-prompt-name (match-string 1))
3774 ;; Insert first prompt.
3775 (setq gdb-filter-output (concat gdb-filter-output gdb-prompt-name)))
3776
3777 ;;;; Window management
3778 (defun gdb-display-buffer (buf dedicated &optional frame)
3779 "Show buffer BUF.
3780
3781 If BUF is already displayed in some window, show it, deiconifying
3782 the frame if necessary. Otherwise, find least recently used
3783 window and show BUF there, if the window is not used for GDB
3784 already, in which case that window is splitted first."
3785 (let ((answer (get-buffer-window buf (or frame 0))))
3786 (if answer
3787 (display-buffer buf nil (or frame 0)) ;Deiconify the frame if necessary.
3788 (let ((window (get-lru-window)))
3789 (if (eq (buffer-local-value 'gud-minor-mode (window-buffer window))
3790 'gdbmi)
3791 (let* ((largest (get-largest-window))
3792 (cur-size (window-height largest)))
3793 (setq answer (split-window largest))
3794 (set-window-buffer answer buf)
3795 (set-window-dedicated-p answer dedicated)
3796 answer)
3797 (set-window-buffer window buf)
3798 window)))))
3799
3800 (defun gdb-preempt-existing-or-display-buffer (buf &optional split-horizontal)
3801 "Find window displaying a buffer with the same
3802 `gdb-buffer-type' as BUF and show BUF there. If no such window
3803 exists, just call `gdb-display-buffer' for BUF. If the window
3804 found is already dedicated, split window according to
3805 SPLIT-HORIZONTAL and show BUF in the new window."
3806 (if buf
3807 (when (not (get-buffer-window buf))
3808 (let* ((buf-type (gdb-buffer-type buf))
3809 (existing-window
3810 (get-window-with-predicate
3811 #'(lambda (w)
3812 (and (eq buf-type
3813 (gdb-buffer-type (window-buffer w)))
3814 (not (window-dedicated-p w)))))))
3815 (if existing-window
3816 (set-window-buffer existing-window buf)
3817 (let ((dedicated-window
3818 (get-window-with-predicate
3819 #'(lambda (w)
3820 (eq buf-type
3821 (gdb-buffer-type (window-buffer w)))))))
3822 (if dedicated-window
3823 (set-window-buffer
3824 (split-window dedicated-window nil split-horizontal) buf)
3825 (gdb-display-buffer buf t))))))
3826 (error "Null buffer")))
3827 \f
3828 ;;; Shared keymap initialization:
3829
3830 (let ((menu (make-sparse-keymap "GDB-Windows")))
3831 (define-key gud-menu-map [displays]
3832 `(menu-item "GDB-Windows" ,menu
3833 :visible (eq gud-minor-mode 'gdbmi)))
3834 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
3835 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
3836 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
3837 (define-key menu [disassembly]
3838 '("Disassembly" . gdb-display-disassembly-buffer))
3839 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
3840 (define-key menu [inferior]
3841 '("IO" . gdb-display-io-buffer))
3842 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
3843 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
3844 (define-key menu [breakpoints]
3845 '("Breakpoints" . gdb-display-breakpoints-buffer)))
3846
3847 (let ((menu (make-sparse-keymap "GDB-Frames")))
3848 (define-key gud-menu-map [frames]
3849 `(menu-item "GDB-Frames" ,menu
3850 :visible (eq gud-minor-mode 'gdbmi)))
3851 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
3852 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
3853 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
3854 (define-key menu [disassembly] '("Disassembly" . gdb-frame-disassembly-buffer))
3855 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
3856 (define-key menu [inferior]
3857 '("IO" . gdb-frame-io-buffer))
3858 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
3859 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
3860 (define-key menu [breakpoints]
3861 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
3862
3863 (let ((menu (make-sparse-keymap "GDB-MI")))
3864 (define-key menu [gdb-customize]
3865 '(menu-item "Customize" (lambda () (interactive) (customize-group 'gdb))
3866 :help "Customize Gdb Graphical Mode options."))
3867 (define-key menu [gdb-many-windows]
3868 '(menu-item "Display Other Windows" gdb-many-windows
3869 :help "Toggle display of locals, stack and breakpoint information"
3870 :button (:toggle . gdb-many-windows)))
3871 (define-key menu [gdb-restore-windows]
3872 '(menu-item "Restore Window Layout" gdb-restore-windows
3873 :help "Restore standard layout for debug session."))
3874 (define-key menu [sep1]
3875 '(menu-item "--"))
3876 (define-key menu [all-threads]
3877 '(menu-item "GUD controls all threads"
3878 (lambda ()
3879 (interactive)
3880 (setq gdb-gud-control-all-threads t))
3881 :help "GUD start/stop commands apply to all threads"
3882 :button (:radio . gdb-gud-control-all-threads)))
3883 (define-key menu [current-thread]
3884 '(menu-item "GUD controls current thread"
3885 (lambda ()
3886 (interactive)
3887 (setq gdb-gud-control-all-threads nil))
3888 :help "GUD start/stop commands apply to current thread only"
3889 :button (:radio . (not gdb-gud-control-all-threads))))
3890 (define-key menu [sep2]
3891 '(menu-item "--"))
3892 (define-key menu [gdb-customize-reasons]
3893 '(menu-item "Customize switching..."
3894 (lambda ()
3895 (interactive)
3896 (customize-option 'gdb-switch-reasons))))
3897 (define-key menu [gdb-switch-when-another-stopped]
3898 (menu-bar-make-toggle gdb-toggle-switch-when-another-stopped gdb-switch-when-another-stopped
3899 "Automatically switch to stopped thread"
3900 "GDB thread switching %s"
3901 "Switch to stopped thread"))
3902 (define-key gud-menu-map [mi]
3903 `(menu-item "GDB-MI" ,menu :visible (eq gud-minor-mode 'gdbmi))))
3904
3905 ;; TODO Fit these into tool-bar-local-item-from-menu call in gud.el.
3906 ;; GDB-MI menu will need to be moved to gud.el. We can't use
3907 ;; tool-bar-local-item-from-menu here because it appends new buttons
3908 ;; to toolbar from right to left while we want our A/T throttle to
3909 ;; show up right before Run button.
3910 (define-key-after gud-tool-bar-map [all-threads]
3911 '(menu-item "Switch to non-stop/A mode" gdb-control-all-threads
3912 :image (find-image '((:type xpm :file "gud/thread.xpm")))
3913 :visible (and (eq gud-minor-mode 'gdbmi)
3914 gdb-non-stop
3915 (not gdb-gud-control-all-threads)))
3916 'run)
3917
3918 (define-key-after gud-tool-bar-map [current-thread]
3919 '(menu-item "Switch to non-stop/T mode" gdb-control-current-thread
3920 :image (find-image '((:type xpm :file "gud/all.xpm")))
3921 :visible (and (eq gud-minor-mode 'gdbmi)
3922 gdb-non-stop
3923 gdb-gud-control-all-threads))
3924 'all-threads)
3925
3926 (defun gdb-frame-gdb-buffer ()
3927 "Display GUD buffer in a new frame."
3928 (interactive)
3929 (let ((special-display-regexps (append special-display-regexps '(".*")))
3930 (special-display-frame-alist
3931 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
3932 gdb-frame-parameters)))
3933 (same-window-regexps nil))
3934 (display-buffer gud-comint-buffer)))
3935
3936 (defun gdb-display-gdb-buffer ()
3937 "Display GUD buffer."
3938 (interactive)
3939 (let ((same-window-regexps nil))
3940 (select-window (display-buffer gud-comint-buffer nil 0))))
3941
3942 (defun gdb-set-window-buffer (name &optional ignore-dedicated)
3943 "Set buffer of selected window to NAME and dedicate window.
3944
3945 When IGNORE-DEDICATED is non-nil, buffer is set even if selected
3946 window is dedicated."
3947 (when ignore-dedicated
3948 (set-window-dedicated-p (selected-window) nil))
3949 (set-window-buffer (selected-window) (get-buffer name))
3950 (set-window-dedicated-p (selected-window) t))
3951
3952 (defun gdb-setup-windows ()
3953 "Layout the window pattern for `gdb-many-windows'."
3954 (gdb-display-locals-buffer)
3955 (gdb-display-stack-buffer)
3956 (delete-other-windows)
3957 (gdb-display-breakpoints-buffer)
3958 (delete-other-windows)
3959 ; Don't dedicate.
3960 (pop-to-buffer gud-comint-buffer)
3961 (split-window nil ( / ( * (window-height) 3) 4))
3962 (split-window nil ( / (window-height) 3))
3963 (split-window-horizontally)
3964 (other-window 1)
3965 (gdb-set-window-buffer (gdb-locals-buffer-name))
3966 (other-window 1)
3967 (switch-to-buffer
3968 (if gud-last-last-frame
3969 (gud-find-file (car gud-last-last-frame))
3970 (if gdb-main-file
3971 (gud-find-file gdb-main-file)
3972 ;; Put buffer list in window if we
3973 ;; can't find a source file.
3974 (list-buffers-noselect))))
3975 (setq gdb-source-window (selected-window))
3976 (split-window-horizontally)
3977 (other-window 1)
3978 (gdb-set-window-buffer
3979 (gdb-get-buffer-create 'gdb-inferior-io))
3980 (other-window 1)
3981 (gdb-set-window-buffer (gdb-stack-buffer-name))
3982 (split-window-horizontally)
3983 (other-window 1)
3984 (gdb-set-window-buffer (if gdb-show-threads-by-default
3985 (gdb-threads-buffer-name)
3986 (gdb-breakpoints-buffer-name)))
3987 (other-window 1))
3988
3989 (defcustom gdb-many-windows nil
3990 "If nil just pop up the GUD buffer unless `gdb-show-main' is t.
3991 In this case it starts with two windows: one displaying the GUD
3992 buffer and the other with the source file with the main routine
3993 of the debugged program. Non-nil means display the layout shown for
3994 `gdb'."
3995 :type 'boolean
3996 :group 'gdb
3997 :version "22.1")
3998
3999 (defun gdb-many-windows (arg)
4000 "Toggle the number of windows in the basic arrangement.
4001 With arg, display additional buffers iff arg is positive."
4002 (interactive "P")
4003 (setq gdb-many-windows
4004 (if (null arg)
4005 (not gdb-many-windows)
4006 (> (prefix-numeric-value arg) 0)))
4007 (message (format "Display of other windows %sabled"
4008 (if gdb-many-windows "en" "dis")))
4009 (if (and gud-comint-buffer
4010 (buffer-name gud-comint-buffer))
4011 (condition-case nil
4012 (gdb-restore-windows)
4013 (error nil))))
4014
4015 (defun gdb-restore-windows ()
4016 "Restore the basic arrangement of windows used by gdb.
4017 This arrangement depends on the value of `gdb-many-windows'."
4018 (interactive)
4019 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
4020 (delete-other-windows)
4021 (if gdb-many-windows
4022 (gdb-setup-windows)
4023 (when (or gud-last-last-frame gdb-show-main)
4024 (split-window)
4025 (other-window 1)
4026 (switch-to-buffer
4027 (if gud-last-last-frame
4028 (gud-find-file (car gud-last-last-frame))
4029 (gud-find-file gdb-main-file)))
4030 (setq gdb-source-window (selected-window))
4031 (other-window 1))))
4032
4033 (defun gdb-reset ()
4034 "Exit a debugging session cleanly.
4035 Kills the gdb buffers, and resets variables and the source buffers."
4036 (dolist (buffer (buffer-list))
4037 (unless (eq buffer gud-comint-buffer)
4038 (with-current-buffer buffer
4039 (if (eq gud-minor-mode 'gdbmi)
4040 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
4041 (kill-buffer nil)
4042 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
4043 (setq gud-minor-mode nil)
4044 (kill-local-variable 'tool-bar-map)
4045 (kill-local-variable 'gdb-define-alist))))))
4046 (setq gdb-disassembly-position nil)
4047 (setq overlay-arrow-variable-list
4048 (delq 'gdb-disassembly-position overlay-arrow-variable-list))
4049 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
4050 (setq gdb-stack-position nil)
4051 (setq overlay-arrow-variable-list
4052 (delq 'gdb-stack-position overlay-arrow-variable-list))
4053 (setq gdb-thread-position nil)
4054 (setq overlay-arrow-variable-list
4055 (delq 'gdb-thread-position overlay-arrow-variable-list))
4056 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
4057 (setq gud-running nil)
4058 (setq gdb-active-process nil)
4059 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
4060
4061 (defun gdb-get-source-file ()
4062 "Find the source file where the program starts and display it with related
4063 buffers, if required."
4064 (goto-char (point-min))
4065 (if (re-search-forward gdb-source-file-regexp nil t)
4066 (setq gdb-main-file (match-string 1)))
4067 (if gdb-many-windows
4068 (gdb-setup-windows)
4069 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
4070 (if gdb-show-main
4071 (let ((pop-up-windows t))
4072 (display-buffer (gud-find-file gdb-main-file))))))
4073
4074 ;;from put-image
4075 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
4076 "Put string PUTSTRING in front of POS in the current buffer.
4077 PUTSTRING is displayed by putting an overlay into the current buffer with a
4078 `before-string' string that has a `display' property whose value is
4079 PUTSTRING."
4080 (let ((string (make-string 1 ?x))
4081 (buffer (current-buffer)))
4082 (setq putstring (copy-sequence putstring))
4083 (let ((overlay (make-overlay pos pos buffer))
4084 (prop (or dprop
4085 (list (list 'margin 'left-margin) putstring))))
4086 (put-text-property 0 1 'display prop string)
4087 (if sprops
4088 (add-text-properties 0 1 sprops string))
4089 (overlay-put overlay 'put-break t)
4090 (overlay-put overlay 'before-string string))))
4091
4092 ;;from remove-images
4093 (defun gdb-remove-strings (start end &optional buffer)
4094 "Remove strings between START and END in BUFFER.
4095 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
4096 BUFFER nil or omitted means use the current buffer."
4097 (unless buffer
4098 (setq buffer (current-buffer)))
4099 (dolist (overlay (overlays-in start end))
4100 (when (overlay-get overlay 'put-break)
4101 (delete-overlay overlay))))
4102
4103 (defun gdb-put-breakpoint-icon (enabled bptno &optional line)
4104 (let* ((posns (gdb-line-posns (or line (line-number-at-pos))))
4105 (start (- (car posns) 1))
4106 (end (+ (cdr posns) 1))
4107 (putstring (if enabled "B" "b"))
4108 (source-window (get-buffer-window (current-buffer) 0)))
4109 (add-text-properties
4110 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
4111 putstring)
4112 (if enabled
4113 (add-text-properties
4114 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
4115 (add-text-properties
4116 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
4117 (gdb-remove-breakpoint-icons start end)
4118 (if (display-images-p)
4119 (if (>= (or left-fringe-width
4120 (if source-window (car (window-fringes source-window)))
4121 gdb-buffer-fringe-width) 8)
4122 (gdb-put-string
4123 nil (1+ start)
4124 `(left-fringe breakpoint
4125 ,(if enabled
4126 'breakpoint-enabled
4127 'breakpoint-disabled))
4128 'gdb-bptno bptno
4129 'gdb-enabled enabled)
4130 (when (< left-margin-width 2)
4131 (save-current-buffer
4132 (setq left-margin-width 2)
4133 (if source-window
4134 (set-window-margins
4135 source-window
4136 left-margin-width right-margin-width))))
4137 (put-image
4138 (if enabled
4139 (or breakpoint-enabled-icon
4140 (setq breakpoint-enabled-icon
4141 (find-image `((:type xpm :data
4142 ,breakpoint-xpm-data
4143 :ascent 100 :pointer hand)
4144 (:type pbm :data
4145 ,breakpoint-enabled-pbm-data
4146 :ascent 100 :pointer hand)))))
4147 (or breakpoint-disabled-icon
4148 (setq breakpoint-disabled-icon
4149 (find-image `((:type xpm :data
4150 ,breakpoint-xpm-data
4151 :conversion disabled
4152 :ascent 100 :pointer hand)
4153 (:type pbm :data
4154 ,breakpoint-disabled-pbm-data
4155 :ascent 100 :pointer hand))))))
4156 (+ start 1)
4157 putstring
4158 'left-margin))
4159 (when (< left-margin-width 2)
4160 (save-current-buffer
4161 (setq left-margin-width 2)
4162 (let ((window (get-buffer-window (current-buffer) 0)))
4163 (if window
4164 (set-window-margins
4165 window left-margin-width right-margin-width)))))
4166 (gdb-put-string
4167 (propertize putstring
4168 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
4169 (1+ start)))))
4170
4171 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
4172 (gdb-remove-strings start end)
4173 (if (display-images-p)
4174 (remove-images start end))
4175 (when remove-margin
4176 (setq left-margin-width 0)
4177 (let ((window (get-buffer-window (current-buffer) 0)))
4178 (if window
4179 (set-window-margins
4180 window left-margin-width right-margin-width)))))
4181
4182 (provide 'gdb-mi)
4183
4184 ;;; gdb-mi.el ends here