debug.cc revision 2665
12SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292665Ssaidi@eecs.umich.edu *          Steve Reinhardt
302SN/A */
312SN/A
322SN/A#include <sys/types.h>
332SN/A#include <signal.h>
342SN/A#include <unistd.h>
352SN/A
362SN/A#include <string>
372SN/A#include <vector>
382SN/A
3956SN/A#include "sim/debug.hh"
4056SN/A#include "sim/eventq.hh"
4156SN/A#include "sim/param.hh"
4256SN/A#include "sim/sim_events.hh"
432SN/A
442SN/Ausing namespace std;
452SN/A
462SN/Avoid
472SN/Adebug_break()
482SN/A{
491252SN/A#ifndef NDEBUG
502SN/A    kill(getpid(), SIGTRAP);
511252SN/A#else
521252SN/A    cprintf("debug_break suppressed, compiled with NDEBUG\n");
531252SN/A#endif
542SN/A}
552SN/A
562SN/A//
572SN/A// Debug event: place a breakpoint on the process function and
582SN/A// schedule the event to break at a particular cycle
592SN/A//
602SN/Aclass DebugBreakEvent : public Event
612SN/A{
622SN/A  public:
632SN/A
642SN/A    DebugBreakEvent(EventQueue *q, Tick _when);
652SN/A
662SN/A    void process();	// process event
672SN/A    virtual const char *description();
682SN/A};
692SN/A
702SN/A//
712SN/A// constructor: schedule at specified time
722SN/A//
732SN/ADebugBreakEvent::DebugBreakEvent(EventQueue *q, Tick _when)
74396SN/A    : Event(q, Debug_Break_Pri)
752SN/A{
76381SN/A    setFlags(AutoDelete);
77396SN/A    schedule(_when);
782SN/A}
792SN/A
802SN/A//
812SN/A// handle debug event: set debugger breakpoint on this function
822SN/A//
832SN/Avoid
842SN/ADebugBreakEvent::process()
852SN/A{
862SN/A    debug_break();
872SN/A}
882SN/A
892SN/A
902SN/Aconst char *
912SN/ADebugBreakEvent::description()
922SN/A{
932SN/A    return "debug break";
942SN/A}
952SN/A
962SN/A//
972SN/A// Parameter context for global debug options
982SN/A//
992SN/Aclass DebugContext : public ParamContext
1002SN/A{
1012SN/A  public:
1022SN/A    DebugContext(const string &_iniSection)
1032SN/A        : ParamContext(_iniSection) {}
1042SN/A    void checkParams();
1052SN/A};
1062SN/A
1072SN/ADebugContext debugParams("debug");
1082SN/A
1092SN/AVectorParam<Tick> break_cycles(&debugParams, "break_cycles",
1102SN/A                                 "cycle(s) to create breakpoint events");
1112SN/A
1122SN/Avoid
1132SN/ADebugContext::checkParams()
1142SN/A{
1152SN/A    if (break_cycles.isValid()) {
1162SN/A        vector<Tick> &cycles = break_cycles;
1172SN/A
1182SN/A        vector<Tick>::iterator i = cycles.begin();
1192SN/A        vector<Tick>::iterator end = cycles.end();
1202SN/A
1212SN/A        for (; i < end; ++i)
1222SN/A            new DebugBreakEvent(&mainEventQueue, *i);
1232SN/A    }
1242SN/A}
1252SN/A
1262SN/A//
1272SN/A// handy function to schedule DebugBreakEvent on main event queue
1282SN/A// (callable from debugger)
1292SN/A//
1302SN/Aextern "C" void sched_break_cycle(Tick when)
1312SN/A{
1322SN/A    new DebugBreakEvent(&mainEventQueue, when);
1332SN/A}
1342SN/A
1352SN/Aextern "C" void eventq_dump()
1362SN/A{
1372SN/A    mainEventQueue.dump();
1382SN/A}
1392SN/A
140