osfKludges.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 // $Id: osfKludges.C,v 1.13 2007/05/30 19:20:27 legendre Exp $
00032 
00033 #include "common/h/headers.h"
00034 #include <sys/procfs.h>
00035 
00036 extern "C" {
00037 //extern int accept(int, struct sockaddr *addr, int *);
00038 //extern int accept(int, struct sockaddr *addr, size_t *);
00039 //extern void bzero(char *b, int length);
00040 extern int gethostname(char*, int);
00041 extern int getrusage(int, struct rusage*);
00042 //extern int getsockname(int, struct sockaddr*, int *);
00043 //extern int getsockname(int, struct sockaddr*, size_t *);
00044 
00045 extern int listen(int, int);
00046 extern int rexec(char **ahost, int inport, char *user, char *passwd,
00047          char *cmd, int *fd2p);
00048 extern int socket(int, int, int);
00049 extern int socketpair(int, int, int, int sv[2]);
00050 extern int pipe(int fd[2]);
00051 extern int vfork();
00052 extern int fork();
00053 extern int strcasecmp(const char *s1, const char *s2);
00054 extern int strncasecmp(const char *s1, const char *s2, long unsigned int  n);
00055 };
00056 
00057 /* Non standard (even from sunos4 to sunos5 -- blech */
00058 
00059 /* POSIX */
00060 void P_abort() { abort();}
00061 int P_close (int FILEDES) { return (close(FILEDES));}
00062 int P_dup2 (int OLD, int NEW) { return (dup2(OLD, NEW));}
00063 // int P_execlp (const char *FILENAME, const char *ARG0) {
00064 //  return (execlp(FILENAME, ARG0, NULL));}
00065 int P_execvp (const char *FILENAME, char *const ARGV[]) {
00066   return (execvp(FILENAME, ARGV));}
00067 int P_execve (const char* FILENAME, char* const ARGV[], char* const ENVP[]) {
00068     return (execve(FILENAME, ARGV, ENVP));
00069 }
00070 void P__exit (int STATUS) { _exit(STATUS);}
00071 int P_fcntl (int filedes, int command, int arg2) {
00072   return (fcntl(filedes, command, arg2));}
00073 FILE * P_fdopen (int FILEDES, const char *OPENTYPE) {
00074   return (fdopen(FILEDES, OPENTYPE));}
00075 FILE * P_fopen (const char *FILENAME, const char *OPENTYPE) {
00076   return (fopen(FILENAME, OPENTYPE));}
00077 int P_fstat (int FILEDES, struct stat *BUF) { return (fstat(FILEDES, BUF));}
00078 pid_t P_getpid () { return (getpid());}
00079 int P_kill(pid_t PID, int SIGNUM) { return (kill(PID, SIGNUM));}
00080 off_t P_lseek (int FILEDES, off_t OFFSET, int WHENCE) {
00081   prmap_t tmp;
00082   tmp.pr_vaddr = (char*)OFFSET; 
00083   off_t ret = lseek(FILEDES, (off_t) tmp.pr_vaddr, WHENCE);
00084   return ret;
00085 }
00086 int P_open(const char *FILENAME, int FLAGS, mode_t MODE) {
00087   return (open(FILENAME, FLAGS, MODE));}
00088 int P_pclose (FILE *STREAM) { return (pclose(STREAM));}
00089 FILE *P_popen (const char *COMMAND, const char *MODE) { return (popen(COMMAND, MODE));}
00090 size_t P_read (int FILEDES, void *BUFFER, size_t SIZE) {
00091   return (read(FILEDES, BUFFER, SIZE));}
00092 int P_uname(struct utsname *unm) { return (uname(unm));}
00093 pid_t P_wait(int *status_ptr) { return (wait(status_ptr));}
00094 pid_t P_waitpid(pid_t pid, int *statusp, int options) {
00095   return (waitpid(pid, statusp, options));}
00096 size_t P_write (int FILEDES, const void *BUFFER, size_t SIZE) {
00097   return (write(FILEDES, BUFFER, SIZE));}
00098 int P_chdir(const char *path) { return (chdir(path)); }
00099 
00100 int P_putenv(char *str) { return putenv(str); }
00101 
00102 /* SYSTEM-V shared memory */
00103 int P_shmget(key_t thekey, int size, int flags) { return shmget(thekey, size, flags); }
00104 void *P_shmat(int shmid, void *addr, int flags) { return shmat(shmid, addr, flags); }
00105 int P_shmdt(void *addr) { return shmdt(addr); }
00106 int P_shmctl(int shmid, int cmd, struct shmid_ds *buf) { return shmctl(shmid, cmd, buf); }
00107 
00108 /* ANSI */
00109 void P_exit (int STATUS) { exit(STATUS);}
00110 int P_fflush(FILE *stream) {return (fflush(stream));}
00111 char *P_fgets (char *S, int COUNT, FILE *STREAM) { return (fgets(S, COUNT, STREAM));}
00112 void * P_malloc (size_t SIZE) { return (malloc(SIZE));}
00113 void * P_memcpy (void *A1, const void *A2, size_t SIZE) {
00114   return (memcpy(A1, A2, SIZE));}
00115 void * P_memset (void *BLOCK, int C, unsigned SIZE) {
00116   return (memset(BLOCK, C, SIZE));}
00117 void P_perror (const char *MESSAGE) { perror(MESSAGE);}
00118 P_sig_handler P_signal (int SIGNUM, P_sig_handler ACTION) {
00119   return (signal(SIGNUM, ACTION));}
00120 char *P_strcat(char *TO, const char *FROM) { return (strcat(TO, FROM));}
00121 char *P_strchr(const char *S, int C) { return (strchr(S, C));}
00122 int P_strcmp(const char *S1, const char *S2) { return (strcmp(S1, S2));}
00123 char *P_strcpy(char *TO, const char *FROM) { return (strcpy(TO, FROM));}
00124 char *P_strdup(const char *FROM) { return (strdup(FROM));}
00125 size_t P_strlen (const char *S) { return (strlen(S));}
00126 char * P_strncat (char *TO, const char *FROM, size_t SIZE) {
00127   return (strncat(TO, FROM, SIZE));}
00128 int P_strncmp (const char *S1, const char *S2, size_t SIZE) {
00129   return (strncmp(S1, S2, SIZE));}
00130 char * P_strncpy (char *TO, const char *FROM, size_t SIZE) {
00131   return (strncpy(TO, FROM, SIZE));}
00132 char * P_strrchr (const char *STRING, int C) { return (strrchr(STRING, C));}
00133 char * P_strstr (const char *HAYSTACK, const char *NEEDLE) {
00134   return (strstr(HAYSTACK, NEEDLE));}
00135 double P_strtod (const char *STRING, char **TAILPTR) {
00136   return(strtod(STRING, TAILPTR));}
00137 char * P_strtok (char *NEWSTRING, const char *DELIMITERS) {
00138   return (strtok(NEWSTRING, DELIMITERS));}
00139 long int P_strtol (const char *STRING, char **TAILPTR, int BASE){
00140   return (strtol(STRING, TAILPTR, BASE));}
00141 unsigned long int P_strtoul(const char *STRING, char **TAILPTR, int BASE){
00142   return (strtoul(STRING, TAILPTR, BASE));}
00143 
00144 /* BSD */
00145 
00146 int P_accept (int SOCK, struct sockaddr *ADDR, size_t *LENGTH_PTR) {
00147 #if defined(CROSSCOMPILER)
00148   return (accept(SOCK, ADDR, (int *)LENGTH_PTR));
00149 #else
00150   return (accept(SOCK, ADDR, (int *)LENGTH_PTR));
00151 #endif
00152 }
00153 
00154 int P_bind(int socket, struct sockaddr *addr, size_t len) {
00155   return (bind(socket, addr, len));}
00156 
00157 // void P_bzero(void *block, size_t size) { bzero(block, size);}
00158 
00159 int P_connect(int socket, struct sockaddr *addr, size_t len) {
00160   return (connect(socket, addr, len));}
00161 
00162 struct hostent *P_gethostbyname (const char *NAME) { 
00163   return (gethostbyname(NAME));
00164 }
00165 
00166 int P_gethostname(char *name, size_t size) {
00167   return (gethostname(name, size));}
00168 
00169 int P_getrusage(int i, struct rusage *r) {return (getrusage(i, r));}
00170 
00171 struct servent *P_getservbyname (const char *NAME, const char *PROTO) {
00172   return (getservbyname(NAME, PROTO));}
00173 
00174 int P_getsockname (int SOCKET, struct sockaddr *ADDR, size_t *LENGTH_PTR) {
00175 #if defined(CROSSCOMPILER)
00176   return (getsockname(SOCKET, ADDR, (int *)LENGTH_PTR));
00177 #else
00178   return (getsockname(SOCKET, ADDR, (int *)LENGTH_PTR));
00179 #endif
00180 }
00181 
00182 /* int P_gettimeofday (struct timeval *TP, struct timezone *TZP) {
00183   return (gettimeofday(TP, TZP));} */
00184 
00185 int P_listen(int socket, unsigned int n) {
00186   return (listen(socket, n));}
00187 
00188 caddr_t P_mmap(caddr_t addr, size_t len, int prot, int flags,
00189            int fd, off_t off) {
00190   return ((caddr_t)mmap(addr, len, prot, flags, fd, off));}
00191 
00192 int P_munmap(caddr_t ca, int i) {return (munmap(ca, i));}
00193 
00194 int P_select (int wid, fd_set *rd, fd_set *wr, fd_set *ex, struct timeval *tm) {
00195   return (select((unsigned long)wid, rd, wr, ex, tm));}
00196 
00197 int P_socket (int NAMESPACE, int STYLE, int PROTOCOL) {
00198   return (socket(NAMESPACE, STYLE, PROTOCOL));}
00199 
00200 int P_socketpair(int NAMESPACE, int style, int protocol, int filedes[2]) {
00201   return (socketpair(NAMESPACE, style, protocol, filedes));}
00202 
00203 int P_pipe(int fd[2]) { return (pipe(fd)); }
00204 
00205 int P_strcasecmp(const char *s1, const char *s2) {
00206   return (strcasecmp(const_cast<char*>(s1), const_cast<char*>(s2)));}
00207 
00208 int P_strncasecmp (const char *S1, const char *S2, size_t N) {
00209   return (strncasecmp(const_cast<char*>(S1), const_cast<char*>(S2), N));}
00210 
00211 int P_rexec(char **ahost, u_short inport, char *user,
00212         char *passwd, char *cmd, int *fd2p){
00213   return (rexec(ahost, inport, user, passwd, cmd, fd2p));}
00214 
00215 void P_endservent(void) { endservent(); }
00216 
00217 void P_xdr_destroy(XDR *xdrs) { xdr_destroy(xdrs); }
00218 bool_t P_xdr_u_char(XDR *x, u_char *u) { return (xdr_u_char(x, u));}
00219 bool_t P_xdr_int(XDR *x, int *i) { return (xdr_int(x, i));}
00220 bool_t P_xdr_double(XDR *x, double *d) { return (xdr_double(x, d));}
00221 bool_t P_xdr_u_int(XDR *x, u_int *u) { return (xdr_u_int(x, u));}
00222 bool_t P_xdr_float(XDR *x, float *f) { return (xdr_float(x, f));}
00223 bool_t P_xdr_char(XDR *x, char *c) { return (xdr_char(x, c));}
00224 bool_t P_xdr_string(XDR *x, char **c, const u_int maxsize) {
00225   return (xdr_string(x, c, maxsize));}
00226 bool_t P_xdrrec_endofrecord(XDR *x, int now) {return (xdrrec_endofrecord(x, now));}
00227 bool_t P_xdrrec_skiprecord(XDR*x) { return (xdrrec_skiprecord(x));}
00228 void P_xdrrec_create(XDR *x, const u_int send_sz, const u_int rec_sz,
00229              const caddr_t handle, 
00230              xdr_rd_func readit, xdr_wr_func writeit) {
00231   xdrrec_create(x, send_sz, rec_sz, handle, 
00232         (int(*)())readit, 
00233         (int(*)())writeit);}
00234 
00235 
00236 unsigned long long PDYN_div1000(unsigned long long in) {
00237    /* Divides by 1000 without an integer division instruction or library call, both of
00238     * which are slow.
00239     * We do only shifts, adds, and subtracts.
00240     *
00241     * We divide by 1000 in this way:
00242     * multiply by 1/1000, or multiply by (1/1000)*2^30 and then right-shift by 30.
00243     * So what is 1/1000 * 2^30?
00244     * It is 1,073,742.   (actually this is rounded)
00245     * So we can multiply by 1,073,742 and then right-shift by 30 (neat, eh?)
00246     *
00247     * Now for multiplying by 1,073,742...
00248     * 1,073,742 = (1,048,576 + 16384 + 8192 + 512 + 64 + 8 + 4 + 2)
00249     * or, slightly optimized:
00250     * = (1,048,576 + 16384 + 8192 + 512 + 64 + 16 - 2)
00251     * for a total of 8 shifts and 6 add/subs, or 14 operations.
00252     *
00253     */
00254 
00255    unsigned long long temp = in << 20; // multiply by 1,048,576
00256       // beware of overflow; left shift by 20 is quite a lot.
00257       // If you know that the input fits in 32 bits (4 billion) then
00258       // no problem.  But if it's much bigger then start worrying...
00259 
00260    temp += in << 14; // 16384
00261    temp += in << 13; // 8192
00262    temp += in << 9;  // 512
00263    temp += in << 6;  // 64
00264    temp += in << 4;  // 16
00265    temp -= in >> 2;  // 2
00266 
00267    return (temp >> 30); // divide by 2^30
00268 }
00269 
00270 unsigned long long PDYN_divMillion(unsigned long long in) {
00271    /* Divides by 1,000,000 without an integer division instruction or library call,
00272     * both of which are slow.
00273     * We do only shifts, adds, and subtracts.
00274     *
00275     * We divide by 1,000,000 in this way:
00276     * multiply by 1/1,000,000, or multiply by (1/1,000,000)*2^30 and then right-shift
00277     * by 30.  So what is 1/1,000,000 * 2^30?
00278     * It is 1,074.   (actually this is rounded)
00279     * So we can multiply by 1,074 and then right-shift by 30 (neat, eh?)
00280     *
00281     * Now for multiplying by 1,074
00282     * 1,074 = (1024 + 32 + 16 + 2)
00283     * for a total of 4 shifts and 4 add/subs, or 8 operations.
00284     *
00285     * Note: compare with div1000 -- it's cheaper to divide by a million than
00286     *       by a thousand (!)
00287     *
00288     */
00289 
00290    unsigned long long temp = in << 10; // multiply by 1024
00291       // beware of overflow...if the input arg uses more than 52 bits
00292       // than start worrying about whether (in << 10) plus the smaller additions
00293       // we're gonna do next will fit in 64...
00294 
00295    temp += in << 5; // 32
00296    temp += in << 4; // 16
00297    temp += in << 1; // 2
00298 
00299    return (temp >> 30); // divide by 2^30
00300 }
00301 
00302 unsigned long long PDYN_mulMillion(unsigned long long in) {
00303    unsigned long long result = in;
00304 
00305    /* multiply by 125 by multiplying by 128 and subtracting 3x */
00306    result = (result << 7) - result - result - result;
00307 
00308    /* multiply by 125 again, for a total of 15625x */
00309    result = (result << 7) - result - result - result;
00310 
00311    /* multiply by 64, for a total of 1,000,000x */
00312    result <<= 6;
00313 
00314    /* cost was: 3 shifts and 6 subtracts
00315     * cost of calling mul1000(mul1000()) would be: 6 shifts and 4 subtracts
00316     *
00317     * Another algorithm is to multiply by 2^6 and then 5^6.
00318     * The former is super-cheap (one shift); the latter is more expensive.
00319     * 5^6 = 15625 = 16384 - 512 - 256 + 8 + 1
00320     * so multiplying by 5^6 means 4 shift operations and 4 add/sub ops
00321     * so multiplying by 1000000 means 5 shift operations and 4 add/sub ops.
00322     * That may or may not be cheaper than what we're doing (3 shifts; 6 subtracts);
00323     * I'm not sure.  --ari
00324     */
00325 
00326    return result;
00327 }
00328 
00329 /* defines taken from demangle.h in libiberty */
00330 #define DMGL_PARAMS (1 << 0)
00331 #define DMGL_ANSI (1 << 1)
00332 #define DMGL_GNU (1 << 9)
00333 #define DMGL_GNU_V3 (1 << 14)
00334 #define DMGL_AUTO (1 << 8)
00335 
00336 extern void dedemangle( const char * demangled, char * dedemangled );
00337 char * P_cplus_demangle( const char * symbol, bool nativeCompiler, 
00338                                 bool includeTypes) 
00339 {
00340      int opts = 0;
00341      opts = includeTypes ? DMGL_PARAMS | DMGL_ANSI : 0;
00342      //opts |= nativeCompiler ? DMGL_AUTO : DMGL_GNU;
00343      opts |= DMGL_GNU_V3;
00344      char *demangled = cplus_demangle( const_cast<char *>( symbol ), opts);
00345      if (demangled == NULL) return NULL;
00346      //fprintf(stderr, "%s[%d]:  cplus_demangle: %p: %s\n", __FILE__, __LINE__, current_demangling_style, libiberty_demanglers[0].demangling_style_name);
00347 
00348      char *open_paren = strchr(demangled, '(');
00349      if (open_paren)
00350        *open_paren = '\0';
00351 
00352      return demangled;
00353 } /* end P_cplus_demangle() */
00354 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1