[Pdbg] [PATCH v2] main: Attempt to automatically discover target

Alistair Popple alistair at popple.id.au
Tue Feb 20 13:38:06 AEDT 2018


Thanks, I've just pushed both this and your other series upstream. It really
highlighted the need to have some more tests.

- Alistair

On Wednesday, 14 February 2018 3:31:47 PM AEDT Cyril Bur wrote:
> Signed-off-by: Cyril Bur <cyrilbur at gmail.com>
> ---
> V2: Fix/add error condition checks
>     Use AC_SUBST() as per Alistairs recommendation
>     Add information about possible targets/backends
> 
>  Makefile.am       |   3 +-
>  configure.ac      |   8 ++++
>  src/main.c        |  21 +++++++++-
>  src/main.h        |   2 +
>  src/options.h     |  36 +++++++++++++++++
>  src/options_arm.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  src/options_def.c |  50 +++++++++++++++++++++++
>  src/options_ppc.c |  92 ++++++++++++++++++++++++++++++++++++++++++
>  8 files changed, 328 insertions(+), 2 deletions(-)
>  create mode 100644 src/options.h
>  create mode 100644 src/options_arm.c
>  create mode 100644 src/options_def.c
>  create mode 100644 src/options_ppc.c
> 
> diff --git a/Makefile.am b/Makefile.am
> index 58666aa..4156b21 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -12,7 +12,8 @@ AM_CFLAGS = -I$(top_srcdir)/ccan/array_size -Wall -Werror
>  
>  pdbg_SOURCES = \
>  	src/main.c src/cfam.c src/scom.c src/reg.c src/mem.c src/thread.c \
> -	src/htm.c
> +	src/htm.c src/options_ at ARCH@.c
> +
>  pdbg_LDADD = fake.dtb.o p8-fsi.dtb.o p8-i2c.dtb.o p9w-fsi.dtb.o	p8-host.dtb.o \
>  	p9z-fsi.dtb.o p9r-fsi.dtb.o p9-kernel.dtb.o libpdbg.la libfdt.la \
>  	p9-host.dtb.o \
> diff --git a/configure.ac b/configure.ac
> index 43348d7..b73bf27 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -8,4 +8,12 @@ AC_CONFIG_MACRO_DIR([m4])
>  AC_CONFIG_HEADERS([config.h])
>  AC_CONFIG_FILES([Makefile])
>  AC_LANG(C)
> +
> +case "$host" in
> +	arm*-*-*)       ARCH="arm" ;;
> +	powerpc*-*-*)   ARCH="ppc" ;;
> +	*)              ARCH="def" ;;
> +esac
> +AC_SUBST([ARCH])
> +
>  AC_OUTPUT
> diff --git a/src/main.c b/src/main.c
> index 11b578e..91b9ef5 100644
> --- a/src/main.c
> +++ b/src/main.c
> @@ -32,6 +32,7 @@
>  
>  #include <libpdbg.h>
>  
> +#include "main.h"
>  #include "bitutils.h"
>  #include "cfam.h"
>  #include "scom.h"
> @@ -39,6 +40,7 @@
>  #include "mem.h"
>  #include "thread.h"
>  #include "htm.h"
> +#include "options.h"
>  
>  #undef PR_DEBUG
>  #define PR_DEBUG(...)
> @@ -47,7 +49,6 @@
>  
>  #define THREADS_PER_CORE	8
>  
> -enum backend { FSI, I2C, KERNEL, FAKE, HOST };
>  static enum backend backend = KERNEL;
>  
>  static char const *device_node;
> @@ -462,9 +463,27 @@ int main(int argc, char *argv[])
>  {
>  	int i, rc = 0;
>  
> +	backend = default_backend();
> +	device_node = default_target(backend);
> +
>  	if (parse_options(argc, argv))
>  		return 1;
>  
> +	if (!backend_is_possible(backend)) {
> +		fprintf(stderr, "Backend not possible\n");
> +		print_backends(stderr);
> +		print_usage(argv[0]);
> +		return 1;
> +	}
> +
> +	if (!target_is_possible(backend, device_node)) {
> +		fprintf(stderr, "Target %s not possible\n",
> +			device_node ? device_node : "(none)");
> +		print_targets(stderr);
> +		print_usage(argv[0]);
> +		return 1;
> +	}
> +
>  	if (optind >= argc) {
>  		print_usage(argv[0]);
>  		return 1;
> diff --git a/src/main.h b/src/main.h
> index 55fbdd4..71599ee 100644
> --- a/src/main.h
> +++ b/src/main.h
> @@ -17,6 +17,8 @@
>  
>  #include <target.h>
>  
> +enum backend { FSI, I2C, KERNEL, FAKE, HOST };
> +
>  static inline bool target_is_disabled(struct pdbg_target *target)
>  {
>  	return pdbg_target_status(target) == PDBG_TARGET_DISABLED;
> diff --git a/src/options.h b/src/options.h
> new file mode 100644
> index 0000000..54436b6
> --- /dev/null
> +++ b/src/options.h
> @@ -0,0 +1,36 @@
> +/* Copyright 2017 IBM Corp.
> + *
> + * Licensed under the Apache License, Version 2.0 (the "License");
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at
> + *
> + * 	http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> + * implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +
> +/* Default backend on this platform */
> +enum backend default_backend(void);
> +
> +/* Print all possible backends on this platform */
> +void print_backends(FILE *stream);
> +
> +/* Is this backend possible on this platform */
> +bool backend_is_possible(enum backend backend);
> +
> +/* The default (perhaps only) target for this backend */
> +const char *default_target(enum backend backend);
> +
> +/* Print all possible targets on this platform */
> +void print_targets(FILE *stream);
> +
> +/*
> + * Does this platform backend support this target,
> + * there is an implied check of is_backend_possible()
> + */
> +bool target_is_possible(enum backend backend, const char *target);
> diff --git a/src/options_arm.c b/src/options_arm.c
> new file mode 100644
> index 0000000..6b1668b
> --- /dev/null
> +++ b/src/options_arm.c
> @@ -0,0 +1,118 @@
> +/* Copyright 2017 IBM Corp.
> + *
> + * Licensed under the Apache License, Version 2.0 (the "License");
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at
> + *
> + * 	http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> + * implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +#include <stdio.h>
> +#include <string.h>
> +#include <unistd.h>
> +
> +#include "main.h"
> +
> +#define AMI_BMC "/proc/ractrends/Helper/FwInfo"
> +#define OPENFSI_BMC "/sys/bus/platform/devices/gpio-fsi/fsi0/"
> +
> +static const char witherspoon[] = "witherspoon";
> +static const char romulus[] = "romulus";
> +static const char zaius[] = "zaius";
> +
> +enum backend default_backend(void)
> +{
> +	int rc;
> +	rc = access(AMI_BMC, F_OK);
> +	if (rc == 0) /* AMI BMC */
> +		return I2C;
> +
> +	rc = access(OPENFSI_BMC, F_OK);
> +	if (rc == 0) /* Kernel interface. OpenBMC */
> +		return KERNEL;
> +
> +	/*
> +	 * "This should never be the default" - Apopple 2017
> +	 */
> +	fprintf(stderr, "Couldn't locate a good backend.\n");
> +	fprintf(stderr, "It is possible that the FSI backend will work.\n");
> +	fprintf(stderr, "You will need to select this along with the correct\n");
> +	fprintf(stderr, "target yourself on the commandline\n");
> +	fprintf(stderr, "`pdbg -b fsi -d [p8 | p9w | p9r | p9z] ...`\n");
> +	return FAKE;
> +}
> +
> +void print_backends(FILE *stream)
> +{
> +	fprintf(stream, "I2C KERNEL FSI");
> +}
> +
> +bool backend_is_possible(enum backend backend)
> +{
> +	if (backend == I2C && access(AMI_BMC, F_OK) == 0)
> +		return true;
> +	if (backend == KERNEL && access(OPENFSI_BMC, F_OK) == 0)
> +		return true;
> +
> +	return backend == FSI;
> +}
> +
> +void print_targets(FILE *stream)
> +{
> +	fprintf(stream, "KERNEL: No target is necessary\n");
> +	fprintf(stream, "I2C: No target is necessary\n");
> +	fprintf(stream, "FSI: p8 p9w p9r p9z\n");
> +}
> +
> +const char *default_target(enum backend backend)
> +{
> +	FILE *dt_compatible;
> +	char line[256];
> +	char *p;
> +
> +	if (backend == I2C || backend == KERNEL) /* No target nessesary */
> +		return NULL;
> +
> +	dt_compatible = fopen("/proc/device-tree/compatible", "r");
> +	if (!dt_compatible)
> +		return NULL;
> +
> +	p = fgets(line, sizeof(line), dt_compatible);
> +	fclose(dt_compatible);
> +	if (!p) /* Uh oh*/
> +		return NULL;
> +
> +	if (strstr(witherspoon, line))
> +		return witherspoon;
> +
> +	if (strstr(romulus, line))
> +		return romulus;
> +
> +	if (strstr(zaius, line))
> +		return zaius;
> +
> +	return NULL;
> +}
> +
> +bool target_is_possible(enum backend backend, const char *target)
> +{
> +	const char *def;
> +
> +	if (!backend_is_possible(backend))
> +		return false;
> +
> +	if (backend == I2C || backend == KERNEL) /* No target is nessesary */
> +		return true;
> +
> +	def = default_target(backend);
> +	if (!def || !target)
> +		return false;
> +
> +	return strcmp(def, target) == 0;
> +}
> diff --git a/src/options_def.c b/src/options_def.c
> new file mode 100644
> index 0000000..719fcde
> --- /dev/null
> +++ b/src/options_def.c
> @@ -0,0 +1,50 @@
> +/* Copyright 2017 IBM Corp.
> + *
> + * Licensed under the Apache License, Version 2.0 (the "License");
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at
> + *
> + * 	http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> + * implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +
> +#include <stdio.h>
> +#include "main.h"
> +
> +enum backend default_backend(void)
> +{
> +	return FAKE;
> +}
> +
> +bool backend_is_possible(enum backend backend)
> +{
> +	return backend == FAKE;
> +}
> +
> +void print_backends(FILE *stream)
> +{
> +	fprintf(stream, "FAKE");
> +}
> +
> +/* Theres no target for FAKE backend */
> +const char *default_target(enum backend backend)
> +{
> +	return NULL;
> +}
> +
> +void print_targets(FILE *stream)
> +{
> +	fprintf(stream, "FAKE: No target is necessary\n");
> +}
> +
> +/* Theres no device for FAKE backend */
> +bool target_is_possible(enum backend backend, const char *target)
> +{
> +	return target == NULL;
> +}
> diff --git a/src/options_ppc.c b/src/options_ppc.c
> new file mode 100644
> index 0000000..30ed5bb
> --- /dev/null
> +++ b/src/options_ppc.c
> @@ -0,0 +1,92 @@
> +/* Copyright 2017 IBM Corp.
> + *
> + * Licensed under the Apache License, Version 2.0 (the "License");
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at
> + *
> + * 	http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> + * implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +#include <stdio.h>
> +#include <string.h>
> +
> +#include "main.h"
> +
> +static const char p8[] = "p8";
> +static const char p9[] = "p9";
> +
> +enum backend default_backend(void)
> +{
> +	return HOST;
> +}
> +
> +bool backend_is_possible(enum backend backend)
> +{
> +	/* TODO */
> +	return backend == HOST;
> +}
> +
> +void print_backends(FILE *stream)
> +{
> +	fprintf(stream, "HOST");
> +}
> +
> +const char *default_target(enum backend backend)
> +{
> +	const char *pos = NULL;
> +	char line[256];
> +	FILE *cpuinfo;
> +
> +	cpuinfo = fopen("/proc/cpuinfo", "r");
> +	if (!cpuinfo)
> +		return NULL;
> +
> +	while ((pos = fgets(line, sizeof(line), cpuinfo)))
> +		if (strncmp(line, "cpu", 3) == 0)
> +			break;
> +	fclose(cpuinfo);
> +
> +	if (!pos) /* Got to EOF without a break */
> +		return NULL;
> +
> +	pos = strchr(line, ':');
> +	if (!pos)
> +		return NULL;
> +
> +	if (*(pos + 1) == '\0')
> +		return NULL;
> +	pos += 2;
> +
> +	if (strncmp(pos, "POWER8", 6) == 0)
> +		return p8;
> +
> +	if (strncmp(pos, "POWER9", 6) == 0)
> +		return p9;
> +
> +	return NULL;
> +}
> +
> +void print_targets(FILE *stream)
> +{
> +	fprintf(stream, "HOST: p8 p9\n");
> +}
> +
> +bool target_is_possible(enum backend backend, const char *target)
> +{
> +	const char *def;
> +
> +	if (!backend_is_possible(backend))
> +		return false;
> +
> +	def = default_target(backend);
> +	if (!def || !target)
> +		return false;
> +
> +	return strcmp(def, target) == 0;
> +}
> 




More information about the Pdbg mailing list