trace.c revision 753d2d2e8e7fd0c9bcf736d9bf2f2faf4d6234cc
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1997-2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
*
* trace.c -- a simple translator from spec source to c source for
* a apptrace interposer library. This file implements the
* (interface to) the front end. Other files implement the middle
* and databases, and generate.c implements the back end.
*
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "parser.h"
#include "trace.h"
#include "util.h"
#include "db.h"
#include "symtab.h"
#include "io.h"
#include "printfuncs.h"
#include "errlog.h"
#include "parseproto.h"
static int Verbose;
/* File globals. This would be better as a class. */
/* The first four (commented out) of these enums are defined in parser.h */
enum {
/* XLATOR_KW_NOTFOUND = 0, */
/* XLATOR_KW_FUNC, */
/* XLATOR_KW_DATA */
/* XLATOR_KW_END */
XLATOR_KW_EXCP = 4,
};
static xlator_keyword_t Keywords[] = {
{ "exception", XLATOR_KW_EXCP },
{ "declaration", XLATOR_KW_DECL },
{ "include", XLATOR_KW_INCL },
{ "errno", XLATOR_KW_ERRNO },
{ "errval", XLATOR_KW_ERRVAL},
{ "arch", XLATOR_KW_ARCH},
{ "weak", XLATOR_KW_WEAK},
{ "weakfor", XLATOR_KW_WEAK},
{ "alias", XLATOR_KW_WEAK},
{ NULL, XLATOR_KW_NOTFOUND }
};
static struct stats_t {
int libraries,
int errors,
end;
} Statistics;
static void stats_init(void);
static void stats_report(void);
static int collect_binding(int const, char *, int);
static int collect_prototype(char *, int, int);
static int collect_include(char *, int);
static int collect_errval(char *, int);
static int collect_arch(char *);
static void generate_includes(void);
static void generate_init(void);
static void generate_interface(void);
static void generate_closedown(void);
static int generate_aux_file();
/* Local (static) parsing functions. */
static char *to_actual();
static int to_basetype(char *);
static char *de_const(char *);
static char *strpqcpy(char *, char *, char *);
/*
* xlator_init -- initialize translator, called at startup-time
* with a struct translator_info of information the translator
* might need, returning a list of ``interesting'' spec keywords
* for the front end to select and pass to the back end translator.
*
*/
{
int i;
/* Save interesting parameters. */
stats_init();
db_set_source_directory(".");
db_set_target_directory(".");
/* Display passed argument and return value. */
}
return (Keywords);
}
/*
* xlator_startlib -- called on starting a new library, so back end
*/
int
xlator_startlib(char const *libname)
{
return (SUCCESS_RC);
}
/*
* xlator_startfile -- ditto, called on starting each new spec file in the
* specified library.
*/
int
xlator_startfile(char const *filename)
{
int rc = SUCCESS_RC;
*lib = db_get_current_library();
Statistics.files++;
/* Generate filenames. */
/* Open .c file. */
if (open_code_file() == NO) {
}
generate_init(); /* Write stuff to the c file. */
symtab_clear_includes(); /* Clear out the per-file data. */
return (rc);
}
/*
* xlator_start_if -- tritto, called on starting each new
* interface in the spec file.
*/
int
{
char *kw;
switch (token) {
case XLATOR_KW_FUNC:
kw = "Function";
break;
case XLATOR_KW_DATA:
kw = "Data";
break;
default:
/* This should never happen */
"\"%s\", line %d: Implementation error! "
return (XLATOR_FATAL);
}
/*
* XXX Note whether interface is function or data in some state data item.
* We'll need it later when writing interceptors.
*/
if (*ifname == '\0') {
"missing argument in \"%s\" line", kw);
}
if (token == XLATOR_KW_DATA) {
Statistics.skips++;
return (SKIP_RC); /* Tell front end to skip it for us. */
}
/* Also cleans junk out of symbol table. */
return (SUCCESS_RC);
}
/*
* xlator_take_kvpair -- the primary call: collect a datum provide by the
* front-end wrapper.
*/
int
{
int retval;
Statistics.lines++;
switch (token) {
case XLATOR_KW_DECL:
/*
* XXX Check state item to see that it is a function,
* else do not emit interceptor
*/
symtab_clear_function(); /* Always use last one. */
break;
case XLATOR_KW_INCL:
}
break;
case XLATOR_KW_EXCP:
symtab_clear_exception(); /* Always use last. */
break;
case XLATOR_KW_ERRNO:
symtab_clear_errval(); /* Always use last. */
break;
case XLATOR_KW_ERRVAL:
symtab_clear_errval(); /* Always use last. */
break;
case XLATOR_KW_ARCH:
break;
case XLATOR_KW_WEAK:
if (m.mi_extended == 1) {
"Warning: Cannot use extends with a weak "
"interface",
m.mi_filename,
m.mi_line_number);
}
retval = SUCCESS_RC;
break;
default:
}
return (retval);
}
/*
* xlator_end_if -- called at the end of the interface, to trigger
* per-interface processing now entire thing has been seen.
*/
/*ARGSUSED*/
int
{
if (symtab_get_skip() == YES) {
Statistics.skips++;
} else {
}
return (SUCCESS_RC);
}
/*
* xlator_endfile -- called at the end of the file, to trigger per-file
* processing.
*/
int
xlator_endfile(void)
{
}
/*
* xlator_endlib -- ditto, at the end of the library.
*/
int
xlator_endlib(void)
{
return (SUCCESS_RC);
}
/*
* xlator_end -- the end of the processing, called so translator
* can do cleanup, write makefiles, etc.
*/
int
xlator_end(void)
{
int rc = SUCCESS_RC;
rc += !generate_aux_file();
stats_report();
return (rc);
}
/*
*/
/*
* stats_init -- note what time it is...
*/
static void
stats_init(void)
{
}
/*
* stats_report -- say how much we just did
*/
#define max(a, b) (a > b)? a: b
static void
stats_report(void)
{
double seconds;
switch (Verbose) {
default:
/*FALLTHROUGH*/
case 1:
" %d libraries\n %d files\n"
" %d interfaces\n %d lines\n"
" %d errors\n %d warnings\n"
" %d skips\n"
break;
case 0:
"spec2trace: %d errors %d warnings.\n",
}
break;
}
}
/*
* Tiny stats class...
*/
void
stats_add_warning(void)
{
}
void
stats_add_error(void)
{
Statistics.errors++;
}
/*
* collect_includes -- collect a global list of include files,
* converting the comma- or space-separated input list into a
* structure for the database to store.
* As this can cause problems will ill-structured
* files, there is a mechanism to allow exclusion of
* certain files, (or certain combinations). At
* the moment, the mechanism is TBD, as is the second arg.
*/
/*ARGSUSED1*/
int
collect_include(char *p, int line)
{
char *include;
int len;
include = skipb(include);
/*
* Make sure the include file's name
* has legitimate C syntax - i.e. it's in double
* quotes or angle brackets.
*/
if (*include != '"' && *include != '<')
return (ERROR_RC);
return (ERROR_RC);
/*
* If include filename syntax is OK, add it to
* the list
*/
symtab_add_includes(include);
}
}
return (SUCCESS_RC);
}
/*
* collect_binding -- take a binding and stuff it into the database
* in canonical form (with the word return in it).
*/
int
{
char *file = db_get_current_file();
if (token == XLATOR_KW_EXCP) {
} else {
}
return (SUCCESS_RC);
}
/*
* collect_errval -- collect the error variable name (only)
* from the line. This is expected to be the first
* or only thing in a space- or comma-separated list.
*/
int
collect_errval(char *p, int line)
{
char *name;
return (SUCCESS_RC);
}
/*
* collect_arch -- collect architecture.
*/
int
collect_arch(char *value)
{
char const *arch = db_get_arch();
char *buf, *p;
char *t;
t = buf;
goto cleanup;
t = NULL;
}
return (SUCCESS_RC);
}
/*
* de_const -- get rid of const meta-types. This is actually a
* dodge to avoid writing a base-type function early in the
* process. This may turn into to_basetype() or to_primitivetype().
*/
static char *
{
char *p, *q;
int i;
if (q > p) {
for (i = 0; i < 5; i++) {
*q++ = '\0';
}
return (type);
} else if (p == q) {
} else {
return (type);
}
}
/*
* to_basetype -- convert a C type declaration into its base type and return
* the number of levels of indirection.
* Destructive and eats ``const''.
*/
static int
to_basetype(char *str)
{
char *p = str,
*q = &buffer[0];
int levels = 0;
for (; *p != NULL; p++) {
switch (*p) {
case ' ': /* Convert spaces to single ' '. */
if (*(q-1) != ' ')
*q++ = ' ';
break;
case '*': /* Convert * to _P. */
if (*(q-1) != ' ')
*q++ = ' ';
levels++;
break;
case 'c': /* This might be a const */
p += 4;
} else {
*q++ = *p;
}
break;
default:
/* Otherwise just copy. */
*q++ = *p;
break;
}
*q = NULL;
}
q--;
while (*q == ' ') {
*q-- = NULL;
}
return (levels);
}
/*
* to_actual -- create an actual-argument list for use
* when calling the function.
*/
static char *
to_actual(void)
{
ENTRY *p;
int n;
if ((p = symtab_get_first_arg()) != NULL) {
for (p = symtab_get_next_arg(); p != NULL;
p = symtab_get_next_arg()) {
", %s", name_of(p));
}
}
return (buffer);
}
/*
* strpqcpy -- string copy that takes whatever begins with p and ends
* just before q.
*/
static char *
{
char saved;
saved = *q;
*q = NULL;
*q = saved;
return (target);
}
#ifndef lint
int
breakpoint(void)
{
return (0);
}
#endif
int
{
char f_basetype[BUFSIZ];
char a_basetype[BUFSIZ];
char *file = db_get_current_file();
char *interface = db_get_current_interface();
char *q;
char const *parse_err;
else
/* save prototype in symbol table */
/* Parse Prototype */
}
if (extcnt == 0) {
" name mismatch\nfunction name = %s,"
" declaration name = %s\n", interface,
dname);
}
if (type_IsPtrFun(tp)) {
" (pointer to function)\n", interface);
} else if (!type_IsFunction(tp)) {
}
if (type_IsVarargs(tp)) {
return (SUCCESS_RC);
}
/* get interface name from 'Begin' line */
/* args list */
while (funargs) {
a_levels++;
}
}
/*
* XXX: This is a hack to work around bug in yacc parser
* "int foo(void)" prototypes get interpreted as having 1
* argument with the d_name of the argument being NULL.
*/
"a_name = %s, a_basetype = %s, a_type = %s\n",
}
}
return (SUCCESS_RC);
}
/*
* generators
*/
/*
* generate_init -- prime the code generator as required.
*/
static void
generate_init(void)
{
"/*\n"
" * Generated by spec2trace %s: do not edit this file.\n */\n\n",
"#ifndef true\n"
"#define\ttrue 1\n"
"#define\tfalse 0\n"
"#endif\n\n"
"static char const *oparen = \"(\";\n"
"static char const *retstr = \" return = \";\n"
"static char const *errnostr = \" errno = \";\n"
"static char const *nilstr = \"<nil>\";\n"
"\n");
}
/*
* generate_interface -- call the two main parts of the per-interface
* code generation.
*/
static void
generate_interface(void)
{
/* Check for required information. */
"it will be skipped");
return;
}
/* Generate the current interface 's print-functions declarations. */
/* Generate the linkage part (a function and a struct */
/* Generate the actual interceptor. */
}
/*
* generate_closedown -- produce includes.
*/
static void
generate_closedown(void)
{
/* Print includes to primary file. */
}
/*
* generate_aux_file -- generate one additional .pf file with
* print-function pointers.
*/
static int
generate_aux_file(void)
{
/* Open file */
}
/*
* Declare and initialize all print function pointers to null.
* Some spec files result in nothing being put into the .pf
* file. We must create the file since make(1) does not cope
* well with absent files that it expects to have built. So
* now the build gets empty compilation unit warnings... So
* we unconditionally create a static pointer.
*/
"/* Do not edit this file: it is a generated one. */\n\n"
"static char const *__abi_place_holder;\n\n");
/* Close file */
}
return (YES);
}
/*
* generate_includes -- generate #includes to Headfp
*/
static void
generate_includes(void)
{
char *include;
for (include = symtab_get_first_include(); include != NULL;
include = symtab_get_next_include())
"#include <dlfcn.h>\n"
"#include <apptrace.h>\n\n");
}