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