Skip to contentSkip to conversation

update gdb scripts for lockless printk ringbuffer

PATCH In review John Ogness 6 participants 12 messages Aug 14, 2020 – Sep 1, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover update gdb scripts for lockless printk ringbuffer

Hi,

When we brought in the new lockless printk ringbuffer, we overlooked the gdb scripts. Here are a set of patches to implement gdb support for the new ringbuffer.

John Ogness (2):
scripts/gdb: add utils.read_ulong() scripts/gdb: update for lockless printk ringbuffer

 Documentation/admin-guide/kdump/gdbmacros.txt | 153 ++++++++++++------
 scripts/gdb/linux/dmesg.py                    | 139 +++++++++++-----
 scripts/gdb/linux/utils.py                    |   7 +
 3 files changed, 216 insertions(+), 83 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 scripts/gdb: add utils.read_ulong()

Add a function for reading unsigned long values, which vary in size depending on the architecture.

---

based on next-20200814

 scripts/gdb/linux/utils.py | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/gdb/linux/utils.py +7 -0
index ea94221dbd39..ff7c1799d588 100644
@@ -123,6 +123,13 @@ def read_u64(buffer, offset):
         return read_u32(buffer, offset + 4) + (read_u32(buffer, offset) << 32)
 
 
+def read_ulong(buffer, offset):
+    if get_long_type().sizeof == 8:
+        return read_u64(buffer, offset)
+    else:
+        return read_u32(buffer, offset)
+
+
 target_arch = None
 
 
Signature · 1 line
2.20.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2][next] scripts/gdb: add utils.read_ulong()

Quoted 5 lines · John Ogness

Add a function for reading unsigned long values, which vary in size depending on the architecture.

/me wonders if there's any non-ILP32 or LP64 ARCH='s supported by the kernel.

Quoted 28 lines

---

based on next-20200814

 scripts/gdb/linux/utils.py | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/gdb/linux/utils.py +7 -1
index ea94221dbd39..ff7c1799d588 100644
@@ -123,6 +123,13 @@ def read_u64(buffer, offset):
         return read_u32(buffer, offset + 4) + (read_u32(buffer, offset) << 32)
 
 
+def read_ulong(buffer, offset):
+    if get_long_type().sizeof == 8:
+        return read_u64(buffer, offset)
+    else:
+        return read_u32(buffer, offset)
+
+
 target_arch = None
 
 
--

2.20.1

Signature · 2 lines
Thanks,
~Nick Desaulniers
#3 lore raw

PATCH 2/2 scripts/gdb: update for lockless printk ringbuffer

In reply to John Ogness

With the introduction of the lockless printk ringbuffer, the data structure for the kernel log buffer was changed. Update the gdb scripts to be able to parse/print the new log buffer structure.

Fixes ("printk: use the lockless ringbuffer")

---

based on next-20200814

 Documentation/admin-guide/kdump/gdbmacros.txt | 153 ++++++++++++------
 scripts/gdb/linux/dmesg.py                    | 139 +++++++++++-----
 2 files changed, 209 insertions(+), 83 deletions(-)
Documentation/admin-guide/kdump/gdbmacros.txt +107 -46
index 220d0a80ca2c..6025534c6c14 100644
@@ -170,57 +170,111 @@ document trapinfo
 	address the kernel panicked.
 end
 
-define dump_log_idx
-	set $idx = $arg0
+define dump_record
+	set var $desc = $arg0
 	if ($argc > 1)
-		set $prev_flags = $arg1
+		set var $prev_flags = $arg1
 	else
-		set $prev_flags = 0
+		set var $prev_flags = 0
 	end
-	set $msg = ((struct printk_log *) (log_buf + $idx))
-	set $prefix = 1
-	set $newline = 1
-	set $log = log_buf + $idx + sizeof(*$msg)
-
-	# prev & LOG_CONT && !(msg->flags & LOG_PREIX)
-	if (($prev_flags & 8) && !($msg->flags & 4))
-		set $prefix = 0
+
+	set var $info = &$desc->info
+	set var $prefix = 1
+	set var $newline = 1
+
+	set var $begin = $desc->text_blk_lpos.begin % (1U << prb->text_data_ring.size_bits)
+	set var $next = $desc->text_blk_lpos.next % (1U << prb->text_data_ring.size_bits)
+
+	# handle data-less record
+	if ($begin & 1)
+		set var $text_len = 0
+		set var $log = ""
+	else
+		# handle wrapping data block
+		if ($begin > $next)
+			set var $begin = 0
+		end
+
+		# skip over descriptor id
+		set var $begin = $begin + sizeof(long)
+
+		# handle truncated message
+		if ($next - $begin < $info->text_len)
+			set var $text_len = $next - $begin
+		else
+			set var $text_len = $info->text_len
+		end
+
+		set var $log = &prb->text_data_ring.data[$begin]
+	end
+
+	# prev & LOG_CONT && !(info->flags & LOG_PREIX)
+	if (($prev_flags & 8) && !($info->flags & 4))
+		set var $prefix = 0
 	end
 
-	# msg->flags & LOG_CONT
-	if ($msg->flags & 8)
+	# info->flags & LOG_CONT
+	if ($info->flags & 8)
 		# (prev & LOG_CONT && !(prev & LOG_NEWLINE))
 		if (($prev_flags & 8) && !($prev_flags & 2))
-			set $prefix = 0
+			set var $prefix = 0
 		end
-		# (!(msg->flags & LOG_NEWLINE))
-		if (!($msg->flags & 2))
-			set $newline = 0
+		# (!(info->flags & LOG_NEWLINE))
+		if (!($info->flags & 2))
+			set var $newline = 0
 		end
 	end
 
 	if ($prefix)
-		printf "[%5lu.%06lu] ", $msg->ts_nsec / 1000000000, $msg->ts_nsec % 1000000000
+		printf "[%5lu.%06lu] ", $info->ts_nsec / 1000000000, $info->ts_nsec % 1000000000
 	end
-	if ($msg->text_len != 0)
-		eval "printf \"%%%d.%ds\", $log", $msg->text_len, $msg->text_len
+	if ($text_len)
+		eval "printf \"%%%d.%ds\", $log", $text_len, $text_len
 	end
 	if ($newline)
 		printf "\n"
 	end
-	if ($msg->dict_len > 0)
-		set $dict = $log + $msg->text_len
-		set $idx = 0
-		set $line = 1
-		while ($idx < $msg->dict_len)
+
+	# handle dictionary data
+
+	set var $begin = $desc->dict_blk_lpos.begin % (1U << prb->dict_data_ring.size_bits)
+	set var $next = $desc->dict_blk_lpos.next % (1U << prb->dict_data_ring.size_bits)
+
+	# handle data-less record
+	if ($begin & 1)
+		set var $dict_len = 0
+		set var $dict = ""
+	else
+		# handle wrapping data block
+		if ($begin > $next)
+			set var $begin = 0
+		end
+
+		# skip over descriptor id
+		set var $begin = $begin + sizeof(long)
+
+		# handle truncated message
+		if ($next - $begin < $info->dict_len)
+			set var $dict_len = $next - $begin
+		else
+			set var $dict_len = $info->dict_len
+		end
+
+		set var $dict = &prb->dict_data_ring.data[$begin]
+	end
+
+	if ($dict_len > 0)
+		set var $idx = 0
+		set var $line = 1
+		while ($idx < $dict_len)
 			if ($line)
 				printf " "
-				set $line = 0
+				set var $line = 0
 			end
-			set $c = $dict[$idx]
+			set var $c = $dict[$idx]
 			if ($c == '\0')
 				printf "\n"
-				set $line = 1
+				set var $line = 1
 			else
 				if ($c < ' ' || $c >= 127 || $c == '\\')
 					printf "\\x%02x", $c
@@ -228,33 +282,40 @@ define dump_log_idx
 					printf "%c", $c
 				end
 			end
-			set $idx = $idx + 1
+			set var $idx = $idx + 1
 		end
 		printf "\n"
 	end
 end
-document dump_log_idx
-	Dump a single log given its index in the log buffer.  The first
-	parameter is the index into log_buf, the second is optional and
-	specified the previous log buffer's flags, used for properly
-	formatting continued lines.
+document dump_record
+	Dump a single record. The first parameter is the descriptor
+	sequence number, the the second is optional and specifies the
+	previous record's flags, used for properly formatting
+	continued lines.
 end
 
 define dmesg
-	set $i = log_first_idx
-	set $end_idx = log_first_idx
-	set $prev_flags = 0
+	set var $desc_committed = 1UL << ((sizeof(long) * 8) - 1)
+	set var $flags_mask = 3UL << ((sizeof(long) * 8) - 2)
+	set var $id_mask = ~$flags_mask
+
+	set var $desc_count = 1U << prb->desc_ring.count_bits
+	set var $prev_flags = 0
+
+	set var $id = prb->desc_ring.tail_id.counter
+	set var $end_id = prb->desc_ring.head_id.counter
 
 	while (1)
-		set $msg = ((struct printk_log *) (log_buf + $i))
-		if ($msg->len == 0)
-			set $i = 0
-		else
-			dump_log_idx $i $prev_flags
-			set $i = $i + $msg->len
-			set $prev_flags = $msg->flags
+		set var $desc = &prb->desc_ring.descs[$id % $desc_count]
+
+		# skip non-committed record
+		if (($desc->state_var.counter & $flags_mask) == $desc_committed)
+			dump_record $desc $prev_flags
+			set var $prev_flags = $desc->info.flags
 		end
-		if ($i == $end_idx)
+
+		set var $id = ($id + 1) & $id_mask
+		if ($id == $end_id)
 			loop_break
 		end
 	end
scripts/gdb/linux/dmesg.py +102 -37

161 lines not on this page · raw message

Signature · 1 line
2.20.1
Tested-by on the v1 series
#4 lore raw
In reply to John Ogness
Quoted 10 lines · John Ogness

Hi,

When we brought in the new lockless printk ringbuffer, we overlooked the gdb scripts. Here are a set of patches to implement gdb support for the new ringbuffer.

John Ogness (2):
scripts/gdb: add utils.read_ulong() scripts/gdb: update for lockless printk ringbuffer

Thanks John! For the series:

Quoted 9 lines
 Documentation/admin-guide/kdump/gdbmacros.txt | 153 ++++++++++++------
 scripts/gdb/linux/dmesg.py                    | 139 +++++++++++-----
 scripts/gdb/linux/utils.py                    |   7 +
 3 files changed, 216 insertions(+), 83 deletions(-)

--
2.20.1

Signature · 2 lines
Thanks,
~Nick Desaulniers
Tested-by on the v1 series
#5 lore raw
In reply to John Ogness
Quoted 9 lines · John Ogness

Hi,

When we brought in the new lockless printk ringbuffer, we overlooked the gdb scripts. Here are a set of patches to implement gdb support for the new ringbuffer.

John Ogness (2):
scripts/gdb: add utils.read_ulong() scripts/gdb: update for lockless printk ringbuffer

I am not fluent in the gdb macros and python so I did not try any deep review. But both patches work for me:

I am going to give it few more days before pushing just in case there is another feedback.

Best Regards,
Petr

BTW: Are you aware of https://github.com/crash-python/crash-python project?
The ambition is to implement "crash" features and even more into gdb using python extensions. It similar approach like scripts/gdb/

crash-python is being developed sporadically in waves. It is mostly during a week that is dedicated for such projects at SUSE. It would be great to get more contributors.

maintainer
#6 lore raw
Quoted 11 lines · Petr Mladek
Quoted 9 lines · John Ogness

Hi,

When we brought in the new lockless printk ringbuffer, we overlooked the gdb scripts. Here are a set of patches to implement gdb support for the new ringbuffer.

John Ogness (2):
scripts/gdb: add utils.read_ulong() scripts/gdb: update for lockless printk ringbuffer

Quoted 8 lines

I am not fluent in the gdb macros and python so I did not try any deep review. But both patches work for me:

I am going to give it few more days before pushing just in case there is another feedback.

Thanks, perfect!

Quoted 12 lines

Best Regards,
Petr

BTW: Are you aware of https://github.com/crash-python/crash-python project?
The ambition is to implement "crash" features and even more into gdb using python extensions. It similar approach like scripts/gdb/

crash-python is being developed sporadically in waves. It is mostly during a week that is dedicated for such projects at SUSE. It would be great to get more contributors.

The problem with all those out-of-tree kernel debugging projects is that they are even further away from the changes in upstream they need to adjust to over and over again. It's already hard for scripts/gdb to keep up as only few people are aware of the dependencies, and it's always easy to forget. But it's getting better and better here IMHO. And that was my idea behind pushing things into upstream.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux
maintainer
#7 lore raw

Hi Petr,

Quoted 12 lines · Jan Kiszka
Quoted 11 lines · Petr Mladek
Quoted 9 lines · John Ogness

Hi,

When we brought in the new lockless printk ringbuffer, we overlooked the gdb scripts. Here are a set of patches to implement gdb support for the new ringbuffer.

John Ogness (2):
scripts/gdb: add utils.read_ulong() scripts/gdb: update for lockless printk ringbuffer

Quoted 7 lines
Quoted 7 lines

I am not fluent in the gdb macros and python so I did not try any deep review. But both patches work for me:

I am going to give it few more days before pushing just in case there is another feedback.

Before 'pushing' ?

What context do you mean here? These patches go through AKPM don't they? Did I miss some update to procedures?

Quoted 17 lines
Quoted 1 line

Thanks, perfect!

Quoted 12 lines

Best Regards,
Petr

BTW: Are you aware of https://github.com/crash-python/crash-python project?
The ambition is to implement "crash" features and even more into gdb using python extensions. It similar approach like scripts/gdb/

crash-python is being developed sporadically in waves. It is mostly during a week that is dedicated for such projects at SUSE. It would be great to get more contributors.

Quoted 6 lines

The problem with all those out-of-tree kernel debugging projects is that they are even further away from the changes in upstream they need to adjust to over and over again. It's already hard for scripts/gdb to keep up as only few people are aware of the dependencies, and it's always easy to forget. But it's getting better and better here IMHO. And that was my idea behind pushing things into upstream.

Seconding that, and having also investigated the crash projects in previous work. I really wish there was more alignment to the kernel ;-)

/me ponders what happened to the kernel awareness integrations on GDB... I know others have been investigating too, If only I had time ...

Quoted 2 lines

Jan

Signature · 2 lines
--
Kieran
#8 lore raw
Quoted 20 lines · Kieran Bingham

Hi Petr,

Quoted 17 lines · Jan Kiszka
Quoted 16 lines · Petr Mladek
Quoted 9 lines · John Ogness

Hi,

When we brought in the new lockless printk ringbuffer, we overlooked the gdb scripts. Here are a set of patches to implement gdb support for the new ringbuffer.

John Ogness (2):
scripts/gdb: add utils.read_ulong() scripts/gdb: update for lockless printk ringbuffer

I am not fluent in the gdb macros and python so I did not try any deep review. But both patches work for me:

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

I am going to give it few more days before pushing just in case there is another feedback.

Before 'pushing' ?

What context do you mean here? These patches go through AKPM don't they? Did I miss some update to procedures?

Both patches should go into mainline together with the lockless printk ring buffer. It is already in linux-next via printk/linux.git, branch printk-rework.

The plan is to get it into mainline when pr_cont() handling is solved. It is more complicated than we hoped for, see
https://lore.kernel.org/r/[email protected]
It is still not clear whether it will be ready for 5.10.

Andrew, would you mind when I take the three gdb/lockless-printk related changes via printk tree to avoid synchronization problems?

Best Regards,
Petr

maintainer
#9 lore raw
Quoted 33 lines · Petr Mladek
Quoted 20 lines · Kieran Bingham

Hi Petr,

Quoted 17 lines · Jan Kiszka
Quoted 16 lines · Petr Mladek
Quoted 9 lines · John Ogness

Hi,

When we brought in the new lockless printk ringbuffer, we overlooked the gdb scripts. Here are a set of patches to implement gdb support for the new ringbuffer.

John Ogness (2):
scripts/gdb: add utils.read_ulong() scripts/gdb: update for lockless printk ringbuffer

I am not fluent in the gdb macros and python so I did not try any deep review. But both patches work for me:

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

I am going to give it few more days before pushing just in case there is another feedback.

Before 'pushing' ?

What context do you mean here? These patches go through AKPM don't they? Did I miss some update to procedures?

Both patches should go into mainline together with the lockless printk ring buffer. It is already in linux-next via printk/linux.git, branch printk-rework.

Quoted 7 lines

The plan is to get it into mainline when pr_cont() handling is solved. It is more complicated than we hoped for, see
https://lore.kernel.org/r/[email protected]
It is still not clear whether it will be ready for 5.10.

Andrew, would you mind when I take the three gdb/lockless-printk related changes via printk tree to avoid synchronization problems?

From my POV, that would be best in this case - but actually also in other cases where developers happen to notice the dependency between their refactorings and gdb scripts.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux
maintainer
#10 lore raw
In reply to Petr Mladek
Quoted 2 lines · On Tue, 25 Aug 2020 14:35:42 +0200 Petr Mladek <[email protected]> wrote:

Andrew, would you mind when I take the three gdb/lockless-printk related changes via printk tree to avoid synchronization problems?

No probs. If such patches turn up in linux-next I just drop my copy. After checking that the patches fully match...

#11 lore raw
Quoted 7 lines · Andrew Morton
Quoted 2 lines · On Tue, 25 Aug 2020 14:35:42 +0200 Petr Mladek <[email protected]> wrote:

Andrew, would you mind when I take the three gdb/lockless-printk related changes via printk tree to avoid synchronization problems?

No probs. If such patches turn up in linux-next I just drop my copy. After checking that the patches fully match...

I have just pushed the two patches into printk/linux.git, branch printk-rework.

Andrew's typo fix has been squashed into the 2nd patch.

Best Regards,
Petr