public inbox for frysk@sourceware.org
 help / color / mirror / Atom feed
* [RFC] initial support for watchpoints in frysk
@ 2008-05-14  7:50 Thiago Jung Bauermann
  2008-05-14 17:31 ` Phil Muldoon
  0 siblings, 1 reply; 4+ messages in thread
From: Thiago Jung Bauermann @ 2008-05-14  7:50 UTC (permalink / raw)
  To: frysk ml

Hi,

Since fhpd is complaining rather loudly about not supporting watchpoints
in Power, I decided to add such support. :-)

This patch has an initial implementation, but it doesn't work yet (more
about it later). I'd like to ask your opinion, especially regarding two
methods I had to add to frysk.sys.Ptrace and
frysk.proc.live.LinuxPtraceTask: setDebugReg and getDebugReg. They are
necessary because on the PowerPC architecture, debug registers are a
priviledged resource, and userland need to access them via
PTRACE_SET_DEBUGREG and PTRACE_GET_DEBUGREG. What do you think, are
these changes ok?

Now regarding why the patch doesn't work: in order to verify if the
process stopped because of the hardware watchpoint, I need to examine
the siginfo structure which comes with the SIGTRAP. This information is
not available to Frysk yet, so I'd like your opinion on the following
approach:

A new method would be added to frysk.sys.Signal to get the siginfo
(cleverly called getSigInfo). It would return a SigInfo class with
methods for each member of the siginfo struct that would be of interst
to frysk (for my purposes, the si_addr member). The method
frysk.sys.Poll.poll would be responsible for creating and the SigInfo
object and setting the member values.

If my understanding is correct, this would make the siginfo available to
LinuxPtraceTaskState.Running.handleStoppedEvent, and from there I just
have to make it reach all the way down to
WatchpointFunctions.hasWatchpointTriggered. How does this sound?

Follows the patch. I didn't write a ChangeLog entry yet, will do when I
commit it.
-- 
[]'s
Thiago Jung Bauermann
Software Engineer
IBM Linux Technology Center

diff --git a/frysk-core/frysk/isa/watchpoints/PPCWatchpointFunctions.java b/frysk-core/frysk/isa/watchpoints/PPCWatchpointFunctions.java
new file mode 100644
index 0000000..2a57010
--- /dev/null
+++ b/frysk-core/frysk/isa/watchpoints/PPCWatchpointFunctions.java
@@ -0,0 +1,184 @@
+// This file is part of the program FRYSK.
+//
+// Copyright 2008, Red Hat Inc.
+//
+// FRYSK is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// FRYSK is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with FRYSK; if not, write to the Free Software Foundation,
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+// 
+// In addition, as a special exception, Red Hat, Inc. gives You the
+// additional right to link the code of FRYSK with code not covered
+// under the GNU General Public License ("Non-GPL Code") and to
+// distribute linked combinations including the two, subject to the
+// limitations in this paragraph. Non-GPL Code permitted under this
+// exception must only link to the code of FRYSK through those well
+// defined interfaces identified in the file named EXCEPTION found in
+// the source code files (the "Approved Interfaces"). The files of
+// Non-GPL Code may instantiate templates or use macros or inline
+// functions from the Approved Interfaces without causing the
+// resulting work to be covered by the GNU General Public
+// License. Only Red Hat, Inc. may make changes or additions to the
+// list of Approved Interfaces. You must obey the GNU General Public
+// License in all respects for all of the FRYSK code and other code
+// used in conjunction with FRYSK except the Non-GPL Code covered by
+// this exception. If you modify this file, you may extend this
+// exception to your version of the file, but you are not obligated to
+// do so. If you do not wish to provide this exception without
+// modification, you must delete this exception statement from your
+// version and license this file solely under the GPL without
+// exception.
+
+package frysk.isa.watchpoints;
+
+import frysk.proc.Task;
+import frysk.proc.live.LinuxPtraceTask;
+
+class PPCWatchpointFunctions extends WatchpointFunctions {
+
+    // Architecture Watchpoint Count. Number of usable
+    // Address-Breakpoint Registers: only one, DABR.
+    public PPCWatchpointFunctions () {
+	noOfWatchpoints = 1;
+    }
+   
+    /**
+     * Builds and sets a hardware watchpoint on a task.
+     *
+     * @param task - task to set a watchpoint on.
+     * @param index - watchpoint number to write. 4 on
+     * x8664
+     * @param addr - linear virtual address to watch.
+     * @param range - length of range to watch. Normally
+     * 1,24, or 8 bytes.
+     * @param writeOnly - When true, only trigger when address is
+     * written. False, trigger when address is read or written to.
+     */
+    public void setWatchpoint(Task task, int index, 
+	       long addr, int range,
+	       boolean writeOnly) {
+
+	// turn bit off (b = bit no): l &= ~(1L << b)
+	// turn bit on ( b= bit no):  l |= (1L << b);
+
+	if (index != 0) {
+	    throw new RuntimeException("Invalid hardware watchpoint index." +
+	       "Has to be 0.");
+	}
+
+	if (range != 8) {
+	    throw new RuntimeException("Invalid size for watchpoint " +
+	       "range. Has to be 8.");
+	}
+	    
+	if ((addr &~ 7) != addr)
+	    throw new RuntimeException("Address 0x"+Long.toHexString(addr) + 
+		    " is not aligned on an 8 byte boundary. Cannot set watchpoint.");
+
+	if (writeOnly)
+	    // set translation and write bits
+	    addr |= 6;
+	else
+	    // set translation and read bits
+	    addr |= 5;
+
+	// Set the debug register on all threads.
+	((LinuxPtraceTask) task).setDebugReg(0, addr);
+    }
+
+    /**
+     * Reads a watchpoint. Takes a task, and an index.
+     *
+     * @param task - task to read a watchpoint from.
+     * @param index - watchpoint number to read.
+
+     * @return Watchpoint - value of Watchpoint at
+     * register. 
+     */
+    public Watchpoint readWatchpoint(Task task, int index) {
+
+	if (index != 0) {
+	    throw new RuntimeException("Invalid hardware watchpoint index." +
+	       "Has to be 0.");
+	}
+
+	// Get Address from watchpoint register
+	long dabr_value = ((LinuxPtraceTask) task).getDebugReg(0);
+	long address = dabr_value & ~7;
+	boolean writeOnly = testBit(address, 0) && !testBit(address, 1);
+
+	return Watchpoint.create(address, 8, index, writeOnly);
+    }	
+
+    /**
+     * Deletes a watchpoint. Takes a task, and an index.
+     *
+     * @param task - task on which to delete a watchpoint.
+     * @param index - watchpoint number to delete.
+     *
+     * @return long - value of register for wp
+     */
+    public final void deleteWatchpoint(Task task, int index) {
+	((LinuxPtraceTask) task).setDebugReg(0, 0);
+    }
+    
+    /**
+     * Reads the Debug control register.
+     *
+     * @param task - task to read the debug control
+     * register from.
+     */
+    protected long readControlRegister(Task task) {
+	throw new RuntimeException("There is no debug control register in PowerPC.");
+    }
+
+    /**
+     * Reads the Debug cstatus register.
+     *
+     * @param task - task to read the debug status
+     * register from.
+     */
+    protected long readStatusRegister(Task task) {
+	throw new RuntimeException("There is no debug status register in PowerPC.");
+    }
+
+    /**
+     * Reads the Debug Status Register and checks if 
+     * the breakpoint specified has fired.
+     *
+     * @param task - task to read the debug control
+     * register from.
+     */
+    public boolean hasWatchpointTriggered(Task task, int index) {
+	// FIXME: in PowerPC, the address which triggered the watchpoint is
+	// sent in the siginfo for the SIGTRAP.  There's no way to grab it
+	// in Frysk yet.
+	throw new RuntimeException("Watchpoints not fully supported in PowerPC yet.");
+    }
+
+    /**
+     * Resets the appropriate bit in the debug status register
+     * after a watchpoint has triggered, thereby reseting it.
+     *
+     * @param task - task to read the debug control
+     * register from.
+     * @param index - Debug register to reset.
+     */
+    public void resetWatchpoint(Task task, int index) {
+	// FIXME: AFAIK, this can be only a NOP for PowerPC. There's no
+	// need to reset a watchpoint after it triggers.
+    }
+
+    private boolean testBit(long register, int bitToTest) {
+	return (register & (1L << bitToTest)) != 0;
+    }
+
+}
diff --git a/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java b/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java
index da434ce..f16615a 100644
--- a/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java
+++ b/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java
@@ -50,6 +50,8 @@ public class WatchpointFunctionFactory {
     private static final ISAMap watchpointTables = new ISAMap("watchpoint table")
 	.put(ISA.IA32, new IA32WatchpointFunctions())
 	.put(ISA.X8664, new X8664WatchpointFunctions())
+	.put(ISA.PPC32BE, new PPCWatchpointFunctions())
+	.put(ISA.PPC64BE, new PPCWatchpointFunctions())
 	;
 
     public static WatchpointFunctions getWatchpointFunctions(ISA isa) {
diff --git a/frysk-core/frysk/proc/live/LinuxPtraceTask.java b/frysk-core/frysk/proc/live/LinuxPtraceTask.java
index 0ecadb3..f1f04c0 100644
--- a/frysk-core/frysk/proc/live/LinuxPtraceTask.java
+++ b/frysk-core/frysk/proc/live/LinuxPtraceTask.java
@@ -380,6 +380,14 @@ public class LinuxPtraceTask extends LiveTask {
 	}
     }
 
+    public void setDebugReg(int reg, long value) {
+	Ptrace.setDebugReg(tid, reg, value);
+    }
+
+    public long getDebugReg(int reg) {
+	return Ptrace.getDebugReg(tid, reg);
+    }
+
     // XXX: Should be selecting the trace flags based on the contents
     // of .observers?  Ptrace.optionTraceSysgood not set by default
     private long ptraceOptions = (Ptrace.OPTION_CLONE
diff --git a/frysk-sys/frysk/sys/ptrace/Ptrace.java b/frysk-sys/frysk/sys/ptrace/Ptrace.java
index aa5b3b8..ea80906 100644
--- a/frysk-sys/frysk/sys/ptrace/Ptrace.java
+++ b/frysk-sys/frysk/sys/ptrace/Ptrace.java
@@ -111,6 +111,29 @@ public class Ptrace {
     private static native long getEventMsg(int pid);
 
     /**
+     * Set PID's debug register number REG.
+     */
+    public static void setDebugReg(ProcessIdentifier pid, int reg, long value) {
+	fine.log("setDebugReg", pid, "...");
+	setDebugReg(pid.intValue(), reg, value);
+	fine.log("... setDebugReg", pid, "returns.");
+    }
+    private static native void setDebugReg(int pid, int reg, long value);
+
+    /**
+     * Get the value of PID's debug register number REG.
+     *
+     * @return value of debug register
+     */
+    public static long getDebugReg(ProcessIdentifier pid, int reg) {
+	fine.log("getDebugReg", pid, "...");
+	long ret = getDebugReg(pid.intValue(), reg);
+	fine.log("... getDebugReg", pid, "returns", ret);
+	return ret;
+    }
+    private static native long getDebugReg(int pid, int reg);
+
+    /**
      * Set PID's trace options.  OPTIONS is formed by or'ing the
      * values returned by the option* methods below.
      */
diff --git a/frysk-sys/frysk/sys/ptrace/cni/Ptrace.cxx b/frysk-sys/frysk/sys/ptrace/cni/Ptrace.cxx
index 4aa12eb..9ea6c13 100644
--- a/frysk-sys/frysk/sys/ptrace/cni/Ptrace.cxx
+++ b/frysk-sys/frysk/sys/ptrace/cni/Ptrace.cxx
@@ -125,6 +125,20 @@ frysk::sys::ptrace::Ptrace::getEventMsg(jint pid) {
   ptraceOp(PTRACE_GETEVENTMSG, pid, NULL, (long) &msg);
   return msg;
 }
+
+void
+frysk::sys::ptrace::Ptrace::setDebugReg(jint pid, jint reg, jlong value) {
+  ptraceOp(PTRACE_SET_DEBUGREG, pid, (void *) reg, value);
+}
+
+jlong
+frysk::sys::ptrace::Ptrace::getDebugReg(jint pid, jint reg) {
+  jlong value;
+
+  ptraceOp(PTRACE_SET_DEBUGREG, pid, (void *) reg, (long) &value);
+
+  return value;
+}
 \f
 void
 frysk::sys::ptrace::Ptrace::setOptions(jint pid, jlong options) {


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-05-15  4:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-14  7:50 [RFC] initial support for watchpoints in frysk Thiago Jung Bauermann
2008-05-14 17:31 ` Phil Muldoon
2008-05-15  4:07   ` Thiago Jung Bauermann
2008-05-15 15:41     ` Thiago Jung Bauermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).