From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25582 invoked from network); 13 Aug 2008 20:43:54 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.5 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 13 Aug 2008 20:43:54 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 36006 invoked from network); 13 Aug 2008 20:43:41 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 13 Aug 2008 20:43:41 -0000 Received: (qmail 22728 invoked by alias); 13 Aug 2008 20:43:32 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 25448 Received: (qmail 22710 invoked from network); 13 Aug 2008 20:43:29 -0000 Received: from bifrost.dotsrc.org (130.225.254.106) by sunsite.dk with SMTP; 13 Aug 2008 20:43:29 -0000 Received: from mtaout03-winn.ispmail.ntl.com (mtaout03-winn.ispmail.ntl.com [81.103.221.49]) by bifrost.dotsrc.org (Postfix) with ESMTP id 2DBBD80EA0B3 for ; Wed, 13 Aug 2008 22:43:25 +0200 (CEST) Received: from aamtaout04-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout03-winn.ispmail.ntl.com with ESMTP id <20080813204321.TDNM10791.mtaout03-winn.ispmail.ntl.com@aamtaout04-winn.ispmail.ntl.com> for ; Wed, 13 Aug 2008 21:43:21 +0100 Received: from pws-pc ([81.107.40.67]) by aamtaout04-winn.ispmail.ntl.com with ESMTP id <20080813204321.RFWB18637.aamtaout04-winn.ispmail.ntl.com@pws-pc> for ; Wed, 13 Aug 2008 21:43:21 +0100 Date: Wed, 13 Aug 2008 21:43:18 +0100 From: Peter Stephenson To: "Zsh hackers list" Subject: Re: functrace and funcsourcetrace (was Re: PATCH: trace information for sourced files) Message-ID: <20080813214318.09f570e7@pws-pc> In-Reply-To: <20080813204212.4c72c96a@pws-pc> References: <6cd6de210808121700kf500ea6p9b3eed0fefadfc8@mail.gmail.com> <20080813204212.4c72c96a@pws-pc> X-Mailer: Claws Mail 3.5.0 (GTK+ 2.12.11; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV 0.92.1/8027/Wed Aug 13 20:31:07 2008 on bifrost X-Virus-Status: Clean On Wed, 13 Aug 2008 20:42:12 +0100 Peter Stephenson wrote: > I'll have a look at this when I get back (unless I get bored on holiday). I lied. I think this one does specifically what you want, and if it doesn't, we should now have enough information to tweak it. No test yet, that really *will* have to wait, but I've checked with autoloaded functions and because I made funcsourcetrace consistent the last time round they seem to work OK. Running your script with $funcfiletrace gave the files and line numbers you wanted. I've left $funcsourcetrace, since we need to have the information for it anyway, but I'm assuming it's going to be of fairly specialist interest, so I've documented it as such. Index: Doc/Zsh/mod_parameter.yo =================================================================== RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_parameter.yo,v retrieving revision 1.10 diff -u -r1.10 mod_parameter.yo --- Doc/Zsh/mod_parameter.yo 12 Aug 2008 20:25:14 -0000 1.10 +++ Doc/Zsh/mod_parameter.yo 13 Aug 2008 20:33:16 -0000 @@ -164,6 +164,16 @@ This associative array maps user names to the pathnames of their home directories. ) +vindex(funcfiletrace) +item(tt(funcfiletrace))( +This array contains the absolute line numbers and corresponding file +names for the point where the current function or sourced file was +called. The array is of the same length as tt(funcsourcetrace) and +tt(functrace), but differs from tt(funcsourcetrace) in that the line and +file are the point of call, not the point of definition, and differs +from tt(functrace) in that all values are absolute line numbers in +files, rather than relative to the start of a function, if any. +) vindex(funcsourcetrace) item(tt(funcsourcetrace))( This array contains the file names and line numbers of the @@ -176,6 +186,9 @@ For files that have been executed by the tt(source) or tt(.) builtins (in which case there is no separate definition) the trace information is shown as tt(source:0). + +Most users will be interested in the information in the +tt(funcfiletrace) array instead. ) vindex(funcstack) item(tt(funcstack))( Index: Src/exec.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/exec.c,v retrieving revision 1.139 diff -u -r1.139 exec.c --- Src/exec.c 11 Aug 2008 19:22:54 -0000 1.139 +++ Src/exec.c 13 Aug 2008 20:33:19 -0000 @@ -4258,6 +4258,7 @@ fstack.caller = dupstring(oargv0 ? oargv0 : argzero); fstack.lineno = lineno; fstack.prev = funcstack; + fstack.sourced = 0; funcstack = &fstack; if ((shf = (Shfunc) shfunctab->getnode(shfunctab, name))) { Index: Src/init.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/init.c,v retrieving revision 1.93 diff -u -r1.93 init.c --- Src/init.c 12 Aug 2008 20:25:14 -0000 1.93 +++ Src/init.c 13 Aug 2008 20:33:19 -0000 @@ -1109,6 +1109,7 @@ fstack.lineno = oldlineno; fstack.filename = fstack.name; fstack.prev = funcstack; + fstack.sourced = 1; funcstack = &fstack; } Index: Src/zsh.h =================================================================== RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v retrieving revision 1.141 diff -u -r1.141 zsh.h --- Src/zsh.h 11 Aug 2008 19:22:54 -0000 1.141 +++ Src/zsh.h 13 Aug 2008 20:33:20 -0000 @@ -1080,11 +1080,12 @@ struct funcstack { Funcstack prev; /* previous in stack */ - char *name; /* name of function called */ + char *name; /* name of function/sourced file called */ char *filename; /* file function resides in */ char *caller; /* name of caller */ zlong flineno; /* line number in file */ zlong lineno; /* line offset from beginning of function */ + int sourced; /* type of entry is a sourced file */ }; /* node in list of function call wrappers */ Index: Src/Modules/parameter.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/Modules/parameter.c,v retrieving revision 1.46 diff -u -r1.46 parameter.c --- Src/Modules/parameter.c 11 Aug 2008 19:22:54 -0000 1.46 +++ Src/Modules/parameter.c 13 Aug 2008 20:33:21 -0000 @@ -566,6 +566,55 @@ return ret; } +/* Functions for the funcfiletrace special parameter. */ + +/**/ +static char ** +funcfiletracegetfn(UNUSED(Param pm)) +{ + Funcstack f; + int num; + char **ret, **p; + + for (f = funcstack, num = 0; f; f = f->prev, num++); + + ret = (char **) zhalloc((num + 1) * sizeof(char *)); + + for (f = funcstack, p = ret; f; f = f->prev, p++) { + char *colonpair, *fname; + + if (!f->prev || f->prev->sourced) { + /* + * Calling context is a file---either the parent + * script or interactive shell, or a sourced + * script. Just print the file information for the caller + * (same as $functrace) + */ + colonpair = zhalloc(strlen(f->caller) + + (f->lineno > 9999 ? 24 : 6)); + sprintf(colonpair, "%s:%ld", f->caller, (long)f->lineno); + } else { + /* + * Calling context is a function; we need to find the line number + * in the file where that function was defined. For this we need + * the $funcsourcetrace information for the context above, + * together with the $functrace line number for the current + * context. + */ + long flineno = (long)(f->prev->flineno + f->lineno); + fname = f->prev->filename ? f->prev->filename : ""; + + colonpair = zhalloc(strlen(fname) + (flineno > 9999 ? 24 : 6)); + sprintf(colonpair, "%s:%ld", fname, flineno); + } + + *p = colonpair; + } + *p = NULL; + + return ret; +} + /* Functions for the builtins special parameter. */ /**/ @@ -1803,6 +1852,8 @@ { functracegetfn, arrsetfn, stdunsetfn }; static const struct gsu_array funcsourcetrace_gsu = { funcsourcetracegetfn, arrsetfn, stdunsetfn }; +static const struct gsu_array funcfiletrace_gsu = +{ funcfiletracegetfn, arrsetfn, stdunsetfn }; static const struct gsu_array reswords_gsu = { reswordsgetfn, arrsetfn, stdunsetfn }; static const struct gsu_array disreswords_gsu = @@ -1831,6 +1882,8 @@ &disreswords_gsu, NULL, NULL), SPECIALPMDEF("dis_saliases", 0, &pmdissaliases_gsu, getpmdissalias, scanpmdissaliases), + SPECIALPMDEF("funcfiletrace", PM_ARRAY|PM_READONLY, + &funcfiletrace_gsu, NULL, NULL), SPECIALPMDEF("funcsourcetrace", PM_ARRAY|PM_READONLY, &funcsourcetrace_gsu, NULL, NULL), SPECIALPMDEF("funcstack", PM_ARRAY|PM_READONLY, Index: Src/Modules/parameter.mdd =================================================================== RCS file: /cvsroot/zsh/zsh/Src/Modules/parameter.mdd,v retrieving revision 1.6 diff -u -r1.6 parameter.mdd --- Src/Modules/parameter.mdd 11 Aug 2008 19:22:54 -0000 1.6 +++ Src/Modules/parameter.mdd 13 Aug 2008 20:33:21 -0000 @@ -2,6 +2,6 @@ link=either load=yes -autofeatures="p:parameters p:commands p:functions p:dis_functions p:funcsourcetrace p:funcstack p:functrace p:builtins p:dis_builtins p:reswords p:dis_reswords p:options p:modules p:dirstack p:history p:historywords p:jobtexts p:jobdirs p:jobstates p:nameddirs p:userdirs p:aliases p:dis_aliases p:galiases p:dis_galiases p:saliases p:dis_saliases" +autofeatures="p:parameters p:commands p:functions p:dis_functions p:funcfiletrace p:funcsourcetrace p:funcstack p:functrace p:builtins p:dis_builtins p:reswords p:dis_reswords p:options p:modules p:dirstack p:history p:historywords p:jobtexts p:jobdirs p:jobstates p:nameddirs p:userdirs p:aliases p:dis_aliases p:galiases p:dis_galiases p:saliases p:dis_saliases" objects="parameter.o" -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/