I''m new to dtrace and trying to figure out how I can use it as a tool for debugging. I have a few lines for testing : ====================================#include<stdio.h> #include<string.h> typedef struct { int age; char name[24]; char class; } student_t; int setdata(int count,student_t *first) { first->age = 24; strcpy(first->name,"Nigel S"); first->class = ''I''; return 100; } int main() { student_t first; int count=12; setdata(count,&first); printf("name is %s\n",first.name); return 1; } ==================================== Compiled with "-g", I would like to use dtrace to examine the structure. Using compiled in probes is not an option. I''ve tried something like this : ====================================#!/usr/sbin/dtrace -s typedef struct { int age; char name[24]; char class; } student_t; translator student_t <student_t *P> { age = *(int *)copyin((uintptr_t)&P->age, sizeof (int)); name = (char *)copyin((uintptr_t)*P->name, sizeof (char *)); class = *(char *)copyin((uintptr_t)&P->class, sizeof (char)); }; pid$target::setdata:return { xlate<struct student_t *>(arg2); printf("\nit is %i ", arg2->age); } ==================================== When run it returns : dtrace: failed to compile script ./structs.d: line 18: cannot translate from "int64_t" to "struct student_t *" There''s probably something obvious that I''m missing here. Any ideas ? If anyone had any example of examining a structure in thier own code using dtrace that would be great to see as well. TIA -- This message posted from opensolaris.org
Actually, my "xlate" line has been partially eaten in posting; it should be : xlate < struct student_t * >(arg2); -- This message posted from opensolaris.org
Hi, Hi, I''m typing this without any testing, so I might be slightly wrong somewhere. You don''t need translators. =======================================================================#!/usr/sbin/dtrace -s #pragma D option quiet /* this will stop dtrace to show whatever it shows by default, so that * your printf functions output won''t get distorted */ typedef struct { int age; char name[24]; char class; } student_t; pid$target::setdata:entry{ /* You have to remember the address in entry point */ self->addr = arg1; } pid$target::setdata:return / self-> addr / { /* Use this probe only when we got address at entry point first */ /* return probe uses only arg0 - address of the returning instruction in fuction */ /* and arg1 - return value */ student = (student_t*)copyin(self->addr, sizeof(student_t)); printf("it is %i\n", student->age); self->addr = 0; /* this will release self->addr variable, otherwise it will occupy dtrace''s memory */ } ======================================================================= Oh, and for 32bit binaries you should to run it using ''dtrace -32 -s .....'' Hope this helps -- Vlad -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 193 bytes Desc: not available URL: <http://mail.opensolaris.org/pipermail/dtrace-discuss/attachments/20080710/d4276321/attachment.bin>
> > translator student_t <student_t *P> { > age = *(int *)copyin((uintptr_t)&P->age, sizeof (int)); > name = (char *)copyin((uintptr_t)*P->name, sizeof (char *)); > class = *(char *)copyin((uintptr_t)&P->class, sizeof (char)); > }; > > pid$target::setdata:return > { > xlate<struct student_t *>(arg2); > printf("\nit is %i ", arg2->age); > } > > ====================================> > When run it returns : > dtrace: failed to compile script ./structs.d: line 18: cannot translate from > "int64_t" to "struct student_t *" > > There''s probably something obvious that I''m missing here. Any ideas ?Two problems: 1. Your translator''s input type is student_t *. The arg2 (and all argN) variables for PID provier have no types associated (they are just integers) So you have to cast arg2 to struct student *, or alternatively declare the translator input type as integer and cast inside of the translator. 2. Your clause consists of an xlate statement and a printf statement. xlate is not a function, it''s an operator -- think of it like a type cast. By making it a separate statement that statement has no effect. You need to printf("it is %i", (xlate<struct student_t *>(arg2))->age); or alternatively you can assign to a local variable etc. -Mike -- Mike Shapiro, Sun Microsystems Fishworks. blogs.sun.com/mws/
Thanks Mike and neuron, both of your suggestions worked out. Following Mike''s suggestion, this is what worked : pid$target::setdata:return { printf("\nit is %i ",(xlate < student_t * > (((student_t *)arg2)))->age); } (added just in case anyone else is curious to see the solution ) -- This message posted from opensolaris.org