In a C program, if I have several calls to printf (say): printf("This is not a test"); printf("How do you like them %s","apples"); printf("%d is a magic %s",3,"number"); and I then want to use Dtrace to fetch the args from all 3 printfs (This will pull out the basic string) pid$1::printf:entry { print_template = stringof(copyinstr(arg0)); printf("%s",print_template); } Is it possible to fetch (or how can I?) the additional args to printf if you don''t know how many args will be used and of what type they are? E.G the first printf above doesn''t have an arg1, the second printf has arg1 of type string, the third printf has arg1 of type int (and an arg2 !) How can I write a probe or probes to handle these variable cases? Cheers! -- This message posted from opensolaris.org
You can certainly do this, but it''s going to be kind of a pain. Two tips: you''ll want to copyin the format string, and then you''ll want to have an iterative step that you unroll that handles the cases of the various format characters. Adam On Wed, Jul 16, 2008 at 10:01:38AM -0700, Fat Ted wrote:> In a C program, if I have several calls to printf (say): > > printf("This is not a test"); > printf("How do you like them %s","apples"); > printf("%d is a magic %s",3,"number"); > > and I then want to use Dtrace to fetch the args from all 3 printfs > > (This will pull out the basic string) > > pid$1::printf:entry > { > print_template = stringof(copyinstr(arg0)); > printf("%s",print_template); > } > > Is it possible to fetch (or how can I?) the additional args to printf if you don''t know how many args will be used and of what type they are? > E.G > the first printf above doesn''t have an arg1, > the second printf has arg1 of type string, > the third printf has arg1 of type int (and an arg2 !) > > How can I write a probe or probes to handle these variable cases? > > Cheers! > > > -- > This message posted from opensolaris.org > _______________________________________________ > dtrace-discuss mailing list > dtrace-discuss at opensolaris.org-- Adam Leventhal, Fishworks http://blogs.sun.com/ahl
> Two tips: you''ll want to copyin the format string, and then you''ll want to have an iterative step that > you unroll that handles the cases of the various format characters.I can fetch the format string easily. (arg0) But how can I print out the next argument if I don''t know what type its going to be? If I try trace() I can get the int''s fine, but strings are returning as numbers. eg (In C program) printf("You are in %s","Ireland"); (In DTRACE results) trace(arg1); returns: 15171930 Is this the address of the character string? or some conversion? If I don''t use trace(), there doesn''t seem to be any way to tell the type of the argument and so I can''t dtrace printf() it out (since I don''t know whether I''ll be handling an int, a char * or whatever)... -- This message posted from opensolaris.org
> I can fetch the format string easily. (arg0) > > But how can I print out the next argument if I don''t know what type its > going to be? If I try trace() I can get the int''s fine, but strings are > returning as numbers.This is why I said it would be a pain. You''re going to have to do something like this: pid123::printf:entry { this->fmt = copyinstr(arg0); } /* begin loop */ pid123::printf:entry /this->fmt/ { this->fmt = strchr(this->fmt, ''%''); } pid123::printf:entry /this->fmt && this->fmt[1] == ''d''/ { trace(arg1); } pid123::printf:entry /this->fmt && this->fmt[1] == ''s''/ { trace(copyinstr(arg1)); } /* ... and unroll loop to desired length ... */ More or less... Adam -- Adam Leventhal, Fishworks http://blogs.sun.com/ahl
Thanks for the help! I''ve been working from an old version of the Solaris Dynamic Tracing guide (Jan 2005), I didn''t realise there was support for the various strXXX functions (doh!), I couldn''t figure out how I''d calculate the type from the format string :) General structure of loop layout is helpful too. Cheers! -- (RTFCM), Read The F''ing Correct Manual... -- This message posted from opensolaris.org
> I''ve been working from an old version of the Solaris Dynamic Tracing > guide (Jan 2005), I didn''t realise there was support for the various > strXXX functions (doh!), I couldn''t figure out how I''d calculate the > type from the format string :)Unfortunately, the documentation never got updated to include those subroutines. Here''s where I end up going if I can''t remember the list of them: http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libdtrace/common/dt_open.c#140 Adam -- Adam Leventhal, Fishworks http://blogs.sun.com/ahl