Print this page
saveargs: let disasm do the lifting


   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #pragma ident   "%Z%%M% %I%     %E% SMI"
  28 
  29 #include <libdisasm.h>
  30 #include <stdlib.h>
  31 #include <stdio.h>
  32 
  33 #include "dis_tables.h"
  34 #include "libdisasm_impl.h"
  35 
  36 struct dis_handle {
  37         void            *dh_data;
  38         int             dh_flags;
  39         dis_lookup_f    dh_lookup;
  40         dis_read_f      dh_read;
  41         int             dh_mode;
  42         dis86_t         dh_dis;
  43         uint64_t        dh_addr;
  44         uint64_t        dh_end;
  45 };
  46 
  47 /*
  48  * Returns true if we are near the end of a function.  This is a cheap hack at


 228         }
 229 
 230         if (addr != pc) {
 231                 /*
 232                  * We scanned past %pc, but didn't find an instruction that
 233                  * started at %pc.  This means that either the caller specified
 234                  * an invalid address, or we ran into something other than code
 235                  * during our scan.  Virtually any combination of bytes can be
 236                  * construed as a valid Intel instruction, so any non-code bytes
 237                  * we encounter will have thrown off the scan.
 238                  */
 239                 goto done;
 240         }
 241 
 242         res = hist[(cur + n - MIN(n, nseen)) % n];
 243 
 244 done:
 245         dis_free(hist, sizeof (uint64_t) * n);
 246         return (res);
 247 }











   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 


  27 #include <libdisasm.h>
  28 #include <stdlib.h>
  29 #include <stdio.h>
  30 
  31 #include "dis_tables.h"
  32 #include "libdisasm_impl.h"
  33 
  34 struct dis_handle {
  35         void            *dh_data;
  36         int             dh_flags;
  37         dis_lookup_f    dh_lookup;
  38         dis_read_f      dh_read;
  39         int             dh_mode;
  40         dis86_t         dh_dis;
  41         uint64_t        dh_addr;
  42         uint64_t        dh_end;
  43 };
  44 
  45 /*
  46  * Returns true if we are near the end of a function.  This is a cheap hack at


 226         }
 227 
 228         if (addr != pc) {
 229                 /*
 230                  * We scanned past %pc, but didn't find an instruction that
 231                  * started at %pc.  This means that either the caller specified
 232                  * an invalid address, or we ran into something other than code
 233                  * during our scan.  Virtually any combination of bytes can be
 234                  * construed as a valid Intel instruction, so any non-code bytes
 235                  * we encounter will have thrown off the scan.
 236                  */
 237                 goto done;
 238         }
 239 
 240         res = hist[(cur + n - MIN(n, nseen)) % n];
 241 
 242 done:
 243         dis_free(hist, sizeof (uint64_t) * n);
 244         return (res);
 245 }
 246 
 247 int
 248 dis_instrlen(dis_handle_t *dhp, uint64_t pc)
 249 {
 250         if (dis_disassemble(dhp, pc, NULL, 0) != 0)
 251                 return (-1);
 252 
 253         return (dhp->dh_addr - pc);
 254 }