Discussion:
plugin: modify attribute arguments inside plugin
Klemen Jan Enova
2014-08-02 08:25:06 UTC
Permalink
I would like to code a plugin, that would count the number of accesses
to a field of some C struct.
That struct would be marked with __attribute__((access_count)). When
handling that attribute, I
would like to assign an index to it (as an attribute argument), so that
I can update the proper
element of my counter array, when there is a COMPONENT_REF with a marked
struct as the target.

But attribute arguments are normally passed by the user, can the plugin
change them, and how?
Juts checking that argument would be simpler and faster than looking for
the target's name
(in a hashtable, or just an ordered array if there aren't going to be so
many tracked structs)
and getting an index from that.
Klemen Jan Enova
2014-08-03 07:31:24 UTC
Permalink
I should have coded the plugin before asking. Sorry for that. I hope
this will provide context.

Here is the function, that sets the attribute argument:

static tree handle_field_access_count_attribute(tree *node, tree name,
tree args, int flags,
bool *no_add_attrs)
{
tree field;

*no_add_attrs = true;
if(TREE_CODE(*node) == RECORD_TYPE || TREE_CODE(*node) == UNION_TYPE) {
if(args != NULL_TREE)
error("attribute does not take any arguments");

/* TODO: Let TREE_VALUE(args) equal curr_index */

for(field = TYPE_FIELDS(*node), field, field = TREE_CHAIN(field))
curr_index++;

any_tracked_structs = true;
}

error("attribute applies to struct and union types only");

return NULL_TREE;
}

Here is the function, that uses the argument:

static size_t get_offset_from_component_ref(tree cref)
{
/*
* If foo is an instance of struct bar with the field x
* [cref: foo.x] -> [target: foo], [field: x]
*/
tree base = GET_OPERAND(cref, 0);
tree field = GET_OPERAND(cref, 1);
tree seek_field = TYPE_FIELDS(base);
size_t offset;

/*
* TODO: The offset of the first field of the struct was stored
* in the attribute arguments, get that.
*/

while(strcmp(IDENTIFIER_POINTER(seek_field), IDENTIFIER_POINTER(field))
{
offset++;
TREE_CHAIN(seek_field);
}

return offset;
}

The whole source is here: https://github.com/jkenova/field-count
I would like to code a plugin, that would count the number of accesses to a
field of some C struct.
That struct would be marked with __attribute__((access_count)). When
handling that attribute, I
would like to assign an index to it (as an attribute argument), so that I
can update the proper
element of my counter array, when there is a COMPONENT_REF with a marked
struct as the target.
But attribute arguments are normally passed by the user, can the plugin
change them, and how?
Juts checking that argument would be simpler and faster than looking for the
target's name
(in a hashtable, or just an ordered array if there aren't going to be so
many tracked structs)
and getting an index from that.
Continue reading on narkive:
Search results for 'plugin: modify attribute arguments inside plugin' (Questions and Answers)
4
replies
can we do system programming through java like that in C language?
started 2006-07-18 03:43:54 UTC
programming & design
Loading...