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