Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Compile-time printf-like function's format string parsing

NotificationsYou must be signed in to change notification settings

0x7f454c46/cprintf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is GCC plugin, made to eliminate cost for parsing printf-like format strings in runtime by doing this at compile time.It uses GCC plugin API to walk through gimple tree, get format-string value and insert calls to specifiedto plugin functions instead of printf-alike. Note:this can be done only to constant format-stringparameters.

So, the idea is to change the function calls like this:

print_on_level(LOG_DEBUG, "cwd:%x swd:%x twd:%x fop:%x mxcsr:%x mxcsr_mask:%x\n",                (int)i387->cwd, (int)i387->swd, (int)i387->twd,                (int)i387->fop, (int)i387->mxcsr, (int)i387->mxcsr_mask);

To sequence of calls like:

print_str_level(LOG_DEBUG, "cwd:");print_int_level(LOG_DEBUG, (int)i387->cwd);print_str_level(LOG_DEBUG, " swd:");print_int_level(LOG_DEBUG, (int)i387->swd);print_str_level(LOG_DEBUG, " twd:");print_int_level(LOG_DEBUG, (int)i387->twd);print_str_level(LOG_DEBUG, " fop:");print_int_level(LOG_DEBUG, (int)i387->fop);print_str_level(LOG_DEBUG, " mxcsr:");print_int_level(LOG_DEBUG, (int)i387->mxcsr);print_str_level(LOG_DEBUG, " mxcsr_mask:");print_int_level(LOG_DEBUG, (int)i387->mxcsr_mask);print_str_level(LOG_DEBUG, "\n");

This will speed up the runtime by eliminating needless parsing of format string.All is done by GCC plugin during compilation.One should specify printf-alike functionprint_on_level andprint_str_level,print_int_levelas a plugin parameters.

TL;DR

To use this GCC compiler plugin, you need compile it with:

make

Then change your makefile so GCC will use plugin like this:

%.o: %.c    gcc -o $@ $< -fplugin=./cprintf.so -fplugin-arg-cprintf-printf="printf(0): %c putchar %s putstring  \                            %d putint %f putfloat %m puterrstr"

Whereprintf(0) means that format string is the first parameter.If you provide function likefprintf(1) which has some (one) parameters before format string,they will be passed to handlers in the same order.

Handlers:putchar function for%c specifier and so on.Note, specifier may be any length, ending with space symbol. I.e.,%h$up is a valid specifierh$up.

Reserved specifiers

For some user-defined functionfoo(arg1, arg2, const char *fmt, ...) cprintfplugin expects that following specifiers have their special meaning ifhandler-functions have been specified in plugin args:

  • %s is for strings (const char*). Cprintf will use it to output parts offormat string between specifiers; Function prototype in example:bar(arg1, arg2, const char *str);
  • %c is for chars (const char). Cprintf will use it to output single charsfrom format string; Function prototype in example:baz(arg1, arg2, const char s);
  • %% special meaning for raw string output; Function prototype in example:quux(arg1, arg2, void *ptr, size_t size, size_t nmemb);

Tip: consider using%% specifier asfwrite() function as it willgive great performance enhance.

About

Compile-time printf-like function's format string parsing

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp