Skip to content

Commit

Permalink
Update LICENSE
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Sep 29, 2023
1 parent b9ab711 commit 3f8582d
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 45 deletions.
29 changes: 23 additions & 6 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
Copyright <YEAR> <COPYRIGHT HOLDER>
BSD 3-Clause License

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Copyright (c) 2023, Qishuai Liu

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# pscope

## Introduction
**pscope** is an interactive tool designed to examine currently running Go and Java processes. It can be considered as a more user-friendly variant of `gops` and `jps`, with a text-based user interface.

## Features
* List all running Go and Java processes
* Show detailed information and runtime state of a process
* Show stack trace of a goroutine or thread
* Show heap profile of a process
* ... and more to come!
## Demo

This project is still under development. Here is a demo of the current progress:

![demo](https://lqs-public-us-west.oss-us-west-1.aliyuncs.com/pscope/demo-teaser.gif)

## Installation

Expand Down
80 changes: 66 additions & 14 deletions gops/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import (
)

type Goroutine struct {
Id int
State string
Wait string
Frames []Frame
Id int
State string
Wait string
Frames []Frame
ParentGoroutineId int
ParentFrame Frame
Children []*Goroutine
}

type Frame struct {
Expand All @@ -25,8 +28,10 @@ type Frame struct {

var headerRegex = regexp.MustCompile(`^goroutine (\d+) \[(.+?)(?:, (.+))?]:`)
var frameRegex = regexp.MustCompile(`^([a-zA-Z0-9._/\-]+\.)((?:\(\*?[^()]*\)\.)?\w+(?:\[\.\.\.])?)(?:\(([^()]*)\))?$`)
var createdByRegex = regexp.MustCompile(`^created by .+ in goroutine (\d+)$`)

func ParseGoStack(stack []byte) (goroutines []Goroutine) {
func ParseGoStack(stack []byte) []*Goroutine {
var goroutines []*Goroutine
reader := bufio.NewReader(bytes.NewReader(stack))
for {
line, err := reader.ReadString('\n')
Expand All @@ -40,7 +45,7 @@ func ParseGoStack(stack []byte) (goroutines []Goroutine) {
}

goroutineId, _ := strconv.Atoi(matches[1])
goroutine := Goroutine{
goroutine := &Goroutine{
Id: goroutineId,
State: matches[2],
Wait: matches[3],
Expand All @@ -52,13 +57,19 @@ func ParseGoStack(stack []byte) (goroutines []Goroutine) {
if line == "" {
break
}
matches := frameRegex.FindStringSubmatch(line)
if matches == nil {
frame.Func = line

matches := createdByRegex.FindStringSubmatch(line)
if matches != nil {
goroutine.ParentGoroutineId, _ = strconv.Atoi(matches[1])
} else {
frame.Package = matches[1]
frame.Func = matches[2]
frame.Params = matches[3]
matches := frameRegex.FindStringSubmatch(line)
if matches != nil {
frame.Package = matches[1]
frame.Func = matches[2]
frame.Params = matches[3]
} else {
frame.Func = line
}
}

line, _ = reader.ReadString('\n')
Expand All @@ -71,8 +82,49 @@ func ParseGoStack(stack []byte) (goroutines []Goroutine) {
goroutines = append(goroutines, goroutine)
}

slices.SortFunc(goroutines, func(a, b Goroutine) int {
roots := makeTree(goroutines)
sortGoroutines(roots)
return roots
}

func makeTree(goroutines []*Goroutine) []*Goroutine {
goroutineMap := make(map[int]*Goroutine)
for i := range goroutines {
goroutineMap[goroutines[i].Id] = goroutines[i]
}

for _, goroutine := range goroutines {
if goroutine.ParentGoroutineId != 0 {
parent, ok := goroutineMap[goroutine.ParentGoroutineId]
if !ok {
// make a dummy parent goroutine
parent = &Goroutine{
Id: goroutine.ParentGoroutineId,
State: "terminated",
ParentGoroutineId: 1,
}
goroutineMap[goroutine.ParentGoroutineId] = parent
goroutines = append(goroutines, parent)
}
parent.Children = append(parent.Children, goroutine)
}
}

var roots []*Goroutine
for i := range goroutines {
goroutine := goroutines[i]
if goroutine.ParentGoroutineId == 0 {
roots = append(roots, goroutine)
}
}
return roots
}

func sortGoroutines(goroutines []*Goroutine) {
slices.SortFunc(goroutines, func(a, b *Goroutine) int {
return b.Id - a.Id
})
return
for _, goroutine := range goroutines {
sortGoroutines(goroutine.Children)
}
}
66 changes: 48 additions & 18 deletions ui/goroutinelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

type GoroutineStackView struct {
*tview.Flex
selectGoroutineIndex func(index int)
cancel context.CancelFunc
selectGoroutine func(goroutine *gops.Goroutine)
cancel context.CancelFunc
}

type GoroutineStackViewParams struct {
Expand All @@ -24,26 +24,59 @@ type GoroutineStackViewParams struct {

type GoroutineListView struct {
*tview.Table
indexToGoroutine map[int]*gops.Goroutine
currentRow int
}

func (g GoroutineListView) Apply(goroutines []gops.Goroutine) {
g.SetTitle(" Goroutines (" + strconv.Itoa(len(goroutines)) + ") ")
for g.GetRowCount() > len(goroutines)+1 {
g.RemoveRow(g.GetRowCount() - 1)
func (g *GoroutineListView) add(goroutine *gops.Goroutine, level int, isLastChild bool) {
prefix := ""
if level > 0 {
for i := 0; i < level-1; i++ {
prefix += "│ "
}
if isLastChild {
prefix += "└ "
} else {
prefix += "├ "
}
}
g.SetCell(g.currentRow, 0, tview.NewTableCell(prefix+strconv.Itoa(goroutine.Id)))
g.SetCell(g.currentRow, 1, tview.NewTableCell(goroutine.State))
g.SetCell(g.currentRow, 2, tview.NewTableCell(goroutine.Frames[0].Func))
g.SetCell(g.currentRow, 3, tview.NewTableCell(goroutine.Wait))
g.indexToGoroutine[g.currentRow] = goroutine
g.currentRow++

for i, child := range goroutine.Children {
g.add(child, level+1, i+1 == len(goroutine.Children))
}
}

func (g *GoroutineListView) Apply(goroutines []*gops.Goroutine) {
//g.SetTitle(" Goroutines (" + strconv.Itoa(len(goroutines)) + ") ")
//for g.GetRowCount() > len(goroutines)+1 {
// g.RemoveRow(g.GetRowCount() - 1)
//}
g.indexToGoroutine = make(map[int]*gops.Goroutine)
g.currentRow = 1
for i, goroutine := range goroutines {
g.SetCell(i+1, 0, tview.NewTableCell(strconv.Itoa(goroutine.Id)))
g.SetCell(i+1, 1, tview.NewTableCell(goroutine.State))
g.SetCell(i+1, 2, tview.NewTableCell(goroutine.Wait))
g.add(goroutine, 0, i+1 == len(goroutines))
}
if row, _ := g.GetSelection(); row <= 0 || row >= len(goroutines)+1 {
for g.GetRowCount() > g.currentRow+1 {
g.RemoveRow(g.GetRowCount() - 1)
}
if row, _ := g.GetSelection(); row <= 0 || row >= g.currentRow+1 {
g.Select(1, 0)
g.ScrollToBeginning()
}
}

func (v *GoroutineStackView) newGoroutineList() GoroutineListView {
func (v *GoroutineStackView) newGoroutineList() *GoroutineListView {
table := tview.NewTable()
g := &GoroutineListView{
Table: table,
}

table.SetBorder(true)
table.SetTitle(" Goroutines ")
table.SetBorderPadding(0, 0, 1, 1)
Expand All @@ -56,13 +89,11 @@ func (v *GoroutineStackView) newGoroutineList() GoroutineListView {
row = 1
table.Select(row, column)
}
v.selectGoroutineIndex(row - 1)
v.selectGoroutine(g.indexToGoroutine[row])
})
table.SetSelectable(true, false)

return GoroutineListView{
Table: table,
}
return g
}

func newStackList(frames []gops.Frame) tview.Primitive {
Expand Down Expand Up @@ -128,14 +159,13 @@ func NewGoroutineStackView(params GoroutineStackViewParams) Widget {
goroutines := gops.ParseGoStack(result)

params.Application.QueueUpdateDraw(func() {
v.selectGoroutineIndex = func(index int) {
v.selectGoroutine = func(goroutine *gops.Goroutine) {
if flex.GetItemCount() > 1 {
flex.RemoveItem(flex.GetItem(1))
}
if index < 0 || index >= len(goroutines) {
if goroutine == nil {
return
}
goroutine := goroutines[index]
stackListView = newStackList(goroutine.Frames)
flex.AddItem(stackListView, 0, 3, false)
}
Expand Down

0 comments on commit 3f8582d

Please sign in to comment.