Просмотр исходного кода

Added ptrace -- does not work, however (ptrace attach --> no such process)

git-svn-id: https://svn.inf.ethz.ch/svn/lecturers/a2/trunk@6918 8c9fc860-2736-0410-a75d-ab315db34111
felixf 9 лет назад
Родитель
Сommit
e36dad2c4e

+ 1 - 2
source/Generic.Linux.I386.Glue.Mod

@@ -217,9 +217,8 @@ SystemTools.DoCommands
 		Runtime Trace Glue Unix Machine Heaps Modules Objects Kernel KernelLog 
 Streams Commands StdIO TrapWriters Traps 
 Files UnixFiles Clock Dates Reals Strings Diagnostics 
-BitSets StringPool ObjectFile GenericLinker Reflection  GenericLoader  BootConsole 
+BitSets StringPool ObjectFile GenericLinker Reflection  GenericLoader  Test BootConsole  
  ~
-~
 
 	FSTools.CloseFiles simple_elf ~
 		~ 

+ 49 - 3
source/Generic.Linux.I386.Unix.Mod

@@ -244,6 +244,21 @@ CONST
 	HUPCL*		= {10};
 	CLOCAL*	= {11};
 	
+	PTRACE_TRACEME     =        0;
+PTRACE_PEEKTEXT    =        1;
+PTRACE_PEEKDATA     =       2;
+PTRACE_PEEKUSR     =        3;
+PTRACE_POKETEXT    =        4;
+PTRACE_POKEDATA    =        5;
+PTRACE_POKEUSR    =         6;
+PTRACE_CONT       =         7;
+PTRACE_KILL           =     8;
+PTRACE_SINGLESTEP    =      9;
+PTRACE_ATTACH         =    16;
+PTRACE_DETACH           =  17;
+	PTRACE_GETREGS  = 12;
+	PTRACE_SETREGS    = 13;
+
 TYPE
 	Termios* = RECORD
 		iflags*, oflags*, cflags*, lflags*: SET;
@@ -364,7 +379,15 @@ TYPE
 		reserved1-: ARRAY 5 OF ADDRESS;
 		reserved2-: ARRAY 4 OF WORD;
 	END;
-
+	
+	PtraceRegsStruct = RECORD
+		ebx, ecx, edx, esi, edi, ebp, eax: WORD;
+		DS, ES, FS, GS, AX, IP, CS: WORD;
+		flags: SET; 
+		esp: WORD;
+		ss: WORD;
+	END;
+	
 VAR
 	argc-: LONGINT;  argv-: ADDRESS;
 	aargc: ADDRESS;
@@ -374,6 +397,8 @@ VAR
 			END;
 			
 	sigstack-: ARRAY 32*4096 OF CHAR;
+	
+	ptrace: PROCEDURE {C} (request: WORD; pid: Thread_t; adr, data: ADDRESS): WORD;
 
 	pthread_mutex_init: PROCEDURE {C} (mutex: ADDRESS; mutexattr: ADDRESS): WORD;
 	pthread_mutex_destroy: PROCEDURE {C} (mutex: ADDRESS): WORD;
@@ -627,7 +652,9 @@ VAR
 	    sigdelset( ADDRESS OF new, SIGSEGV );
 	    sigdelset( ADDRESS OF new, SIGTERM );
 	    sigdelset( ADDRESS OF new, T_SIGSUSPEND );
-	    
+	    res := ptrace(PTRACE_TRACEME, 0,0,0);
+	    TRACE(res);
+
 	    res := pthread_sigmask( SIG_SETMASK, ADDRESS OF new, ADDRESS OF old );
 	    res := pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NIL );
 	    res := pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NIL );
@@ -650,7 +677,8 @@ VAR
 	BEGIN
 		pthread_attr_init(ADDRESS OF attr);
 		pthread_attr_setscope(ADDRESS OF attr, PTHREAD_SCOPE_SYSTEM);
-		pthread_attr_setdetachstate(ADDRESS OF attr, PTHREAD_CREATE_DETACHED);
+		(*pthread_attr_setdetachstate(ADDRESS OF attr, PTHREAD_CREATE_DETACHED);*)
+		pthread_attr_setdetachstate(ADDRESS OF attr, 0);
 		pthread_attr_setstacksize(ADDRESS OF attr, stackSize);
 		res := pthread_create(ADDRESS OF id, ADDRESS OF attr, Starter, p);
 		RETURN id;
@@ -682,6 +710,22 @@ VAR
 	        END
 	    END;  
     END ThrKill;
+    
+    PROCEDURE TraceThread*(p: Thread_t);
+    VAR regs: PtraceRegsStruct; res: WORD;
+    BEGIN
+    	TRACE(p, pthread_self());
+    	res := ptrace(PTRACE_ATTACH, p , NIL, NIL); 
+    	IF res < 0 THEN Perror("attach") END; 
+    	TRACE(res);
+    	res := ptrace(PTRACE_GETREGS,p,NIL, ADDRESS OF regs);
+    	IF res < 0 THEN Perror("getregs") END; 
+    	TRACE(res); 
+    	TRACE(regs.ebx, regs.ecx, regs.edx, regs.eax); 
+    	TRACE(regs.ebp, regs.esp, regs.IP, regs.flags);
+    	res := ptrace(PTRACE_DETACH, p, NIL, NIL);
+    END TraceThread;
+    
 	
 	
 
@@ -1000,6 +1044,8 @@ static void sighandler( int sig, siginfo_t *scp, void *ucp ) {
 		libp := Dlopen( libpthreadname, 2); 
 		
 		Dlsym( libc, "exit",		ADDRESSOF( exit ) );
+		
+		Dlsym( libc, "ptrace", ADDRESSOF(ptrace));
 
 		Dlsym( libc, "pthread_mutex_init", ADDRESSOF(pthread_mutex_init));
 		Dlsym( libc, "pthread_mutex_destroy", ADDRESSOF(pthread_mutex_destroy));

+ 17 - 0
source/Generic.Unix.Objects.Mod

@@ -649,6 +649,23 @@ TYPE
 		END;
 	END ResumeActivities;
 	
+	PROCEDURE TraceAll*;
+	VAR t, me: Process;  
+	BEGIN
+		me:= CurrentProcess();
+		t := root;
+		WHILE t # NIL DO
+			(*IF (t # mainProcess) & (t # finCaller) &(t # me) THEN  
+				Unix.ThrSuspend( t.threadId )  
+			END;
+			*)
+			Unix.TraceThread(t.threadId);
+			t := t.nextProcess
+		END;
+		ResumeActivities;
+	END TraceAll;
+	
+	
 
 	PROCEDURE SaveSP;   (* save current SP for usage by the GC *)
 	VAR me: Unix.Thread_t;  t: Process;