From a06c6923336fe9a652b42140574b6b90f5767900 Mon Sep 17 00:00:00 2001 From: avsm Date: Fri, 27 May 2005 06:31:52 +0000 Subject: [PATCH] Add a 'kernel function call tracer' to automatically insert a call to log() to certain functions in the kernel to help debugging. An example use when building a kernel would be: $ make CC='/usr/local/bin/cilly --trace-regexp=^cache_ --trace-level=0' to instrument all functions starting with cache_* to notify syslog at emergency log level. The output also includes the contents of any char/int/long arguments (pointers and structs not followed yet, too verbose). A full kernel build doesnt quite work with CIL yet so use selectively. --- devel/cil/Makefile | 3 +- devel/cil/files/kerneltrace.ml | 81 +++++++++++++++++++++++++ devel/cil/patches/patch-Makefile_cil_in | 11 ++-- devel/cil/patches/patch-src_cil_ml | 11 ++++ devel/cil/patches/patch-src_cilutil_ml | 9 +-- devel/cil/patches/patch-src_maincil_ml | 7 ++- 6 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 devel/cil/files/kerneltrace.ml create mode 100644 devel/cil/patches/patch-src_cil_ml diff --git a/devel/cil/Makefile b/devel/cil/Makefile index 0fe8fd47bfd..f7e7e1cf193 100644 --- a/devel/cil/Makefile +++ b/devel/cil/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.6 2005/05/09 13:52:38 avsm Exp $ +# $OpenBSD: Makefile,v 1.7 2005/05/27 06:31:52 avsm Exp $ # until tested on others, even though bytecode is produced ONLY_FOR_ARCHS= i386 amd64 macppc @@ -33,6 +33,7 @@ GCC=/usr/bin/gcc # cant use CC in case it's set to cilly post-extract: @cp ${FILESDIR}/nullint.ml ${WRKSRC}/src/ext + @cp ${FILESDIR}/kerneltrace.ml ${WRKSRC}/src/ext pre-fake: ${INSTALL_DATA_DIR} ${PREFIX}/libexec/cil diff --git a/devel/cil/files/kerneltrace.ml b/devel/cil/files/kerneltrace.ml new file mode 100644 index 00000000000..83ec17f517a --- /dev/null +++ b/devel/cil/files/kerneltrace.ml @@ -0,0 +1,81 @@ +(* $OpenBSD: kerneltrace.ml,v 1.1 2005/05/27 06:31:52 avsm Exp $ *) +(* + * Copyright (c) 2004 Anil Madhavapeddy + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * $Id: kerneltrace.ml,v 1.1 2005/05/27 06:31:52 avsm Exp $ + *) + +(* introduce log messages to trace kernel messages *) + +open Pretty +open Cil +module E = Errormsg + +let traceRegexp = ref None +let traceLogLevel = ref 0 + +class callTraceVisitor f outp = object + inherit nopCilVisitor + + method vfunc fundec = + let funname = fundec.svar.vname in + match !traceRegexp with + |None -> SkipChildren + |Some reg -> + if Str.string_match (Str.regexp reg) funname 0 then begin + let args = List.fold_left (fun a b -> + match b.vtype with + |TInt (kind, _) -> + let fmt = match kind with + |IChar |ISChar |IUChar -> "%c" + |IInt |IShort |IUShort -> "%d" + |IUInt -> "%u" |ILong -> "%ld" + |ILongLong -> "%lld" |IULong -> "%llu" + |IULongLong -> "%llu" in + (Printf.sprintf "%s=%s" b.vname fmt, b) :: a + |_ -> a + ) [] fundec.sformals in + let args = List.rev args in + let level = integer (!traceLogLevel) in (* LOG_DEBUG *) + let formargs = String.concat " " (List.map (fun (a,_) -> a) args) in + let form = mkString (Printf.sprintf "%s: %s\n" funname formargs) in + let sargs = List.map (fun (_,b) -> Lval(var b)) args in + let funvar = level :: form :: sargs in + let st = mkStmt (Instr[Call(None,outp,funvar,locUnknown)]) in + fundec.sbody.bstmts <- st :: fundec.sbody.bstmts; + ChangeTo(fundec) + end else + DoChildren + +end + +let calltrace f = + let expify fundec = Lval(Var(fundec.svar), NoOffset) in + let outp = expify (emptyFunction "log") in + visitCilFileSameGlobals (new callTraceVisitor f outp) f + +let feature : featureDescr = + { fd_name = "kerneltrace"; + fd_enabled = Cilutil.kernelTrace; + fd_description = "add log messages to kernel function calls"; + fd_extraopt = [ + ("--trace-regexp", Arg.String (fun x -> traceRegexp := Some x), + " regexp for which functions to insert logging calls"); + ("--trace-level", Arg.Int (fun x -> traceLogLevel := x), + " log level to use for trace messages"); + ]; + fd_doit = (function (f: file) -> calltrace f); + fd_post_check = true; + } diff --git a/devel/cil/patches/patch-Makefile_cil_in b/devel/cil/patches/patch-Makefile_cil_in index 8607c156be0..465b5d47138 100644 --- a/devel/cil/patches/patch-Makefile_cil_in +++ b/devel/cil/patches/patch-Makefile_cil_in @@ -1,21 +1,22 @@ -$OpenBSD: patch-Makefile_cil_in,v 1.2 2005/05/09 13:52:38 avsm Exp $ +$OpenBSD: patch-Makefile_cil_in,v 1.3 2005/05/27 06:31:52 avsm Exp $ --- Makefile.cil.in.orig Mon Jan 31 09:56:18 2005 -+++ Makefile.cil.in Tue Mar 1 10:20:29 2005 ++++ Makefile.cil.in Thu May 26 22:47:50 2005 @@ -64,7 +64,7 @@ CILLY_LIBRARY_MODULES = pretty inthash e patch frontc check mergecil \ dataflow dominators bitmap ssa \ usedef logcalls logwrites rmtmps \ - callgraph epicenter heapify \ -+ callgraph epicenter heapify nullint \ ++ callgraph epicenter heapify nullint kerneltrace \ setp uref steensgaard olf dummy ptranal \ canonicalize heap oneret partial simplemem simplify \ dataslicing \ -@@ -351,7 +351,7 @@ DISTRIB_SRC_FRONTC = cabs.ml cprint.ml c +@@ -351,7 +351,8 @@ DISTRIB_SRC_FRONTC = cabs.ml cprint.ml c DISTRIB_SRC_EXT = logcalls.ml logcalls.mli \ astslicer.ml simplemem.ml heap.ml partial.ml \ - logwrites.ml heapify.ml callgraph.ml callgraph.mli \ -+ logwrites.ml heapify.ml nullint.ml callgraph.ml callgraph.mli \ ++ logwrites.ml heapify.ml nullint.ml kerneltrace.ml \ ++ callgraph.ml callgraph.mli \ epicenter.ml usedef.ml \ dataflow.ml dominators.ml bitmap.ml ssa.ml \ stackoverflow.mli stackoverflow.ml \ diff --git a/devel/cil/patches/patch-src_cil_ml b/devel/cil/patches/patch-src_cil_ml new file mode 100644 index 00000000000..44044b50250 --- /dev/null +++ b/devel/cil/patches/patch-src_cil_ml @@ -0,0 +1,11 @@ +$OpenBSD: patch-src_cil_ml,v 1.1 2005/05/27 06:31:52 avsm Exp $ +--- src/cil.ml.orig Thu May 26 08:47:56 2005 ++++ src/cil.ml Thu May 26 08:48:27 2005 +@@ -1979,6 +1979,7 @@ let gccBuiltins : (string, typ * typ lis + (* When we parse builtin_next_arg we drop the second argument *) + H.add h "__builtin_next_arg" + ((if hasbva then TBuiltin_va_list [] else voidPtrType), [], false); ++ H.add h "__builtin_frame_address" (voidPtrType, [ uintType ], false); + H.add h "__builtin_alloca" (voidPtrType, [ uintType ], false); + H.add h "__builtin_constant_p" (intType, [ intType ], false); + H.add h "__builtin_expect" (longType, [ longType; longType ], false); diff --git a/devel/cil/patches/patch-src_cilutil_ml b/devel/cil/patches/patch-src_cilutil_ml index 1b55b5b8b32..94d12953fc3 100644 --- a/devel/cil/patches/patch-src_cilutil_ml +++ b/devel/cil/patches/patch-src_cilutil_ml @@ -1,11 +1,12 @@ -$OpenBSD: patch-src_cilutil_ml,v 1.1 2004/06/21 18:53:12 avsm Exp $ ---- src/cilutil.ml.orig 2004-04-06 16:51:21.000000000 +0100 -+++ src/cilutil.ml 2004-06-20 18:36:46.000000000 +0100 -@@ -52,6 +52,8 @@ maintain a separate stack for return add +$OpenBSD: patch-src_cilutil_ml,v 1.2 2005/05/27 06:31:52 avsm Exp $ +--- src/cilutil.ml.orig Mon Jan 31 09:56:18 2005 ++++ src/cilutil.ml Thu May 26 20:38:13 2005 +@@ -52,6 +52,9 @@ maintain a separate stack for return add let doHeapify = ref false (* move stack-allocated arrays to the heap *) let makeCFG = ref false (* turn the input CIL file into something more like * a CFG *) +let nullInt = ref true (* look for int and NULL comparisons *) ++let kernelTrace = ref true (* add tracing calls into the kernel *) + let printStats = ref false diff --git a/devel/cil/patches/patch-src_maincil_ml b/devel/cil/patches/patch-src_maincil_ml index 083a530b631..abe559f16cb 100644 --- a/devel/cil/patches/patch-src_maincil_ml +++ b/devel/cil/patches/patch-src_maincil_ml @@ -1,11 +1,12 @@ -$OpenBSD: patch-src_maincil_ml,v 1.2 2005/05/09 13:52:38 avsm Exp $ +$OpenBSD: patch-src_maincil_ml,v 1.3 2005/05/27 06:31:52 avsm Exp $ --- src/maincil.ml.orig Mon Jan 31 09:56:18 2005 -+++ src/maincil.ml Tue Mar 1 10:06:02 2005 -@@ -104,6 +104,7 @@ let features : C.featureDescr list = ++++ src/maincil.ml Thu May 26 23:54:40 2005 +@@ -104,6 +104,8 @@ let features : C.featureDescr list = Partial.feature; Simplemem.feature; Simplify.feature; + Nullint.feature; ++ Kerneltrace.feature; Dataslicing.feature; ] @ Feature_config.features