bluegeneKludges.C

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 #include <common/h/headers.h>
00032 
00033 // taken from LinuxKludges.C.
00034 // TODO: put this in some common place?  It's used by at least 2 platforms.
00035 char * P_cplus_demangle( const char * symbol, bool nativeCompiler, bool includeTypes ) 
00036 {
00037   int opts = 0;
00038   opts |= includeTypes ? DMGL_PARAMS | DMGL_ANSI : 0;
00039   opts |= nativeCompiler ? DMGL_ARM : 0;
00040   
00041   char * demangled = cplus_demangle( const_cast< char *>(symbol), opts);
00042   if( demangled == NULL ) { return NULL; }
00043   
00044   if( ! includeTypes ) {
00045     /* de-demangling never increases the length */   
00046     char * dedemangled = strdup( demangled );   
00047     assert( dedemangled != NULL );
00048     dedemangle( demangled, dedemangled );
00049     assert( dedemangled != NULL );
00050     
00051     free( demangled );
00052     return dedemangled;
00053   }
00054   
00055   return demangled;
00056 } /* end P_cplus_demangle() */
00057 
00058 
00059 #if !defined(os_bg_compute)
00060 #include "parseauxv.h"
00061 #include "auxvtypes.h"
00062 
00063 bool AuxvParser::readAuxvInfo() {
00064   assert(0); //
00065 }
00066 
00067 #define LINE_LEN 1024
00068 map_entries *getLinuxMaps(int pid, unsigned &maps_size) {
00069    char line[LINE_LEN], prems[16], *s;
00070    int result;
00071    int fd = -1;
00072    map_entries *maps = NULL;
00073    unsigned i, no_lines = 0, cur_pos = 0, cur_size = 4096;
00074    unsigned file_size = 0;
00075    char *buffer = NULL;
00076   
00077    sprintf(line, "/proc/%d/maps", pid);
00078    fd = open(line, O_RDONLY);
00079    if (fd == -1)
00080       goto done_err;
00081    
00082    cur_pos = 0;
00083    buffer = (char *) malloc(cur_size);
00084    if (!buffer) {
00085       goto done_err;
00086    }
00087    for (;;) {
00088       result = read(fd, buffer+cur_pos, cur_size - cur_pos);
00089       if (result == -1) {
00090          goto done_err;
00091       }
00092       cur_pos += result;
00093       if (result == 0) {
00094          break;
00095       }
00096       assert(cur_pos <= cur_size);
00097       if (cur_size == cur_pos) {
00098          cur_size *= 2;
00099          buffer = (char *) realloc(buffer, cur_size);
00100          if (!buffer) {
00101             goto done_err;
00102          }
00103       }
00104    }
00105    file_size = cur_pos;
00106 
00107    close(fd);
00108    fd = -1;
00109    //Calc num of entries needed and allocate the buffer.  Assume the 
00110    //process is stopped.
00111    no_lines = file_size ? 1 : 0;
00112    for (i = 0; i < file_size; i++) {
00113       if (buffer[i] == '\n')
00114          no_lines++;
00115    } 
00116 
00117    maps = (map_entries *) malloc(sizeof(map_entries) * (no_lines+1));
00118    memset(maps, 0, sizeof(map_entries) * (no_lines+1));
00119    if (!maps)
00120       goto done_err;
00121 
00122    //Read all of the maps entries
00123    cur_pos = 0;
00124    for (i = 0; i < no_lines; i++) {
00125       if (cur_pos >= file_size)
00126          break;
00127       unsigned next_end = cur_pos;
00128       while (buffer[next_end] != '\n' && next_end < file_size) next_end++;
00129       unsigned int line_size = (next_end - cur_pos) > LINE_LEN ? LINE_LEN : (next_end - cur_pos);
00130       memcpy(line, buffer+cur_pos, line_size);
00131       line[line_size] = '\0';
00132       line[LINE_LEN - 1] = '\0';
00133       cur_pos = next_end+1;
00134 
00135       sscanf(line, "%lx-%lx %16s %lx %x:%x %u %" MAPENTRIES_PATH_SIZE_STR "s\n", 
00136              (Address *) &maps[i].start, (Address *) &maps[i].end, prems, 
00137              (Address *) &maps[i].offset, &maps[i].dev_major,
00138              &maps[i].dev_minor, &maps[i].inode, maps[i].path);
00139       maps[i].prems = 0;
00140       for (s = prems; *s != '\0'; s++) {
00141          switch (*s) {
00142             case 'r':
00143                maps[i].prems |= PREMS_READ;
00144                break;
00145             case 'w':
00146                maps[i].prems |= PREMS_WRITE;
00147                break;
00148             case 'x':
00149                maps[i].prems |= PREMS_EXEC;
00150                break;
00151             case 'p':
00152                maps[i].prems |= PREMS_PRIVATE;
00153                break;
00154             case 's':
00155                maps[i].prems |= PREMS_EXEC;
00156                break;
00157          }
00158       }
00159    }
00160    //Zero out the last entry
00161    memset(&(maps[i]), 0, sizeof(map_entries));
00162    maps_size = i;
00163 
00164    free(buffer);
00165    return maps;
00166 
00167  done_err:
00168    if (fd != -1)
00169       close(fd);
00170    if (buffer)
00171       free(buffer);
00172    return NULL;
00173 }
00174 
00175 
00176 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1