Skip to content

Commit

Permalink
Merge branch 'enum-fix'
Browse files Browse the repository at this point in the history
Fix enum display on negative numbers or bit masks with MSB set.
Do not sign-extend values when comparing.  Thanks a lot Honggyu
for reporting and debugging the issues.

Fixed: #1784
Signed-off-by: Namhyung Kim <[email protected]>
  • Loading branch information
namhyung committed Dec 20, 2023
2 parents 594ee00 + f73febf commit ad86221
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmds/replay.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ void get_argspec_string(struct uftrace_task_reader *task, char *args, size_t len
}

dinfo = &map->mod->dinfo;
estr = get_enum_string(&dinfo->enums, spec->type_name, (int)val.i);
estr = get_enum_string(&dinfo->enums, spec->type_name, val.i);
if (strstr(estr, "|") && strcmp("|", color_enum_or)) {
struct strv enum_vals = STRV_INIT;

Expand Down
24 changes: 24 additions & 0 deletions tests/s-enum2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
static int cnt;

enum memory_order_modifier {
zero = 0,
memory_order_mask = 0x0ffff,
memory_order_modifier_mask = 0xffff0000,
memory_order_hle_acquire = 0x10000,
memory_order_hle_release = 0x20000
};

__attribute__((noinline)) void foo(enum memory_order_modifier m)
{
if (m != zero)
cnt++;
}

int main()
{
foo(memory_order_mask);
foo(memory_order_modifier_mask);
foo(memory_order_hle_acquire);
foo(memory_order_hle_release);
return cnt == 4 ? 0 : -1;
}
29 changes: 29 additions & 0 deletions tests/t287_arg_enum3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3

import re

from runtest import TestBase

class TestCase(TestBase):
def __init__(self):
TestBase.__init__(self, 'enum2', result="""
# DURATION TID FUNCTION
[ 57041] | main() {
0.535 us [ 57041] | foo(memory_order_mask);
0.109 us [ 57041] | foo(memory_order_modifier_mask);
0.069 us [ 57041] | foo(memory_order_hle_acquire);
0.068 us [ 57041] | foo(memory_order_hle_release);
1.783 us [ 57041] | } = 0; /* main */
""", cflags='-g')

def build(self, name, cflags='', ldflags=''):
if not "dwarf" in self.feature:
return TestBase.TEST_SKIP
# cygprof doesn't support arguments now
if cflags.find('-finstrument-functions') >= 0:
return TestBase.TEST_SKIP

return TestBase.build(self, name, cflags, ldflags)

def setup(self):
self.option = '-F main -a'
2 changes: 1 addition & 1 deletion utils/argspec.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ char *get_auto_retspec_str(void);
char *get_auto_enum_str(void);
int extract_trigger_args(char **pargs, char **prets, char *trigger);
int parse_enum_string(char *enum_str, struct rb_root *root);
char *get_enum_string(struct rb_root *root, char *name, int val);
char *get_enum_string(struct rb_root *root, char *name, long val);
void save_enum_def(struct rb_root *root, FILE *fp);
void release_enum_def(struct rb_root *root);

Expand Down
12 changes: 6 additions & 6 deletions utils/auto-args.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ struct enum_def *find_enum_def(struct rb_root *root, char *name)
return NULL;
}

char *convert_enum_val(struct enum_def *e_def, int val)
char *convert_enum_val(struct enum_def *e_def, long val)
{
struct enum_val *e_val;
char *str = NULL;
Expand All @@ -529,22 +529,22 @@ char *convert_enum_val(struct enum_def *e_def, int val)
if (str && val) {
char *tmp;

xasprintf(&tmp, "%s+%#x", str, val);
xasprintf(&tmp, "%s+%#lx", str, val);
free(str);
str = tmp;
}
else if (unlikely(str == NULL)) {
if (labs(val) > 100000)
xasprintf(&str, "%#x", val);
xasprintf(&str, "%#lx", val);
else
xasprintf(&str, "%d", val);
xasprintf(&str, "%ld", val);
}

return str;
}

/* caller should free the return value */
char *get_enum_string(struct rb_root *root, char *name, int val)
char *get_enum_string(struct rb_root *root, char *name, long val)
{
struct enum_def *e_def;
char *ret;
Expand All @@ -554,7 +554,7 @@ char *get_enum_string(struct rb_root *root, char *name, int val)
e_def = find_enum_def(&auto_enum, name);

if (e_def == NULL)
xasprintf(&ret, "%d", val);
xasprintf(&ret, "%ld", val);
else
ret = convert_enum_val(e_def, val);

Expand Down
13 changes: 13 additions & 0 deletions utils/dwarf.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,19 @@ static long int_attr(Dwarf_Die *die, int attr, bool follow)
return 0;

dwarf_formsdata(&da, &data);
switch (dwarf_whatform(&da)) {
case DW_FORM_data1:
data &= 0xff;
break;
case DW_FORM_data2:
data &= 0xffff;
break;
case DW_FORM_data4:
data &= 0xffffffff;
break;
default:
break;
}
return data;
}

Expand Down

0 comments on commit ad86221

Please sign in to comment.