Permalink
Browse files

7085 add support for "if" and "else" statements in dtrace

Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Sebastien Roy <sebastien.roy@delphix.com>
Reviewed by: Paul Dagnelie <pcd@delphix.com>
Reviewed by: Bryan Cantrill <bryan@joyent.com>
Approved by: Richard Lowe <richlowe@richlowe.net>
  • Loading branch information...
ahrens committed Aug 8, 2016
1 parent cfc3b49 commit c3bd3abd8856e8e75d820f65c58031cd6cbac818
@@ -25,6 +25,10 @@
# Use is subject to license terms.
#

#
# Copyright (c) 2014, 2016 by Delphix. All rights reserved.
#

require 5.8.4;

$PNAME = $0;
@@ -131,7 +135,8 @@ sub dstyle
}

if (!/^enum/ && !/^\t*struct/ && !/^\t*union/ && !/^typedef/ &&
!/^translator/ && !/^provider/) {
!/^translator/ && !/^provider/ && !/\tif / &&
!/ else /) {
if (/[\w\s]+{/) {
err "left brace not on its own line";
}
@@ -141,7 +146,7 @@ sub dstyle
}
}

if (!/;$/) {
if (!/;$/ && !/\t*}$/ && !/ else /) {
if (/[\w\s]+}/) {
err "right brace not on its own line";
}
@@ -0,0 +1,33 @@
/*
* CDDL HEADER START
*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2014, 2016 by Delphix. All rights reserved.
*/

/*
* ASSERTION:
* "else" statement is executed
*/

BEGIN
{
if (0) {
n = 1;
} else {
n = 0;
}
exit(n)
}
@@ -0,0 +1,33 @@
/*
* CDDL HEADER START
*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2014, 2016 by Delphix. All rights reserved.
*/

/*
* ASSERTION:
* "if" statement executes the correct body.
*/

BEGIN
{
if (1) {
n = 0;
} else {
n = 1;
}
exit(n)
}
@@ -0,0 +1,33 @@
/*
* CDDL HEADER START
*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2014, 2016 by Delphix. All rights reserved.
*/

/*
* ASSERTION:
* "if" statement executes the correct body.
* parses single-statement, braceless bodies correctly.
*/

BEGIN
{
if (1)
n = 0;
else
n = 1;
exit(n)
}
@@ -0,0 +1,46 @@
/*
* CDDL HEADER START
*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2014, 2016 by Delphix. All rights reserved.
*/

/*
* ASSERTION:
* statements before and after an if statement are executed.
*/

BEGIN
{
i = 1;
if (1) {
i++;
} else {
i++;
}
i++;
}

BEGIN
/i == 3/
{
exit(0);
}

BEGIN
/i != 3/
{
exit(1);
}
@@ -0,0 +1,38 @@
/*
* CDDL HEADER START
*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2014, 2016 by Delphix. All rights reserved.
*/

/*
* ASSERTION:
* nested "if" statement executes the correct body.
*/

BEGIN
{
if (0) {
exit(1);
} else {
if (0) {
exit(1);
} else {
exit(0);
}
exit(1);
}
exit(1);
}
@@ -0,0 +1,30 @@
/*
* CDDL HEADER START
*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2014, 2016 by Delphix. All rights reserved.
*/

/*
* ASSERTION:
* "if" body without trailing semicolon parses correctly
*/

BEGIN
{
if (1) {
exit(0)
}
}
@@ -0,0 +1,31 @@
/*
* CDDL HEADER START
*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2014, 2016 by Delphix. All rights reserved.
*/

/*
* ASSERTION:
* "if" body without trailing semicolon parses correctly
*/

BEGIN
{
if (1) {
i = 1;
exit(0)
}
}
@@ -20,7 +20,7 @@
#
#
# Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2012 by Delphix. All rights reserved.
# Copyright (c) 2011, 2016 by Delphix. All rights reserved.
#

LIBRARY = libdtrace.a
@@ -38,6 +38,7 @@ LIBSRCS = \
dt_dof.c \
dt_error.c \
dt_errtags.c \
dt_sugar.c \
dt_handle.c \
dt_ident.c \
dt_inttab.c \
@@ -21,8 +21,8 @@

/*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2016 by Delphix. All rights reserved.
* Copyright (c) 2013, Joyent Inc. All rights reserved.
* Copyright (c) 2012 by Delphix. All rights reserved.
* Copyright 2015 Gary Mills
*/

@@ -119,7 +119,6 @@ static const dtrace_diftype_t dt_int_rtype = {
static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
uint_t, int, char *const[], FILE *, const char *);


/*ARGSUSED*/
static int
dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
@@ -2418,6 +2417,28 @@ dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
"not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
}

/*
* Perform sugar transformations (for "if" / "else") and replace the
* existing clause chain with the new one.
*/
if (context == DT_CTX_DPROG) {
dt_node_t *dnp, *next_dnp;
dt_node_t *new_list = NULL;

for (dnp = yypcb->pcb_root->dn_list;
dnp != NULL; dnp = next_dnp) {
/* remove this node from the list */
next_dnp = dnp->dn_list;
dnp->dn_list = NULL;

if (dnp->dn_kind == DT_NODE_CLAUSE)
dnp = dt_compile_sugar(dtp, dnp);
/* append node to the new list */
new_list = dt_node_link(new_list, dnp);
}
yypcb->pcb_root->dn_list = new_list;
}

/*
* If we have successfully created a parse tree for a D program, loop
* over the clauses and actions and instantiate the corresponding
@@ -2438,6 +2459,8 @@ dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
for (; dnp != NULL; dnp = dnp->dn_list) {
switch (dnp->dn_kind) {
case DT_NODE_CLAUSE:
if (DT_TREEDUMP_PASS(dtp, 4))
dt_printd(dnp, stderr, 0);
dt_compile_clause(dtp, dnp);
break;
case DT_NODE_XLATOR:
Oops, something went wrong.

0 comments on commit c3bd3ab

Please sign in to comment.