diff --git a/nemu/src/monitor/sdb/sdb.c b/nemu/src/monitor/sdb/sdb.c index 7d27d0f..4a1804e 100644 --- a/nemu/src/monitor/sdb/sdb.c +++ b/nemu/src/monitor/sdb/sdb.c @@ -185,12 +185,6 @@ static int cmd_info_w(char *args) { static int cmd_w(char *args) { char *expr = strtok(NULL, " "); - bool success = false; - parse_expr(expr, &success); - if (!success) { - Error("Failed to parse given expression `%s`", expr); - return 0; - } wp_add(expr); return 0; } diff --git a/nemu/src/monitor/sdb/watchpoint.c b/nemu/src/monitor/sdb/watchpoint.c index 8bd7bfd..5362ecd 100644 --- a/nemu/src/monitor/sdb/watchpoint.c +++ b/nemu/src/monitor/sdb/watchpoint.c @@ -56,7 +56,7 @@ static WP *wp_new() { } static void wp_delete(WP *wp) { - assert(wp); + Assert(wp, "Failed to delete watchpoint from pool."); wp->next = free_; free_ = wp; } @@ -65,7 +65,7 @@ int wp_add(char * expr) { WP *wp = wp_new(); if (wp == NULL) { Error("watchpoint: Failed to add watchpoint, pool is full."); - return 1; + goto failed_create; } wp->NO = wp_count++; @@ -76,15 +76,27 @@ int wp_add(char * expr) { tail->next = wp; tail = wp; } + + bool success = false; + wp->val = parse_expr(expr, &success); + if (!success) { + Error("Failed to parse given expression `%s`", expr); + goto failed_create; + } + int len = strlen(expr); wp->expr = malloc((len + 1) * sizeof(char)); if (wp->expr == NULL) { Error("Failed to allocate memory for expression"); - return 1; + goto failed_create; } strncpy(wp->expr, expr, len + 1); wp->expr[len] = '\0'; return 0; + +failed_create: + wp_delete(wp); + return 1; } int wp_remove_by_number(int number) {