parsing output of 'lspci -t' on Linux

E

Eric Schwartz

I'm trying to parse the output of 'lspci -t' on a Linux system. This
output looks something like:

-+-[a0]-+-01.0-[a1]--+-01.0
| | \-02.0
| \-02.0
+-[80]-+-01.0
| +-02.0-[81]--+-04.0
| | +-05.0
| | \-06.0
| \-04.0
\-[00]---00.0

[xx] is the bus number, nn.n is the PCI device.function number. If
there is a [xx] on a line after a nn.n number, then the [xx] is the
address of a PCI<->PCI bridge.

What I'm trying to figure out is, given bus and device numbers that
identify a PCI bridge, how can I find the PCI bridge number?

I have tried:

#!/usr/bin/perl
use warnings;
use strict;

my $pci_tree;
{ local $/; $pci_tree = <DATA> }

sub get_bridge_addr
{
my ($bus,$dev) = @_;

if ($pci_tree =~ /^[ -]\+- # line starts with ' ' or '-'
[$bus]-.*(?<!\n \+) # bus number, followed by anything
# that ISN'T '^ +' (signifying a new
# top-level bus number)
$dev.*-\[([^]])+\] # device number is always on the same
# line as the bridge address
/smx) {
return $1;
}

die ("Can't find a bus address for $bus : $dev!");
}

print "bridge address on the same line as top-level bus: [",
get_bridge_addr('a0', '01'), "]\n";
print "bridge address on a different line: [",
get_bridge_addr('80', '02'), "]\n";
__DATA__
-+-[a0]-+-01.0-[a1]--+-01.0
| | \-02.0
| \-02.0
+-[80]-+-01.0
| +-02.0-[81]--+-04.0
| | +-05.0
| | \-06.0
| \-04.0
\-[00]---00.0


but I get:

Can't find a bus address for a0 : 01! at /tmp/bah line 22, <DATA> line 1.

I've been staring at this regex for approximately forever, and though
I'm certain there's a blindingly obvious bug in it, I can't find
it. (Obviously, or I wouldn't have posted here!) Suggestions,
comments, boots to da head are all welcome.

-=Eric
 
P

Peter Corlett

Eric Schwartz said:
I've been staring at this regex for approximately forever, and
though I'm certain there's a blindingly obvious bug in it, I can't
find it. (Obviously, or I wouldn't have posted here!) Suggestions,
comments, boots to da head are all welcome.

Wouldn't it be easier to attempt to parse the output of "lspci -m -M"
instead?
 
E

Eric Schwartz

Wouldn't it be easier to attempt to parse the output of "lspci -m -M"
instead?

Maybe; I'm not sure how that handles multiple PCI domains, though.
Running it on a single-domain system gives me only something like:

40: Secondary host bus (?)
01.0 Bridge to 41-41
41: Entered via 40:01.0
80: Secondary host bus (?)
01.0 Bridge to 81-81

Which is highly useful, but I need to find a multiple-domain system
that I can check this on. Thanks for the tip, I didn't know that was
there!

-=Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top