[RFC,2/2] EXAMPLE: Print full DT path when adding a node

Message ID 20171006053029.1118-2-oohall@gmail.com
State RFC
Headers show
Series
  • [RFC,1/2] libc: Add custom printf specifiers
Related show

Commit Message

Oliver O'Halloran Oct. 6, 2017, 5:30 a.m.
An example of how to use the custom printf specifiers. In this case it
will print out the full node path when adding a node to
the device-tree, like so:

[    0.001493894,3] Added /ibm,opal to tree
[    0.001504982,3] Added /ibm,opal/event to tree
[    0.001512892,3] Added /ibm,opal/firmware to tree
[    0.001521920,3] Added /ibm,opal/firmware/exports to tree
[    0.001535936,3] Added /ibm,opal/power-mgt to tree

Not-signed-off-by: Oliver O'Halloran <oohall@gmail.com>
---
 core/device.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

Comments

Stewart Smith March 8, 2018, 1:09 a.m. | #1
Oliver O'Halloran <oohall@gmail.com> writes:
> An example of how to use the custom printf specifiers. In this case it
> will print out the full node path when adding a node to
> the device-tree, like so:
>
> [    0.001493894,3] Added /ibm,opal to tree
> [    0.001504982,3] Added /ibm,opal/event to tree
> [    0.001512892,3] Added /ibm,opal/firmware to tree
> [    0.001521920,3] Added /ibm,opal/firmware/exports to tree
> [    0.001535936,3] Added /ibm,opal/power-mgt to tree

Immediate uses I could see would be replacing the PHB(DBG|INFO|ERR)
macros that do the PHB#%04x[%d:%d]: printing, the lock printing code,
the mbox-flash window printing code, and some of the FSP message logging
stuff.

We can probably end up unit testing this code too without *tooo* much
swearing.

Is cleaning it up/converting somewhere on the radar at all?
Oliver O'Halloran March 8, 2018, 2:06 a.m. | #2
On Thu, Mar 8, 2018 at 12:09 PM, Stewart Smith
<stewart@linux.vnet.ibm.com> wrote:
> Oliver O'Halloran <oohall@gmail.com> writes:
>> An example of how to use the custom printf specifiers. In this case it
>> will print out the full node path when adding a node to
>> the device-tree, like so:
>>
>> [    0.001493894,3] Added /ibm,opal to tree
>> [    0.001504982,3] Added /ibm,opal/event to tree
>> [    0.001512892,3] Added /ibm,opal/firmware to tree
>> [    0.001521920,3] Added /ibm,opal/firmware/exports to tree
>> [    0.001535936,3] Added /ibm,opal/power-mgt to tree
>
> Immediate uses I could see would be replacing the PHB(DBG|INFO|ERR)
> macros that do the PHB#%04x[%d:%d]: printing, the lock printing code,
> the mbox-flash window printing code, and some of the FSP message logging
> stuff.
>
> We can probably end up unit testing this code too without *tooo* much
> swearing.

Due to the stupid linker trick I was doing unit testing this is a bit
hard. I was
going to rework it at some point but haven't gotten around to it yet.

things_to_do++

>
> Is cleaning it up/converting somewhere on the radar at all?
>
> --
> Stewart Smith
> OPAL Architect, IBM.
>
Stewart Smith March 8, 2018, 2:10 a.m. | #3
Oliver <oohall@gmail.com> writes:
> On Thu, Mar 8, 2018 at 12:09 PM, Stewart Smith
> <stewart@linux.vnet.ibm.com> wrote:
>> Oliver O'Halloran <oohall@gmail.com> writes:
>>> An example of how to use the custom printf specifiers. In this case it
>>> will print out the full node path when adding a node to
>>> the device-tree, like so:
>>>
>>> [    0.001493894,3] Added /ibm,opal to tree
>>> [    0.001504982,3] Added /ibm,opal/event to tree
>>> [    0.001512892,3] Added /ibm,opal/firmware to tree
>>> [    0.001521920,3] Added /ibm,opal/firmware/exports to tree
>>> [    0.001535936,3] Added /ibm,opal/power-mgt to tree
>>
>> Immediate uses I could see would be replacing the PHB(DBG|INFO|ERR)
>> macros that do the PHB#%04x[%d:%d]: printing, the lock printing code,
>> the mbox-flash window printing code, and some of the FSP message logging
>> stuff.
>>
>> We can probably end up unit testing this code too without *tooo* much
>> swearing.
>
> Due to the stupid linker trick I was doing unit testing this is a bit
> hard. I was
> going to rework it at some point but haven't gotten around to it yet.
>
> things_to_do++

I'm pretty sure that variable just overflowed.

Patch

diff --git a/core/device.c b/core/device.c
index fc1db5689a73..44463a9398a5 100644
--- a/core/device.c
+++ b/core/device.c
@@ -153,6 +153,8 @@  struct dt_node *dt_new(struct dt_node *parent, const char *name)
 		dt_destroy(new);
 		return NULL;
 	}
+
+	prerror("Added %pD to tree\n", new);
 	return new;
 }
 
@@ -1107,3 +1109,24 @@  void dt_adjust_subtree_phandle(struct dt_node *dev,
 
        set_last_phandle(max_phandle);
 }
+
+static int print_dt_node_path(char **buffer, size_t bufsize, const void *value)
+{
+	char *path = dt_get_path(value);
+	char *into = *buffer;
+	int i;
+
+	for (i = 0; path[i] && i < bufsize; i++)
+		into[i] = path[i];
+
+	*buffer += i;
+	free(path);
+
+	/* return the number of characters added to the buffer */
+	return i;
+}
+
+DECLARE_PRINTFMT(dt_node_path) = {
+	.specifier	= "D",
+	.func	 	= &print_dt_node_path,
+};