Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bpftrace
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
bpftrace
Commits
59626472
Commit
59626472
authored
Aug 05, 2017
by
Alastair Robertson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for escape characters in strings
parent
5a73475e
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
58 additions
and
23 deletions
+58
-23
README.md
README.md
+1
-1
src/bpftrace.cpp
src/bpftrace.cpp
+10
-15
src/driver.h
src/driver.h
+1
-0
src/lexer.l
src/lexer.l
+18
-3
src/parser.yy
src/parser.yy
+1
-3
src/printer.cpp
src/printer.cpp
+10
-1
src/printf.cpp
src/printf.cpp
+1
-0
tests/parser.cpp
tests/parser.cpp
+12
-0
tests/semantic_analyser.cpp
tests/semantic_analyser.cpp
+4
-0
No files found.
README.md
View file @
59626472
...
...
@@ -110,7 +110,7 @@ Functions:
-
`count()`
- count the number of times this function is called
-
`delete()`
- delete the map element this is assigned to
-
`str(char *s)`
- returns the string pointed to by
`s`
-
`printf(char *fmt, ...)`
- write to stdout
(support for escape characters is coming)
-
`printf(char *fmt, ...)`
- write to stdout
# Building
...
...
src/bpftrace.cpp
View file @
59626472
...
...
@@ -58,36 +58,31 @@ void perf_event_printer(void *cb_cookie, void *data, int size)
arg_data
+=
arg
.
size
;
}
// TODO remove when \n works with the lexer
std
::
string
fmt_newline
(
fmt
);
fmt_newline
+=
"
\n
"
;
switch
(
args
.
size
())
{
case
0
:
printf
(
fmt
_newline
.
c_str
()
);
printf
(
fmt
);
break
;
case
1
:
printf
(
fmt
_newline
.
c_str
()
,
arg_values
.
at
(
0
));
printf
(
fmt
,
arg_values
.
at
(
0
));
break
;
case
2
:
printf
(
fmt
_newline
.
c_str
()
,
arg_values
.
at
(
0
),
arg_values
.
at
(
1
));
printf
(
fmt
,
arg_values
.
at
(
0
),
arg_values
.
at
(
1
));
break
;
case
3
:
printf
(
fmt_newline
.
c_str
(),
arg_values
.
at
(
0
),
arg_values
.
at
(
1
),
arg_values
.
at
(
2
));
printf
(
fmt
,
arg_values
.
at
(
0
),
arg_values
.
at
(
1
),
arg_values
.
at
(
2
));
break
;
case
4
:
printf
(
fmt
_newline
.
c_str
(),
arg_values
.
at
(
0
),
arg_values
.
at
(
1
),
arg_values
.
at
(
2
),
arg_values
.
at
(
3
));
printf
(
fmt
,
arg_values
.
at
(
0
),
arg_values
.
at
(
1
),
arg_values
.
at
(
2
),
arg_values
.
at
(
3
));
break
;
case
5
:
printf
(
fmt
_newline
.
c_str
(),
arg_values
.
at
(
0
),
arg_values
.
at
(
1
),
arg_values
.
at
(
2
),
arg_values
.
at
(
3
),
arg_values
.
at
(
4
));
printf
(
fmt
,
arg_values
.
at
(
0
),
arg_values
.
at
(
1
),
arg_values
.
at
(
2
),
arg_values
.
at
(
3
),
arg_values
.
at
(
4
));
break
;
case
6
:
printf
(
fmt
_newline
.
c_str
(),
arg_values
.
at
(
0
),
arg_values
.
at
(
1
),
arg_values
.
at
(
2
),
arg_values
.
at
(
3
),
arg_values
.
at
(
4
),
arg_values
.
at
(
5
));
printf
(
fmt
,
arg_values
.
at
(
0
),
arg_values
.
at
(
1
),
arg_values
.
at
(
2
),
arg_values
.
at
(
3
),
arg_values
.
at
(
4
),
arg_values
.
at
(
5
));
break
;
default:
abort
();
...
...
src/driver.h
View file @
59626472
...
...
@@ -24,6 +24,7 @@ public:
void
error
(
const
location
&
l
,
const
std
::
string
&
m
)
{
std
::
cerr
<<
l
<<
": "
<<
m
<<
std
::
endl
;
exit
(
-
1
);
}
void
error
(
const
std
::
string
&
m
)
...
...
src/lexer.l
View file @
59626472
...
...
@@ -9,6 +9,7 @@
#define yywrap(x) 1
static bpftrace::location loc;
static std::string string_buffer;
#define YY_USER_ACTION loc.columns(yyleng);
#define yyterminate() return bpftrace::Parser::make_END(loc)
...
...
@@ -24,7 +25,7 @@ hspace [ \t]
vspace [\n\r]
space {hspace}|{vspace}
path :(\\.|[_\-\./a-zA-Z0-9])*:
string \"(\\.|[^\\"])*\"
%x STR
%%
...
...
@@ -38,7 +39,6 @@ string \"(\\.|[^\\"])*\"
{ident} { return Parser::make_IDENT(yytext, loc); }
{path} { return Parser::make_PATH(yytext, loc); }
{string} { return Parser::make_STRING(yytext, loc); }
{map} { return Parser::make_MAP(yytext, loc); }
{var} { return Parser::make_VAR(yytext, loc); }
{int} { return Parser::make_INT(strtoul(yytext, NULL, 0), loc); }
...
...
@@ -72,6 +72,21 @@ string \"(\\.|[^\\"])*\"
"!" { return Parser::make_LNOT(loc); }
"~" { return Parser::make_BNOT(loc); }
. { driver.error(loc, std::string("invalid character: ")+std::string(yytext)); exit(1); }
\" { BEGIN(STR); string_buffer.clear(); }
<STR>\" { BEGIN(INITIAL); return Parser::make_STRING(string_buffer, loc); }
<STR>[^\\\n\"]+ { string_buffer += std::string(yytext); }
<STR>\\n { string_buffer += std::string("\n"); }
<STR>\\t { string_buffer += std::string("\t"); }
<STR>\\\" { string_buffer += std::string("\""); }
<STR>\\\\ { string_buffer += std::string("\\"); }
<STR>\n { driver.error(loc, std::string("unterminated string")); }
<STR><<EOF>> { driver.error(loc, std::string("unterminated string")); }
<STR>\\. { driver.error(loc, std::string("invalid escape character '") +
std::string(yytext) + std::string("'")); }
<STR>. { driver.error(loc, std::string("invalid character '") +
std::string(yytext) + std::string("'")); }
. { driver.error(loc, std::string("invalid character '") +
std::string(yytext) + std::string("'")); }
%%
src/parser.yy
View file @
59626472
...
...
@@ -85,8 +85,6 @@ void yyerror(bpftrace::Driver &driver, const char *s);
%type <ast::Variable *> var
%type <ast::ExpressionList *> vargs
%printer { yyoutput << %%; } <*>;
%right ASSIGN
%left LOR
%left LAND
...
...
@@ -130,7 +128,7 @@ stmt : expr { $$ = new ast::ExprStatement($1); }
;
expr : INT { $$ = new ast::Integer($1); }
| STRING { $$ = new ast::String($1
.substr(1, $1.size()-2)
); }
| STRING { $$ = new ast::String($1); }
| IDENT { $$ = new ast::Builtin($1); }
| map { $$ = $1; }
| var { $$ = $1; }
...
...
src/printer.cpp
View file @
59626472
#include <regex>
#include "printer.h"
#include "ast.h"
...
...
@@ -13,7 +15,14 @@ void Printer::visit(Integer &integer)
void
Printer
::
visit
(
String
&
string
)
{
std
::
string
indent
(
depth_
,
' '
);
out_
<<
indent
<<
"string: "
<<
string
.
str
<<
std
::
endl
;
std
::
string
str
=
string
.
str
;
str
=
std
::
regex_replace
(
str
,
std
::
regex
(
"
\\\\
"
),
"
\\\\
"
);
str
=
std
::
regex_replace
(
str
,
std
::
regex
(
"
\n
"
),
"
\\
n"
);
str
=
std
::
regex_replace
(
str
,
std
::
regex
(
"
\t
"
),
"
\\
t"
);
str
=
std
::
regex_replace
(
str
,
std
::
regex
(
"
\"
"
),
"
\\\"
"
);
out_
<<
indent
<<
"string: "
<<
str
<<
std
::
endl
;
}
void
Printer
::
visit
(
Builtin
&
builtin
)
...
...
src/printf.cpp
View file @
59626472
...
...
@@ -40,6 +40,7 @@ std::string verify_format_string(const std::string &fmt, std::vector<SizedType>
case
'u'
:
case
'x'
:
case
'X'
:
case
'p'
:
token_type
=
Type
::
integer
;
break
;
case
's'
:
...
...
tests/parser.cpp
View file @
59626472
...
...
@@ -5,6 +5,8 @@
#include "printer.h"
namespace
bpftrace
{
namespace
test
{
namespace
parser
{
using
Printer
=
ast
::
Printer
;
...
...
@@ -202,4 +204,14 @@ TEST(Parser, uprobe)
" int: 1
\n
"
);
}
TEST
(
Parser
,
escape_chars
)
{
test
(
"kprobe:sys_open {
\"
newline
\\
nand tab
\\
tbackslash
\\\\
quote
\\\"
here
\"
}"
,
"Program
\n
"
" kprobe:sys_open
\n
"
" string: newline
\\
nand tab
\\
tbackslash
\\\\
quote
\\\"
here
\n
"
);
}
}
// namespace parser
}
// namespace test
}
// namespace bpftrace
tests/semantic_analyser.cpp
View file @
59626472
...
...
@@ -5,6 +5,8 @@
#include "semantic_analyser.h"
namespace
bpftrace
{
namespace
test
{
namespace
semantic_analyser
{
class
MockBPFtrace
:
public
BPFtrace
{
public:
...
...
@@ -185,4 +187,6 @@ TEST(semantic_analyser, printf_format_multi)
test
(
"kprobe:f { printf(
\"
%d %s %d
\"
, 1, 2,
\"
mystr
\"
) }"
,
10
);
}
}
// namespace semantic_analyser
}
// namespace test
}
// namespace bpftrace
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment