@@ -20,12 +20,15 @@ func hexByte(s string) int {
2020func (w * ProcessWatcher ) getListeningPorts () ([]portEntry , error ) {
2121 entries , err := w .parseProcNet ("/proc/net/tcp" )
2222 if err != nil {
23+ fmt .Printf ("Error parsing /proc/net/tcp: %v\n " , err )
2324 return nil , err
2425 }
26+ fmt .Printf ("Found %d entries from /proc/net/tcp\n " , len (entries ))
2527
2628 entries6 , err := w .parseProcNet ("/proc/net/tcp6" )
2729 if err == nil {
2830 entries = append (entries , entries6 ... )
31+ fmt .Printf ("Found %d entries from /proc/net/tcp6\n " , len (entries6 ))
2932 }
3033
3134 return entries , nil
@@ -40,6 +43,7 @@ func (w *ProcessWatcher) parseProcNet(path string) ([]portEntry, error) {
4043
4144 inodeToPID := make (map [string ]int )
4245 procDirs , _ := filepath .Glob ("/proc/[0-9]*/fd/*" )
46+ fmt .Printf ("Found %d fd paths\n " , len (procDirs ))
4347 for _ , fdPath := range procDirs {
4448 link , err := os .Readlink (fdPath )
4549 if err != nil {
@@ -59,6 +63,7 @@ func (w *ProcessWatcher) parseProcNet(path string) ([]portEntry, error) {
5963 }
6064 inodeToPID [inode ] = pid
6165 }
66+ fmt .Printf ("Mapped %d inodes to PIDs\n " , len (inodeToPID ))
6267
6368 var result []portEntry
6469 scanner := bufio .NewScanner (file )
@@ -68,6 +73,7 @@ func (w *ProcessWatcher) parseProcNet(path string) ([]portEntry, error) {
6873 line := scanner .Text ()
6974 fields := strings .Fields (line )
7075 if len (fields ) < 10 {
76+ fmt .Printf ("Skipping line with %d fields: %s\n " , len (fields ), line )
7177 continue
7278 }
7379
@@ -79,18 +85,21 @@ func (w *ProcessWatcher) parseProcNet(path string) ([]portEntry, error) {
7985 localAddr := fields [1 ]
8086 addrParts := strings .Split (localAddr , ":" )
8187 if len (addrParts ) != 2 {
88+ fmt .Printf ("Skipping bad address format: %s\n " , localAddr )
8289 continue
8390 }
8491
8592 addrHex := addrParts [0 ]
8693 portHex := addrParts [1 ]
8794 port64 , err := strconv .ParseInt (portHex , 16 , 32 )
8895 if err != nil {
96+ fmt .Printf ("Skipping bad port %s: %v\n " , portHex , err )
8997 continue
9098 }
9199 port := int (port64 )
92100
93101 if port < 1024 {
102+ fmt .Printf ("Skipping port < 1024: %d\n " , port )
94103 continue
95104 }
96105
@@ -108,11 +117,13 @@ func (w *ProcessWatcher) parseProcNet(path string) ([]portEntry, error) {
108117 inode := fields [9 ]
109118 pid , ok := inodeToPID [inode ]
110119 if ! ok {
120+ fmt .Printf ("No PID for inode %s, port %d, ip %s\n " , inode , port , ipStr )
111121 continue
112122 }
113123
114124 addr , err := netip .ParseAddr (ipStr )
115125 if err != nil {
126+ fmt .Printf ("Skipping invalid IP %s: %v\n " , ipStr , err )
116127 continue
117128 }
118129 result = append (result , portEntry {PID : pid , Endpoint : netip .AddrPortFrom (addr , uint16 (port ))})
0 commit comments