From patchwork Tue Oct 8 13:20:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Thompson X-Patchwork-Id: 175494 Delivered-To: patches@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp5695256ill; Tue, 8 Oct 2019 06:21:00 -0700 (PDT) X-Received: by 2002:a7b:cf1a:: with SMTP id l26mr4050570wmg.38.1570540860815; Tue, 08 Oct 2019 06:21:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570540860; cv=none; d=google.com; s=arc-20160816; b=D37F+2cbCJb1G5JQMIyDU4vXAwrpuomOSEGbqWkE5B3A7W+sDRXhMB5JW5R2OxuJUf vWhIEBSPGud3H3PD6MxPErsB5OeUGCGao9onaUMGUjmRqk5YH8FYj+xP6LSEk3jzVzMY hO5vwzCr6YxyPjts0doQTX0v+LBc/BBYP8QZYuKuPF85J67GAnLtDnzTqQelQ1A3J7o8 +qYru0n/72WjWE8Atyi7rbmglGH/FHgYTXW+IXuRyQeOtPxAeH50f0xxyTd5WSb/uuyw EkFEtDMdDxlHQXHprvUYgW3FPYsXEKATb+ErlhUNO2AQId2vGcU6YYQRhGpOOnAByLGt UaYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=8fqv4PyrK6g1lPHnpAokAojNoS9v/RKHcLgZ0jjPkiE=; b=PiCPTYs+t1W19ruGDlaWvi+ADwtS9XYWqVD5rm8ALIJwUXgbu3vnEh+/TbElHQNW8m fY2yMTTpgsiiHfmbSZyEJFiedi+qp2whYmzNPtcbh6mjZDdiICtTIdIinj/oVDH5gVPC Kbu7+TgECL4nRU/+spjngRBMb9ylzOzErv3cAuw/H6VTZhxEzV5Eb69ujNVuE802Ja5v dgiPScxWDAaKTV2p8bqtNf3MDxsfTFTh1fgvVS4R9bqs7OlR871GBaVXEtJqkotkpmu1 4RM/M7R6JJwV5PL64276lfstlMukBL5RKl1iJHB4RjpSBz5e0WRIHnDLXYsAMh9RDMlV P19w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ODVHXP4I; spf=pass (google.com: domain of daniel.thompson@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=daniel.thompson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id a6sor11909463wrs.26.2019.10.08.06.21.00 for (Google Transport Security); Tue, 08 Oct 2019 06:21:00 -0700 (PDT) Received-SPF: pass (google.com: domain of daniel.thompson@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ODVHXP4I; spf=pass (google.com: domain of daniel.thompson@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=daniel.thompson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8fqv4PyrK6g1lPHnpAokAojNoS9v/RKHcLgZ0jjPkiE=; b=ODVHXP4IDvSf3+dsD48e/xrpRuCOaDhFwY0ETdD0g+c6W5KI6m9f5yRoSTouZN8Yoa cB9QrWlm2xGRhKVVoBeZZyg4zDzEneA3xVJe9H71eyZaMqEYORdlxcqqgb8mV9mnQyBQ wkGpS65Ls4slhxHIvY/msOGOsyw9cpyhf974IFkicg2q/OrLJCni/+cv8ObjWiPG/cyG wqlLu0tpZaHiCDwX4YbwOAAas61/U8mSvyqhbB/NAsZD/n1cy4GgTxgi3HLRYQgacHu/ XDTPKtBpW8VVmLtKDLcgyCFXphNOPz/jkK12Y7b1CnFhi6H4/bNBk4Z7Euzp4z3OsEnb dzsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8fqv4PyrK6g1lPHnpAokAojNoS9v/RKHcLgZ0jjPkiE=; b=j6GF0SAhcMP+Panx64LHIQDC8UMYvYEfbK5eM5xqWXIN7O/dJBtLEQkktJfpFlWs1o IYf21F7Wh6J6vLxzI/91k/oElJletvT0WlIR4kEhiMWwuwh0nWNHBf/LDsgWxkDAYuIz vR+3Dq4zLULZ6vioqUp3u/8XSpw7VwWsrfuxk7PVGFydguHzWvcBcqzVz4p4BcdskEEW uTX2iRVVcOdv0WpBtN3hTUpFH9LIiUEPkiL5PwdCAJihZrcRa/Rf2tEZ+5HrEmNpWMPg 9L+TWsfqvpMpVb+N3Pr67glEhPjhZFe516e2ttjQ4flx1HRdHaSHnAjsM1NRh6pZppsr iTMA== X-Gm-Message-State: APjAAAXNwroHkuELslVno/qoqVKrDLn3+M4Hjeuj8WQ52kK2vTge967z 4u+6W0UDry9YcaLLXl+Acs7X5SzA X-Google-Smtp-Source: APXvYqyvHhta90BElIHenHWd9+x1QhxfjH3LN4NyrzJGbBqTx/jDrOa/9mKvHs9er1NZWeb6VecSvA== X-Received: by 2002:a5d:5183:: with SMTP id k3mr25134077wrv.55.1570540860341; Tue, 08 Oct 2019 06:21:00 -0700 (PDT) Return-Path: Received: from wychelm.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.gmail.com with ESMTPSA id t8sm18237214wrx.76.2019.10.08.06.20.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2019 06:20:59 -0700 (PDT) From: Daniel Thompson To: Jason Wessel , Douglas Anderson Cc: Daniel Thompson , kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, patches@linaro.org Subject: [PATCH v2 1/5] kdb: Tidy up code to handle escape sequences Date: Tue, 8 Oct 2019 14:20:39 +0100 Message-Id: <20191008132043.7966-2-daniel.thompson@linaro.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191008132043.7966-1-daniel.thompson@linaro.org> References: <20191008132043.7966-1-daniel.thompson@linaro.org> MIME-Version: 1.0 kdb_read_get_key() has extremely complex break/continue control flow managed by state variables and is very hard to review or modify. In particular the way the escape sequence handling interacts with the general control flow is hard to follow. Separate out the escape key handling, without changing the control flow. This makes the main body of the code easier to review. Signed-off-by: Daniel Thompson --- kernel/debug/kdb/kdb_io.c | 127 ++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 61 deletions(-) -- 2.21.0 Reviewed-by: Douglas Anderson diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index 3a5184eb6977..68e2c29f14f5 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -49,6 +49,63 @@ static int kgdb_transition_check(char *buffer) return 0; } +/* + * kdb_read_handle_escape + * + * Run a validity check on an accumulated escape sequence. + * + * Returns -1 if the escape sequence is unwanted, 0 if it is incomplete, + * otherwise it returns a mapped key value to pass to the upper layers. + */ +static int kdb_read_handle_escape(char *buf, size_t sz) +{ + char *lastkey = buf + sz - 1; + + switch (sz) { + case 1: + if (*lastkey == '\e') + return 0; + break; + + case 2: /* \e */ + if (*lastkey == '[') + return 0; + break; + + case 3: + switch (*lastkey) { + case 'A': /* \e[A, up arrow */ + return 16; + case 'B': /* \e[B, down arrow */ + return 14; + case 'C': /* \e[C, right arrow */ + return 6; + case 'D': /* \e[D, left arrow */ + return 2; + case '1': /* \e[<1,3,4>], may be home, del, end */ + case '3': + case '4': + return 0; + } + break; + + case 4: + if (*lastkey == '~') { + switch (buf[2]) { + case '1': /* \e[1~, home */ + return 1; + case '3': /* \e[3~, del */ + return 4; + case '4': /* \e[4~, end */ + return 5; + } + } + break; + } + + return -1; +} + static int kdb_read_get_key(char *buffer, size_t bufsize) { #define ESCAPE_UDELAY 1000 @@ -102,68 +159,16 @@ static int kdb_read_get_key(char *buffer, size_t bufsize) escape_delay = 2; continue; } - if (ped - escape_data == 1) { - /* \e */ - continue; - } else if (ped - escape_data == 2) { - /* \e */ - if (key != '[') - escape_delay = 2; - continue; - } else if (ped - escape_data == 3) { - /* \e[ */ - int mapkey = 0; - switch (key) { - case 'A': /* \e[A, up arrow */ - mapkey = 16; - break; - case 'B': /* \e[B, down arrow */ - mapkey = 14; - break; - case 'C': /* \e[C, right arrow */ - mapkey = 6; - break; - case 'D': /* \e[D, left arrow */ - mapkey = 2; - break; - case '1': /* dropthrough */ - case '3': /* dropthrough */ - /* \e[<1,3,4>], may be home, del, end */ - case '4': - mapkey = -1; - break; - } - if (mapkey != -1) { - if (mapkey > 0) { - escape_data[0] = mapkey; - escape_data[1] = '\0'; - } - escape_delay = 2; - } - continue; - } else if (ped - escape_data == 4) { - /* \e[<1,3,4> */ - int mapkey = 0; - if (key == '~') { - switch (escape_data[2]) { - case '1': /* \e[1~, home */ - mapkey = 1; - break; - case '3': /* \e[3~, del */ - mapkey = 4; - break; - case '4': /* \e[4~, end */ - mapkey = 5; - break; - } - } - if (mapkey > 0) { - escape_data[0] = mapkey; - escape_data[1] = '\0'; - } - escape_delay = 2; - continue; + + key = kdb_read_handle_escape(escape_data, + ped - escape_data); + if (key > 0) { + escape_data[0] = key; + escape_data[1] = '\0'; } + if (key) + escape_delay = 2; + continue; } break; /* A key to process */ }