Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find location of proc or tcl libraries which been used

1,037 views
Skip to first unread message

Arash Khosravi

unread,
Apr 17, 2021, 8:08:19 PM4/17/21
to
I'm super new to tcl and I want to see the exact location of the proc or libraries which been used in a script, I'm using TCL 8.4 (can not update) in 8.7 the specific functionality been added but it does not work in 8.4
example:
sourcefile /home/dkf/develop/tcl/library/parray.tcl

8.7
% dict filter [tcl::unsupported::getbytecode proc parray] key sourcefile
sourcefile /home/dkf/develop/tcl/library/parray.tcl

Gerald Lester

unread,
Apr 17, 2021, 10:21:29 PM4/17/21
to
The long answer is maybe -- this short answer is no.

Since you say you can not upgrade, I'm assuming that you are using Tcl
embedded in some product. That product can cause procedures to be
defined that do not exist anywhere on disk.

Now if you can early on in the initialization of the interpreter (my
guess is this is unlikely) you could redefine the source and open
commands to track access and log them out.

A better question is: why do you want to do this?

--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

Arash Khosravi

unread,
Apr 18, 2021, 8:36:18 AM4/18/21
to
Thank for your kind answer,
so you mean the only way to achieve the goal is to define the commands on the initialization of interpreter, and track them procs, those commands should be added to each proc?

You are correct and the tcl been embedded in the application, we are going to change the structure of the app but we can not change the complete source or update the TCL in it,
we need to find where each proc comes (the path) and which file used it to restructure.
It would be a pleasure if you could explain me how to do it by steps, so I could use your thoughts and expertise to solve the problem.
I might have access to the source codes.

Lets assume I wrote a proc and saved it some where,
the final goes is:

my_proc ← comes from tcl library at /my_tcl_path

Thanks again for your help

Gerald Lester

unread,
Apr 18, 2021, 5:35:36 PM4/18/21
to
On 4/18/21 7:36 AM, Arash Khosravi wrote:
> On Saturday, April 17, 2021 at 10:21:29 PM UTC-4, Gerald Lester wrote:
>> On 4/17/21 7:08 PM, Arash Khosravi wrote:
>>> I'm super new to tcl and I want to see the exact location of the proc or libraries which been used in a script, I'm using TCL 8.4 (can not update) in 8.7 the specific functionality been added but it does not work in 8.4
>>> example:
>>> sourcefile /home/dkf/develop/tcl/library/parray.tcl
>>>
>>> 8.7
>>> % dict filter [tcl::unsupported::getbytecode proc parray] key sourcefile
>>> sourcefile /home/dkf/develop/tcl/library/parray.tcl
>>>
>> The long answer is maybe -- this short answer is no.
>>
>> Since you say you can not upgrade, I'm assuming that you are using Tcl
>> embedded in some product. That product can cause procedures to be
>> defined that do not exist anywhere on disk.
>>
>> Now if you can early on in the initialization of the interpreter (my
>> guess is this is unlikely) you could redefine the source and open
>> commands to track access and log them out.
>>
>> A better question is: why do you want to do this?
>>
>> --
>> +----------------------------------------------------------------------+
>> | Gerald W. Lester, President, KNG Consulting LLC |
>> | Email: Gerald...@kng-consulting.net |
>> +----------------------------------------------------------------------+
>
> Thank for your kind answer,
> so you mean the only way to achieve the goal is to define the commands on the initialization of interpreter, and track them procs, those commands should be added to each proc?

Basically, yes.

>
> You are correct and the tcl been embedded in the application, we are going to change the structure of the app but we can not change the complete source or update the TCL in it,

Which app -- people here may know about it.

> we need to find where each proc comes (the path) and which file used it to restructure.
> It would be a pleasure if you could explain me how to do it by steps, so I could use your thoughts and expertise to solve the problem.
> I might have access to the source codes.

rename source _source
proc source {filename} {
puts stdout "Sourcing {[file join [pwd] $filename]}"
uplevel 1 _source $filename
}

Do the same with open -- which I will leave as an exercise for you.

>
> Lets assume I wrote a proc and saved it some where,
> the final goes is:
>
> my_proc ← comes from tcl library at /my_tcl_path
>
> Thanks again for your help
>


.

unread,
Apr 19, 2021, 1:12:37 AM4/19/21
to
Here are some suggestions and links that I have found helpful in my own work with tcl in this area.

1) puts $::auto_path

2) https://stackoverflow.com/questions/21987770/how-to-find-the-script-location-where-the-called-proc-resides

3) https://wiki.tcl-lang.org/page/info+body

4) tcl website or Google search based on above are very informative.

5) Application documentation and support channels.
The app developers, docs, or support channels for it will inform where the tcl procs are introduced into the app and can inform you as to the src code location and if it is available to you. Don't be shy, asking them for info. It's also polite to do so.

J

Christian Gollwitzer

unread,
Apr 19, 2021, 1:37:07 AM4/19/21
to
Am 19.04.21 um 07:12 schrieb .:
> Here are some suggestions and links that I have found helpful in my own work with tcl in this area.
>
> 1) puts $::auto_path
>
> 2) https://stackoverflow.com/questions/21987770/how-to-find-the-script-location-where-the-called-proc-resides
>
> 3) https://wiki.tcl-lang.org/page/info+body
>

Also, if the command comes from a package, this can be helpful:

(chris) 56 % package ifneeded fileutil [package require fileutil]
source /Users/chris/bin/wish86/lib/tcllib1.19/fileutil/fileutil.tcl


Christian

Arash Khosravi

unread,
Apr 19, 2021, 10:20:03 AM4/19/21
to
Hi and thanks for kind answer, it was really informative,
but because I'm new in tcl it caused me more confusion, the put $::auto_path returns a list of all libraries which been loaded to interpreter, so using it would not help unless the path was added to the tcl file that has the proc in it.
I wrote my own proc in a .tcl file, saved and sourced it, what would be the code to add into my proc to return when i source it?

Arash Khosravi

unread,
Apr 19, 2021, 10:22:13 AM4/19/21
to
Thanks honestly I got lost,
There is something that I miss or can not understand, why i can not get the pass directly in my proc before soursning it then return that value

Rolf Ade

unread,
Apr 19, 2021, 11:04:36 AM4/19/21
to

Arash Khosravi <arash.kho...@gmail.com> writes:
> I wrote my own proc in a .tcl file, saved and sourced it, what would
> be the code to add into my proc to return when i source it?

Insert

rename proc _proc
_proc proc {name args body} {
puts "$name from [info script]"
_proc $name $args $body
}

at the top of your main script, before all other code.

Beware - it's not the silver bullet to your problem. Rather, it's a
simple approach, which will fail in certain cases (if the code of your
program does advanced, fancy things).

But it is also possible that this code snippet gets you a long way on
your road. Just give it a try and report.

.

unread,
Apr 19, 2021, 12:08:06 PM4/19/21
to

> what would be the code to add into my proc to return when i source it?

ANSWER:
puts "msg to print here"

Example:
puts "name_of_my_proc"



heinrichmartin

unread,
Apr 19, 2021, 4:52:30 PM4/19/21
to
Many hints were given, but let me highlight "[info procs] + grep -r" - sometimes it need not be the gold-plated solution, and expensive operations can be acceptable (especially when executed only once).

Other solutions suggest to install some sort of hook "asap during initialization". If you cannot inject a script "soon enough", think of pkgIndex.tcl; I guess these files can hold more than [package ifneeded].

Arash Khosravi

unread,
Apr 19, 2021, 8:42:44 PM4/19/21
to
Thanks for info,, ill try to read about pkgindex.tcl

Arash Khosravi

unread,
Apr 19, 2021, 8:43:33 PM4/19/21
to
how does it help me to understand which proc comes from which sourced .tcl file?

Arash Khosravi

unread,
Apr 19, 2021, 8:47:43 PM4/19/21
to
Hi Rolf and thanks but it seems I have problem understanding what I should i do,
It seems lack of TCL knowledge,
I added the code above my codes in .tcl file,
When I sourced the file how can I call the _proc to get info I need?

I have tested it on tcl file which have below code only in it:

rename proc _proc
_proc proc {name args body} {
puts "$name from [info script]"
_proc $name $args $body
}

proc my_hello {} { puts “hello”}

And sourced it:
$ source my_hello.tcl

calling the proc
$ my_hello

output willlbe
$ hello

How doe the _proc will help me to get the path of my hello proc?

I’m so sorry that asking juniors question but really got confused!

.

unread,
Apr 19, 2021, 9:42:33 PM4/19/21
to
"Output willbe" is unexpected and incomplete.
Please confirm that is what the tcl interpreter showed or from your mental expected result?

The "output willbe" is suspicipusly missing what this line of code produces in the output:
> puts "$name from [info script]"

That is typical mistake in mental generated result but should not appear in interpreter generated results.

J

Rolf Ade

unread,
Apr 20, 2021, 6:52:18 AM4/20/21
to
If I follow your example I see

my_hello from my_hello.tcl

directly after the source command as output in the interpreter. Which
is, what you asked for (if I got you right).

So, what interpreter is this that you use? An ordinary tclsh or wish
executable or is this some embedded interpreter promt of your app?
0 new messages