001    // This file is part of the program FRYSK.
002    //
003    // Copyright 2005, 2007, IBM Inc.
004    // Copyright 2007, Red Hat, Inc.
005    //
006    // FRYSK is free software; you can redistribute it and/or modify it
007    // under the terms of the GNU General Public License as published by
008    // the Free Software Foundation; version 2 of the License.
009    //
010    // FRYSK is distributed in the hope that it will be useful, but
011    // WITHOUT ANY WARRANTY; without even the implied warranty of
012    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013    // General Public License for more details.
014    // 
015    // You should have received a copy of the GNU General Public License
016    // along with FRYSK; if not, write to the Free Software Foundation,
017    // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
018    // 
019    // In addition, as a special exception, Red Hat, Inc. gives You the
020    // additional right to link the code of FRYSK with code not covered
021    // under the GNU General Public License ("Non-GPL Code") and to
022    // distribute linked combinations including the two, subject to the
023    // limitations in this paragraph. Non-GPL Code permitted under this
024    // exception must only link to the code of FRYSK through those well
025    // defined interfaces identified in the file named EXCEPTION found in
026    // the source code files (the "Approved Interfaces"). The files of
027    // Non-GPL Code may instantiate templates or use macros or inline
028    // functions from the Approved Interfaces without causing the
029    // resulting work to be covered by the GNU General Public
030    // License. Only Red Hat, Inc. may make changes or additions to the
031    // list of Approved Interfaces. You must obey the GNU General Public
032    // License in all respects for all of the FRYSK code and other code
033    // used in conjunction with FRYSK except the Non-GPL Code covered by
034    // this exception. If you modify this file, you may extend this
035    // exception to your version of the file, but you are not obligated to
036    // do so. If you do not wish to provide this exception without
037    // modification, you must delete this exception statement from your
038    // version and license this file solely under the GPL without
039    // exception.
040    
041    package lib.dwfl;
042    
043    import inua.eio.ArrayByteBuffer;
044    import inua.eio.ByteBuffer;
045    
046    public class ElfPrpsinfo extends ElfNhdr.ElfNoteSectionEntry
047    {
048      private char pr_state;
049      private char pr_sname;
050      private char pr_zomb;
051      private char pr_nice;
052      
053      private long pr_flag;
054      
055      // on most platform, pr_uid is unsigned int. 
056      // In java, "int" it sigend, so we have to use long type.
057      private long pr_uid;
058      private long pr_gid;
059      
060      private int pr_pid;
061      private int pr_ppid;
062      private int pr_pgrp;
063      private int pr_sid;
064      
065      //XXX: the following two value must keep the same with the elfutils package.
066      // -1 to allow for null terminator
067      public static int ELF_PRPSINFO_FNAME_MAXLEN = 16 - 1;
068      public static int ELF_PRPSINFO_ARGS_MAXLEN = 80 - 1;
069      
070      // filename of executable
071      private String  pr_fname;
072      private int size = 32;
073      
074      // initial part of arg list
075      private String pr_psargs;
076      
077      //private int pid;
078      
079      public ElfPrpsinfo(int size)
080      {
081        this.size = size;
082      }
083    
084        /** 
085         * Extract note information from a section
086         * containing note data
087         */
088        private ElfPrpsinfo(byte[] rawNoteData, Elf elf) {
089            ByteBuffer noteBuffer = new ArrayByteBuffer(rawNoteData);
090            ElfEHeader header = elf.getEHeader();
091            noteBuffer.order(header.getByteOrder());
092        
093        int machine = header.machine;
094        switch (machine)
095          {
096          case ElfEMachine.EM_386:
097          case ElfEMachine.EM_PPC:
098            noteBuffer.wordSize(4);
099            break;
100          case ElfEMachine.EM_X86_64:
101          case ElfEMachine.EM_PPC64:
102            noteBuffer.wordSize(8);
103            break;
104          default:
105            return;
106          }
107       
108        pr_state = (char) noteBuffer.getByte();
109        pr_sname = (char) noteBuffer.getByte();
110        pr_zomb = (char) noteBuffer.getByte();
111        pr_nice = (char) noteBuffer.getByte();
112    
113        // align to the next long
114        long cp = noteBuffer.position();
115        noteBuffer.position(cp+Math.abs(cp-noteBuffer.wordSize()));
116        pr_flag = noteBuffer.getUWord();
117    
118        // 16 bit integer used on i386 here
119        if (machine == ElfEMachine.EM_386)
120          {
121             pr_uid = noteBuffer.getShort();
122             pr_gid = noteBuffer.getShort();
123          }
124        else
125          {
126            pr_uid = noteBuffer.getInt();
127            pr_gid = noteBuffer.getInt();
128           }
129        
130        pr_pid = noteBuffer.getInt();
131        pr_ppid = noteBuffer.getInt();
132        pr_pgrp = noteBuffer.getInt();
133        pr_sid = noteBuffer.getInt();
134    
135        StringBuffer noteStringBuffer = new StringBuffer();
136        noteBuffer.get(noteBuffer.position(), 16, noteStringBuffer);
137        noteBuffer.position(noteBuffer.position()+16);
138        pr_fname = noteStringBuffer.toString();
139        noteBuffer.get(noteBuffer.position(), 80, noteStringBuffer);
140        pr_psargs = noteStringBuffer.toString();
141    
142      }
143    
144      public static ElfPrpsinfo decode(ElfData noteData)
145      {
146        final byte data[] = getNoteData(noteData);
147        ElfPrpsinfo processData = new ElfPrpsinfo(data,noteData.getParent());
148        return processData;
149      }
150    
151      public void setPrState(char state)
152      {
153        this.pr_state = state;
154      }
155      
156      public char getPrState()
157      {
158        return this.pr_state;
159      }
160      
161      public void setPrSname(char sname)
162      {
163        this.pr_sname = sname;
164      }
165      
166      public char getPrSname()
167      {
168        return this.pr_sname;
169      }
170      
171      public void setPrZomb(char zomb)
172      {
173        this.pr_zomb = zomb;
174      }
175      
176      public char getPrZomb()
177      {
178        return this.pr_zomb;
179      }
180      
181      public void setPrNice(char nice)
182      {
183        this.pr_nice = nice;
184      }
185      
186      public char getPrNice()
187      {
188        return this.pr_nice;
189      }
190      
191      public void setPrFlag(long flag)
192      {
193        this.pr_flag = flag;
194      }
195      
196      public long getPrFlag()
197      {
198        return this.pr_flag;
199      }
200      
201      public void setPrUid(long uid)
202      {
203        this.pr_uid = uid;
204      }
205      
206      public long getPrUid()
207      {
208        return this.pr_uid;
209      }
210      
211      public void setPrGid(long gid)
212      {
213        this.pr_gid = gid;
214      }
215      
216      public long getPrGid()
217      {
218        return this.pr_gid;
219      }
220      
221      public void setPrPid(int pid)
222      { 
223        this.pr_pid = pid;
224      }
225      public int getPrPid()
226      {
227        return this.pr_pid;
228      }
229      
230      public void setPrPpid(int ppid)
231      {     
232    
233        this.pr_ppid = ppid;
234      }
235    
236      public int getPrPpid()
237      {
238        return this.pr_ppid;
239      }
240      
241      public void setPrPgrp(int pgrp)
242      {
243        this.pr_pgrp = pgrp;
244      }
245      
246      public int getPrPgrp()
247      {
248        return this.pr_pgrp;
249      }
250      
251      public void setPrSid(int sid)
252      {
253        this.pr_sid = sid;
254      }
255    
256      public int getPrSid()
257      {
258        return this.pr_sid;
259      }
260      
261      public void setPrFname(String fname)
262      {
263        if (fname == null)
264          return;
265        
266        int length = fname.length();
267        
268        if (length <ELF_PRPSINFO_FNAME_MAXLEN)
269          this.pr_fname = fname.substring(0,length);
270        else
271          this.pr_fname = fname.substring(0,ELF_PRPSINFO_FNAME_MAXLEN);
272      }
273    
274      public String getPrFname()
275      {
276        return this.pr_fname;
277      }
278    
279      public void setPrPsargs(String args)
280      {
281        if (args == null)
282          return;
283    
284        int length = args.length();
285        if (length < ELF_PRPSINFO_ARGS_MAXLEN)
286          this.pr_psargs = args.substring(0,length);
287        else
288          this.pr_psargs = args.substring(0, ELF_PRPSINFO_ARGS_MAXLEN);
289      }
290    
291      public String getPrPsargs()
292      {
293        return this.pr_psargs;
294      }
295    
296      public int getSize()
297      {
298        return this.size;
299      }
300     
301      public native static byte[] getNoteData(ElfData data);
302      public native long getEntrySize();
303      public native long fillMemRegion(byte[] buffer, long startAddress);
304    }