Commit ffbdb06a authored by David Byers's avatar David Byers
Browse files

First cut at making smalloc tracing automatic.

parent 8c60d317
1999-07-02 David Byers <>
Automatic operation of memory trace analysis:
* src/server/ram-smalloc.c: Mention automatic analysis. Update the
description on how to do it manually (MEMTRACE=file, ATTACH=yes)
* src/server/handle-malloc-dump.el: Added automatic operation in
batch mode. Just run -batch and watch the magic happen.
1999-07-02 David Byers <>
Some test cases for name lookup:
;;;; $Id: handle-malloc-dump.el,v 1.8 1999/05/24 09:38:27 ceder Exp $
;;;; $Id: handle-malloc-dump.el,v 1.9 1999/07/02 14:50:36 byers Exp $
;;;; Copyright (C) 1991-1995, 1999 Lysator Academic Computer Association.
;;;; This file is part of the LysKOM server.
......@@ -23,6 +23,8 @@
;;;; Please mail bug reports to
;;;; See ram-smalloc.c for instructions on how to use this file.
(require 'dll)
;;; block - each block that is active is stored on a dll. The dll
......@@ -89,7 +91,7 @@ mallocs/reallocs/frees to *Result*."
(btend (progn (re-search-forward "^==== end ====")
(match-beginning 0))))
(message fn)
; (message fn)
((string= fn "malloc")
(beginning-of-line 0)
......@@ -155,3 +157,156 @@ mallocs/reallocs/frees to *Result*."
(insert (format "From char %d:\n" b))
(insert-buffer-substring gdb-buf (1- b) (1+ e))))
(setq node (dll-next stack node)))))
;;; Batch mode analysis
(defvar gdb-tty nil)
(defvar gdb-proc nil)
(defvar gdb-buffer nil)
(defvar lyskomd-pid nil)
(defvar trace-done nil)
(defun trace-collect-data (proc data)
(goto-char (point-max))
(insert data))
(defun trace-expect (re)
(goto-char trace-last-match)
(if (re-search-forward re nil t)
(progn (set-marker trace-last-match (match-end 0))
(match-beginning 0))))
(defun trace-collect-filter (proc data)
(set-buffer (process-buffer proc))
(trace-collect-data proc data)))
(defun trace-process-get-tty (proc data)
(set-buffer (process-buffer proc))
(trace-collect-data proc data)
(if (trace-expect "\\(/dev\\S-*\\)$")
(progn (setq gdb-tty (match-string 1))
(message "Tracing using gdb on tty %s" gdb-tty)
(set-process-filter proc 'trace-collect-filter)))))
(defun trace-runtest-filter (proc data)
(set-buffer (process-buffer proc))
(trace-collect-data proc data)
(if (trace-expect "Please attach to lyskomd pid \\([0-9]+\\) and hit RETURN$")
(progn (setq lyskomd-pid (string-to-int (match-string 1)))
(message "Attaching to lyskomd pid %d" lyskomd-pid)
(set-process-filter proc 'trace-collect-filter)))))
(defun trace-runtest-sentinel (proc state)
(setq trace-done t))
(defun trace-make-process-buffer (name)
(let ((buf (get-buffer-create name)))
(set-buffer buf)
(make-local-variable 'trace-last-match)
(setq trace-last-match (copy-marker (point-min-marker))))
(defun usage ()
(message "\
Usage: emacs -batch -l handle-malloc-dump.el --test arg [options]
--help Show this help message
--tool arg Use arg as --tool argument for runtest (optional)
--test arg Run the test case arg with runtest (REQUIRED)
--output arg Append the results to file arg (optional)
(defun trace-memory ()
(let ((tool-to-test "lyskomd")
(test-to-run nil)
(output-file nil)
(arg nil)
(done nil)
(result nil))
(while (and command-line-args-left (not done))
(setq arg (car command-line-args-left))
(cond ((string= arg "--usage")
(setq command-line-args-left (cdr command-line-args-left))
(setq tool-to-test nil)
(setq done t)
((string= arg "--tool")
(setq tool-to-test (car (cdr command-line-args-left)))
(setq command-line-args-left (cdr (cdr command-line-args-left))))
((string= arg "--test")
(setq test-to-run (car (cdr command-line-args-left)))
(setq command-line-args-left (cdr (cdr command-line-args-left))))
((string= arg "--output")
(setq output-file (car (cdr command-line-args-left)))
(setq command-line-args-left (cdr (cdr command-line-args-left))))
(t (setq done t))))
(if tool-to-test
(progn (if (null test-to-run)
(setq result (trace-run-programs tool-to-test test-to-run))
(cond ((null output-file) (message "%s" result))
(let ((tmp (get-buffer-create "*tmp*")))
(set-buffer tmp)
(insert result)
(append-to-file (point-min)
(defun trace-run-programs (tool-to-test test-to-run)
(setq lyskomd-pid nil)
(setq gdb-tty nil)
(setq trace-done nil)
(let ((gdb-buffer (trace-make-process-buffer "*gdb*"))
(runtest-buffer (trace-make-process-buffer "*runtest*")))
(setq gdb-proc (start-process "gdb" gdb-buffer "gdb" "../lyskomd"))
(setq gdb-buffer (get-buffer-create "*gdb*"))
(set-process-filter gdb-proc 'trace-process-get-tty)
(process-send-string gdb-proc "shell echo `tty`\n")
(while (null gdb-tty) (accept-process-output gdb-proc))
(setq runtest-proc (start-process "runtest"
(format "--tool=%s" tool-to-test)
(format "MEMTRACE=%s" gdb-tty)))
(set-process-filter runtest-proc 'trace-runtest-filter)
(set-process-sentinel runtest-proc 'trace-runtest-sentinel)
(while (null lyskomd-pid) (accept-process-output runtest-proc))
(process-send-string gdb-proc "source ../trace-mem.gdb\n")
(process-send-string gdb-proc (format "attach %s\n" lyskomd-pid))
(process-send-string gdb-proc "continue\n")
(process-send-string runtest-proc "\n")
(while (null trace-done) (accept-process-output))
(set-buffer gdb-buffer)
(message "Resolving trace...")
(set-buffer "*Result*")
;; If running in batch mode, start things right away
(if noninteractive
(progn (message "Automatic lyskomd memory trace analysis")
* $Id: ram-smalloc.c,v 0.30 1999/05/24 09:34:35 ceder Exp $
* $Id: ram-smalloc.c,v 0.31 1999/07/02 14:50:37 byers Exp $
* Copyright (C) 1991-1996, 1998-1999 Lysator Academic Computer Association.
* This file is part of the LysKOM server.
......@@ -71,13 +71,9 @@
* (gdb) source trace-mem.gdb
* (gdb) shell tty
* /dev/ttypd
* Insert the following two statements in the *.exp script before the
* call to lyskomd_start that you want to trace:
* set attach 1
* set MEMTRACE /dev/ttypd
* MEMTRACE should be set to the tty where gdb is running. Start the
* test suite:
* bash$ runtest --tool lyskomd leaks.0/99.exp
* Pass ATTACH=yes and MEMTRACE to runtest. MEMTRACE should be set
* to the tty where gdb is running. Start the test suite:
* bash$ runtest --tool lyskomd leaks.0/99.exp ATTACH=yes MEMTRACE=/dev/ttypd
* [...]
* Please attach to lyskomd pid 4711 and hit RETURN
* Attach to the process:
......@@ -86,10 +82,21 @@
* Press enter to resume the test suite. Lots of output should appear
* from gdb.
* You can do all this automatically for test cases that succeed in
* starting the server and only start it once.
* emacs -batch -l handle-malloc-dump.el --tool leaks --test 99.exp
* This will do the above in an Emacs, then run the trace analysis
* and print the results on standard output. Use --usage to get a
* summary of available options (--help was already taken by Emacs.)
* This does not work with all Emacs versions.
static const char *
rcsid = "$Id: ram-smalloc.c,v 0.30 1999/05/24 09:34:35 ceder Exp $";
rcsid = "$Id: ram-smalloc.c,v 0.31 1999/07/02 14:50:37 byers Exp $";
#include "rcs.h"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment