1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 /**
20  * @author Vitaly A. Provodin
21  */
22 
23 /**
24  * Created on 31.01.2005
25  */
26 package org.apache.harmony.jpda.tests.share;
27 
28 import java.io.FileReader;
29 import java.io.StreamTokenizer;
30 
31 /**
32  * The class extends <code>Debuggee</code> and adds usage of the
33  * synchronization channel implemented by <code>JPDADebuggeeSynchronizer</code>.
34  */
35 public abstract class SyncDebuggee extends Debuggee {
36 
37     /**
38      * An instance of JPDA debugger-debuggee synchronizer.
39      */
40     public JPDADebuggeeSynchronizer synchronizer;
41 
getPid()42     public String getPid() {
43         try {
44           StreamTokenizer toks = new StreamTokenizer(new FileReader("/proc/self/stat"));
45           toks.parseNumbers();
46           if (toks.nextToken() != StreamTokenizer.TT_NUMBER) {
47             System.out.println("Failed to tokenize /proc/self/stat correctly. " +
48                                "First token isn't a number");
49             return "-1";
50           }
51           return Integer.toString((int)toks.nval);
52         } catch (Exception e) {
53             System.out.println("Failed to get pid! " + e);
54             e.printStackTrace(System.out);
55             return "-1";
56         }
57     }
58 
59     /**
60      * Initializes the synchronization channel.
61      */
62     @Override
onStart()63     public void onStart() {
64         super.onStart();
65         synchronizer = createSynchronizer();
66         synchronizer.startClient();
67         synchronizer.sendMessage(getPid());
68     }
69 
70     /**
71      * Creates wrapper for synchronization channel.
72      */
createSynchronizer()73     protected JPDADebuggeeSynchronizer createSynchronizer() {
74         return new JPDADebuggeeSynchronizer(logWriter, settings);
75     }
76 
77     /**
78      * Terminates the synchronization channel.
79      */
80     @Override
onFinish()81     public void onFinish() {
82         if (synchronizer != null) {
83             synchronizer.stop();
84         }
85         super.onFinish();
86     }
87 }
88